mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
add skew to adjust bid/ask price
This commit is contained in:
parent
94f2bcf6fb
commit
cf9a2e55bf
|
@ -33,6 +33,10 @@ type Strategy struct {
|
||||||
OrderType types.OrderType `json:"orderType"`
|
OrderType types.OrderType `json:"orderType"`
|
||||||
DryRun bool `json:"dryRun"`
|
DryRun bool `json:"dryRun"`
|
||||||
|
|
||||||
|
// SkewFactor is used to calculate the skew of bid/ask price
|
||||||
|
SkewFactor fixedpoint.Value `json:"skewFactor"`
|
||||||
|
TargetWeight fixedpoint.Value `json:"targetWeight"`
|
||||||
|
|
||||||
// persistence fields
|
// persistence fields
|
||||||
Position *types.Position `json:"position,omitempty" persistence:"position"`
|
Position *types.Position `json:"position,omitempty" persistence:"position"`
|
||||||
ProfitStats *types.ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"`
|
ProfitStats *types.ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"`
|
||||||
|
@ -162,6 +166,8 @@ func (s *Strategy) replenish(ctx context.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) generateSubmitOrders(ctx context.Context) ([]types.SubmitOrder, error) {
|
func (s *Strategy) generateSubmitOrders(ctx context.Context) ([]types.SubmitOrder, error) {
|
||||||
|
orders := []types.SubmitOrder{}
|
||||||
|
|
||||||
baseBalance, ok := s.session.GetAccount().Balance(s.Market.BaseCurrency)
|
baseBalance, ok := s.session.GetAccount().Balance(s.Market.BaseCurrency)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("base currency %s balance not found", s.Market.BaseCurrency)
|
return nil, fmt.Errorf("base currency %s balance not found", s.Market.BaseCurrency)
|
||||||
|
@ -181,24 +187,29 @@ 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)
|
||||||
|
|
||||||
orders := []types.SubmitOrder{}
|
// 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))
|
||||||
|
|
||||||
// calculate buy and sell price
|
// calculate bid and ask price
|
||||||
// buy price = mid price * (1 - r)
|
// bid price = mid price * (1 - max(r + skew, 0))
|
||||||
buyPrice := midPrice.Mul(fixedpoint.One.Sub(s.HalfSpreadRatio))
|
bidSpreadRatio := fixedpoint.Max(s.HalfSpreadRatio.Add(skew), fixedpoint.Zero)
|
||||||
log.Infof("buy price: %+v", buyPrice)
|
bidPrice := midPrice.Mul(fixedpoint.One.Sub(bidSpreadRatio))
|
||||||
// sell price = mid price * (1 + r)
|
log.Infof("bid price: %s", bidPrice.String())
|
||||||
sellPrice := midPrice.Mul(fixedpoint.One.Add(s.HalfSpreadRatio))
|
// ask price = mid price * (1 + max(r - skew, 0))
|
||||||
log.Infof("sell price: %+v", sellPrice)
|
askSrasedRatio := fixedpoint.Max(s.HalfSpreadRatio.Sub(skew), fixedpoint.Zero)
|
||||||
|
askPrice := midPrice.Mul(fixedpoint.One.Add(askSrasedRatio))
|
||||||
|
log.Infof("ask price: %s", askPrice.String())
|
||||||
|
|
||||||
// check balance and generate orders
|
// check balance and generate orders
|
||||||
amount := s.Quantity.Mul(buyPrice)
|
amount := s.Quantity.Mul(bidPrice)
|
||||||
if quoteBalance.Available.Compare(amount) > 0 {
|
if quoteBalance.Available.Compare(amount) > 0 {
|
||||||
orders = append(orders, types.SubmitOrder{
|
orders = append(orders, types.SubmitOrder{
|
||||||
Symbol: s.Symbol,
|
Symbol: s.Symbol,
|
||||||
Side: types.SideTypeBuy,
|
Side: types.SideTypeBuy,
|
||||||
Type: s.OrderType,
|
Type: s.OrderType,
|
||||||
Price: buyPrice,
|
Price: bidPrice,
|
||||||
Quantity: s.Quantity,
|
Quantity: s.Quantity,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
@ -210,7 +221,7 @@ func (s *Strategy) generateSubmitOrders(ctx context.Context) ([]types.SubmitOrde
|
||||||
Symbol: s.Symbol,
|
Symbol: s.Symbol,
|
||||||
Side: types.SideTypeSell,
|
Side: types.SideTypeSell,
|
||||||
Type: s.OrderType,
|
Type: s.OrderType,
|
||||||
Price: sellPrice,
|
Price: askPrice,
|
||||||
Quantity: s.Quantity,
|
Quantity: s.Quantity,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user