mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 23:05:15 +00:00
add submit order tag
This commit is contained in:
parent
2953518af9
commit
2784408b8b
|
@ -2,6 +2,7 @@ package bbgo
|
|||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
|
@ -117,12 +118,14 @@ func (e *GeneralOrderExecutor) GracefulCancel(ctx context.Context) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (e *GeneralOrderExecutor) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error {
|
||||
func (e *GeneralOrderExecutor) ClosePosition(ctx context.Context, percentage fixedpoint.Value, tags ...string) error {
|
||||
submitOrder := e.position.NewMarketCloseOrder(percentage)
|
||||
if submitOrder == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
submitOrder.Tag = strings.Join(tags, ",")
|
||||
|
||||
_, err := e.SubmitOrders(ctx, *submitOrder)
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -58,6 +58,7 @@ func (s *ProtectionStopLoss) placeStopOrder(ctx context.Context, position *types
|
|||
Price: s.stopLossPrice.Mul(one.Add(fixedpoint.NewFromFloat(0.005))), // +0.5% from the trigger price, slippage protection
|
||||
StopPrice: s.stopLossPrice,
|
||||
Market: position.Market,
|
||||
Tag: "protectionStopLoss",
|
||||
})
|
||||
|
||||
if len(createdOrders) > 0 {
|
||||
|
@ -174,7 +175,7 @@ func (s *ProtectionStopLoss) checkStopPrice(closePrice fixedpoint.Value, positio
|
|||
|
||||
if s.shouldStop(closePrice) {
|
||||
log.Infof("[ProtectionStopLoss] protection stop order is triggered at price %f, position = %+v", closePrice.Float64(), position)
|
||||
if err := s.orderExecutor.ClosePosition(context.Background(), one); err != nil {
|
||||
if err := s.orderExecutor.ClosePosition(context.Background(), one, "protectionStopLoss"); err != nil {
|
||||
log.WithError(err).Errorf("failed to close position")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ func (s *RoiStopLoss) checkStopPrice(closePrice fixedpoint.Value, position *type
|
|||
if roi.Compare(s.Percentage.Neg()) < 0 {
|
||||
// stop loss
|
||||
bbgo.Notify("[RoiStopLoss] %s stop loss triggered by ROI %s/%s, price: %f", position.Symbol, roi.Percentage(), s.Percentage.Neg().Percentage(), closePrice.Float64())
|
||||
_ = s.orderExecutor.ClosePosition(context.Background(), fixedpoint.One)
|
||||
_ = s.orderExecutor.ClosePosition(context.Background(), fixedpoint.One, "roiStopLoss")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ func (s *RoiTakeProfit) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.
|
|||
if roi.Compare(s.Percentage) > 0 {
|
||||
// stop loss
|
||||
bbgo.Notify("[RoiTakeProfit] %s take profit is triggered by ROI %s/%s, price: %f", position.Symbol, roi.Percentage(), s.Percentage.Percentage(), kline.Close.Float64())
|
||||
_ = orderExecutor.ClosePosition(context.Background(), fixedpoint.One)
|
||||
_ = orderExecutor.ClosePosition(context.Background(), fixedpoint.One, "roiTakeProfit")
|
||||
return
|
||||
}
|
||||
})
|
||||
|
|
|
@ -282,8 +282,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
|||
return
|
||||
}
|
||||
|
||||
isPositionOpened := !s.Position.IsClosed() && !s.Position.IsDust(kline.Close)
|
||||
if isPositionOpened && s.Position.IsShort() {
|
||||
if !s.Position.IsClosed() && !s.Position.IsDust(kline.Close) {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -299,6 +298,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
|||
s.pivotLowPrices = s.pivotLowPrices[len(s.pivotLowPrices)-10:]
|
||||
}
|
||||
|
||||
// stop EMA protection
|
||||
if s.stopEWMA != nil && !s.BreakLow.StopEMARange.IsZero() {
|
||||
ema := fixedpoint.NewFromFloat(s.stopEWMA.Last())
|
||||
if ema.IsZero() {
|
||||
|
@ -319,11 +319,6 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
|||
return
|
||||
}
|
||||
|
||||
if !s.Position.IsClosed() && !s.Position.IsDust(kline.Close) {
|
||||
// s.Notify("skip opening %s position, which is not closed", s.Symbol, s.Position)
|
||||
return
|
||||
}
|
||||
|
||||
_ = s.orderExecutor.GracefulCancel(ctx)
|
||||
|
||||
quantity := s.useQuantityOrBaseBalance(s.BreakLow.Quantity)
|
||||
|
@ -332,6 +327,8 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
|||
s.placeMarketSell(ctx, quantity)
|
||||
} else {
|
||||
sellPrice := kline.Close.Mul(fixedpoint.One.Add(s.BreakLow.BounceRatio))
|
||||
|
||||
bbgo.Notify("%s price %f breaks the previous low %f with ratio %f, submitting limit sell @ %f", s.Symbol, kline.Close.Float64(), previousLow.Float64(), s.BreakLow.Ratio.Float64(), sellPrice.Float64())
|
||||
s.placeLimitSell(ctx, sellPrice, quantity)
|
||||
}
|
||||
})
|
||||
|
@ -370,6 +367,13 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
|||
}
|
||||
})
|
||||
|
||||
if !bbgo.IsBackTesting {
|
||||
// use market trade to submit short order
|
||||
session.MarketDataStream.OnMarketTrade(func(trade types.Trade) {
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {
|
||||
// StrategyController
|
||||
if s.Status != types.StrategyStatusRunning {
|
||||
|
|
|
@ -132,6 +132,8 @@ type SubmitOrder struct {
|
|||
IsFutures bool `json:"is_futures" db:"is_futures"`
|
||||
ReduceOnly bool `json:"reduceOnly" db:"reduce_only"`
|
||||
ClosePosition bool `json:"closePosition" db:"close_position"`
|
||||
|
||||
Tag string `json:"tag" db:"-"`
|
||||
}
|
||||
|
||||
func (o *SubmitOrder) String() string {
|
||||
|
@ -229,6 +231,7 @@ func (o Order) CsvHeader() []string {
|
|||
"quantity",
|
||||
"creation_time",
|
||||
"update_time",
|
||||
"tag",
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -244,6 +247,7 @@ func (o Order) CsvRecords() [][]string {
|
|||
o.Quantity.String(),
|
||||
o.CreationTime.Time().Format(time.RFC1123),
|
||||
o.UpdateTime.Time().Format(time.RFC1123),
|
||||
o.Tag,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user