forked from stellar-deprecated/kelp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccxtExchangeSpecificParamFactory.go
163 lines (130 loc) · 5.12 KB
/
ccxtExchangeSpecificParamFactory.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
package plugins
import (
"fmt"
"strconv"
"github.com/stellar/kelp/api"
"github.com/stellar/kelp/model"
)
/****************************** COINBASE PRO ******************************/
type ccxtExchangeSpecificParamFactoryCoinbasepro struct{}
func (f *ccxtExchangeSpecificParamFactoryCoinbasepro) getInitParams() map[string]interface{} {
return nil
}
func (f *ccxtExchangeSpecificParamFactoryCoinbasepro) getParamsForGetOrderBook() map[string]interface{} {
return nil
}
func (f *ccxtExchangeSpecificParamFactoryCoinbasepro) getParamsForAddOrder(submitMode api.SubmitMode) interface{} {
if submitMode == api.SubmitModeMakerOnly {
return map[string]interface{}{
"post_only": true,
}
}
return nil
}
func (f *ccxtExchangeSpecificParamFactoryCoinbasepro) getParamsForGetTradeHistory() interface{} {
return nil
}
func (f *ccxtExchangeSpecificParamFactoryCoinbasepro) useSignToDenoteSideForTrades() bool {
return false
}
func (f *ccxtExchangeSpecificParamFactoryCoinbasepro) getCursorFetchTrades(t model.Trade) (interface{}, error) {
// not implemented so use default implementation
return nil, nil
}
var _ ccxtExchangeSpecificParamFactory = &ccxtExchangeSpecificParamFactoryCoinbasepro{}
/****************************** BINANCE ******************************/
type ccxtExchangeSpecificParamFactoryBinance struct {
validOrderBookLevels []int
lastValidLimit int
cachedResults map[int]int
}
func makeCcxtExchangeSpecificParamFactoryBinance() *ccxtExchangeSpecificParamFactoryBinance {
validOrderBookLevels := []int{5, 10, 20, 50, 100, 500, 1000, 5000}
return &ccxtExchangeSpecificParamFactoryBinance{
validOrderBookLevels: validOrderBookLevels,
lastValidLimit: validOrderBookLevels[len(validOrderBookLevels)-1],
cachedResults: map[int]int{},
}
}
func (f *ccxtExchangeSpecificParamFactoryBinance) getInitParams() map[string]interface{} {
return nil
}
func (f *ccxtExchangeSpecificParamFactoryBinance) getParamsForGetOrderBook() map[string]interface{} {
return map[string]interface{}{
"transform_limit": f.transformLimit,
}
}
func (f *ccxtExchangeSpecificParamFactoryBinance) transformLimit(limit int) (int /*newLimit*/, error) {
if newLimit, ok := f.cachedResults[limit]; ok {
if newLimit == -1 {
return -1, fmt.Errorf("limit requested (%d) is higher than the maximum limit allowed (%d)", limit, f.lastValidLimit)
}
return newLimit, nil
}
for _, validLimit := range f.validOrderBookLevels {
if limit <= validLimit {
f.cachedResults[limit] = validLimit
return validLimit, nil
}
}
f.cachedResults[limit] = -1
return -1, fmt.Errorf("limit requested (%d) is higher than the maximum limit allowed (%d)", limit, f.lastValidLimit)
}
func (f *ccxtExchangeSpecificParamFactoryBinance) getParamsForAddOrder(submitMode api.SubmitMode) interface{} {
return nil
}
func (f *ccxtExchangeSpecificParamFactoryBinance) getParamsForGetTradeHistory() interface{} {
return map[string]interface{}{
"order_id": func(info interface{}) (string, error) {
rawInfo, ok := info.(map[string]interface{})
if !ok {
return "", fmt.Errorf("unable to convert input 'info' to a map[string]interface{}: %+v (type=%T)", rawInfo, rawInfo)
}
orderIDFloat64, ok := rawInfo["orderId"].(float64)
if !ok {
return "", fmt.Errorf("unable to parse info[\"orderId\"] as a float64: %+v (type=%T)", rawInfo["orderId"], rawInfo["orderId"])
}
orderIDInt64 := int64(orderIDFloat64)
orderID := strconv.FormatInt(orderIDInt64, 10)
return orderID, nil
},
}
}
func (f *ccxtExchangeSpecificParamFactoryBinance) useSignToDenoteSideForTrades() bool {
return false
}
func (f *ccxtExchangeSpecificParamFactoryBinance) getCursorFetchTrades(t model.Trade) (interface{}, error) {
lastCursor, e := t.TransactionID.AsInt64()
if e != nil {
return nil, fmt.Errorf("error converting transactionID (%s) to int64: %s", t.TransactionID.String(), e)
}
// add 1 to lastCursor so we don't repeat the same cursor on the next run
cursor := strconv.FormatInt(lastCursor+1, 10)
return cursor, nil
}
var _ ccxtExchangeSpecificParamFactory = &ccxtExchangeSpecificParamFactoryBinance{}
/****************************** BITSTAMP ******************************/
type ccxtExchangeSpecificParamFactoryBitstamp struct{}
func (f *ccxtExchangeSpecificParamFactoryBitstamp) getInitParams() map[string]interface{} {
return map[string]interface{}{
"enableRateLimit": true,
}
}
func (f *ccxtExchangeSpecificParamFactoryBitstamp) getParamsForGetOrderBook() map[string]interface{} {
return nil
}
func (f *ccxtExchangeSpecificParamFactoryBitstamp) getParamsForAddOrder(submitMode api.SubmitMode) interface{} {
return nil
}
func (f *ccxtExchangeSpecificParamFactoryBitstamp) getParamsForGetTradeHistory() interface{} {
return nil
}
// Bitstamp uses signs to denote which side the trade was on (buy/sell)
func (f *ccxtExchangeSpecificParamFactoryBitstamp) useSignToDenoteSideForTrades() bool {
return true
}
func (f *ccxtExchangeSpecificParamFactoryBitstamp) getCursorFetchTrades(t model.Trade) (interface{}, error) {
// not implemented so use default implementation
return nil, nil
}
var _ ccxtExchangeSpecificParamFactory = &ccxtExchangeSpecificParamFactoryBitstamp{}