Skip to content

Commit 6ea49f9

Browse files
authored
Merge pull request #1122 from c9s/narumi/fixedmaker/clamp
strategy: fixedmaker: clamp skew
2 parents b455ae7 + 7114016 commit 6ea49f9

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

pkg/fixedpoint/convert.go

+20
Original file line numberDiff line numberDiff line change
@@ -590,3 +590,23 @@ func Abs(a Value) Value {
590590
}
591591
return a
592592
}
593+
594+
func Clamp(x, min, max Value) Value {
595+
if x < min {
596+
return min
597+
}
598+
if x > max {
599+
return max
600+
}
601+
return x
602+
}
603+
604+
func (x Value) Clamp(min, max Value) Value {
605+
if x < min {
606+
return min
607+
}
608+
if x > max {
609+
return max
610+
}
611+
return x
612+
}

pkg/fixedpoint/dec.go

+20
Original file line numberDiff line numberDiff line change
@@ -1323,3 +1323,23 @@ func (dn Value) Format(mask string) string {
13231323
}
13241324
return buf.String()
13251325
}
1326+
1327+
func Clamp(x, min, max Value) Value {
1328+
if x.Compare(min) < 0 {
1329+
return min
1330+
}
1331+
if x.Compare(max) > 0 {
1332+
return max
1333+
}
1334+
return x
1335+
}
1336+
1337+
func (x Value) Clamp(min, max Value) Value {
1338+
if x.Compare(min) < 0 {
1339+
return min
1340+
}
1341+
if x.Compare(max) > 0 {
1342+
return max
1343+
}
1344+
return x
1345+
}

pkg/strategy/fixedmaker/strategy.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -211,24 +211,27 @@ func (s *Strategy) generateSubmitOrders(ctx context.Context) ([]types.SubmitOrde
211211
midPrice := ticker.Buy.Add(ticker.Sell).Div(fixedpoint.NewFromFloat(2.0))
212212
log.Infof("mid price: %+v", midPrice)
213213

214-
// calcualte skew by the difference between base weight and target weight
215-
baseValue := baseBalance.Total().Mul(midPrice)
216-
baseWeight := baseValue.Div(baseValue.Add(quoteBalance.Total()))
217-
skew := s.SkewFactor.Mul(baseWeight.Sub(s.TargetWeight))
218-
219214
if s.ATRMultiplier.Float64() > 0 {
220215
atr := fixedpoint.NewFromFloat(s.atr.Last())
221216
log.Infof("atr: %s", atr.String())
222217
s.HalfSpreadRatio = s.ATRMultiplier.Mul(atr).Div(midPrice)
223218
log.Infof("half spread ratio: %s", s.HalfSpreadRatio.String())
224219
}
225220

221+
// calcualte skew by the difference between base weight and target weight
222+
baseValue := baseBalance.Total().Mul(midPrice)
223+
baseWeight := baseValue.Div(baseValue.Add(quoteBalance.Total()))
224+
skew := s.SkewFactor.Mul(s.HalfSpreadRatio).Mul(baseWeight.Sub(s.TargetWeight))
225+
226+
// let the skew be in the range of [-r, r]
227+
skew = skew.Clamp(s.HalfSpreadRatio.Neg(), s.HalfSpreadRatio)
228+
226229
// calculate bid and ask price
227-
// bid price = mid price * (1 - max(r + skew, 0))
230+
// bid price = mid price * (1 - r - skew))
228231
bidSpreadRatio := fixedpoint.Max(s.HalfSpreadRatio.Add(skew), fixedpoint.Zero)
229232
bidPrice := midPrice.Mul(fixedpoint.One.Sub(bidSpreadRatio))
230233
log.Infof("bid price: %s", bidPrice.String())
231-
// ask price = mid price * (1 + max(r - skew, 0))
234+
// ask price = mid price * (1 + r - skew))
232235
askSrasedRatio := fixedpoint.Max(s.HalfSpreadRatio.Sub(skew), fixedpoint.Zero)
233236
askPrice := midPrice.Mul(fixedpoint.One.Add(askSrasedRatio))
234237
log.Infof("ask price: %s", askPrice.String())

0 commit comments

Comments
 (0)