[fix] 修复文件

This commit is contained in:
lychiyu 2024-07-30 22:53:59 +08:00
parent 13481332fe
commit 9e5123155c
2 changed files with 53 additions and 38 deletions

View File

@ -21,13 +21,9 @@ exchangeStrategies:
- OPUSDT - OPUSDT
- OMUSDT - OMUSDT
- WIFUSDT - WIFUSDT
# - BNBUSDT - DYDXUSDT
# - BTCUSDT - XRPUSDT
# - ETHUSDT - PEOPLEUSDT
# - SOLUSDT
# - DYDXUSDT
# - XRPUSDT
# - PEOPLEUSDT
# - STXUSDT # - STXUSDT
# - WLDUSDT # - WLDUSDT
# - FILUSDT # - FILUSDT
@ -35,9 +31,14 @@ exchangeStrategies:
# - MKRUSDT # - MKRUSDT
# - NOTUSDT # - NOTUSDT
# - ENSUSDT # - ENSUSDT
# - BNBUSDT
# - BTCUSDT
# - ETHUSDT
# - SOLUSDT
interval: 1m interval: 1m
nrInterval: 5m nrInterval: 1m
cciInterval: 15m cciInterval: 5m
atrInterval: 1h atrInterval: 1h
nrCount: 4 nrCount: 4
cciWindow: 20 cciWindow: 20
@ -53,6 +54,7 @@ exchangeStrategies:
amount: 20 amount: 20
placePriceType: 2 placePriceType: 2
lossType: 1 lossType: 1
profitOrderType: 0
# recalculate: false # recalculate: false
# dry_run: false # dry_run: false
# # quantity: 3 # # quantity: 3

View File

@ -10,6 +10,7 @@ import (
"git.qtrade.icu/lychiyu/qbtrade/pkg/strategy/common" "git.qtrade.icu/lychiyu/qbtrade/pkg/strategy/common"
"git.qtrade.icu/lychiyu/qbtrade/pkg/types" "git.qtrade.icu/lychiyu/qbtrade/pkg/types"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"strconv"
"strings" "strings"
"sync" "sync"
) )
@ -28,23 +29,24 @@ type Strategy struct {
markets map[string]types.Market markets map[string]types.Market
//Symbol string `json:"symbol"` //Symbol string `json:"symbol"`
Symbols []string `json:"symbols"` Symbols []string `json:"symbols"`
Interval types.Interval `json:"interval"` Interval types.Interval `json:"interval"`
NRInterval types.Interval `json:"nrInterval"` NRInterval types.Interval `json:"nrInterval"`
CCIInterval types.Interval `json:"cciInterval"` CCIInterval types.Interval `json:"cciInterval"`
ATRInterval types.Interval `json:"atrInterval"` ATRInterval types.Interval `json:"atrInterval"`
NrCount int `json:"nrCount"` NrCount int `json:"nrCount"`
StrictMode bool `json:"strictMode"` StrictMode bool `json:"strictMode"`
PlacePriceType int `json:"placePriceType"` PlacePriceType int `json:"placePriceType"`
LossType int `json:"lossType"` LossType int `json:"lossType"`
DryRun bool `json:"dryRun"` ProfitOrderType int `json:"profitOrderType"`
CCIWindow int `json:"cciWindow"` DryRun bool `json:"dryRun"`
ATRWindow int `json:"atrWindow"` CCIWindow int `json:"cciWindow"`
LongCCI fixedpoint.Value `json:"longCCI"` ATRWindow int `json:"atrWindow"`
ShortCCI fixedpoint.Value `json:"shortCCI"` LongCCI fixedpoint.Value `json:"longCCI"`
Leverage fixedpoint.Value `json:"leverage"` ShortCCI fixedpoint.Value `json:"shortCCI"`
ProfitRange fixedpoint.Value `json:"profitRange"` Leverage fixedpoint.Value `json:"leverage"`
LossRange fixedpoint.Value `json:"lossRange"` ProfitRange fixedpoint.Value `json:"profitRange"`
LossRange fixedpoint.Value `json:"lossRange"`
qbtrade.QuantityOrAmount qbtrade.QuantityOrAmount
@ -162,7 +164,7 @@ func (s *Strategy) getPlacePrice(ctx context.Context, kline types.KLine) fixedpo
symbol := kline.Symbol symbol := kline.Symbol
placePrice := fixedpoint.Value(0) placePrice := fixedpoint.Value(0)
midPrice := (kline.High.Add(kline.Low)).Div(fixedpoint.Value(2.0)) midPrice := (kline.High.Add(kline.Low)).Div(fixedpoint.One * 2)
switch s.PlacePriceType { switch s.PlacePriceType {
case 0: case 0:
if s.TradeType[symbol] == "long" { if s.TradeType[symbol] == "long" {
@ -200,32 +202,43 @@ func (s *Strategy) generateOrders(ctx context.Context, kline types.KLine) ([]typ
// 获取下单价格 // 获取下单价格
placePrice := s.getPlacePrice(ctx, kline) placePrice := s.getPlacePrice(ctx, kline)
// 止盈订单类型
profitOrderType := types.OrderTypeTakeProfitMarket
if s.ProfitOrderType == 1 {
profitOrderType = types.OrderTypeStopMarket
}
// 计算止损止盈价格以ATR为基准或者固定百分比 // 计算止损止盈价格以ATR为基准或者固定百分比
lossPrice := fixedpoint.Value(0) lossPrice := fixedpoint.Value(0)
profitPrice := fixedpoint.Value(0) profitPrice := fixedpoint.Value(0)
lastATR := s.atr[symbol].Last(0) lastATR, err := strconv.ParseFloat(strconv.FormatFloat(s.atr[symbol].Last(0), 'f', 6, 64), 64)
if err != nil {
log.WithError(err).Error("failed parse atr last value float")
lastATR = 0.0
}
if s.TradeType[symbol] == "long" { if s.TradeType[symbol] == "long" {
if s.LossType == 0 { if s.LossType == 0 || s.atr[symbol].Last(0) == 0.0 {
lossPrice = placePrice.Sub(placePrice.Mul(s.LossRange)) lossPrice = placePrice.Sub(placePrice.Mul(s.LossRange))
profitPrice = placePrice.Add(placePrice.Mul(s.ProfitRange)) profitPrice = placePrice.Add(placePrice.Mul(s.ProfitRange))
} else if s.LossType == 1 { } else if s.LossType == 1 {
lossPrice = placePrice.Sub(fixedpoint.Value(lastATR)) lossPrice = placePrice.Sub(fixedpoint.Value(1e8 * lastATR))
profitPrice = placePrice.Add(fixedpoint.Value(lastATR * 2)) profitPrice = placePrice.Add(fixedpoint.Value(1e8 * lastATR * 2))
} }
} else if s.TradeType[symbol] == "short" { } else if s.TradeType[symbol] == "short" {
if s.LossType == 0 { if s.LossType == 0 || s.atr[symbol].Last(0) == 0.0 {
lossPrice = placePrice.Add(placePrice.Mul(s.LossRange)) lossPrice = placePrice.Add(placePrice.Mul(s.LossRange))
profitPrice = placePrice.Sub(placePrice.Mul(s.ProfitRange)) profitPrice = placePrice.Sub(placePrice.Mul(s.ProfitRange))
} else if s.LossType == 1 { } else if s.LossType == 1 {
lossPrice = placePrice.Add(fixedpoint.Value(lastATR)) lossPrice = placePrice.Add(fixedpoint.Value(1e8 * lastATR))
profitPrice = placePrice.Sub(fixedpoint.Value(lastATR * 2)) profitPrice = placePrice.Sub(fixedpoint.Value(1e8 * lastATR * 2))
} }
} }
// 下单数量 // 下单数量
placeQuantity := s.QuantityOrAmount.CalculateQuantity(placePrice).Mul(s.Leverage) placeQuantity := s.QuantityOrAmount.CalculateQuantity(placePrice).Mul(s.Leverage)
log.Infof(fmt.Sprintf("will place order, price %v, quantity %v, lossprice %v, profitprice: %v", log.Infof(fmt.Sprintf("will place order, price %v, quantity %v, lossprice %v, profitprice: %v, atr: %v",
placePrice.Float64(), placeQuantity.Float64(), lossPrice.Float64(), profitPrice.Float64())) placePrice.Float64(), placeQuantity.Float64(), lossPrice.Float64(), profitPrice.Float64(),
lastATR))
s.ShortOrder[symbol] = types.SubmitOrder{ s.ShortOrder[symbol] = types.SubmitOrder{
Symbol: symbol, Symbol: symbol,
@ -241,7 +254,7 @@ func (s *Strategy) generateOrders(ctx context.Context, kline types.KLine) ([]typ
s.ShortProfitOrder[symbol] = types.SubmitOrder{ s.ShortProfitOrder[symbol] = types.SubmitOrder{
Symbol: symbol, Symbol: symbol,
Side: types.SideTypeBuy, Side: types.SideTypeBuy,
Type: types.OrderTypeStopMarket, Type: profitOrderType,
PositionSide: types.PositionSideTypeShort, PositionSide: types.PositionSideTypeShort,
StopPrice: profitPrice, StopPrice: profitPrice,
TimeInForce: types.TimeInForceGTC, TimeInForce: types.TimeInForceGTC,
@ -274,7 +287,7 @@ func (s *Strategy) generateOrders(ctx context.Context, kline types.KLine) ([]typ
s.LongProfitOrder[symbol] = types.SubmitOrder{ s.LongProfitOrder[symbol] = types.SubmitOrder{
Symbol: symbol, Symbol: symbol,
Side: types.SideTypeSell, Side: types.SideTypeSell,
Type: types.OrderTypeStopMarket, Type: profitOrderType,
PositionSide: types.PositionSideTypeLong, PositionSide: types.PositionSideTypeLong,
StopPrice: profitPrice, StopPrice: profitPrice,
TimeInForce: types.TimeInForceGTC, TimeInForce: types.TimeInForceGTC,