[add] 新增总收益的通知
This commit is contained in:
parent
de167972ac
commit
f83998420f
|
@ -44,10 +44,10 @@ exchangeStrategies:
|
||||||
longCCI: -150.0
|
longCCI: -150.0
|
||||||
shortCCI: 150.0
|
shortCCI: 150.0
|
||||||
leverage: 5.0
|
leverage: 5.0
|
||||||
profitRange: 2%
|
profitRange: 0.4%
|
||||||
lossRange: 1%
|
lossRange: 1%
|
||||||
amount: 20
|
amount: 20
|
||||||
place: true
|
placePriceType: 0
|
||||||
# recalculate: false
|
# recalculate: false
|
||||||
# dry_run: false
|
# dry_run: false
|
||||||
# # quantity: 3
|
# # quantity: 3
|
||||||
|
|
|
@ -28,18 +28,18 @@ 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"`
|
||||||
NrCount int `json:"nrCount"`
|
NrCount int `json:"nrCount"`
|
||||||
StrictMode bool `json:"strictMode"`
|
StrictMode bool `json:"strictMode"`
|
||||||
Place bool `json:"place"`
|
PlacePriceType int `json:"placePriceType"`
|
||||||
DryRun bool `json:"dryRun"`
|
DryRun bool `json:"dryRun"`
|
||||||
CCIWindow int `json:"cciWindow"`
|
CCIWindow int `json:"cciWindow"`
|
||||||
LongCCI fixedpoint.Value `json:"longCCI"`
|
LongCCI fixedpoint.Value `json:"longCCI"`
|
||||||
ShortCCI fixedpoint.Value `json:"shortCCI"`
|
ShortCCI fixedpoint.Value `json:"shortCCI"`
|
||||||
Leverage fixedpoint.Value `json:"leverage"`
|
Leverage fixedpoint.Value `json:"leverage"`
|
||||||
ProfitRange fixedpoint.Value `json:"profitRange"`
|
ProfitRange fixedpoint.Value `json:"profitRange"`
|
||||||
LossRange fixedpoint.Value `json:"lossRange"`
|
LossRange fixedpoint.Value `json:"lossRange"`
|
||||||
|
|
||||||
qbtrade.QuantityOrAmount
|
qbtrade.QuantityOrAmount
|
||||||
|
|
||||||
|
@ -73,6 +73,10 @@ type Strategy struct {
|
||||||
|
|
||||||
nr map[string]*indicatorv2.NRStrean
|
nr map[string]*indicatorv2.NRStrean
|
||||||
cci map[string]*indicatorv2.CCIStream
|
cci map[string]*indicatorv2.CCIStream
|
||||||
|
|
||||||
|
TotalProfit fixedpoint.Value
|
||||||
|
TotalFree fixedpoint.Value
|
||||||
|
TotalOrderCount int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) ID() string {
|
func (s *Strategy) ID() string {
|
||||||
|
@ -143,33 +147,48 @@ func (s *Strategy) placeOrders(ctx context.Context, kline types.KLine) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Strategy) getPlacePrice(ctx context.Context, kline types.KLine) fixedpoint.Value {
|
||||||
|
symbol := kline.Symbol
|
||||||
|
|
||||||
|
placePrice := fixedpoint.Value(0)
|
||||||
|
midPrice := (kline.High.Add(kline.Low)).Div(fixedpoint.Value(2.0))
|
||||||
|
switch s.PlacePriceType {
|
||||||
|
case 0:
|
||||||
|
if s.TradeType[symbol] == "long" {
|
||||||
|
placePrice = kline.High
|
||||||
|
} else if s.TradeType[symbol] == "short" {
|
||||||
|
placePrice = kline.Low
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
if s.TradeType[symbol] == "long" {
|
||||||
|
placePrice = kline.Low
|
||||||
|
} else if s.TradeType[symbol] == "short" {
|
||||||
|
placePrice = kline.High
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
if s.TradeType[symbol] == "long" {
|
||||||
|
placePrice = midPrice
|
||||||
|
} else if s.TradeType[symbol] == "short" {
|
||||||
|
placePrice = midPrice
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return placePrice
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Strategy) generateOrders(ctx context.Context, kline types.KLine) ([]types.SubmitOrder, error) {
|
func (s *Strategy) generateOrders(ctx context.Context, kline types.KLine) ([]types.SubmitOrder, error) {
|
||||||
var orders []types.SubmitOrder
|
var orders []types.SubmitOrder
|
||||||
symbol := kline.Symbol
|
symbol := kline.Symbol
|
||||||
|
|
||||||
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)
|
|
||||||
//midPrice := (kline.High.Add(kline.Low)).Div(fixedpoint.Value(2.0))
|
|
||||||
//midP := (kline.High + kline.Low) / 2
|
|
||||||
if s.TradeType[symbol] == "long" {
|
|
||||||
if s.Place {
|
|
||||||
placePrice = kline.High
|
|
||||||
} else {
|
|
||||||
placePrice = kline.Low
|
|
||||||
}
|
|
||||||
//placePrice = midP
|
|
||||||
} else if s.TradeType[symbol] == "short" {
|
|
||||||
if s.Place {
|
|
||||||
placePrice = kline.Low
|
|
||||||
} else {
|
|
||||||
placePrice = kline.High
|
|
||||||
}
|
|
||||||
|
|
||||||
//placePrice = midP
|
if s.TradeType[symbol] == "" {
|
||||||
} else {
|
|
||||||
return orders, nil
|
return orders, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取下单价格
|
||||||
|
placePrice := s.getPlacePrice(ctx, kline)
|
||||||
|
|
||||||
// 下单数量
|
// 下单数量
|
||||||
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", placePrice.Float64(),
|
log.Infof(fmt.Sprintf("will place order, price %v, quantity %v", placePrice.Float64(),
|
||||||
|
@ -266,7 +285,7 @@ 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 := 0.
|
profit := fixedpoint.Value(0)
|
||||||
openProfit := fixedpoint.Value(0)
|
openProfit := fixedpoint.Value(0)
|
||||||
endProfit := fixedpoint.Value(0)
|
endProfit := fixedpoint.Value(0)
|
||||||
free := fixedpoint.Value(0)
|
free := fixedpoint.Value(0)
|
||||||
|
@ -292,16 +311,21 @@ func (s *Strategy) notifyProfit(ctx context.Context, symbol string) {
|
||||||
side := s.OpenTrade[symbol][0].Side
|
side := s.OpenTrade[symbol][0].Side
|
||||||
// 做多
|
// 做多
|
||||||
if side == types.SideTypeBuy {
|
if side == types.SideTypeBuy {
|
||||||
profit = (endProfit - openProfit - free).Float64()
|
profit = endProfit.Sub(openProfit).Sub(free)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 做空
|
// 做空
|
||||||
if side == types.SideTypeSell {
|
if side == types.SideTypeSell {
|
||||||
profit = (openProfit - endProfit - free).Float64()
|
profit = openProfit.Sub(endProfit).Sub(free)
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := fmt.Sprintf("交易完成:\n 币种: %s, 方向:%v, 收益:%v, 手续费:%v \n Trade详情:\n 开仓Trade:\n %s\n 清仓Trade:\n %s",
|
msg := fmt.Sprintf("交易完成:\n 币种: %s, 方向:%v, 收益:%v, 手续费:%v \n Trade详情:\n 开仓Trade:\n %s\n 清仓Trade:\n %s",
|
||||||
symbol, s.TradeType[symbol], profit, free.Float64(), strings.Join(openMsgs, "\n"), strings.Join(endMsgs, "\n"))
|
symbol, s.TradeType[symbol], profit.Float64(), free.Float64(), strings.Join(openMsgs, "\n"), strings.Join(endMsgs, "\n"))
|
||||||
|
|
||||||
|
s.TotalProfit = s.TotalProfit.Add(profit)
|
||||||
|
s.TotalFree = s.TotalFree.Add(free)
|
||||||
|
s.TotalOrderCount += 1
|
||||||
|
|
||||||
log.Infof(msg)
|
log.Infof(msg)
|
||||||
qbtrade.Notify(msg)
|
qbtrade.Notify(msg)
|
||||||
|
|
||||||
|
@ -313,6 +337,8 @@ func (s *Strategy) notifyProfit(ctx context.Context, symbol string) {
|
||||||
|
|
||||||
// 记得取消订单
|
// 记得取消订单
|
||||||
s.cancelOrders(ctx, symbol)
|
s.cancelOrders(ctx, symbol)
|
||||||
|
|
||||||
|
qbtrade.Notify(fmt.Sprintf("总开仓:%v, 总收益:%v, 总手续费:%v", s.TotalOrderCount, s.TotalProfit.Float64(), s.TotalFree.Float64()))
|
||||||
}
|
}
|
||||||
func (s *Strategy) Run(ctx context.Context, orderExecutor qbtrade.OrderExecutor, session *qbtrade.ExchangeSession) error {
|
func (s *Strategy) Run(ctx context.Context, orderExecutor qbtrade.OrderExecutor, session *qbtrade.ExchangeSession) error {
|
||||||
|
|
||||||
|
@ -341,6 +367,10 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor qbtrade.OrderExecutor,
|
||||||
s.nr = make(map[string]*indicatorv2.NRStrean)
|
s.nr = make(map[string]*indicatorv2.NRStrean)
|
||||||
s.cci = make(map[string]*indicatorv2.CCIStream)
|
s.cci = make(map[string]*indicatorv2.CCIStream)
|
||||||
|
|
||||||
|
s.TotalProfit = fixedpoint.Value(0)
|
||||||
|
s.TotalFree = fixedpoint.Value(0)
|
||||||
|
s.TotalOrderCount = 0
|
||||||
|
|
||||||
for _, symbol := range s.Symbols {
|
for _, symbol := range s.Symbols {
|
||||||
s.Positions[symbol] = types.NewPositionFromMarket(s.markets[symbol])
|
s.Positions[symbol] = types.NewPositionFromMarket(s.markets[symbol])
|
||||||
s.ProfitStats[symbol] = types.NewProfitStats(s.markets[symbol])
|
s.ProfitStats[symbol] = types.NewProfitStats(s.markets[symbol])
|
||||||
|
|
Loading…
Reference in New Issue
Block a user