pivotshort: fix bounce short

This commit is contained in:
c9s 2022-06-11 16:33:21 +08:00
parent ec68dc2f40
commit 3d0c0717ba
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 25 additions and 16 deletions

View File

@ -54,18 +54,18 @@ exchangeStrategies:
# ratio is the ratio of the resistance price,
# higher the ratio, lower the price
# first_layer_price = resistance_price * (1 - ratio)
# second_layer_price = (resistance_price * (1 - ratio)) * (1.0 + layerSpread)
ratio: 0.1%
numOfLayers: 10
# second_layer_price = (resistance_price * (1 - ratio)) * (2 * layerSpread)
ratio: 0%
numOfLayers: 1
layerSpread: 0.1%
exit:
# roiStopLossPercentage is the stop loss percentage of the position ROI (currently the price change)
roiStopLossPercentage: 1%
roiStopLossPercentage: 2%
# roiTakeProfitPercentage is used to force taking profit by percentage of the position ROI (currently the price change)
# force to take the profit ROI exceeded the percentage.
roiTakeProfitPercentage: 25%
roiTakeProfitPercentage: 30%
# roiMinTakeProfitPercentage applies to lowerShadowRatio and cumulatedVolume exit options
roiMinTakeProfitPercentage: 10%

View File

@ -335,8 +335,16 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
log.Infof("last price: %f, possible resistance prices: %+v", closePrice, s.resistancePrices)
if len(s.resistancePrices) > 0 {
s.currentBounceShortPrice = fixedpoint.NewFromFloat(s.resistancePrices[0])
s.placeBounceSellOrders(ctx, s.currentBounceShortPrice, orderExecutor)
resistancePrice := fixedpoint.NewFromFloat(s.resistancePrices[0])
if resistancePrice.Compare(s.currentBounceShortPrice) != 0 {
log.Infof("updating resistance price... possible resistance prices: %+v", s.resistancePrices)
if err := s.activeMakerOrders.GracefulCancel(ctx, s.session.Exchange); err != nil {
log.WithError(err).Errorf("graceful cancel order error")
}
s.currentBounceShortPrice = resistancePrice
s.placeBounceSellOrders(ctx, s.currentBounceShortPrice, orderExecutor)
}
}
})
@ -508,23 +516,26 @@ func (s *Strategy) placeBounceSellOrders(ctx context.Context, resistancePrice fi
if numLayers == 0 {
numLayers = 1
}
numLayersF := fixedpoint.NewFromInt(int64(numLayers))
layerSpread := s.BounceShort.LayerSpread
if layerSpread.IsZero() {
layerSpread = s.BounceShort.Ratio.Div(numLayersF)
}
quantity := totalQuantity.Div(numLayersF)
log.Infof("placing bounce short orders: resistance price = %f, layer quantity = %f, num of layers = %d", resistancePrice.Float64(), quantity.Float64(), numLayers)
for i := 0; i < numLayers; i++ {
balances := s.session.GetAccount().Balances()
quoteBalance, _ := balances[s.Market.QuoteCurrency]
baseBalance, _ := balances[s.Market.BaseCurrency]
// price = (resistance_price * (1.0 - ratio)) * ((1.0 + layerSpread) * i)
price := resistancePrice.Mul(
fixedpoint.One.Sub(s.BounceShort.Ratio)).Mul(
fixedpoint.One.Add(layerSpread).Mul(fixedpoint.NewFromInt(int64(i))))
price := resistancePrice.Mul(fixedpoint.One.Sub(s.BounceShort.Ratio))
spread := layerSpread.Mul(fixedpoint.NewFromInt(int64(i)))
price = price.Add(spread)
log.Infof("price = %f", price.Float64())
log.Infof("placing bounce short order #%d: price = %f, quantity = %f", i, price.Float64(), quantity.Float64())
if futuresMode {
if quantity.Mul(price).Compare(quoteBalance.Available) <= 0 {
@ -571,7 +582,6 @@ func (s *Strategy) preloadPivot(pivot *indicator.Pivot, store *bbgo.MarketDataSt
func findPossibleResistancePrices(closePrice float64, minDistance float64, lows []float64) []float64 {
// sort float64 in increasing order
sort.Float64s(lows)
log.Infof("sorted resistance lows: %+v", lows)
var resistancePrices []float64
for _, low := range lows {
@ -590,6 +600,5 @@ func findPossibleResistancePrices(closePrice float64, minDistance float64, lows
resistancePrices = append(resistancePrices, low)
}
log.Infof("possible resistance prices: %+v", resistancePrices)
return resistancePrices
}