supertrend: log with symbol

This commit is contained in:
Andy Cheng 2022-06-16 17:14:50 +08:00
parent ae935971f4
commit f6770df50f

View File

@ -134,16 +134,18 @@ func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Valu
} }
if quantity.Compare(s.Market.MinQuantity) < 0 { if quantity.Compare(s.Market.MinQuantity) < 0 {
return fmt.Errorf("order quantity %v is too small, less than %v", quantity, s.Market.MinQuantity) return fmt.Errorf("%s order quantity %v is too small, less than %v", s.Symbol, quantity, s.Market.MinQuantity)
} }
orderForm := s.generateOrderForm(side, quantity, types.SideEffectTypeAutoRepay) orderForm := s.generateOrderForm(side, quantity, types.SideEffectTypeAutoRepay)
s.Notify("Submitting %s %s order to close position by %v", s.Symbol, side.String(), percentage, orderForm) log.Infof("submit close position order %v", orderForm)
s.Notify("Submitting %s %s order to close position by %v", s.Symbol, side.String(), percentage)
createdOrders, err := s.session.Exchange.SubmitOrders(ctx, orderForm) createdOrders, err := s.session.Exchange.SubmitOrders(ctx, orderForm)
if err != nil { if err != nil {
log.WithError(err).Errorf("can not place position close order") log.WithError(err).Errorf("can not place %s position close order", s.Symbol)
s.Notify("can not place %s position close order", s.Symbol)
} }
s.orderStore.Add(createdOrders...) s.orderStore.Add(createdOrders...)
@ -211,7 +213,7 @@ func (s *Strategy) generateOrderForm(side types.SideType, quantity fixedpoint.Va
func (s *Strategy) calculateQuantity(currentPrice fixedpoint.Value) fixedpoint.Value { func (s *Strategy) calculateQuantity(currentPrice fixedpoint.Value) fixedpoint.Value {
balance, ok := s.session.GetAccount().Balance(s.Market.QuoteCurrency) balance, ok := s.session.GetAccount().Balance(s.Market.QuoteCurrency)
if !ok { if !ok {
log.Error("can not update balance from exchange") log.Errorf("can not update %s balance from exchange", s.Symbol)
return fixedpoint.Zero return fixedpoint.Zero
} }
@ -296,29 +298,26 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
if !s.Market.IsDustQuantity(base.Abs(), kline.GetClose()) { if !s.Market.IsDustQuantity(base.Abs(), kline.GetClose()) {
if s.StopLossByTriggeringK && !s.currentStopLossPrice.IsZero() && ((baseSign < 0 && kline.GetClose().Compare(s.currentStopLossPrice) > 0) || (baseSign > 0 && kline.GetClose().Compare(s.currentStopLossPrice) < 0)) { if s.StopLossByTriggeringK && !s.currentStopLossPrice.IsZero() && ((baseSign < 0 && kline.GetClose().Compare(s.currentStopLossPrice) > 0) || (baseSign > 0 && kline.GetClose().Compare(s.currentStopLossPrice) < 0)) {
// SL by triggering Kline low // SL by triggering Kline low
log.Infof("SL by triggering Kline low") log.Infof("%s SL by triggering Kline low", s.Symbol)
if err := s.ClosePosition(ctx, fixedpoint.One); err != nil { s.Notify("%s SL by triggering Kline low", s.Symbol)
s.Notify("can not place SL order") if err := s.ClosePosition(ctx, fixedpoint.One); err == nil {
} else {
s.currentStopLossPrice = fixedpoint.Zero s.currentStopLossPrice = fixedpoint.Zero
s.currentTakeProfitPrice = fixedpoint.Zero s.currentTakeProfitPrice = fixedpoint.Zero
} }
} else if s.TakeProfitMultiplier > 0 && !s.currentTakeProfitPrice.IsZero() && ((baseSign < 0 && kline.GetClose().Compare(s.currentTakeProfitPrice) < 0) || (baseSign > 0 && kline.GetClose().Compare(s.currentTakeProfitPrice) > 0)) { } else if s.TakeProfitMultiplier > 0 && !s.currentTakeProfitPrice.IsZero() && ((baseSign < 0 && kline.GetClose().Compare(s.currentTakeProfitPrice) < 0) || (baseSign > 0 && kline.GetClose().Compare(s.currentTakeProfitPrice) > 0)) {
// TP by multiple of ATR // TP by multiple of ATR
log.Infof("TP by multiple of ATR") log.Infof("%s TP by multiple of ATR", s.Symbol)
if err := s.ClosePosition(ctx, fixedpoint.One); err != nil { s.Notify("%s TP by multiple of ATR", s.Symbol)
s.Notify("can not place TP order") if err := s.ClosePosition(ctx, fixedpoint.One); err == nil {
} else {
s.currentStopLossPrice = fixedpoint.Zero s.currentStopLossPrice = fixedpoint.Zero
s.currentTakeProfitPrice = fixedpoint.Zero s.currentTakeProfitPrice = fixedpoint.Zero
} }
} else if s.TPSLBySignal { } else if s.TPSLBySignal {
// Use signals to TP/SL // Use signals to TP/SL
log.Infof("TP/SL by reverse of DEMA or Supertrend") log.Infof("%s TP/SL by reverse of DEMA or Supertrend", s.Symbol)
s.Notify("%s TP/SL by reverse of DEMA or Supertrend", s.Symbol)
if (baseSign < 0 && (stSignal == types.DirectionUp || demaSignal == types.DirectionUp)) || (baseSign > 0 && (stSignal == types.DirectionDown || demaSignal == types.DirectionDown)) { if (baseSign < 0 && (stSignal == types.DirectionUp || demaSignal == types.DirectionUp)) || (baseSign > 0 && (stSignal == types.DirectionDown || demaSignal == types.DirectionDown)) {
if err := s.ClosePosition(ctx, fixedpoint.One); err != nil { if err := s.ClosePosition(ctx, fixedpoint.One); err == nil {
s.Notify("can not place TP/SL order")
} else {
s.currentStopLossPrice = fixedpoint.Zero s.currentStopLossPrice = fixedpoint.Zero
s.currentTakeProfitPrice = fixedpoint.Zero s.currentTakeProfitPrice = fixedpoint.Zero
} }
@ -347,17 +346,17 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
} }
if side == types.SideTypeSell || side == types.SideTypeBuy { if side == types.SideTypeSell || side == types.SideTypeBuy {
log.Infof("open position for signal %v", side) log.Infof("open %s position for signal %v", s.Symbol, side)
s.Notify("open %s position for signal %v", s.Symbol, side)
// Close opposite position if any // Close opposite position if any
if !s.Market.IsDustQuantity(base.Abs(), kline.GetClose()) { if !s.Position.IsDust(kline.GetClose()) {
if (side == types.SideTypeSell && baseSign > 0) || (side == types.SideTypeBuy && baseSign < 0) { if (side == types.SideTypeSell && s.Position.IsLong()) || (side == types.SideTypeBuy && s.Position.IsShort()) {
log.Infof("close existing position before open a new position") log.Infof("close existing %s position before open a new position", s.Symbol)
if err := s.ClosePosition(ctx, fixedpoint.One); err != nil { s.Notify("close existing %s position before open a new position", s.Symbol)
log.WithError(err).Errorf("can not place close position order") _ = s.ClosePosition(ctx, fixedpoint.One)
s.Notify("can not place close position order")
}
} else { } else {
log.Infof("existing position has the same direction with the signal") log.Infof("existing %s position has the same direction with the signal", s.Symbol)
s.Notify("existing %s position has the same direction with the signal", s.Symbol)
return return
} }
} }
@ -366,8 +365,8 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
log.Infof("submit open position order %v", orderForm) log.Infof("submit open position order %v", orderForm)
order, err := orderExecutor.SubmitOrders(ctx, orderForm) order, err := orderExecutor.SubmitOrders(ctx, orderForm)
if err != nil { if err != nil {
log.WithError(err).Errorf("can not place open position order") log.WithError(err).Errorf("can not place %s open position order", s.Symbol)
s.Notify("can not place open position order") s.Notify("can not place %s open position order", s.Symbol)
} else { } else {
s.orderStore.Add(order...) s.orderStore.Add(order...)
} }