bbgo_origin/pkg/strategy/bollmaker/strategy.go

693 lines
22 KiB
Go
Raw Permalink Normal View History

2022-01-08 17:20:47 +00:00
package bollmaker
2021-10-07 08:39:20 +00:00
import (
"context"
"fmt"
2022-01-09 14:32:23 +00:00
"math"
2021-10-07 08:39:20 +00:00
"sync"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
2023-12-19 14:04:24 +00:00
indicatorv2 "github.com/c9s/bbgo/pkg/indicator/v2"
2021-10-07 08:39:20 +00:00
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
2022-01-27 10:51:51 +00:00
// TODO:
// 1) add option for placing orders only when in neutral band
// 2) add option for only placing buy orders when price is below the SMA line
2022-01-08 17:20:47 +00:00
const ID = "bollmaker"
2021-10-07 08:39:20 +00:00
2022-02-07 03:40:30 +00:00
var notionModifier = fixedpoint.NewFromFloat(1.1)
var two = fixedpoint.NewFromInt(2)
2021-10-07 08:39:20 +00:00
var log = logrus.WithField("strategy", ID)
func init() {
bbgo.RegisterStrategy(ID, &Strategy{})
}
// Deprecated: State is deprecated, please use the persistence tag
2021-10-07 08:39:20 +00:00
type State struct {
// Deprecated: Position is deprecated, please define the Position field in the strategy struct directly.
Position *types.Position `json:"position,omitempty"`
// Deprecated: ProfitStats is deprecated, please define the ProfitStats field in the strategy struct directly.
ProfitStats types.ProfitStats `json:"profitStats,omitempty"`
2021-10-07 08:39:20 +00:00
}
type BollingerSetting struct {
types.IntervalWindow
BandWidth float64 `json:"bandWidth"`
}
2023-12-19 14:04:24 +00:00
type EMACrossSetting struct {
Enabled bool `json:"enabled"`
2023-12-19 14:04:24 +00:00
Interval types.Interval `json:"interval"`
FastWindow int `json:"fastWindow"`
SlowWindow int `json:"slowWindow"`
fastEMA, slowEMA *indicatorv2.EWMAStream
cross *indicatorv2.CrossStream
2023-12-19 14:04:24 +00:00
}
2021-10-07 08:39:20 +00:00
type Strategy struct {
2022-03-11 09:00:09 +00:00
Environment *bbgo.Environment
StandardIndicatorSet *bbgo.StandardIndicatorSet
2022-03-11 09:00:09 +00:00
Market types.Market
2022-01-09 14:43:49 +00:00
// Symbol is the market symbol you want to trade
2022-01-14 18:52:46 +00:00
Symbol string `json:"symbol"`
2022-01-09 14:43:49 +00:00
types.IntervalWindow
2022-01-09 14:43:49 +00:00
bbgo.QuantityOrAmount
2022-01-09 14:43:49 +00:00
2023-12-19 13:55:38 +00:00
// BidQuantity is used for placing buy order, this will override the default quantity
BidQuantity fixedpoint.Value `json:"bidQuantity"`
// AskQuantity is used for placing sell order, this will override the default quantity
AskQuantity fixedpoint.Value `json:"askQuantity"`
2022-09-11 09:28:54 +00:00
// TrendEMA is used for detecting the trend by a given EMA
// you can define interval and window
TrendEMA *bbgo.TrendEMA `json:"trendEMA"`
2022-01-09 14:43:49 +00:00
// Spread is the price spread from the middle price.
// For ask orders, the ask price is ((bestAsk + bestBid) / 2 * (1.0 + spread))
// For bid orders, the bid price is ((bestAsk + bestBid) / 2 * (1.0 - spread))
// Spread can be set by percentage or floating number. e.g., 0.1% or 0.001
2022-01-14 18:52:46 +00:00
Spread fixedpoint.Value `json:"spread"`
2022-01-09 14:43:49 +00:00
2022-01-30 17:08:33 +00:00
// BidSpread overrides the spread setting, this spread will be used for the buy order
BidSpread fixedpoint.Value `json:"bidSpread,omitempty"`
// AskSpread overrides the spread setting, this spread will be used for the sell order
AskSpread fixedpoint.Value `json:"askSpread,omitempty"`
// DynamicSpread enables the automatic adjustment to bid and ask spread.
DynamicSpread DynamicSpreadSettings `json:"dynamicSpread,omitempty"`
2022-05-13 09:58:46 +00:00
2022-01-09 14:43:49 +00:00
// MinProfitSpread is the minimal order price spread from the current average cost.
// For long position, you will only place sell order above the price (= average cost * (1 + minProfitSpread))
// For short position, you will only place buy order below the price (= average cost * (1 - minProfitSpread))
2022-01-08 18:24:10 +00:00
MinProfitSpread fixedpoint.Value `json:"minProfitSpread"`
2022-01-09 14:43:49 +00:00
// MinProfitActivationRate activates MinProfitSpread when position RoI higher than the specified percentage
MinProfitActivationRate *fixedpoint.Value `json:"minProfitActivationRate"`
// UseTickerPrice use the ticker api to get the mid-price instead of the closed kline price.
2022-01-09 14:43:49 +00:00
// The back-test engine is kline-based, so the ticker price api is not supported.
// Turn this on if you want to do real trading.
2022-01-14 18:52:46 +00:00
UseTickerPrice bool `json:"useTickerPrice"`
2022-01-08 18:24:10 +00:00
// MaxExposurePosition is the maximum position you can hold
// +10 means you can hold 10 ETH long position by maximum
// -10 means you can hold -10 ETH short position by maximum
MaxExposurePosition fixedpoint.Value `json:"maxExposurePosition"`
2021-10-07 08:39:20 +00:00
// DynamicExposurePositionScale is used to define the exposure position range with the given percentage
// when DynamicExposurePositionScale is set,
2022-01-26 17:40:54 +00:00
// your MaxExposurePosition will be calculated dynamically according to the bollinger band you set.
DynamicExposurePositionScale *bbgo.PercentageScale `json:"dynamicExposurePositionScale"`
2022-01-26 17:40:54 +00:00
2022-01-26 17:10:39 +00:00
// Long means your position will be long position
// Currently not used yet
Long *bool `json:"long,omitempty"`
// Short means your position will be long position
// Currently not used yet
Short *bool `json:"short,omitempty"`
2022-01-08 18:24:10 +00:00
// DisableShort means you can don't want short position during the market making
2022-01-09 14:43:49 +00:00
// Set to true if you want to hold more spot during market making.
2022-01-08 18:24:10 +00:00
DisableShort bool `json:"disableShort"`
// BuyBelowNeutralSMA if true, the market maker will only place buy order when the current price is below the neutral band SMA.
BuyBelowNeutralSMA bool `json:"buyBelowNeutralSMA"`
2023-12-19 14:04:24 +00:00
// EMACrossSetting is used for defining ema cross signal to turn on/off buy
EMACrossSetting *EMACrossSetting `json:"emaCross"`
2022-01-09 14:32:23 +00:00
// NeutralBollinger is the smaller range of the bollinger band
// If price is in this band, it usually means the price is oscillating.
2022-01-26 17:10:39 +00:00
// If price goes out of this band, we tend to not place sell orders or buy orders
NeutralBollinger *BollingerSetting `json:"neutralBollinger"`
2022-01-26 17:10:39 +00:00
// DefaultBollinger is the wide range of the bollinger band
// for controlling your exposure position
DefaultBollinger *BollingerSetting `json:"defaultBollinger"`
2022-01-09 14:32:23 +00:00
// DowntrendSkew is the order quantity skew for normal downtrend band.
// The price is still in the default bollinger band.
// greater than 1.0 means when placing buy order, place sell order with less quantity
// less than 1.0 means when placing sell order, place buy order with less quantity
DowntrendSkew fixedpoint.Value `json:"downtrendSkew"`
// UptrendSkew is the order quantity skew for normal uptrend band.
// The price is still in the default bollinger band.
// greater than 1.0 means when placing buy order, place sell order with less quantity
// less than 1.0 means when placing sell order, place buy order with less quantity
2022-01-14 18:52:46 +00:00
UptrendSkew fixedpoint.Value `json:"uptrendSkew"`
2022-01-09 14:32:23 +00:00
2022-01-27 17:29:12 +00:00
// TradeInBand
// When this is on, places orders only when the current price is in the bollinger band.
TradeInBand bool `json:"tradeInBand"`
2022-01-15 20:06:19 +00:00
// ShadowProtection is used to avoid placing bid order when price goes down strongly (without shadow)
ShadowProtection bool `json:"shadowProtection"`
2022-01-15 20:06:19 +00:00
ShadowProtectionRatio fixedpoint.Value `json:"shadowProtectionRatio"`
ExitMethods bbgo.ExitMethodSet `json:"exits"`
2021-10-07 08:39:20 +00:00
// persistence fields
Position *types.Position `json:"position,omitempty" persistence:"position"`
ProfitStats *types.ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"`
2023-12-19 14:04:24 +00:00
session *bbgo.ExchangeSession
book *types.StreamOrderBook
orderExecutor *bbgo.GeneralOrderExecutor
2021-10-07 08:39:20 +00:00
// defaultBoll is the BOLLINGER indicator we used for predicting the price.
2023-07-10 08:54:22 +00:00
defaultBoll *indicatorv2.BOLLStream
// neutralBoll is the neutral price section
2023-07-10 08:54:22 +00:00
neutralBoll *indicatorv2.BOLLStream
shouldBuy bool
// StrategyController
bbgo.StrategyController
2021-10-07 08:39:20 +00:00
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) InstanceID() string {
return fmt.Sprintf("%s:%s", ID, s.Symbol)
}
2021-10-07 08:39:20 +00:00
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
2022-05-19 01:48:36 +00:00
Interval: s.Interval,
2021-10-07 08:39:20 +00:00
})
if s.DefaultBollinger != nil && s.DefaultBollinger.Interval != "" {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
2022-05-19 01:48:36 +00:00
Interval: s.DefaultBollinger.Interval,
})
}
if s.NeutralBollinger != nil && s.NeutralBollinger.Interval != "" {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
2022-05-19 01:48:36 +00:00
Interval: s.NeutralBollinger.Interval,
})
}
2022-09-11 09:28:54 +00:00
if s.TrendEMA != nil {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.TrendEMA.Interval})
}
2023-12-20 04:09:19 +00:00
if s.EMACrossSetting != nil && s.EMACrossSetting.Enabled {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.EMACrossSetting.Interval})
}
s.ExitMethods.SetAndSubscribe(session, s)
2021-10-07 08:39:20 +00:00
}
func (s *Strategy) Validate() error {
if len(s.Symbol) == 0 {
return errors.New("symbol is required")
}
return nil
}
2022-01-14 18:52:46 +00:00
func (s *Strategy) CurrentPosition() *types.Position {
return s.Position
2022-01-14 18:52:46 +00:00
}
2022-02-07 03:40:30 +00:00
func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error {
return s.orderExecutor.ClosePosition(ctx, percentage)
2022-01-14 18:52:46 +00:00
}
func (s *Strategy) getCurrentAllowedExposurePosition(bandPercentage float64) (fixedpoint.Value, error) {
if s.DynamicExposurePositionScale != nil {
v, err := s.DynamicExposurePositionScale.Scale(bandPercentage)
if err != nil {
2022-02-07 03:40:30 +00:00
return fixedpoint.Zero, err
}
return fixedpoint.NewFromFloat(v), nil
}
return s.MaxExposurePosition, nil
}
func (s *Strategy) placeOrders(ctx context.Context, midPrice fixedpoint.Value, kline *types.KLine) {
bidSpread := s.Spread
2022-02-07 03:40:30 +00:00
if s.BidSpread.Sign() > 0 {
bidSpread = s.BidSpread
}
askSpread := s.Spread
2022-02-07 03:40:30 +00:00
if s.AskSpread.Sign() > 0 {
askSpread = s.AskSpread
}
2022-02-07 03:40:30 +00:00
askPrice := midPrice.Mul(fixedpoint.One.Add(askSpread))
bidPrice := midPrice.Mul(fixedpoint.One.Sub(bidSpread))
base := s.Position.GetBase()
balances := s.session.GetAccount().Balances()
2021-10-07 08:39:20 +00:00
2022-02-07 03:40:30 +00:00
log.Infof("mid price:%v spread: %s ask:%v bid: %v position: %s",
midPrice,
s.Spread.Percentage(),
2022-02-07 03:40:30 +00:00
askPrice,
bidPrice,
s.Position,
)
2023-12-19 13:55:38 +00:00
sellQuantity := s.AskQuantity
if sellQuantity.IsZero() {
sellQuantity = s.QuantityOrAmount.CalculateQuantity(askPrice)
}
buyQuantity := s.BidQuantity
if buyQuantity.IsZero() {
buyQuantity = s.QuantityOrAmount.CalculateQuantity(bidPrice)
}
2022-01-26 17:10:39 +00:00
2021-10-07 08:39:20 +00:00
sellOrder := types.SubmitOrder{
Symbol: s.Symbol,
Side: types.SideTypeSell,
Type: types.OrderTypeLimitMaker,
Quantity: sellQuantity,
Price: askPrice,
Market: s.Market,
2021-10-07 08:39:20 +00:00
}
2021-10-07 08:39:20 +00:00
buyOrder := types.SubmitOrder{
Symbol: s.Symbol,
Side: types.SideTypeBuy,
Type: types.OrderTypeLimitMaker,
Quantity: buyQuantity,
Price: bidPrice,
Market: s.Market,
2021-10-07 08:39:20 +00:00
}
var submitOrders []types.SubmitOrder
baseBalance, hasBaseBalance := balances[s.Market.BaseCurrency]
quoteBalance, hasQuoteBalance := balances[s.Market.QuoteCurrency]
downBand := s.defaultBoll.DownBand.Last(0)
upBand := s.defaultBoll.UpBand.Last(0)
sma := s.defaultBoll.SMA.Last(0)
2022-06-02 09:55:14 +00:00
log.Infof("%s bollinger band: up %f sma %f down %f", s.Symbol, upBand, sma, downBand)
bandPercentage := calculateBandPercentage(upBand, downBand, sma, midPrice.Float64())
2022-06-02 09:55:14 +00:00
log.Infof("%s mid price band percentage: %v", s.Symbol, bandPercentage)
2022-01-26 18:25:23 +00:00
maxExposurePosition, err := s.getCurrentAllowedExposurePosition(bandPercentage)
if err != nil {
2022-06-02 09:55:14 +00:00
log.WithError(err).Errorf("can not calculate %s CurrentAllowedExposurePosition", s.Symbol)
return
}
2022-06-02 09:55:14 +00:00
log.Infof("calculated %s max exposure position: %v", s.Symbol, maxExposurePosition)
2022-01-26 18:25:23 +00:00
if !s.Position.IsClosed() && !s.Position.IsDust(midPrice) {
log.Infof("current %s unrealized profit: %f %s", s.Symbol, s.Position.UnrealizedProfit(midPrice).Float64(), s.Market.QuoteCurrency)
}
2022-09-11 09:28:54 +00:00
// by default, we turn both sell and buy on,
// which means we will place buy and sell orders
canSell := true
canBuy := true
2022-02-07 03:40:30 +00:00
if maxExposurePosition.Sign() > 0 && base.Compare(maxExposurePosition) > 0 {
canBuy = false
}
2022-02-07 03:40:30 +00:00
if maxExposurePosition.Sign() > 0 {
2022-06-02 09:55:14 +00:00
if s.hasLongSet() && base.Sign() < 0 {
canSell = false
2022-02-07 03:40:30 +00:00
} else if base.Compare(maxExposurePosition.Neg()) < 0 {
canSell = false
}
}
if s.ShadowProtection && kline != nil {
switch kline.Direction() {
case types.DirectionDown:
shadowHeight := kline.GetLowerShadowHeight()
shadowRatio := kline.GetLowerShadowRatio()
2022-02-07 03:40:30 +00:00
if shadowHeight.IsZero() && shadowRatio.Compare(s.ShadowProtectionRatio) < 0 {
log.Infof("%s shadow protection enabled, lower shadow ratio %v < %v", s.Symbol, shadowRatio, s.ShadowProtectionRatio)
canBuy = false
}
case types.DirectionUp:
shadowHeight := kline.GetUpperShadowHeight()
shadowRatio := kline.GetUpperShadowRatio()
2022-02-07 03:40:30 +00:00
if shadowHeight.IsZero() || shadowRatio.Compare(s.ShadowProtectionRatio) < 0 {
log.Infof("%s shadow protection enabled, upper shadow ratio %v < %v", s.Symbol, shadowRatio, s.ShadowProtectionRatio)
canSell = false
}
}
}
2022-01-27 10:51:51 +00:00
// Apply quantity skew
// CASE #1:
// WHEN: price is in the neutral bollinger band (window 1) == neutral
2022-01-27 10:51:51 +00:00
// THEN: we don't apply skew
// CASE #2:
// WHEN: price is in the upper band (window 2 > price > window 1) == upTrend
// THEN: we apply upTrend skew
// CASE #3:
// WHEN: price is in the lower band (window 2 < price < window 1) == downTrend
// THEN: we apply downTrend skew
// CASE #4:
// WHEN: price breaks the lower band (price < window 2) == strongDownTrend
// THEN: we apply strongDownTrend skew
// CASE #5:
// WHEN: price breaks the upper band (price > window 2) == strongUpTrend
// THEN: we apply strongUpTrend skew
2022-01-27 17:29:12 +00:00
if s.TradeInBand {
if !inBetween(midPrice.Float64(), s.neutralBoll.DownBand.Last(0), s.neutralBoll.UpBand.Last(0)) {
2022-01-27 17:29:12 +00:00
log.Infof("tradeInBand is set, skip placing orders when the price is outside of the band")
return
}
}
trend := detectPriceTrend(s.neutralBoll, midPrice.Float64())
2022-01-27 10:51:51 +00:00
switch trend {
case NeutralTrend:
// do nothing
case UpTrend:
2022-02-07 03:40:30 +00:00
skew := s.UptrendSkew
buyOrder.Quantity = fixedpoint.Max(s.Market.MinQuantity, sellOrder.Quantity.Mul(skew))
2022-01-27 10:51:51 +00:00
case DownTrend:
2022-02-07 03:40:30 +00:00
skew := s.DowntrendSkew
ratio := fixedpoint.One.Div(skew)
sellOrder.Quantity = fixedpoint.Max(s.Market.MinQuantity, buyOrder.Quantity.Mul(ratio))
2022-01-27 10:51:51 +00:00
2022-01-09 14:32:23 +00:00
}
2022-09-11 09:28:54 +00:00
// check balance and switch the orders
2022-02-07 03:40:30 +00:00
if !hasQuoteBalance || buyOrder.Quantity.Mul(buyOrder.Price).Compare(quoteBalance.Available) > 0 {
canBuy = false
}
2022-02-07 03:40:30 +00:00
if !hasBaseBalance || sellOrder.Quantity.Compare(baseBalance.Available) > 0 {
canSell = false
}
isLongPosition := s.Position.IsLong()
isShortPosition := s.Position.IsShort()
2022-06-02 09:55:14 +00:00
if s.MinProfitActivationRate == nil || (s.MinProfitActivationRate != nil && s.Position.ROI(midPrice).Compare(*s.MinProfitActivationRate) >= 0) {
minProfitPrice := s.Position.AverageCost.Mul(fixedpoint.One.Add(s.MinProfitSpread))
if isShortPosition {
minProfitPrice = s.Position.AverageCost.Mul(fixedpoint.One.Sub(s.MinProfitSpread))
2022-06-02 09:55:14 +00:00
}
if isLongPosition {
// for long position if the current price is lower than the minimal profitable price then we should stop sell
// this avoids loss trade
if midPrice.Compare(minProfitPrice) < 0 {
canSell = false
}
} else if isShortPosition {
// for short position if the current price is higher than the minimal profitable price then we should stop buy
// this avoids loss trade
if midPrice.Compare(minProfitPrice) > 0 {
canBuy = false
}
2022-06-02 09:55:14 +00:00
}
}
2022-06-02 09:55:14 +00:00
if s.hasLongSet() && base.Sub(sellOrder.Quantity).Sign() < 0 {
canSell = false
}
if s.BuyBelowNeutralSMA && midPrice.Float64() > s.neutralBoll.SMA.Last(0) {
canBuy = false
}
2022-09-11 09:28:54 +00:00
// trend EMA protection
if s.TrendEMA != nil {
if !s.TrendEMA.GradientAllowed() {
log.Infof("trendEMA protection: midPrice price %f, gradient %f, turning buy order off", midPrice.Float64(), s.TrendEMA.Gradient())
canBuy = false
}
}
if !s.shouldBuy {
log.Infof("shouldBuy is turned off, skip placing buy order")
canBuy = false
}
if canSell {
submitOrders = append(submitOrders, sellOrder)
2022-01-09 14:32:23 +00:00
}
if canBuy {
submitOrders = append(submitOrders, buyOrder)
2022-01-09 14:32:23 +00:00
}
// condition for lowering the average cost
/*
if midPrice < s.Position.AverageCost.MulFloat64(1.0-s.MinProfitSpread.Float64()) && canBuy {
2022-01-15 20:06:19 +00:00
submitOrders = append(submitOrders, buyOrder)
}
*/
2021-10-07 08:39:20 +00:00
if len(submitOrders) == 0 {
return
}
2022-01-15 20:06:19 +00:00
for i := range submitOrders {
2022-06-02 18:15:23 +00:00
submitOrders[i] = adjustOrderQuantity(submitOrders[i], s.Market)
2022-01-15 20:06:19 +00:00
}
2023-03-15 05:26:27 +00:00
_, err = s.orderExecutor.SubmitOrders(ctx, submitOrders...)
if err != nil {
log.WithError(err).Errorf("submit order error")
}
2021-10-07 08:39:20 +00:00
}
2022-06-02 09:55:14 +00:00
func (s *Strategy) hasLongSet() bool {
return s.Long != nil && *s.Long
}
func (s *Strategy) hasShortSet() bool {
return s.Short != nil && *s.Short
}
2021-10-07 08:39:20 +00:00
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
2022-08-25 15:30:07 +00:00
// initial required information
s.session = session
// StrategyController
s.Status = types.StrategyStatusRunning
s.shouldBuy = true
s.neutralBoll = session.Indicators(s.Symbol).BOLL(s.NeutralBollinger.IntervalWindow, s.NeutralBollinger.BandWidth)
s.defaultBoll = session.Indicators(s.Symbol).BOLL(s.DefaultBollinger.IntervalWindow, s.DefaultBollinger.BandWidth)
if s.EMACrossSetting != nil && s.EMACrossSetting.Enabled {
s.EMACrossSetting.fastEMA = session.Indicators(s.Symbol).EWMA(types.IntervalWindow{Interval: s.Interval, Window: s.EMACrossSetting.FastWindow})
s.EMACrossSetting.slowEMA = session.Indicators(s.Symbol).EWMA(types.IntervalWindow{Interval: s.Interval, Window: s.EMACrossSetting.SlowWindow})
s.EMACrossSetting.cross = indicatorv2.Cross(s.EMACrossSetting.fastEMA, s.EMACrossSetting.slowEMA)
s.EMACrossSetting.cross.OnUpdate(func(v float64) {
switch indicatorv2.CrossType(v) {
case indicatorv2.CrossOver:
s.shouldBuy = true
case indicatorv2.CrossUnder:
s.shouldBuy = false
// TODO: can partially close position when necessary
// s.orderExecutor.ClosePosition(ctx)
}
})
}
2022-05-13 09:58:46 +00:00
// Setup dynamic spread
if s.DynamicSpread.IsEnabled() {
if s.DynamicSpread.Interval == "" {
s.DynamicSpread.Interval = s.Interval
}
s.DynamicSpread.Initialize(s.Symbol, s.session, s.neutralBoll, s.defaultBoll)
2022-05-13 09:58:46 +00:00
}
if s.DisableShort {
s.Long = &[]bool{true}[0]
}
2022-02-07 03:40:30 +00:00
if s.MinProfitSpread.IsZero() {
s.MinProfitSpread = fixedpoint.NewFromFloat(0.001)
}
2022-02-07 03:40:30 +00:00
if s.UptrendSkew.IsZero() {
s.UptrendSkew = fixedpoint.NewFromFloat(1.0 / 1.2)
2022-01-09 14:32:23 +00:00
}
2022-02-07 03:40:30 +00:00
if s.DowntrendSkew.IsZero() {
s.DowntrendSkew = fixedpoint.NewFromFloat(1.2)
2022-01-09 14:32:23 +00:00
}
2022-02-07 03:40:30 +00:00
if s.ShadowProtectionRatio.IsZero() {
s.ShadowProtectionRatio = fixedpoint.NewFromFloat(0.01)
2022-01-15 20:06:19 +00:00
}
2021-10-07 08:39:20 +00:00
// calculate group id for orders
instanceID := s.InstanceID()
2021-10-07 08:39:20 +00:00
// If position is nil, we need to allocate a new position for calculation
if s.Position == nil {
2022-07-01 09:28:48 +00:00
s.Position = types.NewPositionFromMarket(s.Market)
}
2022-05-13 14:20:23 +00:00
if s.session.MakerFeeRate.Sign() > 0 || s.session.TakerFeeRate.Sign() > 0 {
s.Position.SetExchangeFeeRate(s.session.ExchangeName, types.ExchangeFee{
MakerFeeRate: s.session.MakerFeeRate,
TakerFeeRate: s.session.TakerFeeRate,
})
}
if s.ProfitStats == nil {
2022-07-01 09:28:48 +00:00
s.ProfitStats = types.NewProfitStats(s.Market)
}
// Always update the position fields
s.Position.Strategy = ID
s.Position.StrategyInstanceID = instanceID
2022-03-11 13:15:57 +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.Bind()
s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
2022-10-03 10:45:24 +00:00
bbgo.Sync(ctx, s)
2021-10-07 08:39:20 +00:00
})
s.ExitMethods.Bind(session, s.orderExecutor)
2022-01-29 09:44:42 +00:00
2022-09-11 09:28:54 +00:00
if s.TrendEMA != nil {
s.TrendEMA.Bind(session, s.orderExecutor)
}
2022-06-22 08:18:50 +00:00
if bbgo.IsBackTesting {
log.Warn("turning of useTickerPrice option in the back-testing environment...")
s.UseTickerPrice = false
}
s.OnSuspend(func() {
_ = s.orderExecutor.GracefulCancel(ctx)
2022-10-03 10:45:24 +00:00
bbgo.Sync(ctx, s)
})
s.OnEmergencyStop(func() {
// Close 100% position
percentage := fixedpoint.NewFromFloat(1.0)
_ = s.ClosePosition(ctx, percentage)
})
2021-10-07 08:39:20 +00:00
session.UserDataStream.OnStart(func() {
if !bbgo.IsBackTesting && s.UseTickerPrice {
ticker, err := s.session.Exchange.QueryTicker(ctx, s.Symbol)
if err != nil {
return
}
2022-02-07 03:40:30 +00:00
midPrice := ticker.Buy.Add(ticker.Sell).Div(two)
s.placeOrders(ctx, midPrice, nil)
} else {
if price, ok := session.LastPrice(s.Symbol); ok {
s.placeOrders(ctx, price, nil)
}
}
2021-10-07 08:39:20 +00:00
})
2022-07-26 03:40:03 +00:00
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.Interval, func(kline types.KLine) {
// StrategyController
if s.Status != types.StrategyStatusRunning {
return
}
// Update spreads with dynamic spread
if s.DynamicSpread.IsEnabled() {
s.DynamicSpread.Update(kline)
dynamicBidSpread, err := s.DynamicSpread.GetBidSpread()
if err == nil && dynamicBidSpread > 0 {
s.BidSpread = fixedpoint.NewFromFloat(dynamicBidSpread)
2022-07-26 03:39:12 +00:00
log.Infof("%s dynamic bid spread updated: %s", s.Symbol, s.BidSpread.Percentage())
}
dynamicAskSpread, err := s.DynamicSpread.GetAskSpread()
if err == nil && dynamicAskSpread > 0 {
s.AskSpread = fixedpoint.NewFromFloat(dynamicAskSpread)
2022-07-26 03:39:12 +00:00
log.Infof("%s dynamic ask spread updated: %s", s.Symbol, s.AskSpread.Percentage())
}
2022-05-13 09:58:46 +00:00
}
_ = s.orderExecutor.GracefulCancel(ctx)
if s.UseTickerPrice {
ticker, err := s.session.Exchange.QueryTicker(ctx, s.Symbol)
if err != nil {
return
}
2022-02-07 03:40:30 +00:00
midPrice := ticker.Buy.Add(ticker.Sell).Div(two)
log.Infof("using ticker price: bid %v / ask %v, mid price %v", ticker.Buy, ticker.Sell, midPrice)
s.placeOrders(ctx, midPrice, &kline)
} else {
s.placeOrders(ctx, kline.Close, &kline)
}
2022-07-26 03:40:03 +00:00
}))
2021-10-07 08:39:20 +00:00
// s.book = types.NewStreamBook(s.Symbol)
2021-10-08 05:23:38 +00:00
// s.book.BindStreamForBackground(session.MarketDataStream)
2021-10-07 08:39:20 +00:00
2022-10-03 08:01:08 +00:00
bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) {
2021-10-07 08:39:20 +00:00
defer wg.Done()
_ = s.orderExecutor.GracefulCancel(ctx)
2021-10-07 08:39:20 +00:00
})
return nil
}
func calculateBandPercentage(up, down, sma, midPrice float64) float64 {
if midPrice < sma {
// should be negative percentage
return (midPrice - sma) / math.Abs(sma-down)
} else if midPrice > sma {
// should be positive percentage
return (midPrice - sma) / math.Abs(up-sma)
}
return 0.0
}
2022-01-27 10:51:51 +00:00
func inBetween(x, a, b float64) bool {
return a < x && x < b
}
2022-06-02 18:15:23 +00:00
func adjustOrderQuantity(submitOrder types.SubmitOrder, market types.Market) types.SubmitOrder {
if submitOrder.Quantity.Mul(submitOrder.Price).Compare(market.MinNotional) < 0 {
submitOrder.Quantity = bbgo.AdjustFloatQuantityByMinAmount(submitOrder.Quantity, submitOrder.Price, market.MinNotional.Mul(notionModifier))
}
if submitOrder.Quantity.Compare(market.MinQuantity) < 0 {
submitOrder.Quantity = fixedpoint.Max(submitOrder.Quantity, market.MinQuantity)
}
return submitOrder
}