This commit is contained in:
lychiyu 2024-07-30 22:58:03 +08:00
parent 9e5123155c
commit 29bd6ff6eb

View File

@ -163,7 +163,7 @@ func (s *Strategy) placeOrders(ctx context.Context, kline types.KLine) {
func (s *Strategy) getPlacePrice(ctx context.Context, kline types.KLine) fixedpoint.Value { func (s *Strategy) getPlacePrice(ctx context.Context, kline types.KLine) fixedpoint.Value {
symbol := kline.Symbol symbol := kline.Symbol
placePrice := fixedpoint.Value(0) placePrice := fixedpoint.Zero
midPrice := (kline.High.Add(kline.Low)).Div(fixedpoint.One * 2) midPrice := (kline.High.Add(kline.Low)).Div(fixedpoint.One * 2)
switch s.PlacePriceType { switch s.PlacePriceType {
case 0: case 0:
@ -209,8 +209,8 @@ func (s *Strategy) generateOrders(ctx context.Context, kline types.KLine) ([]typ
} }
// 计算止损止盈价格以ATR为基准或者固定百分比 // 计算止损止盈价格以ATR为基准或者固定百分比
lossPrice := fixedpoint.Value(0) lossPrice := fixedpoint.Zero
profitPrice := fixedpoint.Value(0) profitPrice := fixedpoint.Zero
lastATR, err := strconv.ParseFloat(strconv.FormatFloat(s.atr[symbol].Last(0), 'f', 6, 64), 64) lastATR, err := strconv.ParseFloat(strconv.FormatFloat(s.atr[symbol].Last(0), 'f', 6, 64), 64)
if err != nil { if err != nil {
log.WithError(err).Error("failed parse atr last value float") log.WithError(err).Error("failed parse atr last value float")
@ -331,10 +331,10 @@ func (s *Strategy) notifyProfit(ctx context.Context, symbol string) {
if s.EndQuantity[symbol] != s.OpenQuantity[symbol] { if s.EndQuantity[symbol] != s.OpenQuantity[symbol] {
return return
} }
profit := fixedpoint.Value(0) profit := fixedpoint.Zero
openProfit := fixedpoint.Value(0) openProfit := fixedpoint.Zero
endProfit := fixedpoint.Value(0) endProfit := fixedpoint.Zero
free := fixedpoint.Value(0) free := fixedpoint.Zero
var openMsgs []string var openMsgs []string
var endMsgs []string var endMsgs []string
@ -371,7 +371,7 @@ func (s *Strategy) notifyProfit(ctx context.Context, symbol string) {
s.TotalProfit = s.TotalProfit.Add(profit) s.TotalProfit = s.TotalProfit.Add(profit)
s.TotalFree = s.TotalFree.Add(free) s.TotalFree = s.TotalFree.Add(free)
s.TotalOrderCount += 1 s.TotalOrderCount += 1
if profit > fixedpoint.Value(0) { if profit > fixedpoint.Zero {
s.TotalProfitCount += 1 s.TotalProfitCount += 1
} else { } else {
s.TotalLossCount += 1 s.TotalLossCount += 1
@ -383,8 +383,8 @@ func (s *Strategy) notifyProfit(ctx context.Context, symbol string) {
// 重置 // 重置
s.OpenTrade[symbol] = []types.Trade{} s.OpenTrade[symbol] = []types.Trade{}
s.EndTrade[symbol] = []types.Trade{} s.EndTrade[symbol] = []types.Trade{}
s.OpenQuantity[symbol] = fixedpoint.Value(0) s.OpenQuantity[symbol] = fixedpoint.Zero
s.EndQuantity[symbol] = fixedpoint.Value(0) s.EndQuantity[symbol] = fixedpoint.Zero
// 记得取消订单 // 记得取消订单
s.cancelOrders(ctx, symbol) s.cancelOrders(ctx, symbol)
@ -420,8 +420,8 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor qbtrade.OrderExecutor,
s.cci = make(map[string]*indicatorv2.CCIStream) s.cci = make(map[string]*indicatorv2.CCIStream)
s.atr = make(map[string]*indicatorv2.ATRStream) s.atr = make(map[string]*indicatorv2.ATRStream)
s.TotalProfit = fixedpoint.Value(0) s.TotalProfit = fixedpoint.Zero
s.TotalFree = fixedpoint.Value(0) s.TotalFree = fixedpoint.Zero
s.TotalOrderCount = 0 s.TotalOrderCount = 0
s.TotalProfitCount = 0 s.TotalProfitCount = 0
s.TotalLossCount = 0 s.TotalLossCount = 0
@ -545,11 +545,11 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor qbtrade.OrderExecutor,
if (trade.Side == types.SideTypeBuy && s.TradeType[symbol] == "long") || (trade.Side == types.SideTypeSell && s.TradeType[symbol] == "short") { if (trade.Side == types.SideTypeBuy && s.TradeType[symbol] == "long") || (trade.Side == types.SideTypeSell && s.TradeType[symbol] == "short") {
s.OpenTrade[symbol] = append(s.OpenTrade[symbol], trade) s.OpenTrade[symbol] = append(s.OpenTrade[symbol], trade)
s.OpenQuantity[symbol] += trade.Quantity s.OpenQuantity[symbol] = s.OpenQuantity[symbol].Add(trade.Quantity)
} }
if (trade.Side == types.SideTypeSell && s.TradeType[symbol] == "long") || (trade.Side == types.SideTypeBuy && s.TradeType[symbol] == "short") { if (trade.Side == types.SideTypeSell && s.TradeType[symbol] == "long") || (trade.Side == types.SideTypeBuy && s.TradeType[symbol] == "short") {
s.EndTrade[symbol] = append(s.EndTrade[symbol], trade) s.EndTrade[symbol] = append(s.EndTrade[symbol], trade)
s.EndQuantity[symbol] += trade.Quantity s.EndQuantity[symbol] = s.EndQuantity[symbol].Add(trade.Quantity)
s.notifyProfit(ctx, symbol) s.notifyProfit(ctx, symbol)
} }
log.Infof("trade: symbol %s, side %s, price %f, fee %f, quantity %f, buyer %v, maker %v", log.Infof("trade: symbol %s, side %s, price %f, fee %f, quantity %f, buyer %v, maker %v",