forked from stellar-deprecated/kelp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirrorStrategy.go
189 lines (169 loc) · 6.45 KB
/
mirrorStrategy.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
package plugins
import (
"log"
"github.com/lightyeario/kelp/api"
"github.com/lightyeario/kelp/model"
"github.com/lightyeario/kelp/support/utils"
"github.com/stellar/go/build"
"github.com/stellar/go/clients/horizon"
)
// mirrorConfig contains the configuration params for this strategy
type mirrorConfig struct {
EXCHANGE string `valid:"-"`
EXCHANGE_BASE string `valid:"-"`
EXCHANGE_QUOTE string `valid:"-"`
ORDERBOOK_DEPTH int32 `valid:"-"`
VOLUME_DIVIDE_BY float64 `valid:"-"`
PER_LEVEL_SPREAD float64 `valid:"-"`
}
// String impl.
func (c mirrorConfig) String() string {
return utils.StructString(c, nil)
}
// mirrorStrategy is a strategy to mirror the orderbook of a given exchange
type mirrorStrategy struct {
sdex *SDEX
orderbookPair *model.TradingPair
baseAsset *horizon.Asset
quoteAsset *horizon.Asset
config *mirrorConfig
tradeAPI api.TradeAPI
}
// ensure this implements Strategy
var _ api.Strategy = &mirrorStrategy{}
// makeMirrorStrategy is a factory method
func makeMirrorStrategy(sdex *SDEX, baseAsset *horizon.Asset, quoteAsset *horizon.Asset, config *mirrorConfig) api.Strategy {
exchange := MakeExchange(config.EXCHANGE)
orderbookPair := &model.TradingPair{
Base: exchange.GetAssetConverter().MustFromString(config.EXCHANGE_BASE),
Quote: exchange.GetAssetConverter().MustFromString(config.EXCHANGE_QUOTE),
}
return &mirrorStrategy{
sdex: sdex,
orderbookPair: orderbookPair,
baseAsset: baseAsset,
quoteAsset: quoteAsset,
config: config,
tradeAPI: api.TradeAPI(exchange),
}
}
// PruneExistingOffers deletes any extra offers
func (s mirrorStrategy) PruneExistingOffers(buyingAOffers []horizon.Offer, sellingAOffers []horizon.Offer) ([]build.TransactionMutator, []horizon.Offer, []horizon.Offer) {
return []build.TransactionMutator{}, buyingAOffers, sellingAOffers
}
// PreUpdate changes the strategy's state in prepration for the update
func (s *mirrorStrategy) PreUpdate(maxAssetA float64, maxAssetB float64, trustA float64, trustB float64, buyingAOffers []horizon.Offer, sellingAOffers []horizon.Offer) error {
return nil
}
// UpdateWithOps builds the operations we want performed on the account
func (s mirrorStrategy) UpdateWithOps(
buyingAOffers []horizon.Offer,
sellingAOffers []horizon.Offer,
) ([]build.TransactionMutator, error) {
ob, e := s.tradeAPI.GetOrderBook(s.orderbookPair, s.config.ORDERBOOK_DEPTH)
if e != nil {
return nil, e
}
buyOps := s.updateLevels(
buyingAOffers,
ob.Bids(),
s.sdex.ModifyBuyOffer,
s.sdex.CreateBuyOffer,
(1 - s.config.PER_LEVEL_SPREAD),
true,
)
log.Printf("num. buyOps in this update: %d\n", len(buyOps))
sellOps := s.updateLevels(
sellingAOffers,
ob.Asks(),
s.sdex.ModifySellOffer,
s.sdex.CreateSellOffer,
(1 + s.config.PER_LEVEL_SPREAD),
false,
)
log.Printf("num. sellOps in this update: %d\n", len(sellOps))
ops := []build.TransactionMutator{}
if len(ob.Bids()) > 0 && len(sellingAOffers) > 0 && ob.Bids()[0].Price.AsFloat() >= utils.PriceAsFloat(sellingAOffers[0].Price) {
ops = append(ops, sellOps...)
ops = append(ops, buyOps...)
} else {
ops = append(ops, buyOps...)
ops = append(ops, sellOps...)
}
return ops, nil
}
func (s *mirrorStrategy) updateLevels(
oldOffers []horizon.Offer,
newOrders []model.Order,
modifyOffer func(offer horizon.Offer, price float64, amount float64) *build.ManageOfferBuilder,
createOffer func(baseAsset horizon.Asset, quoteAsset horizon.Asset, price float64, amount float64) *build.ManageOfferBuilder,
priceMultiplier float64,
hackPriceInvertForBuyOrderChangeCheck bool, // needed because createBuy and modBuy inverts price so we need this for price comparison in doModifyOffer
) []build.TransactionMutator {
ops := []build.TransactionMutator{}
if len(newOrders) >= len(oldOffers) {
offset := len(newOrders) - len(oldOffers)
for i := len(newOrders) - 1; (i - offset) >= 0; i-- {
ops = doModifyOffer(oldOffers[i-offset], newOrders[i], priceMultiplier, s.config.VOLUME_DIVIDE_BY, modifyOffer, ops, hackPriceInvertForBuyOrderChangeCheck)
}
// create offers for remaining new bids
for i := offset - 1; i >= 0; i-- {
price := model.NumberFromFloat(newOrders[i].Price.AsFloat()*priceMultiplier, utils.SdexPrecision).AsFloat()
vol := model.NumberFromFloat(newOrders[i].Volume.AsFloat()/s.config.VOLUME_DIVIDE_BY, utils.SdexPrecision).AsFloat()
mo := createOffer(*s.baseAsset, *s.quoteAsset, price, vol)
if mo != nil {
ops = append(ops, *mo)
}
}
} else {
offset := len(oldOffers) - len(newOrders)
for i := len(oldOffers) - 1; (i - offset) >= 0; i-- {
ops = doModifyOffer(oldOffers[i], newOrders[i-offset], priceMultiplier, s.config.VOLUME_DIVIDE_BY, modifyOffer, ops, hackPriceInvertForBuyOrderChangeCheck)
}
// delete remaining prior offers
for i := offset - 1; i >= 0; i-- {
op := s.sdex.DeleteOffer(oldOffers[i])
ops = append(ops, op)
}
}
return ops
}
func doModifyOffer(
oldOffer horizon.Offer,
newOrder model.Order,
priceMultiplier float64,
volumeDivideBy float64,
modifyOffer func(offer horizon.Offer, price float64, amount float64) *build.ManageOfferBuilder,
ops []build.TransactionMutator,
hackPriceInvertForBuyOrderChangeCheck bool, // needed because createBuy and modBuy inverts price so we need this for price comparison in doModifyOffer
) []build.TransactionMutator {
price := newOrder.Price.AsFloat() * priceMultiplier
vol := newOrder.Volume.AsFloat() / volumeDivideBy
oldPrice := model.MustNumberFromString(oldOffer.Price, 6)
oldVol := model.MustNumberFromString(oldOffer.Amount, 6)
if hackPriceInvertForBuyOrderChangeCheck {
// we want to multiply oldVol by the original oldPrice so we can get the correct oldVol, since ModifyBuyOffer multiplies price * vol
oldVol = model.NumberFromFloat(oldVol.AsFloat()*oldPrice.AsFloat(), 6)
oldPrice = model.NumberFromFloat(1/oldPrice.AsFloat(), 6)
}
newPrice := model.NumberFromFloat(price, 6)
newVol := model.NumberFromFloat(vol, 6)
epsilon := 0.0001
sameOrderParams := utils.FloatEquals(oldPrice.AsFloat(), newPrice.AsFloat(), epsilon) && utils.FloatEquals(oldVol.AsFloat(), newVol.AsFloat(), epsilon)
if sameOrderParams {
return ops
}
mo := modifyOffer(
oldOffer,
model.NumberFromFloat(price, utils.SdexPrecision).AsFloat(),
model.NumberFromFloat(vol, utils.SdexPrecision).AsFloat(),
)
if mo != nil {
ops = append(ops, *mo)
}
return ops
}
// PostUpdate changes the strategy's state after the update has taken place
func (s *mirrorStrategy) PostUpdate() error {
return nil
}