forked from thrasher-corp/gocryptotrader
/
calculator.go
226 lines (207 loc) · 6.09 KB
/
calculator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package orderbook
import (
"errors"
"fmt"
"sort"
math "github.com/nbltrust/gocryptotrader/common/math"
"github.com/nbltrust/gocryptotrader/log"
)
// WhaleBombResult returns the whale bomb result
type WhaleBombResult struct {
Amount float64
MinimumPrice float64
MaximumPrice float64
PercentageGainOrLoss float64
Orders orderSummary
Status string
}
// WhaleBomb finds the amount required to target a price
func (b *Base) WhaleBomb(priceTarget float64, buy bool) (*WhaleBombResult, error) {
if priceTarget < 0 {
return nil, errors.New("price target is invalid")
}
if buy {
a, orders := b.findAmount(priceTarget, true)
min, max := orders.MinimumPrice(false), orders.MaximumPrice(true)
var err error
if max < priceTarget {
err = errors.New("unable to hit price target due to insufficient orderbook items")
}
status := fmt.Sprintf("Buying %.2f %v worth of %v will send the price from %v to %v [%.2f%%] and take %v orders.",
a, b.Pair.Quote.String(), b.Pair.Base.String(), min, max,
math.CalculatePercentageGainOrLoss(max, min), len(orders))
return &WhaleBombResult{
Amount: a,
Orders: orders,
MinimumPrice: min,
MaximumPrice: max,
Status: status,
}, err
}
a, orders := b.findAmount(priceTarget, false)
min, max := orders.MinimumPrice(false), orders.MaximumPrice(true)
var err error
if min > priceTarget {
err = errors.New("unable to hit price target due to insufficient orderbook items")
}
status := fmt.Sprintf("Selling %.2f %v worth of %v will send the price from %v to %v [%.2f%%] and take %v orders.",
a, b.Pair.Base.String(), b.Pair.Quote.String(), max, min,
math.CalculatePercentageGainOrLoss(min, max), len(orders))
return &WhaleBombResult{
Amount: a,
Orders: orders,
MinimumPrice: min,
MaximumPrice: max,
Status: status,
}, err
}
// OrderSimulationResult returns the order simulation result
type OrderSimulationResult WhaleBombResult
// SimulateOrder simulates an order
func (b *Base) SimulateOrder(amount float64, buy bool) *OrderSimulationResult {
if buy {
orders, amt := b.buy(amount)
min, max := orders.MinimumPrice(false), orders.MaximumPrice(true)
pct := math.CalculatePercentageGainOrLoss(max, min)
status := fmt.Sprintf("Buying %.2f %v worth of %v will send the price from %v to %v [%.2f%%] and take %v orders.",
amount, b.Pair.Quote.String(), b.Pair.Base.String(), min, max,
pct, len(orders))
return &OrderSimulationResult{
Orders: orders,
Amount: amt,
MinimumPrice: min,
MaximumPrice: max,
PercentageGainOrLoss: pct,
Status: status,
}
}
orders, amt := b.sell(amount)
min, max := orders.MinimumPrice(false), orders.MaximumPrice(true)
pct := math.CalculatePercentageGainOrLoss(min, max)
status := fmt.Sprintf("Selling %f %v worth of %v will send the price from %v to %v [%.2f%%] and take %v orders.",
amount, b.Pair.Base.String(), b.Pair.Quote.String(), max, min,
pct, len(orders))
return &OrderSimulationResult{
Orders: orders,
Amount: amt,
MinimumPrice: min,
MaximumPrice: max,
PercentageGainOrLoss: pct,
Status: status,
}
}
type orderSummary []Item
func (o orderSummary) Print() {
for x := range o {
log.Debugf(log.OrderBook, "Order: Price: %f Amount: %f", o[x].Price, o[x].Amount)
}
}
func (o orderSummary) MinimumPrice(reverse bool) float64 {
if len(o) != 0 {
sortOrdersByPrice(&o, reverse)
return o[0].Price
}
return 0
}
func (o orderSummary) MaximumPrice(reverse bool) float64 {
if len(o) != 0 {
sortOrdersByPrice(&o, reverse)
return o[0].Price
}
return 0
}
// ByPrice used for sorting orders by order date
type ByPrice orderSummary
func (b ByPrice) Len() int { return len(b) }
func (b ByPrice) Less(i, j int) bool { return b[i].Price < b[j].Price }
func (b ByPrice) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
// sortOrdersByPrice the caller function to sort orders
func sortOrdersByPrice(o *orderSummary, reverse bool) {
if reverse {
sort.Sort(sort.Reverse(ByPrice(*o)))
} else {
sort.Sort(ByPrice(*o))
}
}
func (b *Base) findAmount(price float64, buy bool) (float64, orderSummary) {
var orders orderSummary
var amt float64
if buy {
for x := range b.Asks {
if b.Asks[x].Price >= price {
amt += b.Asks[x].Price * b.Asks[x].Amount
orders = append(orders, Item{
Price: b.Asks[x].Price,
Amount: b.Asks[x].Amount,
})
return amt, orders
}
orders = append(orders, Item{
Price: b.Asks[x].Price,
Amount: b.Asks[x].Amount,
})
amt += b.Asks[x].Price * b.Asks[x].Amount
}
return amt, orders
}
for x := range b.Bids {
if b.Bids[x].Price <= price {
amt += b.Bids[x].Amount
orders = append(orders, Item{
Price: b.Bids[x].Price,
Amount: b.Bids[x].Amount,
})
break
}
orders = append(orders, Item{
Price: b.Bids[x].Price,
Amount: b.Bids[x].Amount,
})
amt += b.Bids[x].Amount
}
return amt, orders
}
func (b *Base) buy(amount float64) (orders orderSummary, baseAmount float64) {
var processedAmt float64
for x := range b.Asks {
subtotal := b.Asks[x].Price * b.Asks[x].Amount
if processedAmt+subtotal >= amount {
diff := amount - processedAmt
subAmt := diff / b.Asks[x].Price
orders = append(orders, Item{
Price: b.Asks[x].Price,
Amount: subAmt,
})
baseAmount += subAmt
break
}
processedAmt += subtotal
baseAmount += b.Asks[x].Amount
orders = append(orders, Item{
Price: b.Asks[x].Price,
Amount: b.Asks[x].Amount,
})
}
return
}
func (b *Base) sell(amount float64) (orders orderSummary, quoteAmount float64) {
var processedAmt float64
for x := range b.Bids {
if processedAmt+b.Bids[x].Amount >= amount {
diff := amount - processedAmt
orders = append(orders, Item{
Price: b.Bids[x].Price,
Amount: diff,
})
quoteAmount += diff * b.Bids[x].Price
break
}
processedAmt += b.Bids[x].Amount
quoteAmount += b.Bids[x].Amount * b.Bids[x].Price
orders = append(orders, Item{
Price: b.Bids[x].Price,
Amount: b.Bids[x].Amount,
})
}
return
}