/
toolbelt.go
141 lines (120 loc) · 3.69 KB
/
toolbelt.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
// The toolbelt is a set of helper functions that eases strategies and cross usage.
package dola
import (
"context"
"errors"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
)
var ErrNeedBalancesStrategy = errors.New("Keep should be configured with balances support")
func Holdings(k *Keep, exchangeName string) (*ExchangeHoldings, error) {
st, err := k.Root.Get("balances")
if errors.Is(err, ErrStrategyNotFound) {
return nil, ErrNeedBalancesStrategy
}
balances, ok := st.(*BalancesStrategy)
if !ok {
panic("cast failed")
}
holdings, err := balances.ExchangeHoldings(exchangeName)
if err != nil {
return nil, err
}
return holdings, nil
}
func ModifyOrder(ctx context.Context,
k *Keep,
e exchange.IBotExchange,
mod order.Modify) (ans order.Modify, err error) {
// First, try to use the native exchange functionality.
ans, err = k.ModifyOrder(ctx, e, mod)
if err == nil {
return ans, nil
}
// If there is an error (even if that error says this exchange
// implementation does not support order modifications), we
// fall back to cancel + submit.
// We do not check the cancellation error on purpose as an
// error may be reported for a missing order, which is fine as
// we're submitting a new one anyways.
cancel := ModifyToCancel(mod)
_ = k.CancelOrder(ctx, e, cancel)
// Prepare submission.
var (
submit = ModifyToSubmit(mod)
response order.SubmitResponse
)
// If there is a UserData associated with that order, resubmit
// the order with the same UserData.
value, loaded := k.GetOrderValue(e.GetName(), mod.ID)
if loaded {
response, err = k.SubmitOrderUD(ctx, e, submit, value.UserData)
} else {
// If there's not, just submit the order.
response, err = k.SubmitOrder(ctx, e, submit)
}
ans.Exchange = e.GetName()
ans.AssetType = submit.AssetType
ans.Pair = submit.Pair
ans.ID = response.OrderID
return ans, err
}
// Ticker casts a void* to ticker.Price.
func Ticker(p interface{}) ticker.Price {
x, ok := p.(ticker.Price)
if !ok {
panic("")
}
return x
}
// +---------------+
// | Conversations |
// +---------------+
func ModifyToCancel(mod order.Modify) order.Cancel {
var cancel order.Cancel
// These four are what the GCT engine uses to match an order
// uniquely.
cancel.Exchange = mod.Exchange
cancel.ID = mod.ID
cancel.AssetType = mod.AssetType
cancel.Pair = mod.Pair
return cancel
}
func ModifyToSubmit(mod order.Modify) order.Submit {
sub := order.Submit{
ImmediateOrCancel: mod.ImmediateOrCancel,
HiddenOrder: mod.HiddenOrder,
FillOrKill: mod.FillOrKill,
PostOnly: mod.PostOnly,
ReduceOnly: false, // Missing.
Leverage: mod.Leverage,
Price: mod.Price,
Amount: mod.Amount,
StopPrice: 0, // Missing.
LimitPriceUpper: mod.LimitPriceUpper,
LimitPriceLower: mod.LimitPriceLower,
TriggerPrice: mod.TriggerPrice,
TargetAmount: mod.TargetAmount,
ExecutedAmount: mod.ExecutedAmount,
RemainingAmount: mod.RemainingAmount,
Fee: mod.Fee,
Exchange: mod.Exchange,
InternalOrderID: mod.InternalOrderID,
ID: mod.ID,
AccountID: mod.AccountID,
ClientID: mod.ClientID,
ClientOrderID: mod.ClientOrderID,
WalletAddress: mod.WalletAddress,
Offset: "", // Missing.
Type: mod.Type,
Side: mod.Side,
Status: mod.Status,
AssetType: mod.AssetType,
Date: mod.Date,
LastUpdated: mod.LastUpdated,
Pair: mod.Pair,
Trades: mod.Trades,
}
return sub
}