mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 16:55:15 +00:00
Merge pull request #1122 from c9s/narumi/fixedmaker/clamp
strategy: fixedmaker: clamp skew
This commit is contained in:
commit
6ea49f98c2
|
@ -590,3 +590,23 @@ func Abs(a Value) Value {
|
|||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func Clamp(x, min, max Value) Value {
|
||||
if x < min {
|
||||
return min
|
||||
}
|
||||
if x > max {
|
||||
return max
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
func (x Value) Clamp(min, max Value) Value {
|
||||
if x < min {
|
||||
return min
|
||||
}
|
||||
if x > max {
|
||||
return max
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
|
|
@ -1323,3 +1323,23 @@ func (dn Value) Format(mask string) string {
|
|||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func Clamp(x, min, max Value) Value {
|
||||
if x.Compare(min) < 0 {
|
||||
return min
|
||||
}
|
||||
if x.Compare(max) > 0 {
|
||||
return max
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
func (x Value) Clamp(min, max Value) Value {
|
||||
if x.Compare(min) < 0 {
|
||||
return min
|
||||
}
|
||||
if x.Compare(max) > 0 {
|
||||
return max
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
|
|
@ -211,11 +211,6 @@ func (s *Strategy) generateSubmitOrders(ctx context.Context) ([]types.SubmitOrde
|
|||
midPrice := ticker.Buy.Add(ticker.Sell).Div(fixedpoint.NewFromFloat(2.0))
|
||||
log.Infof("mid price: %+v", midPrice)
|
||||
|
||||
// calcualte skew by the difference between base weight and target weight
|
||||
baseValue := baseBalance.Total().Mul(midPrice)
|
||||
baseWeight := baseValue.Div(baseValue.Add(quoteBalance.Total()))
|
||||
skew := s.SkewFactor.Mul(baseWeight.Sub(s.TargetWeight))
|
||||
|
||||
if s.ATRMultiplier.Float64() > 0 {
|
||||
atr := fixedpoint.NewFromFloat(s.atr.Last())
|
||||
log.Infof("atr: %s", atr.String())
|
||||
|
@ -223,12 +218,20 @@ func (s *Strategy) generateSubmitOrders(ctx context.Context) ([]types.SubmitOrde
|
|||
log.Infof("half spread ratio: %s", s.HalfSpreadRatio.String())
|
||||
}
|
||||
|
||||
// calcualte skew by the difference between base weight and target weight
|
||||
baseValue := baseBalance.Total().Mul(midPrice)
|
||||
baseWeight := baseValue.Div(baseValue.Add(quoteBalance.Total()))
|
||||
skew := s.SkewFactor.Mul(s.HalfSpreadRatio).Mul(baseWeight.Sub(s.TargetWeight))
|
||||
|
||||
// let the skew be in the range of [-r, r]
|
||||
skew = skew.Clamp(s.HalfSpreadRatio.Neg(), s.HalfSpreadRatio)
|
||||
|
||||
// calculate bid and ask price
|
||||
// bid price = mid price * (1 - max(r + skew, 0))
|
||||
// bid price = mid price * (1 - r - skew))
|
||||
bidSpreadRatio := fixedpoint.Max(s.HalfSpreadRatio.Add(skew), fixedpoint.Zero)
|
||||
bidPrice := midPrice.Mul(fixedpoint.One.Sub(bidSpreadRatio))
|
||||
log.Infof("bid price: %s", bidPrice.String())
|
||||
// ask price = mid price * (1 + max(r - skew, 0))
|
||||
// ask price = mid price * (1 + r - skew))
|
||||
askSrasedRatio := fixedpoint.Max(s.HalfSpreadRatio.Sub(skew), fixedpoint.Zero)
|
||||
askPrice := midPrice.Mul(fixedpoint.One.Add(askSrasedRatio))
|
||||
log.Infof("ask price: %s", askPrice.String())
|
||||
|
|
Loading…
Reference in New Issue
Block a user