2020-11-10 11:06:20 +00:00
|
|
|
package bollgrid
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-09 09:23:04 +00:00
|
|
|
"fmt"
|
2020-11-12 06:50:08 +00:00
|
|
|
"sync"
|
2020-11-10 11:06:20 +00:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
|
|
"github.com/c9s/bbgo/pkg/indicator"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2021-02-03 01:08:05 +00:00
|
|
|
const ID = "bollgrid"
|
|
|
|
|
|
|
|
var log = logrus.WithField("strategy", ID)
|
2020-11-10 11:06:20 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Register the pointer of the strategy struct,
|
|
|
|
// so that bbgo knows what struct to be used to unmarshal the configs (YAML or JSON)
|
|
|
|
// Note: built-in strategies need to imported manually in the bbgo cmd package.
|
2021-02-03 01:08:05 +00:00
|
|
|
bbgo.RegisterStrategy(ID, &Strategy{})
|
2020-11-10 11:06:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Strategy struct {
|
|
|
|
// OrderExecutor is an interface for submitting order.
|
|
|
|
// This field will be injected automatically since it's a single exchange strategy.
|
|
|
|
bbgo.OrderExecutor
|
|
|
|
|
|
|
|
// if Symbol string field is defined, bbgo will know it's a symbol-based strategy
|
|
|
|
// The following embedded fields will be injected with the corresponding instances.
|
|
|
|
|
|
|
|
// MarketDataStore is a pointer only injection field. public trades, k-lines (candlestick)
|
|
|
|
// and order book updates are maintained in the market data store.
|
|
|
|
// This field will be injected automatically since we defined the Symbol field.
|
|
|
|
*bbgo.MarketDataStore
|
|
|
|
|
|
|
|
// StandardIndicatorSet contains the standard indicators of a market (symbol)
|
|
|
|
// This field will be injected automatically since we defined the Symbol field.
|
|
|
|
*bbgo.StandardIndicatorSet
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// These fields will be filled from the config file (it translates YAML to JSON)
|
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
|
|
|
|
// Interval is the interval used by the BOLLINGER indicator (which uses K-Line as its source price)
|
|
|
|
Interval types.Interval `json:"interval"`
|
|
|
|
|
|
|
|
// RepostInterval is the interval for re-posting maker orders
|
|
|
|
RepostInterval types.Interval `json:"repostInterval"`
|
|
|
|
|
|
|
|
// GridPips is the pips of grid
|
|
|
|
// e.g., 0.001, so that your orders will be submitted at price like 0.127, 0.128, 0.129, 0.130
|
|
|
|
GridPips fixedpoint.Value `json:"gridPips"`
|
|
|
|
|
|
|
|
ProfitSpread fixedpoint.Value `json:"profitSpread"`
|
|
|
|
|
|
|
|
// GridNum is the grid number, how many orders you want to post on the orderbook.
|
|
|
|
GridNum int `json:"gridNumber"`
|
|
|
|
|
2020-11-11 15:18:53 +00:00
|
|
|
// Quantity 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
|
|
|
Quantity fixedpoint.Value `json:"quantity"`
|
2020-11-10 11:06:20 +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-10 11:06:20 +00:00
|
|
|
|
2022-06-05 21:43:38 +00:00
|
|
|
profitOrders *bbgo.ActiveOrderBook
|
2020-11-12 09:38:13 +00:00
|
|
|
|
2020-11-11 15:18:53 +00:00
|
|
|
orders *bbgo.OrderStore
|
|
|
|
|
2020-11-10 11:06:20 +00:00
|
|
|
// boll is the BOLLINGER indicator we used for predicting the price.
|
|
|
|
boll *indicator.BOLL
|
2021-05-08 06:48:07 +00:00
|
|
|
|
|
|
|
CancelProfitOrdersOnShutdown bool `json: "shutdownCancelProfitOrders"`
|
2020-11-10 11:06:20 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 01:08:05 +00:00
|
|
|
func (s *Strategy) ID() string {
|
|
|
|
return ID
|
|
|
|
}
|
|
|
|
|
2021-04-02 02:22:03 +00:00
|
|
|
func (s *Strategy) Validate() error {
|
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 s.ProfitSpread.Sign() <= 0 {
|
2021-04-02 02:22:03 +00:00
|
|
|
// If profitSpread is empty or its value is negative
|
|
|
|
return fmt.Errorf("profit spread should bigger than 0")
|
|
|
|
}
|
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 s.Quantity.Sign() <= 0 {
|
2021-04-02 02:22:03 +00:00
|
|
|
// If quantity is empty or its value is negative
|
|
|
|
return fmt.Errorf("quantity should bigger than 0")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-10 11:06:20 +00:00
|
|
|
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
|
2021-02-13 08:03:11 +00:00
|
|
|
if s.Interval == "" {
|
|
|
|
panic("bollgrid interval can not be empty")
|
|
|
|
}
|
|
|
|
|
2020-11-10 11:06:20 +00:00
|
|
|
// currently we need the 1m kline to update the last close price and indicators
|
2022-05-19 01:48:36 +00:00
|
|
|
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
|
2021-02-10 14:44:48 +00:00
|
|
|
|
2021-02-13 08:03:11 +00:00
|
|
|
if len(s.RepostInterval) > 0 && s.Interval != s.RepostInterval {
|
2022-05-19 01:48:36 +00:00
|
|
|
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.RepostInterval})
|
2021-02-10 14:44:48 +00:00
|
|
|
}
|
2020-11-10 11:06:20 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 09:23:04 +00:00
|
|
|
func (s *Strategy) generateGridBuyOrders(session *bbgo.ExchangeSession) ([]types.SubmitOrder, error) {
|
2022-04-23 07:43:11 +00:00
|
|
|
balances := session.GetAccount().Balances()
|
2021-02-10 08:00:19 +00:00
|
|
|
quoteBalance := balances[s.Market.QuoteCurrency].Available
|
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 quoteBalance.Sign() <= 0 {
|
|
|
|
return nil, fmt.Errorf("quote balance %s is zero: %v", s.Market.QuoteCurrency, quoteBalance)
|
2020-11-10 11:06:20 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 09:23:04 +00:00
|
|
|
upBand, downBand := s.boll.LastUpBand(), s.boll.LastDownBand()
|
2020-11-12 00:28:59 +00:00
|
|
|
if upBand <= 0.0 {
|
2021-02-09 09:23:04 +00:00
|
|
|
return nil, fmt.Errorf("up band == 0")
|
|
|
|
}
|
|
|
|
if downBand <= 0.0 {
|
|
|
|
return nil, fmt.Errorf("down band == 0")
|
2020-11-10 11:06:20 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 09:23:04 +00:00
|
|
|
currentPrice, ok := session.LastPrice(s.Symbol)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("last price not found")
|
|
|
|
}
|
|
|
|
|
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 currentPrice.Float64() > upBand || currentPrice.Float64() < downBand {
|
|
|
|
return nil, fmt.Errorf("current price %v exceed the bollinger band %f <> %f", currentPrice, upBand, downBand)
|
2021-02-09 09:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ema99 := s.StandardIndicatorSet.EWMA(types.IntervalWindow{Interval: s.Interval, Window: 99})
|
|
|
|
ema25 := s.StandardIndicatorSet.EWMA(types.IntervalWindow{Interval: s.Interval, Window: 25})
|
|
|
|
ema7 := s.StandardIndicatorSet.EWMA(types.IntervalWindow{Interval: s.Interval, Window: 7})
|
|
|
|
if ema7.Last() > ema25.Last()*1.001 && ema25.Last() > ema99.Last()*1.0005 {
|
|
|
|
log.Infof("all ema lines trend up, skip buy")
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
priceRange := upBand - downBand
|
|
|
|
gridSize := priceRange / float64(s.GridNum)
|
|
|
|
|
|
|
|
var orders []types.SubmitOrder
|
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
|
|
|
for pricef := upBand; pricef >= downBand; pricef -= gridSize {
|
|
|
|
if pricef >= currentPrice.Float64() {
|
2021-02-09 09:23:04 +00:00
|
|
|
continue
|
|
|
|
}
|
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
|
|
|
price := fixedpoint.NewFromFloat(pricef)
|
2021-04-15 07:49:06 +00:00
|
|
|
// adjust buy quantity using current quote balance
|
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
|
|
|
quantity := bbgo.AdjustFloatQuantityByMaxAmount(s.Quantity, price, quoteBalance)
|
2021-02-09 09:23:04 +00:00
|
|
|
order := types.SubmitOrder{
|
|
|
|
Symbol: s.Symbol,
|
|
|
|
Side: types.SideTypeBuy,
|
|
|
|
Type: types.OrderTypeLimit,
|
|
|
|
Market: s.Market,
|
2021-04-15 07:49:06 +00:00
|
|
|
Quantity: quantity,
|
2021-02-09 09:23:04 +00:00
|
|
|
Price: price,
|
2022-02-18 05:52:13 +00:00
|
|
|
TimeInForce: types.TimeInForceGTC,
|
2021-02-09 09:23:04 +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
|
|
|
quoteQuantity := order.Quantity.Mul(price)
|
|
|
|
if quantity.Compare(s.MinQuantity) < 0 {
|
2021-04-15 07:49:06 +00:00
|
|
|
// don't submit this order if buy quantity is too small
|
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
|
|
|
log.Infof("quote balance %v is not enough, stop generating buy orders", quoteBalance)
|
2021-02-09 09:23:04 +00:00
|
|
|
break
|
|
|
|
}
|
2021-02-10 13:06:00 +00:00
|
|
|
quoteBalance = quoteBalance.Sub(quoteQuantity)
|
2021-02-09 09:23:04 +00:00
|
|
|
log.Infof("submitting order: %s", order.String())
|
|
|
|
orders = append(orders, order)
|
|
|
|
}
|
|
|
|
return orders, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) generateGridSellOrders(session *bbgo.ExchangeSession) ([]types.SubmitOrder, error) {
|
2022-04-23 07:43:11 +00:00
|
|
|
balances := session.GetAccount().Balances()
|
2021-02-10 08:00:19 +00:00
|
|
|
baseBalance := balances[s.Market.BaseCurrency].Available
|
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 baseBalance.Sign() <= 0 {
|
|
|
|
return nil, fmt.Errorf("base balance %s is zero: %+v", s.Market.BaseCurrency, baseBalance)
|
2021-02-09 09:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
upBand, downBand := s.boll.LastUpBand(), s.boll.LastDownBand()
|
|
|
|
if upBand <= 0.0 {
|
|
|
|
return nil, fmt.Errorf("up band == 0")
|
|
|
|
}
|
2020-11-12 00:28:59 +00:00
|
|
|
if downBand <= 0.0 {
|
2021-02-09 09:23:04 +00:00
|
|
|
return nil, fmt.Errorf("down band == 0")
|
2020-11-10 11:06:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-12 00:28:59 +00:00
|
|
|
currentPrice, ok := session.LastPrice(s.Symbol)
|
|
|
|
if !ok {
|
2021-02-09 09:23:04 +00:00
|
|
|
return nil, fmt.Errorf("last price not found")
|
2020-11-12 00:28:59 +00:00
|
|
|
}
|
2020-11-12 09:41:28 +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
|
|
|
currentPricef := currentPrice.Float64()
|
|
|
|
|
|
|
|
if currentPricef > upBand || currentPricef < downBand {
|
2021-02-09 09:23:04 +00:00
|
|
|
return nil, fmt.Errorf("current price exceed the bollinger band")
|
2020-11-12 00:28:59 +00:00
|
|
|
}
|
2020-11-10 11:06:20 +00:00
|
|
|
|
2020-11-30 05:06:35 +00:00
|
|
|
ema99 := s.StandardIndicatorSet.EWMA(types.IntervalWindow{Interval: s.Interval, Window: 99})
|
|
|
|
ema25 := s.StandardIndicatorSet.EWMA(types.IntervalWindow{Interval: s.Interval, Window: 25})
|
|
|
|
ema7 := s.StandardIndicatorSet.EWMA(types.IntervalWindow{Interval: s.Interval, Window: 7})
|
2021-02-09 09:23:04 +00:00
|
|
|
if ema7.Last() < ema25.Last()*(1-0.004) && ema25.Last() < ema99.Last()*(1-0.0005) {
|
|
|
|
log.Infof("all ema lines trend down, skip sell")
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-11-12 00:28:59 +00:00
|
|
|
|
|
|
|
priceRange := upBand - downBand
|
|
|
|
gridSize := priceRange / float64(s.GridNum)
|
|
|
|
|
|
|
|
var orders []types.SubmitOrder
|
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
|
|
|
for pricef := downBand; pricef <= upBand; pricef += gridSize {
|
|
|
|
if pricef <= currentPricef {
|
2021-02-09 09:23:04 +00:00
|
|
|
continue
|
2020-11-12 00:28:59 +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
|
|
|
price := fixedpoint.NewFromFloat(pricef)
|
2021-04-15 07:49:06 +00:00
|
|
|
// adjust sell quantity using current base balance
|
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
|
|
|
quantity := fixedpoint.Min(s.Quantity, baseBalance)
|
2021-02-08 09:32:31 +00:00
|
|
|
order := types.SubmitOrder{
|
|
|
|
Symbol: s.Symbol,
|
2021-02-09 09:23:04 +00:00
|
|
|
Side: types.SideTypeSell,
|
2021-02-08 09:32:31 +00:00
|
|
|
Type: types.OrderTypeLimit,
|
|
|
|
Market: s.Market,
|
2021-04-15 07:49:06 +00:00
|
|
|
Quantity: quantity,
|
2021-02-08 09:32:31 +00:00
|
|
|
Price: price,
|
2022-02-18 05:52:13 +00:00
|
|
|
TimeInForce: types.TimeInForceGTC,
|
2021-02-08 09:32:31 +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
|
|
|
baseQuantity := order.Quantity
|
|
|
|
if quantity.Compare(s.MinQuantity) < 0 {
|
2021-04-15 07:49:06 +00:00
|
|
|
// don't submit this order if sell quantity is too small
|
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
|
|
|
log.Infof("base balance %s is not enough, stop generating sell orders", baseBalance)
|
2021-02-09 09:23:04 +00:00
|
|
|
break
|
2020-11-12 00:28:59 +00:00
|
|
|
}
|
2021-02-10 08:00:19 +00:00
|
|
|
baseBalance = baseBalance.Sub(baseQuantity)
|
2020-11-12 00:28:59 +00:00
|
|
|
log.Infof("submitting order: %s", order.String())
|
|
|
|
orders = append(orders, order)
|
|
|
|
}
|
2021-02-09 09:23:04 +00:00
|
|
|
return orders, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) placeGridOrders(orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) {
|
|
|
|
sellOrders, err := s.generateGridSellOrders(session)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn(err.Error())
|
|
|
|
}
|
2021-04-15 04:13:15 +00:00
|
|
|
createdSellOrders, err := orderExecutor.SubmitOrders(context.Background(), sellOrders...)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("can not place sell orders")
|
|
|
|
}
|
|
|
|
|
2021-02-09 09:23:04 +00:00
|
|
|
buyOrders, err := s.generateGridBuyOrders(session)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn(err.Error())
|
|
|
|
}
|
2021-04-15 04:13:15 +00:00
|
|
|
createdBuyOrders, err := orderExecutor.SubmitOrders(context.Background(), buyOrders...)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("can not place buy orders")
|
|
|
|
}
|
2021-02-09 09:23:04 +00:00
|
|
|
|
2021-04-15 04:13:15 +00:00
|
|
|
createdOrders := append(createdSellOrders, createdBuyOrders...)
|
2021-04-15 03:29:28 +00:00
|
|
|
s.activeOrders.Add(createdOrders...)
|
|
|
|
s.orders.Add(createdOrders...)
|
2020-11-12 00:28:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) updateOrders(orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) {
|
2022-03-16 10:25:27 +00:00
|
|
|
if err := orderExecutor.CancelOrders(context.Background(), s.activeOrders.Orders()...); err != nil {
|
2020-11-12 00:28:59 +00:00
|
|
|
log.WithError(err).Errorf("cancel order error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip order updates if up-band - down-band < min profit spread
|
|
|
|
if (s.boll.LastUpBand() - s.boll.LastDownBand()) <= s.ProfitSpread.Float64() {
|
|
|
|
log.Infof("boll: down band price == up band price, skipping...")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.placeGridOrders(orderExecutor, session)
|
|
|
|
|
2020-11-10 11:06:20 +00:00
|
|
|
s.activeOrders.Print()
|
|
|
|
}
|
|
|
|
|
2021-04-14 12:50:11 +00:00
|
|
|
func (s *Strategy) submitReverseOrder(order types.Order, session *bbgo.ExchangeSession) {
|
2022-04-23 07:43:11 +00:00
|
|
|
balances := session.GetAccount().Balances()
|
2021-04-14 12:50:11 +00:00
|
|
|
|
2020-11-11 15:18:53 +00:00
|
|
|
var side = order.Side.Reverse()
|
|
|
|
var price = order.Price
|
2021-04-14 12:50:11 +00:00
|
|
|
var quantity = order.Quantity
|
2020-11-10 11:06:20 +00:00
|
|
|
|
2020-11-11 15:18:53 +00:00
|
|
|
switch side {
|
|
|
|
case types.SideTypeSell:
|
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
|
|
|
price = price.Add(s.ProfitSpread)
|
|
|
|
maxQuantity := balances[s.Market.BaseCurrency].Available
|
|
|
|
quantity = fixedpoint.Min(quantity, maxQuantity)
|
2020-11-10 11:06:20 +00:00
|
|
|
|
2020-11-11 15:18:53 +00:00
|
|
|
case types.SideTypeBuy:
|
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
|
|
|
price = price.Sub(s.ProfitSpread)
|
|
|
|
maxQuantity := balances[s.Market.QuoteCurrency].Available.Div(price)
|
|
|
|
quantity = fixedpoint.Min(quantity, maxQuantity)
|
2020-11-11 15:18:53 +00:00
|
|
|
}
|
2020-11-10 11:06:20 +00:00
|
|
|
|
2020-11-11 15:18:53 +00:00
|
|
|
submitOrder := types.SubmitOrder{
|
|
|
|
Symbol: s.Symbol,
|
|
|
|
Side: side,
|
|
|
|
Type: types.OrderTypeLimit,
|
2021-04-14 12:50:11 +00:00
|
|
|
Quantity: quantity,
|
2020-11-11 15:18:53 +00:00
|
|
|
Price: price,
|
2022-02-18 05:52:13 +00:00
|
|
|
TimeInForce: types.TimeInForceGTC,
|
2020-11-11 15:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("submitting reverse order: %s against %s", submitOrder.String(), order.String())
|
|
|
|
|
|
|
|
createdOrders, err := s.OrderExecutor.SubmitOrders(context.Background(), submitOrder)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("can not place orders")
|
|
|
|
return
|
2020-11-10 11:06:20 +00:00
|
|
|
}
|
2020-11-11 15:18:53 +00:00
|
|
|
|
2020-11-12 09:38:13 +00:00
|
|
|
s.profitOrders.Add(createdOrders...)
|
2020-11-11 15:18:53 +00:00
|
|
|
s.orders.Add(createdOrders...)
|
2020-11-10 11:06:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
|
|
|
|
if s.GridNum == 0 {
|
|
|
|
s.GridNum = 2
|
|
|
|
}
|
|
|
|
|
2020-11-30 05:06:35 +00:00
|
|
|
s.boll = s.StandardIndicatorSet.BOLL(types.IntervalWindow{
|
2020-11-10 11:06:20 +00:00
|
|
|
Interval: s.Interval,
|
|
|
|
Window: 21,
|
2020-11-11 15:18:53 +00:00
|
|
|
}, 2.0)
|
|
|
|
|
2021-01-21 06:51:37 +00:00
|
|
|
s.orders = bbgo.NewOrderStore(s.Symbol)
|
2021-05-27 06:45:06 +00:00
|
|
|
s.orders.BindStream(session.UserDataStream)
|
2020-11-10 11:06:20 +00:00
|
|
|
|
|
|
|
// 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)
|
2020-11-11 15:18:53 +00:00
|
|
|
s.activeOrders.OnFilled(func(o types.Order) {
|
2021-04-14 12:50:11 +00:00
|
|
|
s.submitReverseOrder(o, session)
|
2020-11-11 15:18:53 +00:00
|
|
|
})
|
2021-05-27 06:45:06 +00:00
|
|
|
s.activeOrders.BindStream(session.UserDataStream)
|
2020-11-10 11:06:20 +00:00
|
|
|
|
2022-06-05 22:57:25 +00:00
|
|
|
s.profitOrders = bbgo.NewActiveOrderBook(s.Symbol)
|
2020-11-12 09:38:13 +00:00
|
|
|
s.profitOrders.OnFilled(func(o types.Order) {
|
|
|
|
// we made profit here!
|
|
|
|
})
|
2021-05-27 06:45:06 +00:00
|
|
|
s.profitOrders.BindStream(session.UserDataStream)
|
2020-11-12 09:38:13 +00:00
|
|
|
|
|
|
|
// setup graceful shutting down handler
|
2022-10-03 08:01:08 +00:00
|
|
|
bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) {
|
2020-11-12 09:38:13 +00:00
|
|
|
// call Done to notify the main process.
|
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-12 09:38:13 +00:00
|
|
|
|
2021-05-08 06:48:07 +00:00
|
|
|
if s.CancelProfitOrdersOnShutdown {
|
|
|
|
log.Infof("canceling profit orders...")
|
2022-03-16 10:25:27 +00:00
|
|
|
err := orderExecutor.CancelOrders(ctx, s.profitOrders.Orders()...)
|
2021-05-08 06:48:07 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("cancel profit order error")
|
|
|
|
}
|
2020-11-12 09:38:13 +00:00
|
|
|
}
|
2020-11-12 06:50:08 +00:00
|
|
|
})
|
|
|
|
|
2021-05-27 06:45:06 +00:00
|
|
|
session.UserDataStream.OnStart(func() {
|
2020-11-12 09:41:28 +00:00
|
|
|
log.Infof("connected, submitting the first round of the orders")
|
2020-11-12 08:31:09 +00:00
|
|
|
s.updateOrders(orderExecutor, session)
|
|
|
|
})
|
|
|
|
|
2020-11-10 11:06:20 +00:00
|
|
|
// avoid using time ticker since we will need back testing here
|
2021-05-27 19:13:50 +00:00
|
|
|
session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {
|
2020-11-10 11:06:20 +00:00
|
|
|
// skip kline events that does not belong to this symbol
|
|
|
|
if kline.Symbol != s.Symbol {
|
|
|
|
log.Infof("%s != %s", kline.Symbol, s.Symbol)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-12 08:31:09 +00:00
|
|
|
if s.RepostInterval != "" {
|
2020-11-10 11:06:20 +00:00
|
|
|
// see if we have enough balances and then we create limit orders on the up band and the down band.
|
2020-11-12 08:31:09 +00:00
|
|
|
if s.RepostInterval == kline.Interval {
|
|
|
|
s.updateOrders(orderExecutor, session)
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if s.Interval == kline.Interval {
|
2020-11-10 11:06:20 +00:00
|
|
|
s.updateOrders(orderExecutor, session)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|