diff --git a/.gitignore b/.gitignore index 0ca7bbe..12e7f95 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ .idea .vscode *.log -*.exe \ No newline at end of file +*.exe +qbtrade.yaml +*.png \ No newline at end of file diff --git a/otp.png b/otp.png index 171f583..54c6e89 100644 Binary files a/otp.png and b/otp.png differ diff --git a/pkg/strategy/ccinr/strategy.go b/pkg/strategy/ccinr/strategy.go index 5c5fc67..a2b2153 100644 --- a/pkg/strategy/ccinr/strategy.go +++ b/pkg/strategy/ccinr/strategy.go @@ -53,6 +53,7 @@ type Strategy struct { Traded map[string]bool TradeType map[string]string TradeKLine map[string]types.KLine + TradeRetry map[string]int // orders LongOrder map[string]types.SubmitOrder @@ -115,6 +116,7 @@ func (s *Strategy) cancelOrders(ctx context.Context, symbol string) { s.Traded[symbol] = false s.TradeType[symbol] = "" + s.TradeRetry[symbol] = 0 } func (s *Strategy) placeOrders(ctx context.Context, kline types.KLine) { @@ -147,10 +149,11 @@ func (s *Strategy) generateOrders(ctx context.Context, kline types.KLine) ([]typ log.Infof(fmt.Sprintf("place order keline info: symbol %s, high %v, low %v, open %v, close %v", symbol, kline.High.Float64(), kline.Low.Float64(), kline.Open.Float64(), kline.Close.Float64())) placePrice := fixedpoint.Value(0) + midPrice := (kline.High.Add(kline.Low)).Div(2) if s.TradeType[symbol] == "long" { - placePrice = kline.Low + placePrice = midPrice } else if s.TradeType[symbol] == "short" { - placePrice = kline.High + placePrice = midPrice } else { return orders, nil } @@ -306,6 +309,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor qbtrade.OrderExecutor, s.Traded = make(map[string]bool) s.TradeType = make(map[string]string) s.TradeKLine = make(map[string]types.KLine) + s.TradeRetry = make(map[string]int) s.ShortOrder = make(map[string]types.SubmitOrder) s.ShortProfitOrder = make(map[string]types.SubmitOrder) @@ -345,6 +349,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor qbtrade.OrderExecutor, for _, symbol := range s.Symbols { s.nr[symbol] = session.Indicators(symbol).NR(s.Interval, s.NrCount, s.StrictMode) s.cci[symbol] = session.Indicators(symbol).CCI(s.Interval, s.CCIWindow) + s.TradeRetry[symbol] = 0 } session.MarketDataStream.OnKLineClosed(func(k types.KLine) { @@ -355,10 +360,14 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor qbtrade.OrderExecutor, if !s.Traded[symbol] { // 如若在下一根k线未成交 则取消订单 - if s.TradeType[symbol] != "" { + if s.TradeType[symbol] != "" && s.TradeRetry[symbol] > 1 { qbtrade.Notify(fmt.Sprintf("交易信号未成交,取消订单: %s", symbol)) s.cancelOrders(ctx, symbol) } + + if s.TradeType[symbol] != "" && s.TradeRetry[symbol] <= 1 { + s.TradeRetry[symbol] = s.TradeRetry[symbol] + 1 + } } s.TradeKLine[symbol] = k } @@ -402,6 +411,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor qbtrade.OrderExecutor, log.Infof("the long order is filled: %+v,id is %d, symbol is %s, type is %s, status is %s", order, order.OrderID, orderSymbol, order.Type, order.Status) s.Traded[orderSymbol] = true + s.TradeRetry[orderSymbol] = 0 qbtrade.Notify("订单成交通知:\n 币种:%s, 方向:%s, 价格:%s, 数量:%s", order.Symbol, s.TradeType, order.Price, order.Quantity) } @@ -409,6 +419,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor qbtrade.OrderExecutor, log.Infof("the short order is filled: %+v,id is %d, symbol is %s, type is %s, status is %s", order, order.OrderID, orderSymbol, order.Type, order.Status) s.Traded[orderSymbol] = true + s.TradeRetry[orderSymbol] = 0 qbtrade.Notify("订单成交通知:\n 币种:%s, 方向:%s, 价格:%s, 数量:%s", order.Symbol, s.TradeType, order.Price, order.Quantity) } @@ -418,6 +429,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor qbtrade.OrderExecutor, "status is %s", order, order.OrderID, orderSymbol, order.Type, order.Status) qbtrade.Notify("订单止盈或止损通知:\n %s", order.Symbol) s.Traded[orderSymbol] = false + s.TradeRetry[orderSymbol] = 0 s.TradeType[orderSymbol] = "" } else { log.Infof("the order is: %+v,id is %d, symbol is %s, type is %s, status is %s",