bbgo_origin/pkg/strategy/pivotshort/strategy.go

310 lines
9.4 KiB
Go
Raw Normal View History

package pivotshort
import (
"context"
"fmt"
2022-05-19 01:48:36 +00:00
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
2022-05-13 10:05:25 +00:00
"github.com/c9s/bbgo/pkg/indicator"
"github.com/c9s/bbgo/pkg/types"
"github.com/sirupsen/logrus"
)
const ID = "pivotshort"
var log = logrus.WithField("strategy", ID)
func init() {
bbgo.RegisterStrategy(ID, &Strategy{})
}
type IntervalWindowSetting struct {
types.IntervalWindow
}
2022-06-03 08:38:06 +00:00
type Entry struct {
2022-06-03 15:23:26 +00:00
Immediate bool `json:"immediate"`
2022-06-03 08:38:06 +00:00
CatBounceRatio fixedpoint.Value `json:"catBounceRatio"`
Quantity fixedpoint.Value `json:"quantity"`
NumLayers fixedpoint.Value `json:"numLayers"`
MarginSideEffect types.MarginOrderSideEffectType `json:"marginOrderSideEffect"`
}
type Exit struct {
TakeProfitPercentage fixedpoint.Value `json:"takeProfitPercentage"`
StopLossPercentage fixedpoint.Value `json:"stopLossPercentage"`
2022-06-03 15:28:48 +00:00
ShadowTPRatio fixedpoint.Value `json:"shadowTakeProfitRatio"`
2022-06-03 08:38:06 +00:00
MarginSideEffect types.MarginOrderSideEffectType `json:"marginOrderSideEffect"`
}
type Strategy struct {
2022-05-12 11:27:57 +00:00
*bbgo.Graceful
*bbgo.Notifiability
*bbgo.Persistence
Environment *bbgo.Environment
Symbol string `json:"symbol"`
Market types.Market
Interval types.Interval `json:"interval"`
TotalQuantity fixedpoint.Value `json:"totalQuantity"`
2022-05-12 11:27:57 +00:00
// persistence fields
Position *types.Position `json:"position,omitempty" persistence:"position"`
ProfitStats *types.ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"`
2022-06-03 08:38:06 +00:00
PivotLength int `json:"pivotLength"`
Entry Entry
Exit Exit
activeMakerOrders *bbgo.LocalActiveOrderBook
orderStore *bbgo.OrderStore
tradeCollector *bbgo.TradeCollector
session *bbgo.ExchangeSession
2022-06-03 08:38:06 +00:00
pivot *indicator.Pivot
pivotBuffer []fixedpoint.Value
2022-05-12 11:27:57 +00:00
// StrategyController
bbgo.StrategyController
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
log.Infof("subscribe %s", s.Symbol)
2022-05-19 01:48:36 +00:00
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
//session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: types.Interval1d})
}
2022-06-03 15:23:26 +00:00
func (s *Strategy) placeOrder(ctx context.Context, marketPrice fixedpoint.Value, limitPrice fixedpoint.Value, currentPrice fixedpoint.Value, qty fixedpoint.Value, orderExecutor bbgo.OrderExecutor) {
submitOrder := types.SubmitOrder{
Symbol: s.Symbol,
Side: types.SideTypeSell,
Type: types.OrderTypeLimit,
2022-06-03 15:23:26 +00:00
Price: limitPrice,
Quantity: qty,
}
2022-06-03 15:23:26 +00:00
if s.Entry.Immediate && marketPrice.Compare(currentPrice) <= 0 {
submitOrder.Type = types.OrderTypeMarket
}
if s.session.Margin {
2022-06-03 08:38:06 +00:00
submitOrder.MarginSideEffect = s.Entry.MarginSideEffect
}
createdOrders, err := orderExecutor.SubmitOrders(ctx, submitOrder)
if err != nil {
log.WithError(err).Errorf("can not place orders")
}
s.orderStore.Add(createdOrders...)
s.activeMakerOrders.Add(createdOrders...)
//s.tradeCollector.Process()
}
func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error {
base := s.Position.GetBase()
if base.IsZero() {
return fmt.Errorf("no opened %s position", s.Position.Symbol)
}
// 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,
2022-05-12 11:27:57 +00:00
Quantity: quantity,
Market: s.Market,
}
if s.session.Margin {
2022-06-03 08:38:06 +00:00
submitOrder.MarginSideEffect = s.Exit.MarginSideEffect
}
//s.Notify("Submitting %s %s order to close position by %v", s.Symbol, side.String(), percentage, submitOrder)
createdOrders, err := s.session.Exchange.SubmitOrders(ctx, submitOrder)
if err != nil {
log.WithError(err).Errorf("can not place position close order")
}
s.orderStore.Add(createdOrders...)
s.activeMakerOrders.Add(createdOrders...)
return err
}
2022-05-12 11:27:57 +00:00
func (s *Strategy) InstanceID() string {
return fmt.Sprintf("%s:%s", ID, s.Symbol)
}
2022-06-03 08:38:06 +00:00
// check if position can be close or not
func canClosePosition(position *types.Position, signal fixedpoint.Value, price fixedpoint.Value) bool {
return !signal.IsZero() && position.IsShort() && !position.IsDust(price)
}
// get last available pivot low, the most recent pivot point higher than current price
func (s *Strategy) getValidPivotLow(price fixedpoint.Value) fixedpoint.Value {
for l := len(s.pivotBuffer) - 1; l > 0; l-- {
if s.pivotBuffer[l].Compare(price) > 0 {
return s.pivotBuffer[l]
}
}
return price
}
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
// initial required information
s.session = session
s.activeMakerOrders = bbgo.NewLocalActiveOrderBook(s.Symbol)
s.activeMakerOrders.BindStream(session.UserDataStream)
s.orderStore = bbgo.NewOrderStore(s.Symbol)
s.orderStore.BindStream(session.UserDataStream)
if s.Position == nil {
s.Position = types.NewPositionFromMarket(s.Market)
}
2022-05-12 11:27:57 +00:00
instanceID := s.InstanceID()
// Always update the position fields
s.Position.Strategy = ID
s.Position.StrategyInstanceID = instanceID
s.tradeCollector = bbgo.NewTradeCollector(s.Symbol, s.Position, s.orderStore)
2022-05-12 11:27:57 +00:00
s.tradeCollector.OnTrade(func(trade types.Trade, profit, netProfit fixedpoint.Value) {
// StrategyController
if s.Status != types.StrategyStatusRunning {
return
}
s.Notifiability.Notify(trade)
s.ProfitStats.AddTrade(trade)
if profit.Compare(fixedpoint.Zero) == 0 {
s.Environment.RecordPosition(s.Position, trade, nil)
} else {
log.Infof("%s generated profit: %v", s.Symbol, profit)
p := s.Position.NewProfit(trade, profit, netProfit)
p.Strategy = ID
p.StrategyInstanceID = instanceID
s.Notify(&p)
s.ProfitStats.AddProfit(p)
s.Notify(&s.ProfitStats)
s.Environment.RecordPosition(s.Position, trade, &p)
}
})
s.tradeCollector.OnPositionUpdate(func(position *types.Position) {
log.Infof("position changed: %s", s.Position)
s.Notify(s.Position)
})
s.tradeCollector.BindStream(session.UserDataStream)
iw := types.IntervalWindow{Window: s.PivotLength, Interval: s.Interval}
st, _ := session.MarketDataStore(s.Symbol)
2022-05-13 10:05:25 +00:00
s.pivot = &indicator.Pivot{IntervalWindow: iw}
s.pivot.Bind(st)
session.UserDataStream.OnStart(func() {
log.Infof("connected")
})
var lastLow fixedpoint.Value
2022-05-13 10:05:25 +00:00
futuresMode := s.session.Futures || s.session.IsolatedFutures
2022-06-03 08:38:06 +00:00
d := s.Entry.CatBounceRatio.Div(s.Entry.NumLayers)
q := s.Entry.Quantity
if !s.TotalQuantity.IsZero() {
2022-06-03 08:38:06 +00:00
q = s.TotalQuantity.Div(s.Entry.NumLayers)
}
session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {
if kline.Symbol != s.Symbol || kline.Interval != s.Interval {
return
}
if s.pivot.LastLow() > 0. {
2022-06-03 08:38:06 +00:00
log.Infof("pivot low signal detected: %f %s", s.pivot.LastLow(), kline.EndTime.Time())
lastLow = fixedpoint.NewFromFloat(s.pivot.LastLow())
} else {
2022-06-03 08:38:06 +00:00
if canClosePosition(s.Position, lastLow, kline.Close) {
R := kline.Close.Div(s.Position.AverageCost)
2022-06-03 08:38:06 +00:00
if R.Compare(fixedpoint.One.Add(s.Exit.StopLossPercentage)) > 0 {
// SL
2022-06-03 08:38:06 +00:00
log.Infof("%s SL triggered", s.Symbol)
s.ClosePosition(ctx, fixedpoint.One)
s.tradeCollector.Process()
2022-06-03 08:38:06 +00:00
} else if R.Compare(fixedpoint.One.Sub(s.Exit.TakeProfitPercentage)) < 0 {
// TP
2022-06-03 08:38:06 +00:00
log.Infof("%s TP triggered", s.Symbol)
s.ClosePosition(ctx, fixedpoint.One)
2022-06-03 08:38:06 +00:00
} else if kline.GetLowerShadowHeight().Div(kline.Close).Compare(s.Exit.ShadowTPRatio) > 0 {
// shadow TP
2022-06-03 08:38:06 +00:00
log.Infof("%s shadow TP triggered", s.Symbol)
s.ClosePosition(ctx, fixedpoint.One)
s.tradeCollector.Process()
}
}
lastLow = fixedpoint.Zero
}
if !lastLow.IsZero() {
2022-06-03 08:38:06 +00:00
s.pivotBuffer = append(s.pivotBuffer, lastLow)
if err := s.activeMakerOrders.GracefulCancel(ctx, s.session.Exchange); err != nil {
log.WithError(err).Errorf("graceful cancel order error")
}
2022-06-03 08:38:06 +00:00
limitPrice := s.getValidPivotLow(kline.Close)
log.Infof("place limit sell start from %f adds up to %f percent with %f layers of orders", limitPrice.Float64(), s.Entry.CatBounceRatio.Mul(fixedpoint.NewFromInt(100)).Float64(), s.Entry.NumLayers.Float64())
2022-06-03 08:38:06 +00:00
for i := 0; i < int(s.Entry.NumLayers.Float64()); i++ {
2022-05-12 11:27:57 +00:00
balances := s.session.GetAccount().Balances()
quoteBalance, _ := balances[s.Market.QuoteCurrency]
baseBalance, _ := balances[s.Market.BaseCurrency]
2022-06-03 08:38:06 +00:00
p := limitPrice.Mul(fixedpoint.One.Add(s.Entry.CatBounceRatio.Sub(fixedpoint.NewFromFloat(d.Float64() * float64(i)))))
2022-05-13 10:05:25 +00:00
if futuresMode {
2022-06-03 08:38:06 +00:00
//log.Infof("futures mode on")
if q.Mul(p).Compare(quoteBalance.Available) <= 0 {
2022-06-03 15:23:26 +00:00
s.placeOrder(ctx, lastLow, p, kline.Close, q, orderExecutor)
2022-05-13 10:05:25 +00:00
s.tradeCollector.Process()
}
} else if s.Environment.IsBackTesting() {
2022-06-03 08:38:06 +00:00
//log.Infof("spot backtest mode on")
if q.Compare(baseBalance.Available) <= 0 {
2022-06-03 15:23:26 +00:00
s.placeOrder(ctx, lastLow, p, kline.Close, q, orderExecutor)
2022-05-13 10:05:25 +00:00
s.tradeCollector.Process()
}
} else {
2022-06-03 08:38:06 +00:00
//log.Infof("spot mode on")
if q.Compare(baseBalance.Available) <= 0 {
2022-06-03 15:23:26 +00:00
s.placeOrder(ctx, lastLow, p, kline.Close, q, orderExecutor)
2022-05-13 10:05:25 +00:00
s.tradeCollector.Process()
}
2022-05-12 11:27:57 +00:00
}
2022-05-13 10:05:25 +00:00
}
//s.placeOrder(ctx, lastLow.Mul(fixedpoint.One.Add(s.CatBounceRatio)), s.Quantity, orderExecutor)
}
})
return nil
}