bbgo_origin/pkg/strategy/support/strategy.go

640 lines
20 KiB
Go
Raw Normal View History

2021-02-14 17:26:46 +00:00
package support
2021-02-10 16:21:06 +00:00
import (
"context"
"fmt"
2021-05-30 17:02:35 +00:00
"sync"
2021-02-10 16:21:06 +00:00
"github.com/sirupsen/logrus"
2021-06-21 11:03:50 +00:00
"github.com/c9s/bbgo/pkg/indicator"
2021-05-30 17:02:35 +00:00
"github.com/c9s/bbgo/pkg/service"
2021-02-10 16:21:06 +00:00
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
2021-02-14 17:26:46 +00:00
const ID = "support"
2021-02-10 16:21:06 +00:00
2021-05-30 17:02:35 +00:00
const stateKey = "state-v1"
2021-02-10 16:21:06 +00:00
var log = logrus.WithField("strategy", ID)
var zeroiw = types.IntervalWindow{}
2021-02-10 16:21:06 +00:00
func init() {
bbgo.RegisterStrategy(ID, &Strategy{})
}
2021-05-30 17:02:35 +00:00
type State struct {
Position *types.Position `json:"position,omitempty"`
CurrentHighestPrice *fixedpoint.Value `json:"position,omitempty"`
2021-05-30 17:02:35 +00:00
}
2021-02-10 16:21:06 +00:00
type Target struct {
2021-02-14 17:26:46 +00:00
ProfitPercentage float64 `json:"profitPercentage"`
QuantityPercentage float64 `json:"quantityPercentage"`
MarginOrderSideEffect types.MarginOrderSideEffectType `json:"marginOrderSideEffect"`
2021-02-10 16:21:06 +00:00
}
2021-10-15 04:38:16 +00:00
// PercentageTargetStop is a kind of stop order by setting fixed percentage target
type PercentageTargetStop struct {
Targets []Target `json:"targets"`
}
// GenerateOrders generates the orders from the given targets
func (stop *PercentageTargetStop) GenerateOrders(market types.Market, pos *types.Position) []types.SubmitOrder {
var price = pos.AverageCost
var quantity = pos.GetBase()
// submit target orders
var targetOrders []types.SubmitOrder
for _, target := range stop.Targets {
targetPrice := price.Float64() * (1.0 + target.ProfitPercentage)
targetQuantity := quantity.Float64() * target.QuantityPercentage
targetQuoteQuantity := targetPrice * targetQuantity
if targetQuoteQuantity <= market.MinNotional {
continue
}
if targetQuantity <= market.MinQuantity {
continue
}
targetOrders = append(targetOrders, types.SubmitOrder{
Symbol: market.Symbol,
Market: market,
Type: types.OrderTypeLimit,
Side: types.SideTypeSell,
Price: targetPrice,
Quantity: targetQuantity,
MarginSideEffect: target.MarginOrderSideEffect,
TimeInForce: "GTC",
})
}
return targetOrders
}
type TrailingStopTarget struct {
TrailingStopCallBackRatio fixedpoint.Value `json:"callBackRatio"`
MinimumProfitPercentage fixedpoint.Value `json:"minimumProfitPercentage"`
}
type TrailingStopControl struct {
2022-01-24 03:34:57 +00:00
symbol string
market types.Market
marginSideEffect types.MarginOrderSideEffectType
trailingStopCallBackRatio fixedpoint.Value
minimumProfitPercentage fixedpoint.Value
CurrentHighestPrice fixedpoint.Value
OrderID uint64
}
func (control *TrailingStopControl) IsHigherThanMin(minTargetPrice float64) bool {
targetPrice := control.CurrentHighestPrice.Float64() * (1 - control.trailingStopCallBackRatio.Float64())
return targetPrice >= minTargetPrice
}
func (control *TrailingStopControl) GenerateStopOrder(quantity float64) types.SubmitOrder {
targetPrice := control.CurrentHighestPrice.Float64() * (1 - control.trailingStopCallBackRatio.Float64())
orderForm := types.SubmitOrder{
2022-01-24 03:34:57 +00:00
Symbol: control.symbol,
Market: control.market,
Side: types.SideTypeSell,
Type: types.OrderTypeStopLimit,
Quantity: quantity,
MarginSideEffect: control.marginSideEffect,
TimeInForce: "GTC",
Price: targetPrice,
StopPrice: targetPrice,
}
return orderForm
}
// Not implemented yet
// ResistanceStop is a kind of stop order by detecting resistance
//type ResistanceStop struct {
// Interval types.Interval `json:"interval"`
// sensitivity fixedpoint.Value `json:"sensitivity"`
// MinVolume fixedpoint.Value `json:"minVolume"`
// TakerBuyRatio fixedpoint.Value `json:"takerBuyRatio"`
//}
2021-02-10 16:21:06 +00:00
type Strategy struct {
2021-06-16 12:33:52 +00:00
*bbgo.Notifiability `json:"-"`
2021-05-30 17:02:35 +00:00
*bbgo.Persistence
2021-06-24 07:38:55 +00:00
*bbgo.Graceful `json:"-"`
2021-02-20 02:50:57 +00:00
2021-10-15 04:38:16 +00:00
Symbol string `json:"symbol"`
Market types.Market `json:"-"`
2021-06-21 11:03:50 +00:00
// Interval for checking support
2021-10-15 04:38:16 +00:00
Interval types.Interval `json:"interval"`
2021-06-21 11:03:50 +00:00
// moving average window for checking support (support should be under the moving average line)
TriggerMovingAverage types.IntervalWindow `json:"triggerMovingAverage"`
2021-06-24 07:49:25 +00:00
2021-06-21 11:03:50 +00:00
// LongTermMovingAverage is the second moving average line for checking support position
LongTermMovingAverage types.IntervalWindow `json:"longTermMovingAverage"`
2021-06-16 12:33:52 +00:00
2021-02-14 17:26:46 +00:00
Quantity fixedpoint.Value `json:"quantity"`
MinVolume fixedpoint.Value `json:"minVolume"`
2021-06-16 05:14:10 +00:00
Sensitivity fixedpoint.Value `json:"sensitivity"`
TakerBuyRatio fixedpoint.Value `json:"takerBuyRatio"`
2021-02-14 17:26:46 +00:00
MarginOrderSideEffect types.MarginOrderSideEffectType `json:"marginOrderSideEffect"`
Targets []Target `json:"targets"`
2022-01-24 03:34:57 +00:00
// Not implemented yet
// ResistanceStop *ResistanceStop `json:"resistanceStop"`
//
//ResistanceTakerBuyRatio fixedpoint.Value `json:"resistanceTakerBuyRatio"`
2021-06-16 05:23:33 +00:00
2021-06-21 11:03:50 +00:00
// Min BaseAsset balance to keep
MinBaseAssetBalance fixedpoint.Value `json:"minBaseAssetBalance"`
// Max BaseAsset balance to buy
MaxBaseAssetBalance fixedpoint.Value `json:"maxBaseAssetBalance"`
MinQuoteAssetBalance fixedpoint.Value `json:"minQuoteAssetBalance"`
ScaleQuantity *bbgo.PriceVolumeScale `json:"scaleQuantity"`
2021-06-24 07:38:55 +00:00
tradeCollector *bbgo.TradeCollector
orderStore *bbgo.OrderStore
2021-05-30 17:02:35 +00:00
state *State
2021-06-21 11:03:50 +00:00
triggerEMA *indicator.EWMA
longTermEMA *indicator.EWMA
// Trailing stop
2022-01-24 03:34:57 +00:00
TrailingStopTarget TrailingStopTarget `json:"trailingStopTarget"`
trailingStopControl *TrailingStopControl
2021-02-10 16:21:06 +00:00
}
func (s *Strategy) ID() string {
return ID
}
2021-04-02 02:32:24 +00:00
func (s *Strategy) Validate() error {
if s.Quantity == 0 && s.ScaleQuantity == nil {
return fmt.Errorf("quantity or scaleQuantity can not be zero")
}
2021-06-16 05:14:10 +00:00
if s.MinVolume == 0 && s.Sensitivity == 0 {
return fmt.Errorf("either minVolume nor sensitivity can not be zero")
2021-04-02 02:32:24 +00:00
}
return nil
}
2021-02-10 16:21:06 +00:00
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: string(s.Interval)})
if s.TriggerMovingAverage != zeroiw {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: string(s.TriggerMovingAverage.Interval)})
}
if s.LongTermMovingAverage != zeroiw {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: string(s.LongTermMovingAverage.Interval)})
}
2021-02-10 16:21:06 +00:00
}
2021-05-30 17:02:35 +00:00
func (s *Strategy) SaveState() error {
if err := s.Persistence.Save(s.state, ID, s.Symbol, stateKey); err != nil {
return err
} else {
log.Infof("state is saved => %+v", s.state)
}
return nil
}
func (s *Strategy) LoadState() error {
var state State
// load position
if err := s.Persistence.Load(&state, ID, s.Symbol, stateKey); err != nil {
if err != service.ErrPersistenceNotExists {
return err
}
s.state = &State{}
} else {
s.state = &state
log.Infof("state is restored: %+v", s.state)
}
2021-06-24 07:38:55 +00:00
if s.state.Position == nil {
s.state.Position = types.NewPositionFromMarket(s.Market)
2021-06-24 07:38:55 +00:00
}
if s.state.CurrentHighestPrice == nil {
s.trailingStopControl.CurrentHighestPrice = fixedpoint.NewFromInt(0)
}
s.state.CurrentHighestPrice = &s.trailingStopControl.CurrentHighestPrice
2021-05-30 17:02:35 +00:00
return nil
}
func (s *Strategy) submitOrders(ctx context.Context, orderExecutor bbgo.OrderExecutor, orderForms ...types.SubmitOrder) (types.OrderSlice, error) {
2021-06-16 12:33:52 +00:00
for _, o := range orderForms {
s.Notifiability.Notify(o)
}
createdOrders, err := orderExecutor.SubmitOrders(ctx, orderForms...)
if err != nil {
return nil, err
2021-06-16 12:33:52 +00:00
}
s.orderStore.Add(createdOrders...)
2021-06-24 07:49:25 +00:00
s.tradeCollector.Emit()
return createdOrders, nil
}
// Cancel order
func (s *Strategy) cancelOrder(orderID uint64, ctx context.Context, session *bbgo.ExchangeSession) error {
// Cancel the original order
order, ok := s.orderStore.Get(orderID)
2022-01-24 03:34:57 +00:00
if ok {
switch order.Status {
case types.OrderStatusCanceled, types.OrderStatusRejected, types.OrderStatusFilled:
// Do nothing
default:
if err := session.Exchange.CancelOrders(ctx, order); err != nil {
return err
}
}
2022-01-24 03:34:57 +00:00
}
2022-01-24 03:34:57 +00:00
return nil
2021-06-16 12:33:52 +00:00
}
func (s *Strategy) calculateQuantity(session *bbgo.ExchangeSession, side types.SideType, closePrice fixedpoint.Value, volume float64) (fixedpoint.Value, error) {
var quantity fixedpoint.Value
if s.Quantity > 0 {
quantity = s.Quantity
} else if s.ScaleQuantity != nil {
qf, err := s.ScaleQuantity.Scale(closePrice.Float64(), volume)
if err != nil {
return 0, err
}
quantity = fixedpoint.NewFromFloat(qf)
}
2021-06-17 11:28:11 +00:00
baseBalance, _ := session.Account.Balance(s.Market.BaseCurrency)
if side == types.SideTypeSell {
// quantity = bbgo.AdjustQuantityByMaxAmount(quantity, closePrice, quota)
2021-06-21 11:03:50 +00:00
if s.MinBaseAssetBalance > 0 && (baseBalance.Total()-quantity) < s.MinBaseAssetBalance {
2021-06-17 11:28:11 +00:00
quota := baseBalance.Available - s.MinBaseAssetBalance
quantity = bbgo.AdjustQuantityByMaxAmount(quantity, closePrice, quota)
}
} else if side == types.SideTypeBuy {
2021-06-16 12:33:52 +00:00
if s.MaxBaseAssetBalance > 0 && baseBalance.Total()+quantity > s.MaxBaseAssetBalance {
quota := s.MaxBaseAssetBalance - baseBalance.Total()
quantity = bbgo.AdjustQuantityByMaxAmount(quantity, closePrice, quota)
}
quoteBalance, ok := session.Account.Balance(s.Market.QuoteCurrency)
if !ok {
return 0, fmt.Errorf("quote balance %s not found", s.Market.QuoteCurrency)
}
// for spot, we need to modify the quantity according to the quote balance
if !session.Margin {
// add 0.3% for price slippage
notional := closePrice.Mul(quantity).MulFloat64(1.003)
if s.MinQuoteAssetBalance > 0 && quoteBalance.Available-notional < s.MinQuoteAssetBalance {
log.Warnf("modifying quantity %f according to the min quote asset balance %f %s",
quantity.Float64(),
quoteBalance.Available.Float64(),
s.Market.QuoteCurrency)
quota := quoteBalance.Available - s.MinQuoteAssetBalance
quantity = bbgo.AdjustQuantityByMinAmount(quantity, closePrice, quota)
} else if notional > quoteBalance.Available {
log.Warnf("modifying quantity %f according to the quote asset balance %f %s",
quantity.Float64(),
quoteBalance.Available.Float64(),
s.Market.QuoteCurrency)
quantity = bbgo.AdjustQuantityByMaxAmount(quantity, closePrice, quoteBalance.Available)
}
}
}
return quantity, nil
}
2021-02-10 16:21:06 +00:00
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
// set default values
if s.Interval == "" {
s.Interval = types.Interval5m
}
2021-06-16 05:14:10 +00:00
if s.Sensitivity > 0 {
volRange, err := s.ScaleQuantity.ByVolumeRule.Range()
if err != nil {
return err
}
2021-06-16 12:33:52 +00:00
s.MinVolume = fixedpoint.NewFromFloat(volRange[0]) + fixedpoint.NewFromFloat(volRange[1]-volRange[0]).Mul(fixedpoint.NewFromFloat(1.0)-s.Sensitivity)
2021-06-16 05:23:33 +00:00
log.Infof("adjusted minimal support volume to %f according to sensitivity %f", s.MinVolume.Float64(), s.Sensitivity.Float64())
}
2021-02-10 16:21:06 +00:00
market, ok := session.Market(s.Symbol)
if !ok {
return fmt.Errorf("market %s is not defined", s.Symbol)
}
2021-06-16 12:33:52 +00:00
s.Market = market
2021-02-10 16:21:06 +00:00
standardIndicatorSet, ok := session.StandardIndicatorSet(s.Symbol)
if !ok {
return fmt.Errorf("standardIndicatorSet is nil, symbol %s", s.Symbol)
}
if s.TriggerMovingAverage != zeroiw {
s.triggerEMA = standardIndicatorSet.EWMA(s.TriggerMovingAverage)
2022-01-10 05:51:14 +00:00
} else {
s.triggerEMA = standardIndicatorSet.EWMA(types.IntervalWindow{
Interval: s.Interval,
2022-01-24 03:34:57 +00:00
Window: 99, // default window
2022-01-10 05:51:14 +00:00
})
}
if s.LongTermMovingAverage != zeroiw {
s.longTermEMA = standardIndicatorSet.EWMA(s.LongTermMovingAverage)
}
s.orderStore = bbgo.NewOrderStore(s.Symbol)
s.orderStore.BindStream(session.UserDataStream)
2021-06-21 11:03:50 +00:00
if s.TrailingStopTarget.TrailingStopCallBackRatio.Float64() != 0 {
s.trailingStopControl = &TrailingStopControl{
symbol: s.Symbol,
market: s.Market,
marginSideEffect: s.MarginOrderSideEffect,
CurrentHighestPrice: fixedpoint.NewFromInt(0),
trailingStopCallBackRatio: s.TrailingStopTarget.TrailingStopCallBackRatio,
minimumProfitPercentage: s.TrailingStopTarget.MinimumProfitPercentage,
}
}
2021-05-30 17:02:35 +00:00
if err := s.LoadState(); err != nil {
return err
} else {
s.Notify("%s state is restored => %+v", s.Symbol, s.state)
}
2021-06-24 07:38:55 +00:00
s.tradeCollector = bbgo.NewTradeCollector(s.Symbol, s.state.Position, s.orderStore)
if s.TrailingStopTarget.TrailingStopCallBackRatio.Float64() != 0 {
// Update trailing stop when the position changes
s.tradeCollector.OnPositionUpdate(func(position *types.Position) {
2022-01-24 03:34:57 +00:00
if position.Base.Float64() > 0 { // Update order if we have a position
// Cancel the original order
2022-01-24 03:34:57 +00:00
if err := s.cancelOrder(s.trailingStopControl.OrderID, ctx, session); err != nil {
log.WithError(err).Errorf("Can not cancel the original trailing stop order!")
}
s.trailingStopControl.OrderID = 0
// Calculate minimum target price
var minTargetPrice = 0.0
if s.trailingStopControl.minimumProfitPercentage.Float64() > 0 {
minTargetPrice = position.AverageCost.Float64() * (1 + s.trailingStopControl.minimumProfitPercentage.Float64())
2022-01-24 03:34:57 +00:00
}
// Place new order if the target price is higher than the minimum target price
if s.trailingStopControl.IsHigherThanMin(minTargetPrice) {
orderForm := s.trailingStopControl.GenerateStopOrder(position.Base.Float64())
orders, err := s.submitOrders(ctx, orderExecutor, orderForm)
if err != nil {
2022-01-24 03:34:57 +00:00
log.WithError(err).Error("submit profit trailing stop order error")
} else {
s.trailingStopControl.OrderID = orders.IDs()[0]
2022-01-24 03:34:57 +00:00
}
}
}
// Save state
if err := s.SaveState(); err != nil {
log.WithError(err).Errorf("can not save state: %+v", s.state)
} else {
s.Notify("%s position is saved", s.Symbol, s.state.Position)
}
})
}
2021-06-24 07:38:55 +00:00
s.tradeCollector.BindStream(session.UserDataStream)
2021-10-08 05:23:38 +00:00
// s.tradeCollector.BindStreamForBackground(session.UserDataStream)
// go s.tradeCollector.Run(ctx)
2021-05-27 19:15:29 +00:00
session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {
2021-02-10 16:21:06 +00:00
// skip k-lines from other symbols
if kline.Symbol != s.Symbol {
return
}
2021-08-19 09:01:02 +00:00
if kline.Interval != s.Interval {
return
}
2021-02-10 16:21:06 +00:00
closePriceF := kline.GetClose()
2021-06-16 05:23:33 +00:00
closePrice := fixedpoint.NewFromFloat(closePriceF)
highPriceF := kline.GetHigh()
highPrice := fixedpoint.NewFromFloat(highPriceF)
if s.TrailingStopTarget.TrailingStopCallBackRatio.Float64() > 0 {
2022-01-24 03:34:57 +00:00
if s.state.Position.Base.Float64() <= 0 { // Without a position
// Update trailing orders with current high price
s.trailingStopControl.CurrentHighestPrice = highPrice
2022-01-24 03:34:57 +00:00
} else if s.trailingStopControl.CurrentHighestPrice.Float64() < highPriceF { // With a position
// Update trailing orders with current high price if it's higher
s.trailingStopControl.CurrentHighestPrice = highPrice
// Cancel the original order
2022-01-24 03:34:57 +00:00
if err := s.cancelOrder(s.trailingStopControl.OrderID, ctx, session); err != nil {
log.WithError(err).Errorf("Can not cancel the original trailing stop order!")
}
s.trailingStopControl.OrderID = 0
// Calculate minimum target price
var minTargetPrice = 0.0
if s.trailingStopControl.minimumProfitPercentage.Float64() > 0 {
minTargetPrice = s.state.Position.AverageCost.Float64() * (1 + s.trailingStopControl.minimumProfitPercentage.Float64())
2022-01-24 03:34:57 +00:00
}
// Place new order if the target price is higher than the minimum target price
if s.trailingStopControl.IsHigherThanMin(minTargetPrice) {
orderForm := s.trailingStopControl.GenerateStopOrder(s.state.Position.Base.Float64())
orders, err := s.submitOrders(ctx, orderExecutor, orderForm)
if err != nil {
2022-01-24 03:34:57 +00:00
log.WithError(err).Error("submit profit trailing stop order error")
} else {
s.trailingStopControl.OrderID = orders.IDs()[0]
2022-01-24 03:34:57 +00:00
}
}
}
// Save state
if err := s.SaveState(); err != nil {
log.WithError(err).Errorf("can not save state: %+v", s.state)
} else {
s.Notify("%s position is saved", s.Symbol, s.state.Position)
}
}
2021-06-16 05:23:33 +00:00
2021-06-21 11:03:50 +00:00
// check support volume
2021-02-10 16:21:06 +00:00
if kline.Volume < s.MinVolume.Float64() {
return
}
2021-06-21 11:03:50 +00:00
// check taker buy ratio, we need strong buy taker
if s.TakerBuyRatio > 0 {
2021-06-24 07:38:55 +00:00
takerBuyRatio := kline.TakerBuyBaseAssetVolume / kline.Volume
takerBuyBaseVolumeThreshold := kline.Volume * s.TakerBuyRatio.Float64()
2021-06-24 07:38:55 +00:00
if takerBuyRatio < s.TakerBuyRatio.Float64() {
s.Notify("%s: taker buy base volume %f (volume ratio %f) is less than %f (volume ratio %f)",
s.Symbol,
kline.TakerBuyBaseAssetVolume,
2021-06-24 07:38:55 +00:00
takerBuyRatio,
takerBuyBaseVolumeThreshold,
2021-06-16 05:23:33 +00:00
kline.Volume,
s.TakerBuyRatio.Float64(),
2021-06-24 07:38:55 +00:00
kline,
)
2021-06-01 08:39:35 +00:00
return
}
}
if s.longTermEMA != nil && closePriceF < s.longTermEMA.Last() {
s.Notify("%s: closed price is below the long term moving average line %f, skipping this support",
2021-06-21 11:03:50 +00:00
s.Symbol,
2021-06-24 11:29:41 +00:00
s.longTermEMA.Last(),
2021-06-21 11:03:50 +00:00
kline,
)
return
}
if s.triggerEMA != nil && closePriceF > s.triggerEMA.Last() {
s.Notify("%s: closed price is above the trigger moving average line %f, skipping this support",
s.Symbol,
s.triggerEMA.Last(),
kline,
)
return
}
2022-01-10 05:49:36 +00:00
if s.triggerEMA != nil && s.longTermEMA != nil {
s.Notify("Found %s support: the close price %f is below trigger EMA %f and above long term EMA %f and volume %f > minimum volume %f",
s.Symbol,
closePrice.Float64(),
s.triggerEMA.Last(),
s.longTermEMA.Last(),
kline.Volume,
s.MinVolume.Float64(),
kline)
} else {
s.Notify("Found %s support: the close price %f and volume %f > minimum volume %f",
s.Symbol,
closePrice.Float64(),
kline.Volume,
s.MinVolume.Float64(),
kline)
}
2021-02-10 16:21:06 +00:00
2021-06-16 12:33:52 +00:00
quantity, err := s.calculateQuantity(session, types.SideTypeBuy, closePrice, kline.Volume)
if err != nil {
log.WithError(err).Errorf("%s quantity calculation error", s.Symbol)
return
}
2021-02-14 17:26:46 +00:00
orderForm := types.SubmitOrder{
Symbol: s.Symbol,
Market: market,
Side: types.SideTypeBuy,
Type: types.OrderTypeMarket,
Quantity: quantity.Float64(),
2021-02-14 17:26:46 +00:00
MarginSideEffect: s.MarginOrderSideEffect,
}
2021-06-16 12:33:52 +00:00
s.Notify("Submitting %s market order buy with quantity %f according to the base volume %f, taker buy base volume %f",
s.Symbol,
quantity.Float64(),
kline.Volume,
kline.TakerBuyBaseAssetVolume,
orderForm)
if _, err := s.submitOrders(ctx, orderExecutor, orderForm); err != nil {
2021-02-10 16:21:06 +00:00
log.WithError(err).Error("submit order error")
return
}
// Save state
if err := s.SaveState(); err != nil {
log.WithError(err).Errorf("can not save state: %+v", s.state)
} else {
s.Notify("%s position is saved", s.Symbol, s.state.Position)
}
2021-06-16 12:33:52 +00:00
if s.TrailingStopTarget.TrailingStopCallBackRatio.Float64() == 0 { // submit fixed target orders
2022-01-24 03:34:57 +00:00
var targetOrders []types.SubmitOrder
for _, target := range s.Targets {
targetPrice := closePrice.Float64() * (1.0 + target.ProfitPercentage)
targetQuantity := quantity.Float64() * target.QuantityPercentage
targetQuoteQuantity := targetPrice * targetQuantity
if targetQuoteQuantity <= market.MinNotional {
continue
}
if targetQuantity <= market.MinQuantity {
continue
}
targetOrders = append(targetOrders, types.SubmitOrder{
Symbol: kline.Symbol,
Market: market,
Type: types.OrderTypeLimit,
Side: types.SideTypeSell,
Price: targetPrice,
Quantity: targetQuantity,
MarginSideEffect: target.MarginOrderSideEffect,
TimeInForce: "GTC",
})
}
if _, err := s.submitOrders(ctx, orderExecutor, targetOrders...); err != nil {
log.WithError(err).Error("submit profit target order error")
return
}
2021-02-10 16:21:06 +00:00
}
s.tradeCollector.Process()
2021-02-10 16:21:06 +00:00
})
2021-05-30 17:02:35 +00:00
s.Graceful.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
// Cancel trailing stop order
if s.TrailingStopTarget.TrailingStopCallBackRatio.Float64() > 0 {
if err := s.cancelOrder(s.trailingStopControl.OrderID, ctx, session); err != nil {
log.WithError(err).Errorf("Can not cancel the trailing stop order!")
}
s.trailingStopControl.OrderID = 0
}
2021-05-30 17:02:35 +00:00
if err := s.SaveState(); err != nil {
log.WithError(err).Errorf("can not save state: %+v", s.state)
} else {
s.Notify("%s position is saved", s.Symbol, s.state.Position)
}
})
2021-02-10 16:21:06 +00:00
return nil
}