2020-11-07 04:00:45 +00:00
|
|
|
// flashcrash strategy tries to place the orders at 30%~50% of the current price,
|
|
|
|
// so that you can catch the orders while flashcrash happens
|
|
|
|
package flashcrash
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-12 06:50:08 +00:00
|
|
|
"sync"
|
2020-11-07 04:00:45 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
2022-02-15 05:55:19 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
2020-11-07 04:00:45 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/indicator"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2021-02-03 01:08:05 +00:00
|
|
|
const ID = "flashcrash"
|
|
|
|
|
2020-11-07 04:00:45 +00:00
|
|
|
func init() {
|
2021-02-03 01:08:05 +00:00
|
|
|
bbgo.RegisterStrategy(ID, &Strategy{})
|
2020-11-07 04:00:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Strategy struct {
|
|
|
|
// These fields will be filled from the config file (it translates YAML to JSON)
|
|
|
|
// Symbol is the symbol of market you want to run this strategy
|
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
|
|
|
|
// Interval is the interval used to trigger order updates
|
|
|
|
Interval types.Interval `json:"interval"`
|
|
|
|
|
|
|
|
// GridNum is the grid number, how many orders you want to places
|
|
|
|
GridNum int `json:"gridNumber"`
|
|
|
|
|
fix bollgrid, emstop, flashcrash, funding, grid, pricealert, pricedrop, rebalance, schedule, swing, xbalance, xgap, xmaker and speedup fixedpoint
2022-02-04 11:39:23 +00:00
|
|
|
Percentage fixedpoint.Value `json:"percentage"`
|
2020-11-07 04:00:45 +00:00
|
|
|
|
|
|
|
// BaseQuantity is the quantity you want to submit for each order.
|
fix bollgrid, emstop, flashcrash, funding, grid, pricealert, pricedrop, rebalance, schedule, swing, xbalance, xgap, xmaker and speedup fixedpoint
2022-02-04 11:39:23 +00:00
|
|
|
BaseQuantity fixedpoint.Value `json:"baseQuantity"`
|
2020-11-07 04:00:45 +00:00
|
|
|
|
|
|
|
// activeOrders is the locally maintained active order book of the maker orders.
|
2022-06-05 21:43:38 +00:00
|
|
|
activeOrders *bbgo.ActiveOrderBook
|
2020-11-07 04:00:45 +00:00
|
|
|
|
|
|
|
// Injection fields start
|
|
|
|
// --------------------------
|
|
|
|
// Market stores the configuration of the market, for example, VolumePrecision, PricePrecision, MinLotSize... etc
|
|
|
|
// This field will be injected automatically since we defined the Symbol field.
|
|
|
|
types.Market
|
|
|
|
|
|
|
|
// StandardIndicatorSet contains the standard indicators of a market (symbol)
|
|
|
|
// This field will be injected automatically since we defined the Symbol field.
|
|
|
|
*bbgo.StandardIndicatorSet
|
2020-11-12 06:50:08 +00:00
|
|
|
|
2020-11-07 04:00:45 +00:00
|
|
|
// ewma is the exponential weighted moving average indicator
|
|
|
|
ewma *indicator.EWMA
|
|
|
|
}
|
|
|
|
|
2021-02-03 01:08:05 +00:00
|
|
|
func (s *Strategy) ID() string {
|
|
|
|
return ID
|
|
|
|
}
|
|
|
|
|
2020-11-07 04:00:45 +00:00
|
|
|
func (s *Strategy) updateOrders(orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) {
|
2022-06-05 13:45:43 +00:00
|
|
|
if err := s.activeOrders.GracefulCancel(context.Background(), session.Exchange); err != nil {
|
2020-11-07 04:00:45 +00:00
|
|
|
log.WithError(err).Errorf("cancel order error")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.updateBidOrders(orderExecutor, session)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) updateBidOrders(orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) {
|
|
|
|
quoteCurrency := s.Market.QuoteCurrency
|
2022-04-23 07:43:11 +00:00
|
|
|
balances := session.GetAccount().Balances()
|
2020-11-07 04:00:45 +00:00
|
|
|
|
|
|
|
balance, ok := balances[quoteCurrency]
|
fix bollgrid, emstop, flashcrash, funding, grid, pricealert, pricedrop, rebalance, schedule, swing, xbalance, xgap, xmaker and speedup fixedpoint
2022-02-04 11:39:23 +00:00
|
|
|
if !ok || balance.Available.Sign() <= 0 {
|
|
|
|
log.Infof("insufficient balance of %s: %v", quoteCurrency, balance.Available)
|
2020-11-07 04:00:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-31 11:35:44 +00:00
|
|
|
var startPrice = fixedpoint.NewFromFloat(s.ewma.Last(0)).Mul(s.Percentage)
|
2020-11-07 04:00:45 +00:00
|
|
|
|
|
|
|
var submitOrders []types.SubmitOrder
|
2020-11-07 04:19:57 +00:00
|
|
|
for i := 0; i < s.GridNum; i++ {
|
2020-11-07 04:00:45 +00:00
|
|
|
submitOrders = append(submitOrders, types.SubmitOrder{
|
|
|
|
Symbol: s.Symbol,
|
|
|
|
Side: types.SideTypeBuy,
|
|
|
|
Type: types.OrderTypeLimit,
|
|
|
|
Market: s.Market,
|
|
|
|
Quantity: s.BaseQuantity,
|
|
|
|
Price: startPrice,
|
2022-02-18 05:52:13 +00:00
|
|
|
TimeInForce: types.TimeInForceGTC,
|
2020-11-07 04:00:45 +00:00
|
|
|
})
|
|
|
|
|
fix bollgrid, emstop, flashcrash, funding, grid, pricealert, pricedrop, rebalance, schedule, swing, xbalance, xgap, xmaker and speedup fixedpoint
2022-02-04 11:39:23 +00:00
|
|
|
startPrice = startPrice.Mul(s.Percentage)
|
2020-11-07 04:00:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
orders, err := orderExecutor.SubmitOrders(context.Background(), submitOrders...)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("submit bid order error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.activeOrders.Add(orders...)
|
|
|
|
}
|
|
|
|
|
|
|
|
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})
|
2020-11-07 04:00:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
|
|
|
|
// we don't persist orders so that we can not clear the previous orders for now. just need time to support this.
|
2022-06-05 22:57:25 +00:00
|
|
|
s.activeOrders = bbgo.NewActiveOrderBook(s.Symbol)
|
2021-05-27 06:45:06 +00:00
|
|
|
s.activeOrders.BindStream(session.UserDataStream)
|
2020-11-12 06:50:08 +00:00
|
|
|
|
2022-10-03 08:01:08 +00:00
|
|
|
bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) {
|
2020-11-12 06:59:47 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
2020-11-12 06:50:08 +00:00
|
|
|
log.Infof("canceling active orders...")
|
|
|
|
|
2022-03-16 10:25:27 +00:00
|
|
|
if err := orderExecutor.CancelOrders(ctx, s.activeOrders.Orders()...); err != nil {
|
2020-11-12 06:50:08 +00:00
|
|
|
log.WithError(err).Errorf("cancel order error")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-11-30 05:06:35 +00:00
|
|
|
s.ewma = s.StandardIndicatorSet.EWMA(types.IntervalWindow{
|
2020-11-07 04:00:45 +00:00
|
|
|
Interval: s.Interval,
|
|
|
|
Window: 25,
|
|
|
|
})
|
|
|
|
|
2022-03-15 11:51:15 +00:00
|
|
|
session.UserDataStream.OnStart(func() {
|
|
|
|
s.updateOrders(orderExecutor, session)
|
|
|
|
})
|
|
|
|
|
2021-05-27 19:15:29 +00:00
|
|
|
session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {
|
2020-11-07 04:00:45 +00:00
|
|
|
s.updateOrders(orderExecutor, session)
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|