pivotshort: improve useQuantityOrBaseBalance and add bounce short check

This commit is contained in:
c9s 2022-06-26 19:45:37 +08:00
parent 4d862a4286
commit 1557423229
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 34 additions and 25 deletions

View File

@ -130,10 +130,14 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
}
func (s *Strategy) useQuantityOrBaseBalance(quantity fixedpoint.Value) fixedpoint.Value {
if quantity.IsZero() {
if balance, ok := s.session.Account.Balance(s.Market.BaseCurrency); ok {
balance, hasBalance := s.session.Account.Balance(s.Market.BaseCurrency)
if hasBalance {
if quantity.IsZero() {
bbgo.Notify("sell quantity is not set, submitting sell with all base balance: %s", balance.Available.String())
quantity = balance.Available
} else {
quantity = fixedpoint.Min(quantity, balance.Available)
}
}
@ -240,37 +244,39 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
method.Bind(session, s.orderExecutor)
}
session.UserDataStream.OnStart(func() {
lastKLine := s.preloadPivot(s.pivot, store)
if s.BounceShort != nil && s.BounceShort.Enabled {
session.UserDataStream.OnStart(func() {
lastKLine := s.preloadPivot(s.pivot, store)
if s.resistancePivot != nil {
s.preloadPivot(s.resistancePivot, store)
}
if s.resistancePivot != nil {
s.preloadPivot(s.resistancePivot, store)
}
if lastKLine == nil {
return
}
if lastKLine == nil {
return
}
if s.resistancePivot != nil {
lows := s.resistancePivot.Lows
minDistance := s.BounceShort.MinDistance.Float64()
closePrice := lastKLine.Close.Float64()
s.resistancePrices = findPossibleResistancePrices(closePrice, minDistance, lows)
log.Infof("last price: %f, possible resistance prices: %+v", closePrice, s.resistancePrices)
if s.resistancePivot != nil {
lows := s.resistancePivot.Lows
minDistance := s.BounceShort.MinDistance.Float64()
closePrice := lastKLine.Close.Float64()
s.resistancePrices = findPossibleResistancePrices(closePrice, minDistance, lows)
log.Infof("last price: %f, possible resistance prices: %+v", closePrice, s.resistancePrices)
if len(s.resistancePrices) > 0 {
resistancePrice := fixedpoint.NewFromFloat(s.resistancePrices[0])
if resistancePrice.Compare(s.currentBounceShortPrice) != 0 {
log.Infof("updating resistance price... possible resistance prices: %+v", s.resistancePrices)
if len(s.resistancePrices) > 0 {
resistancePrice := fixedpoint.NewFromFloat(s.resistancePrices[0])
if resistancePrice.Compare(s.currentBounceShortPrice) != 0 {
log.Infof("updating resistance price... possible resistance prices: %+v", s.resistancePrices)
_ = s.orderExecutor.GracefulCancel(ctx)
_ = s.orderExecutor.GracefulCancel(ctx)
s.currentBounceShortPrice = resistancePrice
s.placeBounceSellOrders(ctx, s.currentBounceShortPrice)
s.currentBounceShortPrice = resistancePrice
s.placeBounceSellOrders(ctx, s.currentBounceShortPrice)
}
}
}
}
})
})
}
// Always check whether you can open a short position or not
session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {

View File

@ -31,6 +31,9 @@ func (s *TradeStats) Add(pnl fixedpoint.Value) {
s.MostLossTrade = fixedpoint.Min(s.MostLossTrade, pnl)
}
// The win/loss ratio is your wins divided by your losses.
// In the example, suppose for the sake of simplicity that 60 trades were winners, and 40 were losers.
// Your win/loss ratio would be 60/40 = 1.5. That would mean that you are winning 50% more often than you are losing.
if s.NumOfLossTrade == 0 && s.NumOfProfitTrade > 0 {
s.WinningRatio = fixedpoint.One
} else {