bbgo_origin/pkg/strategy/ewo_dgtrd/strategy.go

268 lines
7.9 KiB
Go
Raw Normal View History

package ewo_dgtrd
import (
"context"
2022-04-13 01:43:31 +00:00
"sync"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
2022-04-11 08:07:52 +00:00
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/indicator"
"github.com/c9s/bbgo/pkg/types"
)
const ID = "ewo_dgtrd"
var log = logrus.WithField("strategy", ID)
2022-04-11 10:55:34 +00:00
var modifier = fixedpoint.NewFromFloat(0.99)
func init() {
bbgo.RegisterStrategy(ID, &Strategy{})
}
type Strategy struct {
2022-04-13 01:43:31 +00:00
*bbgo.Graceful
bbgo.SmartStops
tradeCollector *bbgo.TradeCollector
Symbol string `json:"symbol"`
Interval types.Interval `json:"interval"`
UseEma bool `json:"useEma"` // use exponential ma or simple ma
SignalWindow int `json:"sigWin"` // signal window
}
func (s *Strategy) ID() string {
return ID
}
2022-04-13 01:43:31 +00:00
func (s *Strategy) Initialize() error {
return s.SmartStops.InitializeStopControllers(s.Symbol)
}
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
log.Infof("subscribe %s", s.Symbol)
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval.String()})
2022-04-13 01:43:31 +00:00
s.SmartStops.Subscribe(session)
}
type EwoSignal interface {
types.Series
Update(value float64)
}
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
2022-04-13 01:43:31 +00:00
market, ok := session.Market(s.Symbol)
if !ok {
log.Errorf("fetch market fail %s", s.Symbol)
return nil
}
indicatorSet, ok := session.StandardIndicatorSet(s.Symbol)
if !ok {
log.Errorf("cannot get indicatorSet of %s", s.Symbol)
return nil
}
2022-04-13 01:43:31 +00:00
orderbook, ok := session.OrderStore(s.Symbol)
2022-04-11 10:55:34 +00:00
if !ok {
log.Errorf("cannot get orderbook of %s", s.Symbol)
return nil
}
2022-04-13 01:43:31 +00:00
position := types.NewPositionFromMarket(market)
s.tradeCollector = bbgo.NewTradeCollector(s.Symbol, position, orderbook)
s.tradeCollector.OnTrade(func(trade types.Trade, profit, netprofit fixedpoint.Value) {
if !profit.IsZero() {
log.Warnf("generate profit: %v, netprofit: %v", profit, netprofit)
}
})
s.tradeCollector.OnPositionUpdate(func(position *types.Position) {
log.Infof("position changed: %s", position)
})
s.tradeCollector.BindStream(session.UserDataStream)
s.SmartStops.RunStopControllers(ctx, session, s.tradeCollector)
2022-04-11 10:55:34 +00:00
/*store, ok := session.MarketDataStore(s.Symbol)
if !ok {
log.Errorf("cannot get marketdatastore of %s", s.Symbol)
return nil
}*/
2022-04-13 01:43:31 +00:00
2022-04-11 10:55:34 +00:00
/*window, ok := store.KLinesOfInterval(s.Interval)
if !ok {
log.Errorf("cannot get klinewindow of %s", s.Interval)
}*/
2022-04-13 01:43:31 +00:00
var ma5, ma34, ma50, ewo types.Series
if s.UseEma {
ma5 = indicatorSet.EWMA(types.IntervalWindow{s.Interval, 5})
ma34 = indicatorSet.EWMA(types.IntervalWindow{s.Interval, 34})
2022-04-13 01:43:31 +00:00
ma50 = indicatorSet.EWMA(types.IntervalWindow{s.Interval, 50})
} else {
ma5 = indicatorSet.SMA(types.IntervalWindow{s.Interval, 5})
ma34 = indicatorSet.SMA(types.IntervalWindow{s.Interval, 34})
2022-04-13 01:43:31 +00:00
ma50 = indicatorSet.SMA(types.IntervalWindow{s.Interval, 50})
}
ewo = types.Mul(types.Minus(types.Div(ma5, ma34), 1.0), 100.)
var ewoSignal EwoSignal
if s.UseEma {
ewoSignal = &indicator.EWMA{IntervalWindow: types.IntervalWindow{s.Interval, s.SignalWindow}}
} else {
ewoSignal = &indicator.SMA{IntervalWindow: types.IntervalWindow{s.Interval, s.SignalWindow}}
}
session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {
2022-04-11 08:07:52 +00:00
if kline.Symbol != s.Symbol {
return
}
2022-04-11 08:07:52 +00:00
if kline.Interval == s.Interval {
if ewoSignal.Length() == 0 {
// lazy init
ewoVals := types.ToReverseArray(ewo)
for _, ewoValue := range ewoVals {
ewoSignal.Update(ewoValue)
}
} else {
ewoSignal.Update(ewo.Last())
}
}
2022-04-13 01:43:31 +00:00
/*lastPrice, ok := session.LastPrice(s.Symbol)
if !ok {
2022-04-13 01:43:31 +00:00
log.Errorf("cannot get last price")
return
2022-04-13 01:43:31 +00:00
}*/
// cancel non-traded orders
var toCancel []types.Order
var toRepost []types.SubmitOrder
for _, order := range orderbook.Orders() {
if order.Status == types.OrderStatusNew || order.Status == types.OrderStatusPartiallyFilled {
toCancel = append(toCancel, order)
}
}
2022-04-13 01:43:31 +00:00
if len(toCancel) > 0 {
if err := orderExecutor.CancelOrders(ctx, toCancel...); err != nil {
log.WithError(err).Errorf("cancel order error")
}
2022-04-13 01:43:31 +00:00
s.tradeCollector.Process()
}
// well, only track prices on 1m
if kline.Interval != s.Interval {
for _, order := range toCancel {
if order.Side == types.SideTypeBuy && order.Price.Compare(kline.Low) < 0 {
order.Quantity = order.Quantity.Mul(order.Price).Div(kline.Low)
order.Price = kline.Low
toRepost = append(toRepost, order.SubmitOrder)
} else if order.Side == types.SideTypeSell && order.Price.Compare(kline.High) > 0 {
order.Price = kline.High
toRepost = append(toRepost, order.SubmitOrder)
2022-04-11 08:07:52 +00:00
}
2022-04-11 10:55:34 +00:00
}
2022-04-13 01:43:31 +00:00
if len(toRepost) > 0 {
createdOrders, err := orderExecutor.SubmitOrders(ctx, toRepost...)
2022-04-11 08:07:52 +00:00
if err != nil {
2022-04-13 01:43:31 +00:00
log.WithError(err).Errorf("cannot place order")
2022-04-11 08:07:52 +00:00
return
}
2022-04-13 01:43:31 +00:00
log.Infof("repost order %v", createdOrders)
s.tradeCollector.Process()
2022-04-11 08:07:52 +00:00
}
}
if kline.Interval != s.Interval {
return
}
2022-04-13 01:43:31 +00:00
// To get the threshold for ewo
mean := types.Mean(types.Abs(ewo), 7)
2022-04-11 08:07:52 +00:00
longSignal := types.CrossOver(ewo, ewoSignal)
shortSignal := types.CrossUnder(ewo, ewoSignal)
2022-04-13 01:43:31 +00:00
bull := types.Predict(ma50, 50, 2) > ma50.Last()
// kline breakthrough ma5, ma50 trend up, and ewo > threshold
IsBull := bull && kline.High.Float64() > ma5.Last() && ewo.Last() > mean
// kline downthrough ma5, ma50 trend down, and ewo < threshold
IsBear := !bull && kline.Low.Float64() < ma5.Last() && ewo.Last() < -mean
2022-04-11 08:07:52 +00:00
var orders []types.SubmitOrder
2022-04-13 01:43:31 +00:00
2022-04-11 08:07:52 +00:00
if longSignal.Index(1) && !shortSignal.Last() && IsBull {
2022-04-13 01:43:31 +00:00
price := kline.Low
2022-04-11 08:07:52 +00:00
quoteBalance, ok := session.Account.Balance(market.QuoteCurrency)
if !ok {
return
}
2022-04-13 01:43:31 +00:00
quantityAmount := quoteBalance.Available.Mul(modifier)
totalQuantity := quantityAmount.Div(price)
2022-04-11 08:07:52 +00:00
if quantityAmount.Sign() <= 0 ||
quantityAmount.Compare(market.MinNotional) < 0 ||
totalQuantity.Compare(market.MinQuantity) < 0 {
log.Infof("quote balance %v is not enough. stop generating buy orders", quoteBalance)
return
}
2022-04-13 01:43:31 +00:00
// strong long
log.Infof("long at %v, timestamp: %s", price, kline.StartTime)
2022-04-11 08:07:52 +00:00
2022-04-13 01:43:31 +00:00
orders = append(orders, types.SubmitOrder{
Symbol: kline.Symbol,
Side: types.SideTypeBuy,
Type: types.OrderTypeLimit,
Price: price,
Quantity: totalQuantity,
Market: market,
TimeInForce: types.TimeInForceGTC,
})
} else if shortSignal.Index(1) && !longSignal.Last() && IsBear {
price := kline.High
2022-04-11 08:07:52 +00:00
balances := session.Account.Balances()
2022-04-13 01:43:31 +00:00
baseBalance := balances[market.BaseCurrency].Available.Mul(modifier)
2022-04-11 10:55:34 +00:00
baseAmount := baseBalance.Mul(price)
2022-04-11 08:07:52 +00:00
if baseBalance.Sign() <= 0 ||
baseBalance.Compare(market.MinQuantity) < 0 ||
baseAmount.Compare(market.MinNotional) < 0 {
log.Infof("base balance %v is not enough. stop generating sell orders", baseBalance)
return
}
2022-04-13 01:43:31 +00:00
log.Infof("short at %v, timestamp: %s", price, kline.StartTime)
orders = append(orders, types.SubmitOrder{
Symbol: s.Symbol,
Side: types.SideTypeSell,
Type: types.OrderTypeLimit,
Market: market,
Quantity: baseBalance,
Price: price,
TimeInForce: types.TimeInForceGTC,
})
2022-04-11 08:07:52 +00:00
}
if len(orders) > 0 {
createdOrders, err := orderExecutor.SubmitOrders(ctx, orders...)
if err != nil {
log.WithError(err).Errorf("cannot place order")
return
}
2022-04-13 01:43:31 +00:00
log.Infof("post order %v", createdOrders)
s.tradeCollector.Process()
}
})
s.Graceful.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
log.Infof("canceling active orders...")
var toCancel []types.Order
for _, order := range orderbook.Orders() {
if order.Status == types.OrderStatusNew || order.Status == types.OrderStatusPartiallyFilled {
toCancel = append(toCancel, order)
}
}
2022-04-13 01:43:31 +00:00
if err := orderExecutor.CancelOrders(ctx, toCancel...); err != nil {
log.WithError(err).Errorf("cancel order error")
}
s.tradeCollector.Process()
})
return nil
}