forked from stellar-deprecated/kelp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuysellStrategy.go
111 lines (104 loc) · 3.02 KB
/
buysellStrategy.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
package plugins
import (
"fmt"
"github.com/stellar/go/clients/horizon"
"github.com/stellar/kelp/api"
"github.com/stellar/kelp/model"
"github.com/stellar/kelp/support/utils"
)
// buySellConfig contains the configuration params for this strategy
type buySellConfig struct {
PriceTolerance float64 `valid:"-" toml:"PRICE_TOLERANCE"`
AmountTolerance float64 `valid:"-" toml:"AMOUNT_TOLERANCE"`
RateOffsetPercent float64 `valid:"-" toml:"RATE_OFFSET_PERCENT"`
RateOffset float64 `valid:"-" toml:"RATE_OFFSET"`
RateOffsetPercentFirst bool `valid:"-" toml:"RATE_OFFSET_PERCENT_FIRST"`
AmountOfABase float64 `valid:"-" toml:"AMOUNT_OF_A_BASE"` // the size of order to keep on either side
DataTypeA string `valid:"-" toml:"DATA_TYPE_A"`
DataFeedAURL string `valid:"-" toml:"DATA_FEED_A_URL"`
DataTypeB string `valid:"-" toml:"DATA_TYPE_B"`
DataFeedBURL string `valid:"-" toml:"DATA_FEED_B_URL"`
Levels []staticLevel `valid:"-" toml:"LEVELS"`
}
// String impl.
func (c buySellConfig) String() string {
return utils.StructString(c, nil)
}
// makeBuySellStrategy is a factory method
func makeBuySellStrategy(
sdex *SDEX,
pair *model.TradingPair,
assetBase *horizon.Asset,
assetQuote *horizon.Asset,
config *buySellConfig,
) (api.Strategy, error) {
offsetSell := rateOffset{
percent: config.RateOffsetPercent,
absolute: config.RateOffset,
percentFirst: config.RateOffsetPercentFirst,
}
sellSideFeedPair, e := MakeFeedPair(
config.DataTypeA,
config.DataFeedAURL,
config.DataTypeB,
config.DataFeedBURL,
)
if e != nil {
return nil, fmt.Errorf("cannot make the buysell strategy because we could not make the sell side feed pair: %s", e)
}
orderConstraints := sdex.GetOrderConstraints(pair)
sellSideStrategy := makeSellSideStrategy(
sdex,
orderConstraints,
assetBase,
assetQuote,
makeStaticSpreadLevelProvider(
config.Levels,
config.AmountOfABase,
offsetSell,
sellSideFeedPair,
orderConstraints,
),
config.PriceTolerance,
config.AmountTolerance,
false,
)
offsetBuy := rateOffset{
percent: config.RateOffsetPercent,
absolute: config.RateOffset,
percentFirst: config.RateOffsetPercentFirst,
invert: true,
}
buySideFeedPair, e := MakeFeedPair(
config.DataTypeB,
config.DataFeedBURL,
config.DataTypeA,
config.DataFeedAURL,
)
if e != nil {
return nil, fmt.Errorf("cannot make the buysell strategy because we could not make the buy side feed pair: %s", e)
}
// switch sides of base/quote here for buy side
buySideStrategy := makeSellSideStrategy(
sdex,
orderConstraints,
assetQuote,
assetBase,
makeStaticSpreadLevelProvider(
config.Levels,
config.AmountOfABase,
offsetBuy,
buySideFeedPair,
orderConstraints,
),
config.PriceTolerance,
config.AmountTolerance,
true,
)
return makeComposeStrategy(
assetBase,
assetQuote,
buySideStrategy,
sellSideStrategy,
), nil
}