mid price

This commit is contained in:
ying liu 2024-07-29 12:42:15 +08:00
parent 855ce2324d
commit 7404bdf0dc
3 changed files with 18 additions and 4 deletions

4
.gitignore vendored
View File

@ -1,4 +1,6 @@
.idea .idea
.vscode .vscode
*.log *.log
*.exe *.exe
qbtrade.yaml
*.png

BIN
otp.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 3.0 KiB

View File

@ -53,6 +53,7 @@ type Strategy struct {
Traded map[string]bool Traded map[string]bool
TradeType map[string]string TradeType map[string]string
TradeKLine map[string]types.KLine TradeKLine map[string]types.KLine
TradeRetry map[string]int
// orders // orders
LongOrder map[string]types.SubmitOrder LongOrder map[string]types.SubmitOrder
@ -115,6 +116,7 @@ func (s *Strategy) cancelOrders(ctx context.Context, symbol string) {
s.Traded[symbol] = false s.Traded[symbol] = false
s.TradeType[symbol] = "" s.TradeType[symbol] = ""
s.TradeRetry[symbol] = 0
} }
func (s *Strategy) placeOrders(ctx context.Context, kline types.KLine) { 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, 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())) kline.High.Float64(), kline.Low.Float64(), kline.Open.Float64(), kline.Close.Float64()))
placePrice := fixedpoint.Value(0) placePrice := fixedpoint.Value(0)
midPrice := (kline.High.Add(kline.Low)).Div(2)
if s.TradeType[symbol] == "long" { if s.TradeType[symbol] == "long" {
placePrice = kline.Low placePrice = midPrice
} else if s.TradeType[symbol] == "short" { } else if s.TradeType[symbol] == "short" {
placePrice = kline.High placePrice = midPrice
} else { } else {
return orders, nil return orders, nil
} }
@ -306,6 +309,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor qbtrade.OrderExecutor,
s.Traded = make(map[string]bool) s.Traded = make(map[string]bool)
s.TradeType = make(map[string]string) s.TradeType = make(map[string]string)
s.TradeKLine = make(map[string]types.KLine) s.TradeKLine = make(map[string]types.KLine)
s.TradeRetry = make(map[string]int)
s.ShortOrder = make(map[string]types.SubmitOrder) s.ShortOrder = make(map[string]types.SubmitOrder)
s.ShortProfitOrder = 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 { for _, symbol := range s.Symbols {
s.nr[symbol] = session.Indicators(symbol).NR(s.Interval, s.NrCount, s.StrictMode) 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.cci[symbol] = session.Indicators(symbol).CCI(s.Interval, s.CCIWindow)
s.TradeRetry[symbol] = 0
} }
session.MarketDataStream.OnKLineClosed(func(k types.KLine) { 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] { if !s.Traded[symbol] {
// 如若在下一根k线未成交 则取消订单 // 如若在下一根k线未成交 则取消订单
if s.TradeType[symbol] != "" { if s.TradeType[symbol] != "" && s.TradeRetry[symbol] > 1 {
qbtrade.Notify(fmt.Sprintf("交易信号未成交,取消订单: %s", symbol)) qbtrade.Notify(fmt.Sprintf("交易信号未成交,取消订单: %s", symbol))
s.cancelOrders(ctx, symbol) s.cancelOrders(ctx, symbol)
} }
if s.TradeType[symbol] != "" && s.TradeRetry[symbol] <= 1 {
s.TradeRetry[symbol] = s.TradeRetry[symbol] + 1
}
} }
s.TradeKLine[symbol] = k 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", 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) order, order.OrderID, orderSymbol, order.Type, order.Status)
s.Traded[orderSymbol] = true s.Traded[orderSymbol] = true
s.TradeRetry[orderSymbol] = 0
qbtrade.Notify("订单成交通知:\n 币种:%s, 方向:%s, 价格:%s, 数量:%s", order.Symbol, s.TradeType, qbtrade.Notify("订单成交通知:\n 币种:%s, 方向:%s, 价格:%s, 数量:%s", order.Symbol, s.TradeType,
order.Price, order.Quantity) 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", 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) order, order.OrderID, orderSymbol, order.Type, order.Status)
s.Traded[orderSymbol] = true s.Traded[orderSymbol] = true
s.TradeRetry[orderSymbol] = 0
qbtrade.Notify("订单成交通知:\n 币种:%s, 方向:%s, 价格:%s, 数量:%s", order.Symbol, s.TradeType, qbtrade.Notify("订单成交通知:\n 币种:%s, 方向:%s, 价格:%s, 数量:%s", order.Symbol, s.TradeType,
order.Price, order.Quantity) 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) "status is %s", order, order.OrderID, orderSymbol, order.Type, order.Status)
qbtrade.Notify("订单止盈或止损通知:\n %s", order.Symbol) qbtrade.Notify("订单止盈或止损通知:\n %s", order.Symbol)
s.Traded[orderSymbol] = false s.Traded[orderSymbol] = false
s.TradeRetry[orderSymbol] = 0
s.TradeType[orderSymbol] = "" s.TradeType[orderSymbol] = ""
} else { } else {
log.Infof("the order is: %+v,id is %d, symbol is %s, type is %s, status is %s", log.Infof("the order is: %+v,id is %d, symbol is %s, type is %s, status is %s",