2022-05-10 09:11:24 +00:00
|
|
|
package pivotshort
|
2022-05-09 21:11:22 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-06-09 18:39:14 +00:00
|
|
|
"os"
|
2022-06-09 16:49:32 +00:00
|
|
|
"sync"
|
2022-06-09 03:30:24 +00:00
|
|
|
|
2022-06-04 17:09:31 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2022-05-09 21:11:22 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
2022-06-30 10:29:02 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/dynamic"
|
2022-05-09 21:11:22 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
2022-05-13 10:05:25 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/indicator"
|
2022-05-09 21:11:22 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2022-05-10 09:11:24 +00:00
|
|
|
const ID = "pivotshort"
|
2022-05-09 21:11:22 +00:00
|
|
|
|
2022-06-26 08:13:58 +00:00
|
|
|
var one = fixedpoint.One
|
|
|
|
|
2022-05-09 21:11:22 +00:00
|
|
|
var log = logrus.WithField("strategy", ID)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
bbgo.RegisterStrategy(ID, &Strategy{})
|
|
|
|
}
|
|
|
|
|
2022-07-02 05:21:27 +00:00
|
|
|
type SupportTakeProfit struct {
|
|
|
|
Symbol string
|
|
|
|
types.IntervalWindow
|
2022-07-03 18:20:15 +00:00
|
|
|
|
2022-07-02 05:21:27 +00:00
|
|
|
Ratio fixedpoint.Value `json:"ratio"`
|
|
|
|
|
2022-07-26 10:59:00 +00:00
|
|
|
pivot *indicator.PivotLow
|
2022-07-03 09:13:01 +00:00
|
|
|
orderExecutor *bbgo.GeneralOrderExecutor
|
|
|
|
session *bbgo.ExchangeSession
|
|
|
|
activeOrders *bbgo.ActiveOrderBook
|
|
|
|
currentSupportPrice fixedpoint.Value
|
2022-07-03 18:20:15 +00:00
|
|
|
|
|
|
|
triggeredPrices []fixedpoint.Value
|
2022-07-02 05:21:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SupportTakeProfit) Subscribe(session *bbgo.ExchangeSession) {
|
|
|
|
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SupportTakeProfit) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.GeneralOrderExecutor) {
|
|
|
|
s.session = session
|
|
|
|
s.orderExecutor = orderExecutor
|
|
|
|
s.activeOrders = bbgo.NewActiveOrderBook(s.Symbol)
|
2022-07-03 18:20:15 +00:00
|
|
|
session.UserDataStream.OnOrderUpdate(func(order types.Order) {
|
|
|
|
if s.activeOrders.Exists(order) {
|
|
|
|
if !s.currentSupportPrice.IsZero() {
|
|
|
|
s.triggeredPrices = append(s.triggeredPrices, s.currentSupportPrice)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
s.activeOrders.BindStream(session.UserDataStream)
|
2022-07-02 05:21:27 +00:00
|
|
|
|
|
|
|
position := orderExecutor.Position()
|
2022-07-26 10:59:00 +00:00
|
|
|
|
|
|
|
s.pivot = session.StandardIndicatorSet(s.Symbol).PivotLow(s.IntervalWindow)
|
2022-07-02 05:21:27 +00:00
|
|
|
|
2022-07-03 18:20:15 +00:00
|
|
|
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.Interval, func(kline types.KLine) {
|
|
|
|
if !s.updateSupportPrice(kline.Close) {
|
2022-07-02 05:21:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-03 18:20:15 +00:00
|
|
|
if !position.IsOpened(kline.Close) {
|
|
|
|
log.Infof("position is not opened, skip updating support take profit order")
|
2022-07-02 05:21:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-03 09:13:01 +00:00
|
|
|
buyPrice := s.currentSupportPrice.Mul(one.Add(s.Ratio))
|
2022-07-02 05:21:27 +00:00
|
|
|
quantity := position.GetQuantity()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
if err := orderExecutor.GracefulCancelActiveOrderBook(ctx, s.activeOrders); err != nil {
|
|
|
|
log.WithError(err).Errorf("cancel order failed")
|
|
|
|
}
|
|
|
|
|
2022-07-03 09:13:01 +00:00
|
|
|
bbgo.Notify("placing %s take profit order at price %f", s.Symbol, buyPrice.Float64())
|
2022-07-02 05:21:27 +00:00
|
|
|
createdOrders, err := orderExecutor.SubmitOrders(ctx, types.SubmitOrder{
|
2022-07-29 07:41:35 +00:00
|
|
|
Symbol: s.Symbol,
|
|
|
|
Type: types.OrderTypeLimitMaker,
|
|
|
|
Side: types.SideTypeBuy,
|
|
|
|
Price: buyPrice,
|
|
|
|
Quantity: quantity,
|
|
|
|
Tag: "supportTakeProfit",
|
|
|
|
MarginSideEffect: types.SideEffectTypeAutoRepay,
|
2022-07-02 05:21:27 +00:00
|
|
|
})
|
2022-07-03 09:22:29 +00:00
|
|
|
|
2022-07-02 05:21:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("can not submit orders: %+v", createdOrders)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.activeOrders.Add(createdOrders...)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2022-07-03 18:20:15 +00:00
|
|
|
func (s *SupportTakeProfit) updateSupportPrice(closePrice fixedpoint.Value) bool {
|
2022-07-27 04:28:53 +00:00
|
|
|
log.Infof("[supportTakeProfit] lows: %v", s.pivot.Values)
|
2022-07-03 18:20:15 +00:00
|
|
|
|
|
|
|
groupDistance := 0.01
|
|
|
|
minDistance := 0.05
|
2022-07-27 04:28:53 +00:00
|
|
|
supportPrices := findPossibleSupportPrices(closePrice.Float64()*(1.0-minDistance), groupDistance, s.pivot.Values)
|
2022-07-03 18:20:15 +00:00
|
|
|
if len(supportPrices) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("[supportTakeProfit] found possible support prices: %v", supportPrices)
|
|
|
|
|
|
|
|
// nextSupportPrice are sorted in increasing order
|
|
|
|
nextSupportPrice := fixedpoint.NewFromFloat(supportPrices[len(supportPrices)-1])
|
|
|
|
|
|
|
|
// it's price that we have been used to take profit
|
|
|
|
for _, p := range s.triggeredPrices {
|
|
|
|
var l = p.Mul(one.Sub(fixedpoint.NewFromFloat(0.01)))
|
|
|
|
var h = p.Mul(one.Add(fixedpoint.NewFromFloat(0.01)))
|
|
|
|
if p.Compare(l) > 0 && p.Compare(h) < 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
currentBuyPrice := s.currentSupportPrice.Mul(one.Add(s.Ratio))
|
|
|
|
|
|
|
|
if s.currentSupportPrice.IsZero() {
|
|
|
|
log.Infof("setup next support take profit price at %f", nextSupportPrice.Float64())
|
|
|
|
s.currentSupportPrice = nextSupportPrice
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// the close price is already lower than the support price, than we should update
|
|
|
|
if closePrice.Compare(currentBuyPrice) < 0 || nextSupportPrice.Compare(s.currentSupportPrice) > 0 {
|
|
|
|
log.Infof("setup next support take profit price at %f", nextSupportPrice.Float64())
|
|
|
|
s.currentSupportPrice = nextSupportPrice
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-05-09 21:11:22 +00:00
|
|
|
type Strategy struct {
|
2022-06-05 23:29:25 +00:00
|
|
|
Environment *bbgo.Environment
|
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
Market types.Market
|
2022-06-10 07:34:57 +00:00
|
|
|
|
|
|
|
// pivot interval and window
|
|
|
|
types.IntervalWindow
|
2022-05-12 11:27:57 +00:00
|
|
|
|
2022-07-14 09:44:25 +00:00
|
|
|
Leverage fixedpoint.Value `json:"leverage"`
|
|
|
|
Quantity fixedpoint.Value `json:"quantity"`
|
|
|
|
|
2022-05-12 11:27:57 +00:00
|
|
|
// persistence fields
|
2022-07-14 09:44:25 +00:00
|
|
|
|
2022-06-18 08:31:53 +00:00
|
|
|
Position *types.Position `persistence:"position"`
|
|
|
|
ProfitStats *types.ProfitStats `persistence:"profit_stats"`
|
|
|
|
TradeStats *types.TradeStats `persistence:"trade_stats"`
|
2022-05-09 21:11:22 +00:00
|
|
|
|
2022-06-30 10:29:02 +00:00
|
|
|
// BreakLow is one of the entry method
|
2022-07-01 09:22:09 +00:00
|
|
|
BreakLow *BreakLow `json:"breakLow"`
|
2022-06-10 16:26:44 +00:00
|
|
|
|
2022-06-30 10:29:02 +00:00
|
|
|
// ResistanceShort is one of the entry method
|
2022-06-29 09:58:43 +00:00
|
|
|
ResistanceShort *ResistanceShort `json:"resistanceShort"`
|
2022-06-10 16:26:44 +00:00
|
|
|
|
2022-07-03 18:20:15 +00:00
|
|
|
SupportTakeProfit []*SupportTakeProfit `json:"supportTakeProfit"`
|
2022-07-02 05:21:27 +00:00
|
|
|
|
2022-06-30 07:13:42 +00:00
|
|
|
ExitMethods bbgo.ExitMethodSet `json:"exits"`
|
2022-05-09 21:11:22 +00:00
|
|
|
|
2022-06-18 03:45:24 +00:00
|
|
|
session *bbgo.ExchangeSession
|
2022-06-18 08:31:53 +00:00
|
|
|
orderExecutor *bbgo.GeneralOrderExecutor
|
2022-05-09 21:11:22 +00:00
|
|
|
|
2022-05-12 11:27:57 +00:00
|
|
|
// StrategyController
|
|
|
|
bbgo.StrategyController
|
2022-05-09 21:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) ID() string {
|
|
|
|
return ID
|
|
|
|
}
|
|
|
|
|
2022-07-19 09:38:32 +00:00
|
|
|
func (s *Strategy) InstanceID() string {
|
|
|
|
return fmt.Sprintf("%s:%s", ID, s.Symbol)
|
|
|
|
}
|
|
|
|
|
2022-05-09 21:11:22 +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})
|
2022-06-05 23:29:25 +00:00
|
|
|
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: types.Interval1m})
|
2022-06-10 16:26:44 +00:00
|
|
|
|
2022-06-29 09:58:43 +00:00
|
|
|
if s.ResistanceShort != nil && s.ResistanceShort.Enabled {
|
2022-06-30 10:29:02 +00:00
|
|
|
dynamic.InheritStructValues(s.ResistanceShort, s)
|
2022-06-29 09:58:43 +00:00
|
|
|
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.ResistanceShort.Interval})
|
2022-06-10 16:26:44 +00:00
|
|
|
}
|
2022-06-26 11:06:16 +00:00
|
|
|
|
2022-07-01 09:22:09 +00:00
|
|
|
if s.BreakLow != nil {
|
|
|
|
dynamic.InheritStructValues(s.BreakLow, s)
|
2022-07-02 05:21:27 +00:00
|
|
|
s.BreakLow.Subscribe(session)
|
|
|
|
}
|
|
|
|
|
2022-07-03 09:13:01 +00:00
|
|
|
for i := range s.SupportTakeProfit {
|
2022-07-03 18:20:15 +00:00
|
|
|
m := s.SupportTakeProfit[i]
|
|
|
|
dynamic.InheritStructValues(m, s)
|
|
|
|
m.Subscribe(session)
|
2022-07-01 09:22:09 +00:00
|
|
|
}
|
|
|
|
|
2022-06-26 11:06:16 +00:00
|
|
|
if !bbgo.IsBackTesting {
|
|
|
|
session.Subscribe(types.MarketTradeChannel, s.Symbol, types.SubscribeOptions{})
|
|
|
|
}
|
2022-06-29 09:58:43 +00:00
|
|
|
|
2022-06-30 07:13:42 +00:00
|
|
|
s.ExitMethods.SetAndSubscribe(session, s)
|
2022-06-05 23:29:25 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 03:39:18 +00:00
|
|
|
func (s *Strategy) CurrentPosition() *types.Position {
|
|
|
|
return s.Position
|
|
|
|
}
|
|
|
|
|
2022-05-09 21:11:22 +00:00
|
|
|
func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error {
|
2022-06-22 05:45:48 +00:00
|
|
|
return s.orderExecutor.ClosePosition(ctx, percentage)
|
2022-05-12 11:27:57 +00:00
|
|
|
}
|
2022-05-09 21:11:22 +00:00
|
|
|
|
|
|
|
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
|
2022-06-18 03:45:24 +00:00
|
|
|
var instanceID = s.InstanceID()
|
|
|
|
|
2022-05-09 21:11:22 +00:00
|
|
|
if s.Position == nil {
|
|
|
|
s.Position = types.NewPositionFromMarket(s.Market)
|
|
|
|
}
|
|
|
|
|
2022-06-04 17:48:56 +00:00
|
|
|
if s.ProfitStats == nil {
|
|
|
|
s.ProfitStats = types.NewProfitStats(s.Market)
|
|
|
|
}
|
|
|
|
|
2022-06-09 16:49:32 +00:00
|
|
|
if s.TradeStats == nil {
|
2022-07-05 03:14:50 +00:00
|
|
|
s.TradeStats = types.NewTradeStats(s.Symbol)
|
2022-06-09 16:49:32 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 09:44:25 +00:00
|
|
|
if s.Leverage.IsZero() {
|
|
|
|
// the default leverage is 3x
|
|
|
|
s.Leverage = fixedpoint.NewFromInt(3)
|
|
|
|
}
|
|
|
|
|
2022-06-20 03:39:18 +00:00
|
|
|
// StrategyController
|
|
|
|
s.Status = types.StrategyStatusRunning
|
|
|
|
|
|
|
|
s.OnSuspend(func() {
|
|
|
|
// Cancel active orders
|
|
|
|
_ = s.orderExecutor.GracefulCancel(ctx)
|
|
|
|
})
|
|
|
|
|
|
|
|
s.OnEmergencyStop(func() {
|
|
|
|
// Cancel active orders
|
|
|
|
_ = s.orderExecutor.GracefulCancel(ctx)
|
|
|
|
// Close 100% position
|
|
|
|
_ = s.ClosePosition(ctx, fixedpoint.One)
|
|
|
|
})
|
|
|
|
|
2022-06-18 07:27:11 +00:00
|
|
|
// initial required information
|
|
|
|
s.session = session
|
2022-06-18 08:31:53 +00:00
|
|
|
s.orderExecutor = bbgo.NewGeneralOrderExecutor(session, s.Symbol, ID, instanceID, s.Position)
|
|
|
|
s.orderExecutor.BindEnvironment(s.Environment)
|
2022-06-19 04:29:36 +00:00
|
|
|
s.orderExecutor.BindProfitStats(s.ProfitStats)
|
2022-06-18 08:32:53 +00:00
|
|
|
s.orderExecutor.BindTradeStats(s.TradeStats)
|
2022-06-21 07:57:26 +00:00
|
|
|
s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
|
|
|
|
bbgo.Sync(s)
|
|
|
|
})
|
2022-06-19 05:01:22 +00:00
|
|
|
s.orderExecutor.Bind()
|
2022-05-09 21:11:22 +00:00
|
|
|
|
2022-07-06 18:23:55 +00:00
|
|
|
s.ExitMethods.Bind(session, s.orderExecutor)
|
2022-06-26 11:06:16 +00:00
|
|
|
|
2022-06-29 09:58:43 +00:00
|
|
|
if s.ResistanceShort != nil && s.ResistanceShort.Enabled {
|
2022-06-30 10:29:02 +00:00
|
|
|
s.ResistanceShort.Bind(session, s.orderExecutor)
|
2022-06-26 11:45:37 +00:00
|
|
|
}
|
2022-05-09 21:11:22 +00:00
|
|
|
|
2022-07-01 09:22:09 +00:00
|
|
|
if s.BreakLow != nil {
|
|
|
|
s.BreakLow.Bind(session, s.orderExecutor)
|
2022-06-27 10:17:57 +00:00
|
|
|
}
|
|
|
|
|
2022-07-03 18:20:15 +00:00
|
|
|
for i := range s.SupportTakeProfit {
|
|
|
|
s.SupportTakeProfit[i].Bind(session, s.orderExecutor)
|
2022-07-03 09:22:29 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 05:48:04 +00:00
|
|
|
bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) {
|
2022-07-01 08:29:03 +00:00
|
|
|
defer wg.Done()
|
2022-07-01 09:22:09 +00:00
|
|
|
|
2022-06-09 18:39:14 +00:00
|
|
|
_, _ = fmt.Fprintln(os.Stderr, s.TradeStats.String())
|
2022-07-01 08:29:03 +00:00
|
|
|
_ = s.orderExecutor.GracefulCancel(ctx)
|
2022-06-09 16:49:32 +00:00
|
|
|
})
|
|
|
|
|
2022-05-09 21:11:22 +00:00
|
|
|
return nil
|
|
|
|
}
|