From e7a56690181cfd6d56f0514255dca8b3500ce686 Mon Sep 17 00:00:00 2001 From: zenix Date: Wed, 7 Sep 2022 15:02:38 +0900 Subject: [PATCH] fix: lowestPrice in elliottwave, add more logs --- pkg/bbgo/source.go | 2 ++ pkg/strategy/elliottwave/strategy.go | 22 +++++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/pkg/bbgo/source.go b/pkg/bbgo/source.go index 25f7becbd..34a3de131 100644 --- a/pkg/bbgo/source.go +++ b/pkg/bbgo/source.go @@ -56,6 +56,8 @@ func (s *selectorInternal) init() { } case "open": s.sourceGetter = func(kline *types.KLine) fixedpoint.Value { return kline.Open } + case "oc2": + s.sourceGetter = func(kline *types.KLine) fixedpoint.Value { return kline.Open.Add(kline.Close).Div(fixedpoint.Two) } default: log.Infof("source not set: %s, use hl2 by default", s.Source) s.sourceGetter = func(kline *types.KLine) fixedpoint.Value { return kline.High.Add(kline.Low).Div(fixedpoint.Two) } diff --git a/pkg/strategy/elliottwave/strategy.go b/pkg/strategy/elliottwave/strategy.go index 81d25803d..c5dd4a443 100644 --- a/pkg/strategy/elliottwave/strategy.go +++ b/pkg/strategy/elliottwave/strategy.go @@ -218,7 +218,7 @@ func (s *Strategy) trailingCheck(price float64, direction string) bool { if s.highestPrice > 0 && s.highestPrice < price { s.highestPrice = price } - if s.lowestPrice > 0 && s.lowestPrice < price { + if s.lowestPrice > 0 && s.lowestPrice > price { s.lowestPrice = price } isShort := direction == "short" @@ -402,6 +402,7 @@ func (s *Strategy) klineHandler1m(ctx context.Context, kline types.KLine) { stoploss := s.Stoploss.Float64() price := s.getLastPrice() pricef := price.Float64() + atr := s.atr.Last() numPending := s.smartCancel(ctx, pricef) if numPending > 0 { @@ -416,9 +417,9 @@ func (s *Strategy) klineHandler1m(ctx context.Context, kline types.KLine) { if s.highestPrice > 0 && highf > s.highestPrice { s.highestPrice = highf } - exitShortCondition := s.sellPrice > 0 && (s.sellPrice*(1.+stoploss) <= highf || + exitShortCondition := s.sellPrice > 0 && (s.sellPrice*(1.+stoploss) <= highf || s.sellPrice+atr <= highf || s.trailingCheck(highf, "short")) - exitLongCondition := s.buyPrice > 0 && (s.buyPrice*(1.-stoploss) >= lowf || + exitLongCondition := s.buyPrice > 0 && (s.buyPrice*(1.-stoploss) >= lowf || s.buyPrice-atr >= lowf || s.trailingCheck(lowf, "long")) if exitShortCondition || exitLongCondition { @@ -456,11 +457,22 @@ func (s *Strategy) klineHandler(ctx context.Context, kline types.KLine) { ewo := types.Array(s.ewo, 4) bull := kline.Close.Compare(kline.Open) > 0 + balances := s.GeneralOrderExecutor.Session().GetAccount().Balances() + bbgo.Notify("source: %.4f, price: %.4f lowest: %.4f highest: %.4f sp %.4f bp %.4f", sourcef, pricef, s.lowestPrice, s.highestPrice, s.sellPrice, s.buyPrice) + bbgo.Notify("balances: [Total] %v %s [Base] %s(%v %s) [Quote] %s", + s.CalcAssetValue(price), + s.Market.QuoteCurrency, + balances[s.Market.BaseCurrency].String(), + balances[s.Market.BaseCurrency].Total().Mul(price), + s.Market.QuoteCurrency, + balances[s.Market.QuoteCurrency].String(), + ) + shortCondition := ewo[0] < ewo[1] && ewo[1] >= ewo[2] && (ewo[1] <= ewo[2] || ewo[2] >= ewo[3]) || s.sellPrice == 0 && ewo[0] < ewo[1] && ewo[1] < ewo[2] longCondition := ewo[0] > ewo[1] && ewo[1] <= ewo[2] && (ewo[1] >= ewo[2] || ewo[2] <= ewo[3]) || s.buyPrice == 0 && ewo[0] > ewo[1] && ewo[1] > ewo[2] - exitShortCondition := s.sellPrice > 0 && !shortCondition && s.sellPrice*(1.+stoploss) <= highf || s.sellPrice+atr <= sourcef || s.trailingCheck(highf, "short") - exitLongCondition := s.buyPrice > 0 && !longCondition && s.buyPrice*(1.-stoploss) >= lowf || s.buyPrice-atr >= sourcef || s.trailingCheck(lowf, "long") + exitShortCondition := s.sellPrice > 0 && !shortCondition && s.sellPrice*(1.+stoploss) <= highf || s.sellPrice+atr <= highf || s.trailingCheck(highf, "short") + exitLongCondition := s.buyPrice > 0 && !longCondition && s.buyPrice*(1.-stoploss) >= lowf || s.buyPrice-atr >= lowf || s.trailingCheck(lowf, "long") if exitShortCondition || exitLongCondition { if err := s.GeneralOrderExecutor.GracefulCancel(ctx); err != nil {