-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfactory.go
398 lines (371 loc) · 13.1 KB
/
factory.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package plugins
import (
"database/sql"
"fmt"
"log"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/support/config"
"github.com/stellar/kelp/api"
"github.com/stellar/kelp/model"
"github.com/stellar/kelp/support/sdk"
"github.com/stellar/kelp/support/utils"
)
// strategyFactoryData is a data container that has all the information needed to make a strategy
type strategyFactoryData struct {
sdex *SDEX
exchangeShim api.ExchangeShim
tradeFetcher api.TradeFetcher
ieif *IEIF
tradingPair *model.TradingPair
assetBase *hProtocol.Asset
assetQuote *hProtocol.Asset
marketID string
stratConfigPath string
simMode bool
isTradingSdex bool
filterFactory *FilterFactory
db *sql.DB
}
// StrategyContainer contains the strategy factory method along with some metadata
type StrategyContainer struct {
SortOrder uint8
Description string
NeedsConfig bool
Complexity string
makeFn func(strategyFactoryData strategyFactoryData) (api.Strategy, error)
}
var ccxtExchangeSpecificParamFactoryMap = map[string]ccxtExchangeSpecificParamFactory{
"ccxt-coinbasepro": &ccxtExchangeSpecificParamFactoryCoinbasepro{},
"ccxt-binance": makeCcxtExchangeSpecificParamFactoryBinance(),
"ccxt-bitstamp": &ccxtExchangeSpecificParamFactoryBitstamp{},
}
// strategies is a map of all the strategies available
var strategies = map[string]StrategyContainer{
"buysell": {
SortOrder: 1,
Description: "Creates buy and sell offers based on a reference price with a pre-specified liquidity depth",
NeedsConfig: true,
Complexity: "Beginner",
makeFn: func(strategyFactoryData strategyFactoryData) (api.Strategy, error) {
var cfg BuySellConfig
err := config.Read(strategyFactoryData.stratConfigPath, &cfg)
utils.CheckConfigError(cfg, err, strategyFactoryData.stratConfigPath)
utils.LogConfig(cfg)
s, e := makeBuySellStrategy(strategyFactoryData.sdex, strategyFactoryData.tradingPair, strategyFactoryData.ieif, strategyFactoryData.assetBase, strategyFactoryData.assetQuote, &cfg)
if e != nil {
return nil, fmt.Errorf("makeFn failed: %s", e)
}
return s, nil
},
},
"mirror": {
SortOrder: 5,
Description: "Mirrors an orderbook from another exchange by placing the same orders on Stellar",
NeedsConfig: true,
Complexity: "Advanced",
makeFn: func(strategyFactoryData strategyFactoryData) (api.Strategy, error) {
var cfg mirrorConfig
err := config.Read(strategyFactoryData.stratConfigPath, &cfg)
utils.CheckConfigError(cfg, err, strategyFactoryData.stratConfigPath)
utils.LogConfig(cfg)
s, e := makeMirrorStrategy(strategyFactoryData.sdex, strategyFactoryData.ieif, strategyFactoryData.tradingPair, strategyFactoryData.assetBase, strategyFactoryData.assetQuote, strategyFactoryData.marketID, &cfg, strategyFactoryData.db, strategyFactoryData.simMode)
if e != nil {
return nil, fmt.Errorf("makeFn failed: %s", e)
}
return s, nil
},
},
"sell": {
SortOrder: 0,
Description: "Creates sell offers based on a reference price with a pre-specified liquidity depth",
NeedsConfig: true,
Complexity: "Beginner",
makeFn: func(strategyFactoryData strategyFactoryData) (api.Strategy, error) {
var cfg sellConfig
err := config.Read(strategyFactoryData.stratConfigPath, &cfg)
utils.CheckConfigError(cfg, err, strategyFactoryData.stratConfigPath)
utils.LogConfig(cfg)
s, e := makeSellStrategy(strategyFactoryData.sdex, strategyFactoryData.tradingPair, strategyFactoryData.ieif, strategyFactoryData.assetBase, strategyFactoryData.assetQuote, &cfg)
if e != nil {
return nil, fmt.Errorf("makeFn failed: %s", e)
}
return s, nil
},
},
"balanced": {
SortOrder: 4,
Description: "Dynamically prices two tokens based on their relative demand",
NeedsConfig: true,
Complexity: "Intermediate",
makeFn: func(strategyFactoryData strategyFactoryData) (api.Strategy, error) {
var cfg balancedConfig
err := config.Read(strategyFactoryData.stratConfigPath, &cfg)
utils.CheckConfigError(cfg, err, strategyFactoryData.stratConfigPath)
utils.LogConfig(cfg)
return makeBalancedStrategy(strategyFactoryData.sdex, strategyFactoryData.tradingPair, strategyFactoryData.ieif, strategyFactoryData.assetBase, strategyFactoryData.assetQuote, &cfg), nil
},
},
"delete": {
SortOrder: 3,
Description: "Deletes all orders for the configured orderbook",
NeedsConfig: false,
Complexity: "Beginner",
makeFn: func(strategyFactoryData strategyFactoryData) (api.Strategy, error) {
return makeDeleteStrategy(strategyFactoryData.sdex, strategyFactoryData.assetBase, strategyFactoryData.assetQuote), nil
},
},
"pendulum": {
SortOrder: 2,
Description: "Oscillating bids and asks like a pendulum based on last trade price as the equilibrium poistion",
NeedsConfig: true,
Complexity: "Beginner",
makeFn: func(strategyFactoryData strategyFactoryData) (api.Strategy, error) {
var cfg pendulumConfig
err := config.Read(strategyFactoryData.stratConfigPath, &cfg)
utils.CheckConfigError(cfg, err, strategyFactoryData.stratConfigPath)
utils.LogConfig(cfg)
return makePendulumStrategy(
strategyFactoryData.sdex,
strategyFactoryData.exchangeShim,
strategyFactoryData.ieif,
strategyFactoryData.assetBase,
strategyFactoryData.assetQuote,
&cfg,
strategyFactoryData.tradeFetcher,
strategyFactoryData.tradingPair,
!strategyFactoryData.isTradingSdex,
), nil
},
},
"sell_twap": {
SortOrder: 6,
Description: "Creates sell offers by distributing orders over time for a given day using a twap metric",
NeedsConfig: true,
Complexity: "Intermediate",
makeFn: func(strategyFactoryData strategyFactoryData) (api.Strategy, error) {
var cfg sellTwapConfig
err := config.Read(strategyFactoryData.stratConfigPath, &cfg)
utils.CheckConfigError(cfg, err, strategyFactoryData.stratConfigPath)
utils.LogConfig(cfg)
s, e := makeSellTwapStrategy(
strategyFactoryData.sdex,
strategyFactoryData.tradingPair,
strategyFactoryData.ieif,
strategyFactoryData.assetBase,
strategyFactoryData.assetQuote,
strategyFactoryData.filterFactory,
&cfg,
)
if e != nil {
return nil, fmt.Errorf("makeFn failed: %s", e)
}
return s, nil
},
},
"buy_twap": {
SortOrder: 7,
Description: "Creates buy offers by distributing orders over time for a given day using a twap metric",
NeedsConfig: true,
Complexity: "Intermediate",
makeFn: func(strategyFactoryData strategyFactoryData) (api.Strategy, error) {
// reuse the sellTwapConfig struct since we need the same info for buyTwap
var cfg sellTwapConfig
err := config.Read(strategyFactoryData.stratConfigPath, &cfg)
utils.CheckConfigError(cfg, err, strategyFactoryData.stratConfigPath)
utils.LogConfig(cfg)
s, e := makeBuyTwapStrategy(
strategyFactoryData.sdex,
strategyFactoryData.tradingPair,
strategyFactoryData.ieif,
strategyFactoryData.assetBase,
strategyFactoryData.assetQuote,
strategyFactoryData.filterFactory,
&cfg,
)
if e != nil {
return nil, fmt.Errorf("make Fn failed: %s", e)
}
return s, nil
},
},
}
// MakeStrategy makes a strategy
func MakeStrategy(
sdex *SDEX,
exchangeShim api.ExchangeShim,
tradeFetcher api.TradeFetcher,
ieif *IEIF,
tradingPair *model.TradingPair,
assetBase *hProtocol.Asset,
assetQuote *hProtocol.Asset,
marketID string,
strategy string,
stratConfigPath string,
simMode bool,
isTradingSdex bool,
filterFactory *FilterFactory,
db *sql.DB,
) (api.Strategy, error) {
log.Printf("Making strategy: %s\n", strategy)
if s, ok := strategies[strategy]; ok {
if s.NeedsConfig && stratConfigPath == "" {
return nil, fmt.Errorf("the '%s' strategy needs a config file", strategy)
}
s, e := s.makeFn(strategyFactoryData{
sdex: sdex,
exchangeShim: exchangeShim,
tradeFetcher: tradeFetcher,
ieif: ieif,
tradingPair: tradingPair,
assetBase: assetBase,
assetQuote: assetQuote,
marketID: marketID,
stratConfigPath: stratConfigPath,
simMode: simMode,
isTradingSdex: isTradingSdex,
filterFactory: filterFactory,
db: db,
})
if e != nil {
return nil, fmt.Errorf("cannot make '%s' strategy: %s", strategy, e)
}
return s, nil
}
return nil, fmt.Errorf("invalid strategy type: %s", strategy)
}
// Strategies returns the list of strategies along with metadata
func Strategies() map[string]StrategyContainer {
return strategies
}
// exchangeFactoryData is a data container that has all the information needed to make an exchange
type exchangeFactoryData struct {
simMode bool
apiKeys []api.ExchangeAPIKey
exchangeParams []api.ExchangeParam
headers []api.ExchangeHeader
}
// ExchangeContainer contains the exchange factory method along with some metadata
type ExchangeContainer struct {
SortOrder uint16
Description string
TradeEnabled bool
Tested bool
AtomicPostOnly bool
TradeHasOrderId bool
makeFn func(exchangeFactoryData exchangeFactoryData) (api.Exchange, error)
}
// exchanges is a map of all the exchange integrations available
var exchanges *map[string]ExchangeContainer
// getExchanges returns a map of all the exchange integrations available
func getExchanges() map[string]ExchangeContainer {
if exchanges == nil {
loadExchanges()
}
return *exchanges
}
func loadExchanges() {
// marked as tested if key exists in this map (regardless of bool value)
testedCcxtExchanges := map[string]bool{
"binance": true,
"poloniex": true,
"coinbasepro": true,
"bitstamp": true,
}
// marked as atomicPostOnly if key exists in this map (regardless of bool value)
atomicPostOnlyCcxtExchanges := map[string]bool{
"kraken": true,
"coinbasepro": true,
}
// marked as tradeHasOrderId if key exists in this map (regardless of bool value)
tradeHasOrderIdCcxtExchanges := map[string]bool{
"binance": true,
}
exchanges = &map[string]ExchangeContainer{
"kraken": {
SortOrder: 0,
Description: "Kraken is a popular centralized cryptocurrency exchange",
TradeEnabled: true,
Tested: true,
makeFn: func(exchangeFactoryData exchangeFactoryData) (api.Exchange, error) {
return makeKrakenExchange(exchangeFactoryData.apiKeys, exchangeFactoryData.simMode)
},
},
}
// add all CCXT exchanges (tested exchanges first)
sortOrderIndex := len(*exchanges)
for _, t := range []bool{true, false} {
for _, exchangeName := range sdk.GetExchangeList() {
key := fmt.Sprintf("ccxt-%s", exchangeName)
_, tested := testedCcxtExchanges[exchangeName]
if tested != t {
continue
}
boundExchangeName := exchangeName
_, atomicPostOnly := atomicPostOnlyCcxtExchanges[exchangeName]
_, tradeHasOrderId := tradeHasOrderIdCcxtExchanges[exchangeName]
// maybeEsParamFactory can be nil
maybeEsParamFactory := ccxtExchangeSpecificParamFactoryMap[key]
(*exchanges)[key] = ExchangeContainer{
SortOrder: uint16(sortOrderIndex),
Description: exchangeName + " is automatically added via ccxt-rest",
TradeEnabled: true,
Tested: tested,
AtomicPostOnly: atomicPostOnly,
TradeHasOrderId: tradeHasOrderId,
makeFn: func(exchangeFactoryData exchangeFactoryData) (api.Exchange, error) {
return makeCcxtExchange(
boundExchangeName,
nil,
exchangeFactoryData.apiKeys,
exchangeFactoryData.exchangeParams,
exchangeFactoryData.headers,
exchangeFactoryData.simMode,
maybeEsParamFactory,
)
},
}
sortOrderIndex++
}
}
}
// MakeExchange is a factory method to make an exchange based on a given type
func MakeExchange(exchangeType string, simMode bool) (api.Exchange, error) {
if exchange, ok := getExchanges()[exchangeType]; ok {
exchangeAPIKey := api.ExchangeAPIKey{Key: "", Secret: ""}
x, e := exchange.makeFn(exchangeFactoryData{
simMode: simMode,
apiKeys: []api.ExchangeAPIKey{exchangeAPIKey},
})
if e != nil {
return nil, fmt.Errorf("error when making the '%s' exchange: %s", exchangeType, e)
}
return x, nil
}
return nil, fmt.Errorf("invalid exchange type: %s", exchangeType)
}
// MakeTradingExchange is a factory method to make an exchange based on a given type
func MakeTradingExchange(exchangeType string, apiKeys []api.ExchangeAPIKey, exchangeParams []api.ExchangeParam, headers []api.ExchangeHeader, simMode bool) (api.Exchange, error) {
if exchange, ok := getExchanges()[exchangeType]; ok {
if !exchange.TradeEnabled {
return nil, fmt.Errorf("trading is not enabled on this exchange: %s", exchangeType)
}
if len(apiKeys) == 0 {
return nil, fmt.Errorf("cannot make trading exchange, apiKeys mising")
}
x, e := exchange.makeFn(exchangeFactoryData{
simMode: simMode,
apiKeys: apiKeys,
exchangeParams: exchangeParams,
headers: headers,
})
if e != nil {
return nil, fmt.Errorf("error when making the '%s' exchange: %s", exchangeType, e)
}
return x, nil
}
return nil, fmt.Errorf("invalid exchange type: %s", exchangeType)
}
// Exchanges returns the list of exchanges
func Exchanges() map[string]ExchangeContainer {
return getExchanges()
}