grid2: refactor check spread

This commit is contained in:
c9s 2022-12-07 12:24:52 +08:00
parent 02bebe8ed1
commit 489b025702
2 changed files with 31 additions and 14 deletions

View File

@ -36,7 +36,7 @@ exchangeStrategies:
symbol: BTCUSDT symbol: BTCUSDT
upperPrice: 18_000.0 upperPrice: 18_000.0
lowerPrice: 16_000.0 lowerPrice: 16_000.0
gridNumber: 200 gridNumber: 100
## compound is used for buying more inventory when the profit is made by the filled SELL order. ## compound is used for buying more inventory when the profit is made by the filled SELL order.
## when compound is disabled, fixed quantity is used for each grid order. ## when compound is disabled, fixed quantity is used for each grid order.
@ -70,12 +70,12 @@ exchangeStrategies:
# amount: 10.0 # amount: 10.0
## 2) fixed quantity: it will use your balance to place orders with the fixed quantity. e.g. 0.001 BTC ## 2) fixed quantity: it will use your balance to place orders with the fixed quantity. e.g. 0.001 BTC
# quantity: 0.001 quantity: 0.001
## 3) quoteInvestment and baseInvestment: when using quoteInvestment, the strategy will automatically calculate your best quantity for the whole grid. ## 3) quoteInvestment and baseInvestment: when using quoteInvestment, the strategy will automatically calculate your best quantity for the whole grid.
## quoteInvestment is required, and baseInvestment is optional (could be zero) ## quoteInvestment is required, and baseInvestment is optional (could be zero)
## if you have existing BTC position and want to reuse it you can set the baseInvestment. ## if you have existing BTC position and want to reuse it you can set the baseInvestment.
quoteInvestment: 10_000 # quoteInvestment: 10_000
# baseInvestment: 1.0 # baseInvestment: 1.0
feeRate: 0.075% feeRate: 0.075%

View File

@ -136,17 +136,8 @@ func (s *Strategy) Validate() error {
s.FeeRate = fixedpoint.NewFromFloat(0.1 * 0.01) // 0.1%, 0.075% with BNB s.FeeRate = fixedpoint.NewFromFloat(0.1 * 0.01) // 0.1%, 0.075% with BNB
} }
if !s.ProfitSpread.IsZero() { if err := s.checkSpread(); err != nil {
// the min fee rate from 2 maker/taker orders (with 0.1 rate for profit) return err
gridFeeRate := s.FeeRate.Mul(fixedpoint.NewFromFloat(2.01))
if s.ProfitSpread.Div(s.LowerPrice).Compare(gridFeeRate) < 0 {
return fmt.Errorf("profitSpread %f %s is too small for lower price, less than the fee rate: %s", s.ProfitSpread.Float64(), s.ProfitSpread.Div(s.LowerPrice).Percentage(), s.FeeRate.Percentage())
}
if s.ProfitSpread.Div(s.UpperPrice).Compare(gridFeeRate) < 0 {
return fmt.Errorf("profitSpread %f %s is too small for upper price, less than the fee rate: %s", s.ProfitSpread.Float64(), s.ProfitSpread.Div(s.UpperPrice).Percentage(), s.FeeRate.Percentage())
}
} }
if err := s.QuantityOrAmount.Validate(); err != nil { if err := s.QuantityOrAmount.Validate(); err != nil {
@ -171,6 +162,32 @@ func (s *Strategy) InstanceID() string {
return fmt.Sprintf("%s-%s-%d-%d-%d", ID, s.Symbol, s.GridNum, s.UpperPrice.Int(), s.LowerPrice.Int()) return fmt.Sprintf("%s-%s-%d-%d-%d", ID, s.Symbol, s.GridNum, s.UpperPrice.Int(), s.LowerPrice.Int())
} }
func (s *Strategy) checkSpread() error {
gridNum := fixedpoint.NewFromInt(s.GridNum)
spread := s.ProfitSpread
if spread.IsZero() {
spread = s.UpperPrice.Sub(s.LowerPrice).Div(gridNum)
}
feeRate := s.FeeRate
if feeRate.IsZero() {
feeRate = fixedpoint.NewFromFloat(0.075 * 0.01)
}
// the min fee rate from 2 maker/taker orders (with 0.1 rate for profit)
gridFeeRate := feeRate.Mul(fixedpoint.NewFromFloat(2.01))
if spread.Div(s.LowerPrice).Compare(gridFeeRate) < 0 {
return fmt.Errorf("profitSpread %f %s is too small for lower price, less than the grid fee rate: %s", spread.Float64(), spread.Div(s.LowerPrice).Percentage(), gridFeeRate.Percentage())
}
if spread.Div(s.UpperPrice).Compare(gridFeeRate) < 0 {
return fmt.Errorf("profitSpread %f %s is too small for upper price, less than the grid fee rate: %s", spread.Float64(), spread.Div(s.UpperPrice).Percentage(), gridFeeRate.Percentage())
}
return nil
}
func (s *Strategy) handleOrderCanceled(o types.Order) { func (s *Strategy) handleOrderCanceled(o types.Order) {
s.logger.Infof("GRID ORDER CANCELED: %s", o.String()) s.logger.Infof("GRID ORDER CANCELED: %s", o.String())