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
|
|
|
|
2022-01-08 16:35:45 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2021-02-10 16:21:06 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
2022-06-19 09:23:10 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/indicator"
|
2021-02-10 16:21:06 +00:00
|
|
|
"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
|
|
|
|
|
|
|
var log = logrus.WithField("strategy", ID)
|
|
|
|
|
2021-08-17 03:06:41 +00:00
|
|
|
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 {
|
2022-01-24 05:09:12 +00:00
|
|
|
Position *types.Position `json:"position,omitempty"`
|
2022-01-28 05:14:58 +00:00
|
|
|
CurrentHighestPrice *fixedpoint.Value `json:"currentHighestPrice,omitempty"`
|
2021-05-30 17:02:35 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 16:21:06 +00:00
|
|
|
type Target struct {
|
2022-02-15 05:55:19 +00:00
|
|
|
ProfitPercentage fixedpoint.Value `json:"profitPercentage"`
|
|
|
|
QuantityPercentage fixedpoint.Value `json:"quantityPercentage"`
|
2021-02-14 17:26:46 +00:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2021-10-15 08:10:57 +00:00
|
|
|
// GenerateOrders generates the orders from the given targets
|
2021-12-11 11:16:16 +00:00
|
|
|
func (stop *PercentageTargetStop) GenerateOrders(market types.Market, pos *types.Position) []types.SubmitOrder {
|
2021-10-15 08:10:57 +00:00
|
|
|
var price = pos.AverageCost
|
2022-01-08 16:35:45 +00:00
|
|
|
var quantity = pos.GetBase()
|
2021-10-15 08:10:57 +00:00
|
|
|
|
|
|
|
// submit target orders
|
|
|
|
var targetOrders []types.SubmitOrder
|
|
|
|
for _, target := range stop.Targets {
|
2022-02-04 03:56:49 +00:00
|
|
|
targetPrice := price.Mul(fixedpoint.One.Add(target.ProfitPercentage))
|
|
|
|
targetQuantity := quantity.Mul(target.QuantityPercentage)
|
|
|
|
targetQuoteQuantity := targetPrice.Mul(targetQuantity)
|
2021-10-15 08:10:57 +00:00
|
|
|
|
2022-02-04 03:56:49 +00:00
|
|
|
if targetQuoteQuantity.Compare(market.MinNotional) <= 0 {
|
2021-10-15 08:10:57 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-02-04 03:56:49 +00:00
|
|
|
if targetQuantity.Compare(market.MinQuantity) <= 0 {
|
2021-10-15 08:10:57 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
targetOrders = append(targetOrders, types.SubmitOrder{
|
2021-12-11 11:16:16 +00:00
|
|
|
Symbol: market.Symbol,
|
|
|
|
Market: market,
|
|
|
|
Type: types.OrderTypeLimit,
|
|
|
|
Side: types.SideTypeSell,
|
|
|
|
Price: targetPrice,
|
|
|
|
Quantity: targetQuantity,
|
2021-10-15 08:10:57 +00:00
|
|
|
MarginSideEffect: target.MarginOrderSideEffect,
|
2022-02-18 05:52:13 +00:00
|
|
|
TimeInForce: types.TimeInForceGTC,
|
2021-10-15 08:10:57 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return targetOrders
|
|
|
|
}
|
|
|
|
|
2022-01-12 06:33:31 +00:00
|
|
|
type TrailingStopTarget struct {
|
2022-01-28 07:47:12 +00:00
|
|
|
TrailingStopCallbackRatio fixedpoint.Value `json:"callbackRatio"`
|
2022-01-28 05:06:57 +00:00
|
|
|
MinimumProfitPercentage fixedpoint.Value `json:"minimumProfitPercentage"`
|
2022-01-12 06:33:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TrailingStopControl struct {
|
2022-01-24 03:34:57 +00:00
|
|
|
symbol string
|
|
|
|
market types.Market
|
2022-01-12 06:33:31 +00:00
|
|
|
marginSideEffect types.MarginOrderSideEffectType
|
|
|
|
|
2022-01-28 07:47:12 +00:00
|
|
|
trailingStopCallbackRatio fixedpoint.Value
|
2022-01-28 05:06:57 +00:00
|
|
|
minimumProfitPercentage fixedpoint.Value
|
2022-01-12 06:33:31 +00:00
|
|
|
|
|
|
|
CurrentHighestPrice fixedpoint.Value
|
2022-06-19 09:49:38 +00:00
|
|
|
StopOrder *types.Order
|
2022-01-12 06:33:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 09:23:10 +00:00
|
|
|
func (control *TrailingStopControl) UpdateCurrentHighestPrice(p fixedpoint.Value) bool {
|
|
|
|
orig := control.CurrentHighestPrice
|
|
|
|
control.CurrentHighestPrice = fixedpoint.Max(control.CurrentHighestPrice, p)
|
|
|
|
return orig.Compare(control.CurrentHighestPrice) == 0
|
|
|
|
}
|
|
|
|
|
2022-02-10 12:39:20 +00:00
|
|
|
func (control *TrailingStopControl) IsHigherThanMin(minTargetPrice fixedpoint.Value) bool {
|
|
|
|
targetPrice := control.CurrentHighestPrice.Mul(fixedpoint.One.Sub(control.trailingStopCallbackRatio))
|
2022-01-12 06:33:31 +00:00
|
|
|
|
2022-02-10 12:39:20 +00:00
|
|
|
return targetPrice.Compare(minTargetPrice) >= 0
|
2022-01-12 06:33:31 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 12:39:20 +00:00
|
|
|
func (control *TrailingStopControl) GenerateStopOrder(quantity fixedpoint.Value) types.SubmitOrder {
|
|
|
|
targetPrice := control.CurrentHighestPrice.Mul(fixedpoint.One.Sub(control.trailingStopCallbackRatio))
|
2022-01-12 06:33:31 +00:00
|
|
|
|
|
|
|
orderForm := types.SubmitOrder{
|
2022-01-24 03:34:57 +00:00
|
|
|
Symbol: control.symbol,
|
|
|
|
Market: control.market,
|
|
|
|
Side: types.SideTypeSell,
|
2022-04-02 13:32:40 +00:00
|
|
|
Type: types.OrderTypeStopLimit,
|
2022-01-24 03:34:57 +00:00
|
|
|
Quantity: quantity,
|
|
|
|
MarginSideEffect: control.marginSideEffect,
|
2022-04-02 13:32:40 +00:00
|
|
|
TimeInForce: types.TimeInForceGTC,
|
2022-01-24 03:34:57 +00:00
|
|
|
|
|
|
|
Price: targetPrice,
|
|
|
|
StopPrice: targetPrice,
|
|
|
|
}
|
|
|
|
|
|
|
|
return orderForm
|
2022-01-12 06:33:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Not implemented yet
|
|
|
|
// ResistanceStop is a kind of stop order by detecting resistance
|
2022-06-19 04:29:36 +00:00
|
|
|
// type ResistanceStop struct {
|
2022-01-12 06:33:31 +00:00
|
|
|
// Interval types.Interval `json:"interval"`
|
|
|
|
// sensitivity fixedpoint.Value `json:"sensitivity"`
|
|
|
|
// MinVolume fixedpoint.Value `json:"minVolume"`
|
|
|
|
// TakerBuyRatio fixedpoint.Value `json:"takerBuyRatio"`
|
2022-06-19 04:29:36 +00:00
|
|
|
// }
|
2022-01-12 06:33:31 +00:00
|
|
|
|
2021-02-10 16:21:06 +00:00
|
|
|
type Strategy struct {
|
2022-06-19 09:49:38 +00:00
|
|
|
*bbgo.Environment `json:"-"`
|
|
|
|
|
|
|
|
session *bbgo.ExchangeSession
|
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)
|
2021-12-19 10:28:47 +00:00
|
|
|
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"`
|
2021-05-30 16:31:31 +00:00
|
|
|
TakerBuyRatio fixedpoint.Value `json:"takerBuyRatio"`
|
2021-02-14 17:26:46 +00:00
|
|
|
MarginOrderSideEffect types.MarginOrderSideEffectType `json:"marginOrderSideEffect"`
|
|
|
|
Targets []Target `json:"targets"`
|
2021-02-28 03:57:25 +00:00
|
|
|
|
2022-01-24 03:34:57 +00:00
|
|
|
// Not implemented yet
|
2022-01-12 06:33:31 +00:00
|
|
|
// ResistanceStop *ResistanceStop `json:"resistanceStop"`
|
|
|
|
//
|
2022-06-19 04:29:36 +00:00
|
|
|
// 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"`
|
2021-05-30 16:31:31 +00:00
|
|
|
// Max BaseAsset balance to buy
|
|
|
|
MaxBaseAssetBalance fixedpoint.Value `json:"maxBaseAssetBalance"`
|
|
|
|
MinQuoteAssetBalance fixedpoint.Value `json:"minQuoteAssetBalance"`
|
|
|
|
|
2021-02-28 06:51:24 +00:00
|
|
|
ScaleQuantity *bbgo.PriceVolumeScale `json:"scaleQuantity"`
|
2021-05-30 16:31:31 +00:00
|
|
|
|
2022-06-19 07:57:59 +00:00
|
|
|
orderExecutor *bbgo.GeneralOrderExecutor
|
2022-03-15 11:19:44 +00:00
|
|
|
|
2022-06-19 09:23:10 +00:00
|
|
|
Position *types.Position `persistence:"position"`
|
|
|
|
ProfitStats *types.ProfitStats `persistence:"profit_stats"`
|
|
|
|
TradeStats *types.TradeStats `persistence:"trade_stats"`
|
|
|
|
CurrentHighestPrice fixedpoint.Value `persistence:"current_highest_price"`
|
2021-06-24 07:38:55 +00:00
|
|
|
|
2021-06-21 11:03:50 +00:00
|
|
|
triggerEMA *indicator.EWMA
|
|
|
|
longTermEMA *indicator.EWMA
|
2022-01-12 06:33:31 +00:00
|
|
|
|
|
|
|
// Trailing stop
|
2022-01-24 03:34:57 +00:00
|
|
|
TrailingStopTarget TrailingStopTarget `json:"trailingStopTarget"`
|
|
|
|
trailingStopControl *TrailingStopControl
|
2022-03-21 02:20:12 +00:00
|
|
|
|
|
|
|
// StrategyController
|
2022-04-20 11:20:15 +00:00
|
|
|
bbgo.StrategyController
|
2021-02-10 16:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) ID() string {
|
|
|
|
return ID
|
|
|
|
}
|
|
|
|
|
2022-06-06 09:34:39 +00:00
|
|
|
func (s *Strategy) InstanceID() string {
|
|
|
|
return fmt.Sprintf("%s:%s", ID, s.Symbol)
|
|
|
|
}
|
|
|
|
|
2021-04-02 02:32:24 +00:00
|
|
|
func (s *Strategy) Validate() error {
|
2022-02-04 03:56:49 +00:00
|
|
|
if s.Quantity.IsZero() && s.ScaleQuantity == nil {
|
2021-04-02 02:32:24 +00:00
|
|
|
return fmt.Errorf("quantity or scaleQuantity can not be zero")
|
|
|
|
}
|
|
|
|
|
2022-02-04 03:56:49 +00:00
|
|
|
if s.MinVolume.IsZero() && s.Sensitivity.IsZero() {
|
2021-06-16 05:14:10 +00:00
|
|
|
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) {
|
2022-05-19 01:48:36 +00:00
|
|
|
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
|
2021-08-17 03:06:41 +00:00
|
|
|
|
2021-12-19 10:28:47 +00:00
|
|
|
if s.TriggerMovingAverage != zeroiw {
|
2022-05-19 01:48:36 +00:00
|
|
|
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.TriggerMovingAverage.Interval})
|
2021-12-19 10:28:47 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 03:06:41 +00:00
|
|
|
if s.LongTermMovingAverage != zeroiw {
|
2022-05-19 01:48:36 +00:00
|
|
|
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.LongTermMovingAverage.Interval})
|
2021-08-17 03:06:41 +00:00
|
|
|
}
|
2021-02-10 16:21:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 11:20:15 +00:00
|
|
|
func (s *Strategy) CurrentPosition() *types.Position {
|
2022-06-19 09:49:38 +00:00
|
|
|
return s.Position
|
2022-04-20 11:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error {
|
2022-06-19 09:49:38 +00:00
|
|
|
base := s.Position.GetBase()
|
2022-04-20 11:20:15 +00:00
|
|
|
if base.IsZero() {
|
2022-06-19 09:49:38 +00:00
|
|
|
return fmt.Errorf("no opened %s position", s.Position.Symbol)
|
2022-04-20 11:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// make it negative
|
|
|
|
quantity := base.Mul(percentage).Abs()
|
|
|
|
side := types.SideTypeBuy
|
|
|
|
if base.Sign() > 0 {
|
|
|
|
side = types.SideTypeSell
|
|
|
|
}
|
|
|
|
|
|
|
|
if quantity.Compare(s.Market.MinQuantity) < 0 {
|
|
|
|
return fmt.Errorf("order quantity %v is too small, less than %v", quantity, s.Market.MinQuantity)
|
|
|
|
}
|
|
|
|
|
|
|
|
submitOrder := types.SubmitOrder{
|
|
|
|
Symbol: s.Symbol,
|
|
|
|
Side: side,
|
|
|
|
Type: types.OrderTypeMarket,
|
|
|
|
Quantity: quantity,
|
|
|
|
Market: s.Market,
|
|
|
|
}
|
|
|
|
|
2022-06-19 04:29:36 +00:00
|
|
|
bbgo.Notify("Submitting %s %s order to close position by %v", s.Symbol, side.String(), percentage, submitOrder)
|
2022-06-19 07:57:59 +00:00
|
|
|
_, err := s.orderExecutor.SubmitOrders(ctx, submitOrder)
|
2022-04-20 11:20:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-24 04:11:26 +00:00
|
|
|
func (s *Strategy) submitOrders(ctx context.Context, orderExecutor bbgo.OrderExecutor, orderForms ...types.SubmitOrder) (types.OrderSlice, error) {
|
2022-06-19 07:57:59 +00:00
|
|
|
return s.orderExecutor.SubmitOrders(ctx, orderForms...)
|
2021-06-16 12:33:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-04 03:56:49 +00:00
|
|
|
var slippageModifier = fixedpoint.NewFromFloat(1.003)
|
|
|
|
|
|
|
|
func (s *Strategy) calculateQuantity(session *bbgo.ExchangeSession, side types.SideType, closePrice fixedpoint.Value, volume fixedpoint.Value) (fixedpoint.Value, error) {
|
2021-06-16 12:33:52 +00:00
|
|
|
var quantity fixedpoint.Value
|
2022-02-04 03:56:49 +00:00
|
|
|
if s.Quantity.Sign() > 0 {
|
2021-06-16 12:33:52 +00:00
|
|
|
quantity = s.Quantity
|
|
|
|
} else if s.ScaleQuantity != nil {
|
2022-02-04 03:56:49 +00:00
|
|
|
q, err := s.ScaleQuantity.Scale(closePrice.Float64(), volume.Float64())
|
2021-06-16 12:33:52 +00:00
|
|
|
if err != nil {
|
2022-02-04 03:56:49 +00:00
|
|
|
return fixedpoint.Zero, err
|
2021-06-16 12:33:52 +00:00
|
|
|
}
|
2022-02-04 03:56:49 +00:00
|
|
|
quantity = fixedpoint.NewFromFloat(q)
|
2021-06-16 12:33:52 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 07:43:11 +00:00
|
|
|
baseBalance, _ := session.GetAccount().Balance(s.Market.BaseCurrency)
|
2021-06-17 11:28:11 +00:00
|
|
|
if side == types.SideTypeSell {
|
|
|
|
// quantity = bbgo.AdjustQuantityByMaxAmount(quantity, closePrice, quota)
|
2022-02-15 05:55:19 +00:00
|
|
|
if s.MinBaseAssetBalance.Sign() > 0 &&
|
2022-02-04 03:56:49 +00:00
|
|
|
baseBalance.Total().Sub(quantity).Compare(s.MinBaseAssetBalance) < 0 {
|
|
|
|
quota := baseBalance.Available.Sub(s.MinBaseAssetBalance)
|
2021-06-17 11:28:11 +00:00
|
|
|
quantity = bbgo.AdjustQuantityByMaxAmount(quantity, closePrice, quota)
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if side == types.SideTypeBuy {
|
2022-02-04 03:56:49 +00:00
|
|
|
if s.MaxBaseAssetBalance.Sign() > 0 &&
|
|
|
|
baseBalance.Total().Add(quantity).Compare(s.MaxBaseAssetBalance) > 0 {
|
|
|
|
quota := s.MaxBaseAssetBalance.Sub(baseBalance.Total())
|
2021-06-16 12:33:52 +00:00
|
|
|
quantity = bbgo.AdjustQuantityByMaxAmount(quantity, closePrice, quota)
|
|
|
|
}
|
|
|
|
|
2022-04-23 07:43:11 +00:00
|
|
|
quoteBalance, ok := session.GetAccount().Balance(s.Market.QuoteCurrency)
|
2021-06-16 12:33:52 +00:00
|
|
|
if !ok {
|
2022-02-04 03:56:49 +00:00
|
|
|
return fixedpoint.Zero, fmt.Errorf("quote balance %s not found", s.Market.QuoteCurrency)
|
2021-06-16 12:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// for spot, we need to modify the quantity according to the quote balance
|
|
|
|
if !session.Margin {
|
|
|
|
// add 0.3% for price slippage
|
2022-02-04 03:56:49 +00:00
|
|
|
notional := closePrice.Mul(quantity).Mul(slippageModifier)
|
2021-06-16 12:33:52 +00:00
|
|
|
|
2022-02-04 03:56:49 +00:00
|
|
|
if s.MinQuoteAssetBalance.Sign() > 0 &&
|
|
|
|
quoteBalance.Available.Sub(notional).Compare(s.MinQuoteAssetBalance) < 0 {
|
2022-02-09 10:48:40 +00:00
|
|
|
log.Warnf("modifying quantity %v according to the min quote asset balance %v %s",
|
|
|
|
quantity,
|
|
|
|
quoteBalance.Available,
|
2021-06-16 12:33:52 +00:00
|
|
|
s.Market.QuoteCurrency)
|
2022-02-04 03:56:49 +00:00
|
|
|
quota := quoteBalance.Available.Sub(s.MinQuoteAssetBalance)
|
2021-06-16 12:33:52 +00:00
|
|
|
quantity = bbgo.AdjustQuantityByMinAmount(quantity, closePrice, quota)
|
2022-02-04 03:56:49 +00:00
|
|
|
} else if notional.Compare(quoteBalance.Available) > 0 {
|
2022-02-09 10:48:40 +00:00
|
|
|
log.Warnf("modifying quantity %v according to the quote asset balance %v %s",
|
|
|
|
quantity,
|
|
|
|
quoteBalance.Available,
|
2021-06-16 12:33:52 +00:00
|
|
|
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 {
|
2022-06-19 09:49:38 +00:00
|
|
|
s.session = session
|
2022-06-19 07:57:59 +00:00
|
|
|
instanceID := s.InstanceID()
|
|
|
|
|
|
|
|
if s.Position == nil {
|
|
|
|
s.Position = types.NewPositionFromMarket(s.Market)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.ProfitStats == nil {
|
|
|
|
s.ProfitStats = types.NewProfitStats(s.Market)
|
|
|
|
}
|
|
|
|
|
|
|
|
// trade stats
|
|
|
|
if s.TradeStats == nil {
|
2022-07-05 03:14:50 +00:00
|
|
|
s.TradeStats = types.NewTradeStats(s.Symbol)
|
2022-06-19 07:57:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.orderExecutor = bbgo.NewGeneralOrderExecutor(session, s.Symbol, ID, instanceID, s.Position)
|
|
|
|
s.orderExecutor.BindEnvironment(s.Environment)
|
|
|
|
s.orderExecutor.BindProfitStats(s.ProfitStats)
|
|
|
|
s.orderExecutor.BindTradeStats(s.TradeStats)
|
|
|
|
s.orderExecutor.Bind()
|
2022-03-15 11:19:44 +00:00
|
|
|
|
2022-03-21 02:20:12 +00:00
|
|
|
// StrategyController
|
2022-04-20 11:20:15 +00:00
|
|
|
s.Status = types.StrategyStatusRunning
|
2022-04-18 09:26:43 +00:00
|
|
|
|
2022-04-21 10:03:54 +00:00
|
|
|
s.OnSuspend(func() {
|
2022-04-18 09:26:43 +00:00
|
|
|
// Cancel all order
|
2022-06-19 07:57:59 +00:00
|
|
|
_ = s.orderExecutor.GracefulCancel(ctx)
|
2022-10-03 10:45:24 +00:00
|
|
|
bbgo.Sync(ctx, s)
|
2022-04-18 09:26:43 +00:00
|
|
|
})
|
|
|
|
|
2022-04-21 10:03:54 +00:00
|
|
|
s.OnEmergencyStop(func() {
|
2022-04-18 09:26:43 +00:00
|
|
|
// Close 100% position
|
2022-04-21 09:57:49 +00:00
|
|
|
percentage := fixedpoint.NewFromFloat(1.0)
|
2022-04-21 10:03:54 +00:00
|
|
|
if err := s.ClosePosition(context.Background(), percentage); err != nil {
|
|
|
|
errMsg := "failed to close position"
|
|
|
|
log.WithError(err).Errorf(errMsg)
|
2022-06-19 04:29:36 +00:00
|
|
|
bbgo.Notify(errMsg)
|
2022-04-18 09:26:43 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 10:03:54 +00:00
|
|
|
if err := s.Suspend(); err != nil {
|
|
|
|
errMsg := "failed to suspend strategy"
|
|
|
|
log.WithError(err).Errorf(errMsg)
|
2022-06-19 04:29:36 +00:00
|
|
|
bbgo.Notify(errMsg)
|
2022-04-21 10:03:54 +00:00
|
|
|
}
|
2022-04-18 09:26:43 +00:00
|
|
|
})
|
2022-03-21 02:20:12 +00:00
|
|
|
|
2021-02-10 16:21:06 +00:00
|
|
|
// set default values
|
|
|
|
if s.Interval == "" {
|
|
|
|
s.Interval = types.Interval5m
|
|
|
|
}
|
|
|
|
|
2022-02-04 03:56:49 +00:00
|
|
|
if s.Sensitivity.Sign() > 0 {
|
2021-06-16 05:14:10 +00:00
|
|
|
volRange, err := s.ScaleQuantity.ByVolumeRule.Range()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-04 03:56:49 +00:00
|
|
|
scaleUp := fixedpoint.NewFromFloat(volRange[1])
|
|
|
|
scaleLow := fixedpoint.NewFromFloat(volRange[0])
|
|
|
|
s.MinVolume = scaleUp.Sub(scaleLow).
|
|
|
|
Mul(fixedpoint.One.Sub(s.Sensitivity)).
|
|
|
|
Add(scaleLow)
|
|
|
|
log.Infof("adjusted minimal support volume to %s according to sensitivity %s", s.MinVolume.String(), s.Sensitivity.String())
|
2021-06-16 05:23:33 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 10:35:50 +00:00
|
|
|
standardIndicatorSet := session.StandardIndicatorSet(s.Symbol)
|
2021-02-10 16:21:06 +00:00
|
|
|
|
2021-12-19 10:28:47 +00:00
|
|
|
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
|
|
|
})
|
2021-12-19 10:28:47 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 12:16:53 +00:00
|
|
|
if s.LongTermMovingAverage != zeroiw {
|
|
|
|
s.longTermEMA = standardIndicatorSet.EWMA(s.LongTermMovingAverage)
|
|
|
|
}
|
|
|
|
|
2022-02-10 12:39:20 +00:00
|
|
|
if !s.TrailingStopTarget.TrailingStopCallbackRatio.IsZero() {
|
2022-01-28 04:58:35 +00:00
|
|
|
s.trailingStopControl = &TrailingStopControl{
|
|
|
|
symbol: s.Symbol,
|
|
|
|
market: s.Market,
|
|
|
|
marginSideEffect: s.MarginOrderSideEffect,
|
2022-01-28 07:47:12 +00:00
|
|
|
trailingStopCallbackRatio: s.TrailingStopTarget.TrailingStopCallbackRatio,
|
2022-01-28 04:58:35 +00:00
|
|
|
minimumProfitPercentage: s.TrailingStopTarget.MinimumProfitPercentage,
|
2022-06-19 09:23:10 +00:00
|
|
|
CurrentHighestPrice: s.CurrentHighestPrice,
|
2022-01-28 04:58:35 +00:00
|
|
|
}
|
2022-01-24 05:09:12 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 12:39:20 +00:00
|
|
|
if !s.TrailingStopTarget.TrailingStopCallbackRatio.IsZero() {
|
2022-01-12 06:33:31 +00:00
|
|
|
// Update trailing stop when the position changes
|
2022-06-19 07:57:59 +00:00
|
|
|
s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
|
2022-03-21 02:20:12 +00:00
|
|
|
// StrategyController
|
2022-04-20 11:20:15 +00:00
|
|
|
if s.Status != types.StrategyStatusRunning {
|
2022-03-21 02:20:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-19 09:23:10 +00:00
|
|
|
if !position.IsLong() || position.IsDust(position.AverageCost) {
|
|
|
|
return
|
2022-01-12 06:33:31 +00:00
|
|
|
}
|
2022-06-19 09:23:10 +00:00
|
|
|
|
|
|
|
s.updateStopOrder(ctx)
|
2022-01-12 06:33:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-27 19:15:29 +00:00
|
|
|
session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {
|
2022-03-21 02:20:12 +00:00
|
|
|
// StrategyController
|
2022-04-20 11:20:15 +00:00
|
|
|
if s.Status != types.StrategyStatusRunning {
|
2022-03-21 02:20:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-02-04 03:56:49 +00:00
|
|
|
closePrice := kline.GetClose()
|
|
|
|
highPrice := kline.GetHigh()
|
2022-01-12 06:33:31 +00:00
|
|
|
|
2022-06-19 09:23:10 +00:00
|
|
|
// check our trailing stop
|
2022-02-10 12:39:20 +00:00
|
|
|
if s.TrailingStopTarget.TrailingStopCallbackRatio.Sign() > 0 {
|
2022-06-19 09:23:10 +00:00
|
|
|
if s.Position.IsLong() && !s.Position.IsDust(closePrice) {
|
|
|
|
changed := s.trailingStopControl.UpdateCurrentHighestPrice(highPrice)
|
|
|
|
if changed {
|
|
|
|
// Cancel the original order
|
|
|
|
s.updateStopOrder(ctx)
|
2022-01-24 03:34:57 +00:00
|
|
|
}
|
2022-01-12 06:33:31 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-16 05:23:33 +00:00
|
|
|
|
2021-06-21 11:03:50 +00:00
|
|
|
// check support volume
|
2022-02-04 03:56:49 +00:00
|
|
|
if kline.Volume.Compare(s.MinVolume) < 0 {
|
2021-02-10 16:21:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:03:50 +00:00
|
|
|
// check taker buy ratio, we need strong buy taker
|
2022-02-04 03:56:49 +00:00
|
|
|
if s.TakerBuyRatio.Sign() > 0 {
|
|
|
|
takerBuyRatio := kline.TakerBuyBaseAssetVolume.Div(kline.Volume)
|
|
|
|
takerBuyBaseVolumeThreshold := kline.Volume.Mul(s.TakerBuyRatio)
|
|
|
|
if takerBuyRatio.Compare(s.TakerBuyRatio) < 0 {
|
2022-06-19 04:29:36 +00:00
|
|
|
bbgo.Notify("%s: taker buy base volume %s (volume ratio %s) is less than %s (volume ratio %s)",
|
2021-05-30 16:31:31 +00:00
|
|
|
s.Symbol,
|
2022-02-04 03:56:49 +00:00
|
|
|
kline.TakerBuyBaseAssetVolume.String(),
|
|
|
|
takerBuyRatio.String(),
|
|
|
|
takerBuyBaseVolumeThreshold.String(),
|
|
|
|
kline.Volume.String(),
|
|
|
|
s.TakerBuyRatio.String(),
|
2021-06-24 07:38:55 +00:00
|
|
|
kline,
|
2021-05-30 16:31:31 +00:00
|
|
|
)
|
2021-06-01 08:39:35 +00:00
|
|
|
return
|
2021-05-30 16:31:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 11:35:44 +00:00
|
|
|
if s.longTermEMA != nil && closePrice.Float64() < s.longTermEMA.Last(0) {
|
2022-06-19 04:29:36 +00:00
|
|
|
bbgo.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,
|
2023-05-31 11:35:44 +00:00
|
|
|
s.longTermEMA.Last(0),
|
2021-06-21 11:03:50 +00:00
|
|
|
kline,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-31 11:35:44 +00:00
|
|
|
if s.triggerEMA != nil && closePrice.Float64() > s.triggerEMA.Last(0) {
|
2022-06-19 04:29:36 +00:00
|
|
|
bbgo.Notify("%s: closed price is above the trigger moving average line %f, skipping this support",
|
2021-12-19 09:53:34 +00:00
|
|
|
s.Symbol,
|
2023-05-31 11:35:44 +00:00
|
|
|
s.triggerEMA.Last(0),
|
2021-12-19 09:53:34 +00:00
|
|
|
kline,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-10 05:49:36 +00:00
|
|
|
if s.triggerEMA != nil && s.longTermEMA != nil {
|
2022-06-19 04:29:36 +00:00
|
|
|
bbgo.Notify("Found %s support: the close price %s is below trigger EMA %f and above long term EMA %f and volume %s > minimum volume %s",
|
2022-01-10 05:49:36 +00:00
|
|
|
s.Symbol,
|
2022-02-04 03:56:49 +00:00
|
|
|
closePrice.String(),
|
2023-05-31 11:35:44 +00:00
|
|
|
s.triggerEMA.Last(0),
|
|
|
|
s.longTermEMA.Last(0),
|
2022-02-04 03:56:49 +00:00
|
|
|
kline.Volume.String(),
|
|
|
|
s.MinVolume.String(),
|
2022-01-10 05:49:36 +00:00
|
|
|
kline)
|
|
|
|
} else {
|
2022-06-19 04:29:36 +00:00
|
|
|
bbgo.Notify("Found %s support: the close price %s and volume %s > minimum volume %s",
|
2022-01-10 05:49:36 +00:00
|
|
|
s.Symbol,
|
2022-02-04 03:56:49 +00:00
|
|
|
closePrice.String(),
|
|
|
|
kline.Volume.String(),
|
|
|
|
s.MinVolume.String(),
|
2022-01-10 05:49:36 +00:00
|
|
|
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)
|
2021-05-30 16:31:31 +00:00
|
|
|
return
|
|
|
|
}
|
2021-05-11 05:25:29 +00:00
|
|
|
|
2021-02-14 17:26:46 +00:00
|
|
|
orderForm := types.SubmitOrder{
|
|
|
|
Symbol: s.Symbol,
|
2022-06-19 07:57:59 +00:00
|
|
|
Market: s.Market,
|
2021-02-14 17:26:46 +00:00
|
|
|
Side: types.SideTypeBuy,
|
|
|
|
Type: types.OrderTypeMarket,
|
2022-02-04 03:56:49 +00:00
|
|
|
Quantity: quantity,
|
2021-02-14 17:26:46 +00:00
|
|
|
MarginSideEffect: s.MarginOrderSideEffect,
|
|
|
|
}
|
|
|
|
|
2022-06-19 04:29:36 +00:00
|
|
|
bbgo.Notify("Submitting %s market order buy with quantity %s according to the base volume %s, taker buy base volume %s",
|
2021-06-16 12:33:52 +00:00
|
|
|
s.Symbol,
|
2022-02-04 03:56:49 +00:00
|
|
|
quantity.String(),
|
|
|
|
kline.Volume.String(),
|
|
|
|
kline.TakerBuyBaseAssetVolume.String(),
|
2021-06-16 12:33:52 +00:00
|
|
|
orderForm)
|
|
|
|
|
2022-01-12 06:33:31 +00:00
|
|
|
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
|
|
|
|
}
|
2021-06-16 12:33:52 +00:00
|
|
|
|
2022-02-10 12:39:20 +00:00
|
|
|
if s.TrailingStopTarget.TrailingStopCallbackRatio.IsZero() { // submit fixed target orders
|
2022-01-24 03:34:57 +00:00
|
|
|
var targetOrders []types.SubmitOrder
|
|
|
|
for _, target := range s.Targets {
|
2022-02-04 03:56:49 +00:00
|
|
|
targetPrice := closePrice.Mul(fixedpoint.One.Add(target.ProfitPercentage))
|
|
|
|
targetQuantity := quantity.Mul(target.QuantityPercentage)
|
|
|
|
targetQuoteQuantity := targetPrice.Mul(targetQuantity)
|
2022-01-24 03:34:57 +00:00
|
|
|
|
2022-06-19 07:57:59 +00:00
|
|
|
if targetQuoteQuantity.Compare(s.Market.MinNotional) <= 0 {
|
2022-01-24 03:34:57 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-19 07:57:59 +00:00
|
|
|
if targetQuantity.Compare(s.Market.MinQuantity) <= 0 {
|
2022-01-24 03:34:57 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
targetOrders = append(targetOrders, types.SubmitOrder{
|
|
|
|
Symbol: kline.Symbol,
|
2022-06-19 07:57:59 +00:00
|
|
|
Market: s.Market,
|
2022-01-24 03:34:57 +00:00
|
|
|
Type: types.OrderTypeLimit,
|
|
|
|
Side: types.SideTypeSell,
|
|
|
|
Price: targetPrice,
|
|
|
|
Quantity: targetQuantity,
|
|
|
|
|
|
|
|
MarginSideEffect: target.MarginOrderSideEffect,
|
2022-02-18 05:52:13 +00:00
|
|
|
TimeInForce: types.TimeInForceGTC,
|
2022-01-24 03:34:57 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-19 07:57:59 +00:00
|
|
|
_, err = s.orderExecutor.SubmitOrders(ctx, targetOrders...)
|
|
|
|
if err != nil {
|
|
|
|
bbgo.Notify("submit %s profit trailing stop order error: %s", s.Symbol, err.Error())
|
2022-01-24 03:34:57 +00:00
|
|
|
}
|
2021-02-10 16:21:06 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-10-03 08:01:08 +00:00
|
|
|
bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) {
|
2021-05-30 17:02:35 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
2022-01-24 05:09:12 +00:00
|
|
|
// Cancel trailing stop order
|
2022-02-10 12:39:20 +00:00
|
|
|
if s.TrailingStopTarget.TrailingStopCallbackRatio.Sign() > 0 {
|
2022-06-19 07:57:59 +00:00
|
|
|
_ = s.orderExecutor.GracefulCancel(ctx)
|
2021-05-30 17:02:35 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-02-10 16:21:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-06-19 09:23:10 +00:00
|
|
|
|
|
|
|
func (s *Strategy) updateStopOrder(ctx context.Context) {
|
|
|
|
// cancel the original stop order
|
2022-06-19 09:49:38 +00:00
|
|
|
if s.trailingStopControl.StopOrder != nil {
|
|
|
|
if err := s.session.Exchange.CancelOrders(ctx, *s.trailingStopControl.StopOrder); err != nil {
|
|
|
|
log.WithError(err).Error("cancel order error")
|
|
|
|
}
|
|
|
|
s.trailingStopControl.StopOrder = nil
|
|
|
|
s.orderExecutor.TradeCollector().Process()
|
|
|
|
}
|
2022-06-19 09:23:10 +00:00
|
|
|
|
|
|
|
// Calculate minimum target price
|
|
|
|
var minTargetPrice = fixedpoint.Zero
|
|
|
|
if s.trailingStopControl.minimumProfitPercentage.Sign() > 0 {
|
2022-06-19 09:49:38 +00:00
|
|
|
minTargetPrice = s.Position.AverageCost.Mul(fixedpoint.One.Add(s.trailingStopControl.minimumProfitPercentage))
|
2022-06-19 09:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Place new order if the target price is higher than the minimum target price
|
|
|
|
if s.trailingStopControl.IsHigherThanMin(minTargetPrice) {
|
2022-06-19 09:49:38 +00:00
|
|
|
orderForm := s.trailingStopControl.GenerateStopOrder(s.Position.Base)
|
2022-06-19 09:23:10 +00:00
|
|
|
orders, err := s.orderExecutor.SubmitOrders(ctx, orderForm)
|
|
|
|
if err != nil {
|
|
|
|
bbgo.Notify("failed to submit the trailing stop order on %s", s.Symbol)
|
|
|
|
log.WithError(err).Error("submit profit trailing stop order error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(orders) == 0 {
|
|
|
|
log.Error("unexpected error: len(createdOrders) = 0")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-19 09:49:38 +00:00
|
|
|
s.trailingStopControl.StopOrder = &orders[0]
|
2022-06-19 09:23:10 +00:00
|
|
|
}
|
|
|
|
}
|