Merge pull request #1122 from c9s/narumi/fixedmaker/clamp

strategy: fixedmaker: clamp skew
This commit is contained in:
なるみ 2023-03-18 23:38:23 +08:00 committed by GitHub
commit 6ea49f98c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 7 deletions

View File

@ -590,3 +590,23 @@ func Abs(a Value) Value {
} }
return a 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
}

View File

@ -1323,3 +1323,23 @@ func (dn Value) Format(mask string) string {
} }
return buf.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
}

View File

@ -211,11 +211,6 @@ func (s *Strategy) generateSubmitOrders(ctx context.Context) ([]types.SubmitOrde
midPrice := ticker.Buy.Add(ticker.Sell).Div(fixedpoint.NewFromFloat(2.0)) midPrice := ticker.Buy.Add(ticker.Sell).Div(fixedpoint.NewFromFloat(2.0))
log.Infof("mid price: %+v", midPrice) 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 { if s.ATRMultiplier.Float64() > 0 {
atr := fixedpoint.NewFromFloat(s.atr.Last()) atr := fixedpoint.NewFromFloat(s.atr.Last())
log.Infof("atr: %s", atr.String()) 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()) 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 // 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) bidSpreadRatio := fixedpoint.Max(s.HalfSpreadRatio.Add(skew), fixedpoint.Zero)
bidPrice := midPrice.Mul(fixedpoint.One.Sub(bidSpreadRatio)) bidPrice := midPrice.Mul(fixedpoint.One.Sub(bidSpreadRatio))
log.Infof("bid price: %s", bidPrice.String()) 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) askSrasedRatio := fixedpoint.Max(s.HalfSpreadRatio.Sub(skew), fixedpoint.Zero)
askPrice := midPrice.Mul(fixedpoint.One.Add(askSrasedRatio)) askPrice := midPrice.Mul(fixedpoint.One.Add(askSrasedRatio))
log.Infof("ask price: %s", askPrice.String()) log.Infof("ask price: %s", askPrice.String())