2021-03-21 02:44:06 +00:00
|
|
|
package xmaker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-12-26 04:10:10 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
2022-01-09 03:33:34 +00:00
|
|
|
"golang.org/x/time/rate"
|
2021-12-26 04:10:10 +00:00
|
|
|
|
2021-03-21 02:44:06 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
2021-03-22 09:27:07 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/exchange/max"
|
2021-03-21 02:44:06 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
2021-05-30 06:46:48 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/indicator"
|
2021-03-21 02:44:06 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/service"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2022-01-06 16:14:24 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/util"
|
2021-03-21 02:44:06 +00:00
|
|
|
)
|
|
|
|
|
2021-05-17 13:33:55 +00:00
|
|
|
var defaultMargin = fixedpoint.NewFromFloat(0.003)
|
2021-03-21 02:44:06 +00:00
|
|
|
|
|
|
|
const ID = "xmaker"
|
|
|
|
|
|
|
|
const stateKey = "state-v1"
|
|
|
|
|
|
|
|
var log = logrus.WithField("strategy", ID)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
bbgo.RegisterStrategy(ID, &Strategy{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type Strategy struct {
|
|
|
|
*bbgo.Graceful
|
|
|
|
*bbgo.Notifiability
|
|
|
|
*bbgo.Persistence
|
|
|
|
|
2021-05-30 06:46:48 +00:00
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
|
|
|
|
// SourceExchange session name
|
2021-03-21 02:44:06 +00:00
|
|
|
SourceExchange string `json:"sourceExchange"`
|
2021-05-30 06:46:48 +00:00
|
|
|
|
|
|
|
// MakerExchange session name
|
|
|
|
MakerExchange string `json:"makerExchange"`
|
2021-03-21 02:44:06 +00:00
|
|
|
|
2021-05-09 13:14:51 +00:00
|
|
|
UpdateInterval types.Duration `json:"updateInterval"`
|
|
|
|
HedgeInterval types.Duration `json:"hedgeInterval"`
|
2021-05-09 10:55:56 +00:00
|
|
|
OrderCancelWaitTime types.Duration `json:"orderCancelWaitTime"`
|
2021-03-21 02:44:06 +00:00
|
|
|
|
2021-06-06 18:49:44 +00:00
|
|
|
Margin fixedpoint.Value `json:"margin"`
|
|
|
|
BidMargin fixedpoint.Value `json:"bidMargin"`
|
|
|
|
AskMargin fixedpoint.Value `json:"askMargin"`
|
|
|
|
UseDepthPrice bool `json:"useDepthPrice"`
|
2021-05-09 18:52:41 +00:00
|
|
|
|
2021-05-17 12:03:42 +00:00
|
|
|
EnableBollBandMargin bool `json:"enableBollBandMargin"`
|
|
|
|
BollBandInterval types.Interval `json:"bollBandInterval"`
|
|
|
|
BollBandMargin fixedpoint.Value `json:"bollBandMargin"`
|
|
|
|
BollBandMarginFactor fixedpoint.Value `json:"bollBandMarginFactor"`
|
|
|
|
|
2021-05-11 04:47:45 +00:00
|
|
|
StopHedgeQuoteBalance fixedpoint.Value `json:"stopHedgeQuoteBalance"`
|
|
|
|
StopHedgeBaseBalance fixedpoint.Value `json:"stopHedgeBaseBalance"`
|
|
|
|
|
2021-05-09 18:52:41 +00:00
|
|
|
// Quantity is used for fixed quantity of the first layer
|
|
|
|
Quantity fixedpoint.Value `json:"quantity"`
|
|
|
|
|
|
|
|
// QuantityMultiplier is the factor that multiplies the quantity of the previous layer
|
|
|
|
QuantityMultiplier fixedpoint.Value `json:"quantityMultiplier"`
|
|
|
|
|
|
|
|
// QuantityScale helps user to define the quantity by layer scale
|
|
|
|
QuantityScale *bbgo.LayerScale `json:"quantityScale,omitempty"`
|
|
|
|
|
2021-05-11 04:47:45 +00:00
|
|
|
// MaxExposurePosition defines the unhedged quantity of stop
|
2021-04-04 03:14:09 +00:00
|
|
|
MaxExposurePosition fixedpoint.Value `json:"maxExposurePosition"`
|
2021-05-11 04:47:45 +00:00
|
|
|
|
|
|
|
DisableHedge bool `json:"disableHedge"`
|
2021-03-21 02:44:06 +00:00
|
|
|
|
2021-05-28 17:31:13 +00:00
|
|
|
NotifyTrade bool `json:"notifyTrade"`
|
|
|
|
|
2021-03-21 02:44:06 +00:00
|
|
|
NumLayers int `json:"numLayers"`
|
2021-05-11 04:47:45 +00:00
|
|
|
|
|
|
|
// Pips is the pips of the layer prices
|
2021-05-17 15:55:09 +00:00
|
|
|
Pips fixedpoint.Value `json:"pips"`
|
2021-05-11 04:47:45 +00:00
|
|
|
|
|
|
|
// --------------------------------
|
|
|
|
// private field
|
2021-03-21 02:44:06 +00:00
|
|
|
|
2021-12-26 04:10:10 +00:00
|
|
|
makerSession, sourceSession *bbgo.ExchangeSession
|
2021-03-21 02:44:06 +00:00
|
|
|
|
2021-12-26 04:10:10 +00:00
|
|
|
makerMarket, sourceMarket types.Market
|
2021-03-21 02:44:06 +00:00
|
|
|
|
2021-05-17 12:03:42 +00:00
|
|
|
// boll is the BOLLINGER indicator we used for predicting the price.
|
|
|
|
boll *indicator.BOLL
|
|
|
|
|
2021-03-21 02:44:06 +00:00
|
|
|
state *State
|
|
|
|
|
|
|
|
book *types.StreamOrderBook
|
|
|
|
activeMakerOrders *bbgo.LocalActiveOrderBook
|
|
|
|
|
2022-01-09 03:33:34 +00:00
|
|
|
hedgeErrorLimiter *rate.Limiter
|
|
|
|
|
2021-12-26 04:10:10 +00:00
|
|
|
orderStore *bbgo.OrderStore
|
|
|
|
tradeCollector *bbgo.TradeCollector
|
2021-03-21 02:44:06 +00:00
|
|
|
|
|
|
|
lastPrice float64
|
2021-03-22 09:27:07 +00:00
|
|
|
groupID uint32
|
2021-03-21 02:44:06 +00:00
|
|
|
|
|
|
|
stopC chan struct{}
|
|
|
|
}
|
|
|
|
|
2021-05-09 12:03:06 +00:00
|
|
|
func (s *Strategy) ID() string {
|
|
|
|
return ID
|
|
|
|
}
|
|
|
|
|
2021-03-21 02:44:06 +00:00
|
|
|
func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) {
|
|
|
|
sourceSession, ok := sessions[s.SourceExchange]
|
|
|
|
if !ok {
|
2021-03-21 04:43:41 +00:00
|
|
|
panic(fmt.Errorf("source session %s is not defined", s.SourceExchange))
|
2021-03-21 02:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sourceSession.Subscribe(types.BookChannel, s.Symbol, types.SubscribeOptions{})
|
2021-05-17 11:19:32 +00:00
|
|
|
sourceSession.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: "1m"})
|
2021-05-17 13:33:55 +00:00
|
|
|
|
|
|
|
makerSession, ok := sessions[s.MakerExchange]
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Errorf("maker session %s is not defined", s.MakerExchange))
|
|
|
|
}
|
|
|
|
makerSession.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: "1m"})
|
2021-03-21 02:44:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-10 16:58:11 +00:00
|
|
|
func aggregatePrice(pvs types.PriceVolumeSlice, requiredQuantity fixedpoint.Value) (price fixedpoint.Value) {
|
|
|
|
q := requiredQuantity
|
|
|
|
totalAmount := fixedpoint.Value(0)
|
|
|
|
|
|
|
|
if len(pvs) == 0 {
|
|
|
|
price = 0
|
|
|
|
return price
|
|
|
|
} else if pvs[0].Volume >= requiredQuantity {
|
|
|
|
return pvs[0].Price
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(pvs); i++ {
|
|
|
|
pv := pvs[i]
|
|
|
|
if pv.Volume >= q {
|
|
|
|
totalAmount += q.Mul(pv.Price)
|
|
|
|
break
|
|
|
|
}
|
2021-05-10 16:10:49 +00:00
|
|
|
|
2021-05-10 16:58:11 +00:00
|
|
|
q -= pv.Volume
|
|
|
|
totalAmount += pv.Volume.Mul(pv.Price)
|
|
|
|
}
|
|
|
|
|
|
|
|
price = totalAmount.Div(requiredQuantity)
|
|
|
|
return price
|
2021-05-10 16:10:49 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 10:58:20 +00:00
|
|
|
func (s *Strategy) updateQuote(ctx context.Context, orderExecutionRouter bbgo.OrderExecutionRouter) {
|
2021-03-21 02:44:06 +00:00
|
|
|
if err := s.makerSession.Exchange.CancelOrders(ctx, s.activeMakerOrders.Orders()...); err != nil {
|
2021-05-10 16:10:49 +00:00
|
|
|
log.WithError(err).Errorf("can not cancel %s orders", s.Symbol)
|
2021-03-21 02:44:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-10 16:10:49 +00:00
|
|
|
// avoid unlock issue and wait for the balance update
|
2021-05-09 10:55:56 +00:00
|
|
|
if s.OrderCancelWaitTime > 0 {
|
|
|
|
time.Sleep(s.OrderCancelWaitTime.Duration())
|
|
|
|
} else {
|
|
|
|
// use the default wait time
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
}
|
2021-05-09 10:32:05 +00:00
|
|
|
|
|
|
|
if s.activeMakerOrders.NumOfAsks() > 0 || s.activeMakerOrders.NumOfBids() > 0 {
|
2021-05-09 10:55:56 +00:00
|
|
|
log.Warnf("there are some %s orders not canceled, skipping placing maker orders", s.Symbol)
|
2021-05-09 12:03:06 +00:00
|
|
|
s.activeMakerOrders.Print()
|
2021-05-09 10:32:05 +00:00
|
|
|
return
|
|
|
|
}
|
2021-03-21 02:44:06 +00:00
|
|
|
|
2021-06-07 09:02:24 +00:00
|
|
|
bestBid, bestAsk, hasPrice := s.book.BestBidAndAsk()
|
|
|
|
if !hasPrice {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-26 04:10:10 +00:00
|
|
|
// use mid-price for the last price
|
|
|
|
s.lastPrice = (bestBid.Price + bestAsk.Price).Float64() / 2
|
|
|
|
|
2021-06-07 09:02:24 +00:00
|
|
|
sourceBook := s.book.CopyDepth(20)
|
2021-03-21 02:44:06 +00:00
|
|
|
if valid, err := sourceBook.IsValid(); !valid {
|
2021-06-07 09:02:24 +00:00
|
|
|
log.WithError(err).Errorf("%s invalid copied order book, skip quoting: %v", s.Symbol, err)
|
2021-03-21 02:44:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-21 03:16:15 +00:00
|
|
|
var disableMakerBid = false
|
|
|
|
var disableMakerAsk = false
|
2021-03-21 02:44:06 +00:00
|
|
|
|
2021-05-10 16:10:49 +00:00
|
|
|
// check maker's balance quota
|
|
|
|
// we load the balances from the account while we're generating the orders,
|
2021-03-21 03:16:15 +00:00
|
|
|
// the balance may have a chance to be deducted by other strategies or manual orders submitted by the user
|
|
|
|
makerBalances := s.makerSession.Account.Balances()
|
2021-03-21 02:44:06 +00:00
|
|
|
makerQuota := &bbgo.QuotaTransaction{}
|
2021-03-21 03:16:15 +00:00
|
|
|
if b, ok := makerBalances[s.makerMarket.BaseCurrency]; ok {
|
2021-05-10 16:10:49 +00:00
|
|
|
if b.Available.Float64() > s.makerMarket.MinQuantity {
|
|
|
|
makerQuota.BaseAsset.Add(b.Available)
|
|
|
|
} else {
|
2021-03-21 04:43:41 +00:00
|
|
|
disableMakerAsk = true
|
|
|
|
}
|
2021-03-21 02:44:06 +00:00
|
|
|
}
|
2021-03-21 04:43:41 +00:00
|
|
|
|
2021-03-21 03:16:15 +00:00
|
|
|
if b, ok := makerBalances[s.makerMarket.QuoteCurrency]; ok {
|
2021-05-10 16:10:49 +00:00
|
|
|
if b.Available.Float64() > s.makerMarket.MinNotional {
|
|
|
|
makerQuota.QuoteAsset.Add(b.Available)
|
|
|
|
} else {
|
2021-04-04 03:14:09 +00:00
|
|
|
disableMakerBid = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 02:44:06 +00:00
|
|
|
hedgeBalances := s.sourceSession.Account.Balances()
|
|
|
|
hedgeQuota := &bbgo.QuotaTransaction{}
|
|
|
|
if b, ok := hedgeBalances[s.sourceMarket.BaseCurrency]; ok {
|
2021-03-21 04:43:41 +00:00
|
|
|
// to make bid orders, we need enough base asset in the foreign exchange,
|
2021-03-21 03:16:15 +00:00
|
|
|
// if the base asset balance is not enough for selling
|
2021-05-26 15:04:25 +00:00
|
|
|
if s.StopHedgeBaseBalance > 0 {
|
|
|
|
if b.Available > (s.StopHedgeBaseBalance + fixedpoint.NewFromFloat(s.sourceMarket.MinQuantity)) {
|
|
|
|
hedgeQuota.BaseAsset.Add(b.Available - s.StopHedgeBaseBalance - fixedpoint.NewFromFloat(s.sourceMarket.MinQuantity))
|
|
|
|
} else {
|
|
|
|
log.Warnf("%s maker bid disabled: insufficient base balance %s", s.Symbol, b.String())
|
|
|
|
disableMakerBid = true
|
|
|
|
}
|
2021-05-11 04:47:45 +00:00
|
|
|
} else if b.Available.Float64() > s.sourceMarket.MinQuantity {
|
2021-05-10 16:10:49 +00:00
|
|
|
hedgeQuota.BaseAsset.Add(b.Available)
|
|
|
|
} else {
|
2021-05-11 04:53:32 +00:00
|
|
|
log.Warnf("%s maker bid disabled: insufficient base balance %s", s.Symbol, b.String())
|
2021-03-21 03:16:15 +00:00
|
|
|
disableMakerBid = true
|
|
|
|
}
|
2021-03-21 02:44:06 +00:00
|
|
|
}
|
2021-03-21 03:16:15 +00:00
|
|
|
|
2021-03-21 02:44:06 +00:00
|
|
|
if b, ok := hedgeBalances[s.sourceMarket.QuoteCurrency]; ok {
|
2021-03-21 04:43:41 +00:00
|
|
|
// to make ask orders, we need enough quote asset in the foreign exchange,
|
2021-03-21 03:16:15 +00:00
|
|
|
// if the quote asset balance is not enough for buying
|
2021-05-26 15:04:25 +00:00
|
|
|
if s.StopHedgeQuoteBalance > 0 {
|
|
|
|
if b.Available > (s.StopHedgeQuoteBalance + fixedpoint.NewFromFloat(s.sourceMarket.MinNotional)) {
|
|
|
|
hedgeQuota.QuoteAsset.Add(b.Available - s.StopHedgeQuoteBalance - fixedpoint.NewFromFloat(s.sourceMarket.MinNotional))
|
|
|
|
} else {
|
|
|
|
log.Warnf("%s maker ask disabled: insufficient quote balance %s", s.Symbol, b.String())
|
|
|
|
disableMakerAsk = true
|
|
|
|
}
|
2021-05-11 04:47:45 +00:00
|
|
|
} else if b.Available.Float64() > s.sourceMarket.MinNotional {
|
2021-05-10 16:10:49 +00:00
|
|
|
hedgeQuota.QuoteAsset.Add(b.Available)
|
|
|
|
} else {
|
2021-05-11 04:53:32 +00:00
|
|
|
log.Warnf("%s maker ask disabled: insufficient quote balance %s", s.Symbol, b.String())
|
2021-03-21 03:16:15 +00:00
|
|
|
disableMakerAsk = true
|
|
|
|
}
|
|
|
|
}
|
2021-03-21 02:44:06 +00:00
|
|
|
|
2021-05-10 16:10:49 +00:00
|
|
|
// if max exposure position is configured, we should not:
|
|
|
|
// 1. place bid orders when we already bought too much
|
|
|
|
// 2. place ask orders when we already sold too much
|
|
|
|
if s.MaxExposurePosition > 0 {
|
2022-01-08 16:35:45 +00:00
|
|
|
pos := s.state.Position.GetBase()
|
2021-12-31 07:13:26 +00:00
|
|
|
|
2021-05-10 16:10:49 +00:00
|
|
|
if pos < -s.MaxExposurePosition {
|
|
|
|
// stop sell if we over-sell
|
|
|
|
disableMakerAsk = true
|
|
|
|
} else if pos > s.MaxExposurePosition {
|
|
|
|
// stop buy if we over buy
|
|
|
|
disableMakerBid = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 04:43:41 +00:00
|
|
|
if disableMakerAsk && disableMakerBid {
|
2021-05-17 13:33:55 +00:00
|
|
|
log.Warnf("%s bid/ask maker is disabled due to insufficient balances", s.Symbol)
|
2021-03-21 04:43:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-06 18:23:07 +00:00
|
|
|
bestBidPrice := bestBid.Price
|
2021-05-22 08:32:29 +00:00
|
|
|
bestAskPrice := bestAsk.Price
|
2021-05-17 12:03:42 +00:00
|
|
|
log.Infof("%s book ticker: best ask / best bid = %f / %f", s.Symbol, bestAskPrice.Float64(), bestBidPrice.Float64())
|
2021-05-10 16:10:49 +00:00
|
|
|
|
|
|
|
var submitOrders []types.SubmitOrder
|
2021-05-10 16:58:11 +00:00
|
|
|
var accumulativeBidQuantity, accumulativeAskQuantity fixedpoint.Value
|
2021-05-10 16:10:49 +00:00
|
|
|
var bidQuantity = s.Quantity
|
|
|
|
var askQuantity = s.Quantity
|
2021-05-17 12:03:42 +00:00
|
|
|
var bidMargin = s.BidMargin
|
|
|
|
var askMargin = s.AskMargin
|
2021-05-17 15:55:09 +00:00
|
|
|
var pips = s.Pips
|
2021-05-17 12:03:42 +00:00
|
|
|
|
|
|
|
if s.EnableBollBandMargin {
|
|
|
|
lastDownBand := s.boll.LastDownBand()
|
|
|
|
lastUpBand := s.boll.LastUpBand()
|
|
|
|
|
|
|
|
// when bid price is lower than the down band, then it's in the downtrend
|
|
|
|
// when ask price is higher than the up band, then it's in the uptrend
|
|
|
|
if bestBidPrice.Float64() < lastDownBand {
|
|
|
|
// ratio here should be greater than 1.00
|
|
|
|
ratio := lastDownBand / bestBidPrice.Float64()
|
|
|
|
|
|
|
|
// so that the original bid margin can be multiplied by 1.x
|
|
|
|
bollMargin := s.BollBandMargin.MulFloat64(ratio).Mul(s.BollBandMarginFactor)
|
|
|
|
|
|
|
|
log.Infof("%s bollband downtrend: adjusting ask margin %f + %f = %f",
|
|
|
|
s.Symbol,
|
|
|
|
askMargin.Float64(),
|
|
|
|
bollMargin.Float64(),
|
|
|
|
(askMargin + bollMargin).Float64())
|
|
|
|
|
|
|
|
askMargin = askMargin + bollMargin
|
2021-05-17 15:55:09 +00:00
|
|
|
pips = pips.MulFloat64(ratio)
|
2021-05-17 12:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if bestAskPrice.Float64() > lastUpBand {
|
|
|
|
// ratio here should be greater than 1.00
|
|
|
|
ratio := bestAskPrice.Float64() / lastUpBand
|
|
|
|
|
|
|
|
// so that the original bid margin can be multiplied by 1.x
|
|
|
|
bollMargin := s.BollBandMargin.MulFloat64(ratio).Mul(s.BollBandMarginFactor)
|
|
|
|
|
|
|
|
log.Infof("%s bollband uptrend adjusting bid margin %f + %f = %f",
|
|
|
|
s.Symbol,
|
|
|
|
bidMargin.Float64(),
|
|
|
|
bollMargin.Float64(),
|
|
|
|
(bidMargin + bollMargin).Float64())
|
|
|
|
|
|
|
|
bidMargin = bidMargin + bollMargin
|
2021-05-17 15:55:09 +00:00
|
|
|
pips = pips.MulFloat64(ratio)
|
2021-05-17 12:03:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-06 18:49:44 +00:00
|
|
|
bidPrice := bestBidPrice
|
|
|
|
askPrice := bestAskPrice
|
2021-03-21 02:44:06 +00:00
|
|
|
for i := 0; i < s.NumLayers; i++ {
|
2021-03-21 03:16:15 +00:00
|
|
|
// for maker bid orders
|
|
|
|
if !disableMakerBid {
|
2021-05-09 18:52:41 +00:00
|
|
|
if s.QuantityScale != nil {
|
|
|
|
qf, err := s.QuantityScale.Scale(i + 1)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("quantityScale error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-17 12:03:42 +00:00
|
|
|
log.Infof("%s scaling bid #%d quantity to %f", s.Symbol, i+1, qf)
|
2021-05-09 18:52:41 +00:00
|
|
|
|
|
|
|
// override the default bid quantity
|
|
|
|
bidQuantity = fixedpoint.NewFromFloat(qf)
|
|
|
|
}
|
|
|
|
|
2021-05-10 16:58:11 +00:00
|
|
|
accumulativeBidQuantity += bidQuantity
|
2021-06-06 18:49:44 +00:00
|
|
|
if s.UseDepthPrice {
|
|
|
|
bidPrice = aggregatePrice(sourceBook.SideBook(types.SideTypeBuy), accumulativeBidQuantity)
|
|
|
|
}
|
2021-05-17 15:55:09 +00:00
|
|
|
|
2021-06-06 18:49:44 +00:00
|
|
|
bidPrice = bidPrice.MulFloat64(1.0 - bidMargin.Float64())
|
2021-05-17 15:55:09 +00:00
|
|
|
if i > 0 && pips > 0 {
|
2022-01-05 03:34:07 +00:00
|
|
|
bidPrice -= pips.MulFloat64(float64(i) * s.makerMarket.TickSize)
|
2021-05-10 17:06:39 +00:00
|
|
|
}
|
2021-05-10 16:58:11 +00:00
|
|
|
|
2021-03-21 03:16:15 +00:00
|
|
|
if makerQuota.QuoteAsset.Lock(bidQuantity.Mul(bidPrice)) && hedgeQuota.BaseAsset.Lock(bidQuantity) {
|
|
|
|
// if we bought, then we need to sell the base from the hedge session
|
|
|
|
submitOrders = append(submitOrders, types.SubmitOrder{
|
|
|
|
Symbol: s.Symbol,
|
|
|
|
Type: types.OrderTypeLimit,
|
|
|
|
Side: types.SideTypeBuy,
|
|
|
|
Price: bidPrice.Float64(),
|
|
|
|
Quantity: bidQuantity.Float64(),
|
|
|
|
TimeInForce: "GTC",
|
|
|
|
GroupID: s.groupID,
|
|
|
|
})
|
|
|
|
|
|
|
|
makerQuota.Commit()
|
|
|
|
hedgeQuota.Commit()
|
|
|
|
} else {
|
|
|
|
makerQuota.Rollback()
|
|
|
|
hedgeQuota.Rollback()
|
|
|
|
}
|
2021-05-09 18:52:41 +00:00
|
|
|
|
|
|
|
if s.QuantityMultiplier > 0 {
|
|
|
|
bidQuantity = bidQuantity.Mul(s.QuantityMultiplier)
|
|
|
|
}
|
2021-03-21 02:44:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 03:16:15 +00:00
|
|
|
// for maker ask orders
|
|
|
|
if !disableMakerAsk {
|
2021-05-09 18:52:41 +00:00
|
|
|
if s.QuantityScale != nil {
|
|
|
|
qf, err := s.QuantityScale.Scale(i + 1)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("quantityScale error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-17 12:03:42 +00:00
|
|
|
log.Infof("%s scaling ask #%d quantity to %f", s.Symbol, i+1, qf)
|
|
|
|
|
2021-05-09 18:52:41 +00:00
|
|
|
// override the default bid quantity
|
|
|
|
askQuantity = fixedpoint.NewFromFloat(qf)
|
|
|
|
}
|
2021-05-10 16:58:11 +00:00
|
|
|
accumulativeAskQuantity += askQuantity
|
|
|
|
|
2021-06-06 18:49:44 +00:00
|
|
|
if s.UseDepthPrice {
|
|
|
|
askPrice = aggregatePrice(sourceBook.SideBook(types.SideTypeSell), accumulativeAskQuantity)
|
|
|
|
}
|
|
|
|
|
2021-05-17 12:03:42 +00:00
|
|
|
askPrice = askPrice.MulFloat64(1.0 + askMargin.Float64())
|
2021-05-17 15:55:09 +00:00
|
|
|
if i > 0 && pips > 0 {
|
2022-01-05 03:34:07 +00:00
|
|
|
askPrice += pips.MulFloat64(float64(i) * s.makerMarket.TickSize)
|
2021-05-10 17:06:39 +00:00
|
|
|
}
|
2021-05-09 18:52:41 +00:00
|
|
|
|
2021-03-21 03:16:15 +00:00
|
|
|
if makerQuota.BaseAsset.Lock(askQuantity) && hedgeQuota.QuoteAsset.Lock(askQuantity.Mul(askPrice)) {
|
|
|
|
// if we bought, then we need to sell the base from the hedge session
|
|
|
|
submitOrders = append(submitOrders, types.SubmitOrder{
|
|
|
|
Symbol: s.Symbol,
|
2021-06-06 18:03:58 +00:00
|
|
|
Market: s.makerMarket,
|
2021-03-21 03:16:15 +00:00
|
|
|
Type: types.OrderTypeLimit,
|
|
|
|
Side: types.SideTypeSell,
|
|
|
|
Price: askPrice.Float64(),
|
|
|
|
Quantity: askQuantity.Float64(),
|
|
|
|
TimeInForce: "GTC",
|
|
|
|
GroupID: s.groupID,
|
|
|
|
})
|
|
|
|
makerQuota.Commit()
|
|
|
|
hedgeQuota.Commit()
|
|
|
|
} else {
|
|
|
|
makerQuota.Rollback()
|
|
|
|
hedgeQuota.Rollback()
|
|
|
|
}
|
2021-05-09 18:52:41 +00:00
|
|
|
|
|
|
|
if s.QuantityMultiplier > 0 {
|
|
|
|
askQuantity = askQuantity.Mul(s.QuantityMultiplier)
|
|
|
|
}
|
2021-03-21 02:44:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(submitOrders) == 0 {
|
2021-05-17 13:33:55 +00:00
|
|
|
log.Warnf("no orders generated")
|
2021-03-21 02:44:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-12 10:58:20 +00:00
|
|
|
makerOrders, err := orderExecutionRouter.SubmitOrdersTo(ctx, s.MakerExchange, submitOrders...)
|
2021-03-21 02:44:06 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("order error: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.activeMakerOrders.Add(makerOrders...)
|
|
|
|
s.orderStore.Add(makerOrders...)
|
|
|
|
}
|
|
|
|
|
2021-05-30 06:46:48 +00:00
|
|
|
func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) {
|
2021-05-28 16:28:13 +00:00
|
|
|
side := types.SideTypeBuy
|
2021-03-21 02:44:06 +00:00
|
|
|
if pos == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-30 06:46:48 +00:00
|
|
|
quantity := fixedpoint.Abs(pos)
|
|
|
|
|
2021-03-21 02:44:06 +00:00
|
|
|
if pos < 0 {
|
|
|
|
side = types.SideTypeSell
|
|
|
|
}
|
|
|
|
|
|
|
|
lastPrice := s.lastPrice
|
2021-05-28 16:28:13 +00:00
|
|
|
sourceBook := s.book.CopyDepth(1)
|
2021-03-21 02:44:06 +00:00
|
|
|
switch side {
|
|
|
|
|
|
|
|
case types.SideTypeBuy:
|
2021-05-22 08:32:29 +00:00
|
|
|
if bestAsk, ok := sourceBook.BestAsk(); ok {
|
|
|
|
lastPrice = bestAsk.Price.Float64()
|
2021-03-21 02:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case types.SideTypeSell:
|
2021-05-22 08:32:29 +00:00
|
|
|
if bestBid, ok := sourceBook.BestBid(); ok {
|
|
|
|
lastPrice = bestBid.Price.Float64()
|
2021-03-21 02:44:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notional := quantity.MulFloat64(lastPrice)
|
|
|
|
if notional.Float64() <= s.sourceMarket.MinNotional {
|
2021-06-06 18:17:13 +00:00
|
|
|
log.Warnf("%s %f less than min notional, skipping hedge", s.Symbol, notional.Float64())
|
2021-03-21 02:44:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-10 12:13:23 +00:00
|
|
|
// adjust quantity according to the balances
|
|
|
|
account := s.sourceSession.Account
|
|
|
|
switch side {
|
|
|
|
|
|
|
|
case types.SideTypeBuy:
|
|
|
|
// check quote quantity
|
2021-05-10 12:22:33 +00:00
|
|
|
if quote, ok := account.Balance(s.sourceMarket.QuoteCurrency); ok {
|
2021-05-10 12:13:23 +00:00
|
|
|
if quote.Available < notional {
|
2021-05-17 11:17:22 +00:00
|
|
|
// adjust price to higher 0.1%, so that we can ensure that the order can be executed
|
|
|
|
quantity = bbgo.AdjustQuantityByMaxAmount(quantity, fixedpoint.NewFromFloat(lastPrice*1.001), quote.Available)
|
2021-12-26 07:45:39 +00:00
|
|
|
quantity = s.sourceMarket.TruncateQuantity(quantity)
|
2021-05-10 12:13:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case types.SideTypeSell:
|
|
|
|
// check quote quantity
|
2021-05-10 12:22:33 +00:00
|
|
|
if base, ok := account.Balance(s.sourceMarket.BaseCurrency); ok {
|
2021-05-10 12:13:23 +00:00
|
|
|
if base.Available < quantity {
|
|
|
|
quantity = base.Available
|
|
|
|
}
|
|
|
|
}
|
2022-01-08 16:30:18 +00:00
|
|
|
}
|
2021-05-10 12:13:23 +00:00
|
|
|
|
2022-01-08 16:30:18 +00:00
|
|
|
// truncate quantity for the supported precision
|
|
|
|
quantity = s.sourceMarket.TruncateQuantity(quantity)
|
|
|
|
|
2022-01-09 07:39:59 +00:00
|
|
|
if notional.Float64() <= s.sourceMarket.MinNotional*1.02 {
|
2022-01-09 03:33:34 +00:00
|
|
|
log.Warnf("the adjusted amount %f is less than minimal notional %f, skipping hedge", notional.Float64(), s.sourceMarket.MinNotional)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-09 07:39:59 +00:00
|
|
|
if quantity.Float64() <= s.sourceMarket.MinQuantity*1.0 {
|
2022-01-09 03:33:34 +00:00
|
|
|
log.Warnf("the adjusted quantity %f is less than minimal quantity %f, skipping hedge", quantity.Float64(), s.sourceMarket.MinQuantity)
|
2022-01-08 16:30:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-09 03:33:34 +00:00
|
|
|
if !s.hedgeErrorLimiter.Allow() {
|
|
|
|
log.Warn("rate limit hit, not allowed to hedge again, skip")
|
2022-01-08 16:30:18 +00:00
|
|
|
return
|
2021-05-10 12:13:23 +00:00
|
|
|
}
|
|
|
|
|
2021-05-28 16:28:13 +00:00
|
|
|
log.Infof("submitting %s hedge order %s %f", s.Symbol, side.String(), quantity.Float64())
|
2021-05-15 17:16:03 +00:00
|
|
|
s.Notifiability.Notify("Submitting %s hedge order %s %f", s.Symbol, side.String(), quantity.Float64())
|
2021-03-21 02:44:06 +00:00
|
|
|
orderExecutor := &bbgo.ExchangeOrderExecutor{Session: s.sourceSession}
|
|
|
|
returnOrders, err := orderExecutor.SubmitOrders(ctx, types.SubmitOrder{
|
2021-06-06 18:03:58 +00:00
|
|
|
Market: s.sourceMarket,
|
2021-03-21 02:44:06 +00:00
|
|
|
Symbol: s.Symbol,
|
|
|
|
Type: types.OrderTypeMarket,
|
|
|
|
Side: side,
|
|
|
|
Quantity: quantity.Float64(),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2022-01-09 03:33:34 +00:00
|
|
|
s.hedgeErrorLimiter.Reserve()
|
2021-03-21 02:44:06 +00:00
|
|
|
log.WithError(err).Errorf("market order submit error: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-30 06:46:48 +00:00
|
|
|
// if it's selling, than we should add positive position
|
|
|
|
if side == types.SideTypeSell {
|
|
|
|
s.state.CoveredPosition.AtomicAdd(quantity)
|
|
|
|
} else {
|
|
|
|
s.state.CoveredPosition.AtomicAdd(-quantity)
|
|
|
|
}
|
|
|
|
|
2021-03-21 02:44:06 +00:00
|
|
|
s.orderStore.Add(returnOrders...)
|
|
|
|
}
|
|
|
|
|
2021-05-09 18:52:41 +00:00
|
|
|
func (s *Strategy) Validate() error {
|
|
|
|
if s.Quantity == 0 || s.QuantityScale == nil {
|
|
|
|
return errors.New("quantity or quantityScale can not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.QuantityMultiplier != 0 && s.QuantityMultiplier < 0 {
|
|
|
|
return errors.New("quantityMultiplier can not be a negative number")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.Symbol) == 0 {
|
|
|
|
return errors.New("symbol is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-22 09:44:20 +00:00
|
|
|
func (s *Strategy) LoadState() error {
|
|
|
|
var state State
|
|
|
|
|
|
|
|
// load position
|
|
|
|
if err := s.Persistence.Load(&state, ID, s.Symbol, stateKey); err != nil {
|
|
|
|
if err != service.ErrPersistenceNotExists {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.state = &State{}
|
|
|
|
} else {
|
|
|
|
s.state = &state
|
|
|
|
}
|
|
|
|
|
2021-06-26 12:11:20 +00:00
|
|
|
// if position is nil, we need to allocate a new position for calculation
|
|
|
|
if s.state.Position == nil {
|
2021-12-11 11:16:16 +00:00
|
|
|
s.state.Position = types.NewPositionFromMarket(s.makerMarket)
|
2021-06-26 12:11:20 +00:00
|
|
|
}
|
2022-01-06 15:27:06 +00:00
|
|
|
s.state.Position.Market = s.makerMarket
|
2021-06-26 12:11:20 +00:00
|
|
|
|
2021-10-09 05:30:16 +00:00
|
|
|
s.state.ProfitStats.Symbol = s.makerMarket.Symbol
|
|
|
|
s.state.ProfitStats.BaseCurrency = s.makerMarket.BaseCurrency
|
|
|
|
s.state.ProfitStats.QuoteCurrency = s.makerMarket.QuoteCurrency
|
2021-06-26 12:11:20 +00:00
|
|
|
s.state.ProfitStats.MakerExchange = s.makerSession.ExchangeName
|
|
|
|
if s.state.ProfitStats.AccumulatedSince == 0 {
|
|
|
|
s.state.ProfitStats.AccumulatedSince = time.Now().Unix()
|
|
|
|
}
|
|
|
|
|
2021-05-22 09:44:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) SaveState() error {
|
|
|
|
if err := s.Persistence.Save(s.state, ID, s.Symbol, stateKey); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
2021-10-09 05:33:10 +00:00
|
|
|
log.Infof("%s state is saved => %+v", ID, s.state)
|
2021-05-22 09:44:20 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-12 10:58:20 +00:00
|
|
|
func (s *Strategy) CrossRun(ctx context.Context, orderExecutionRouter bbgo.OrderExecutionRouter, sessions map[string]*bbgo.ExchangeSession) error {
|
2021-05-17 12:03:42 +00:00
|
|
|
if s.BollBandInterval == "" {
|
|
|
|
s.BollBandInterval = types.Interval1m
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.BollBandMarginFactor == 0 {
|
|
|
|
s.BollBandMarginFactor = fixedpoint.NewFromFloat(1.0)
|
|
|
|
}
|
|
|
|
if s.BollBandMargin == 0 {
|
|
|
|
s.BollBandMargin = fixedpoint.NewFromFloat(0.001)
|
|
|
|
}
|
|
|
|
|
2021-03-21 02:44:06 +00:00
|
|
|
// configure default values
|
|
|
|
if s.UpdateInterval == 0 {
|
|
|
|
s.UpdateInterval = types.Duration(time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.HedgeInterval == 0 {
|
|
|
|
s.HedgeInterval = types.Duration(10 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.NumLayers == 0 {
|
|
|
|
s.NumLayers = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.BidMargin == 0 {
|
|
|
|
if s.Margin != 0 {
|
|
|
|
s.BidMargin = s.Margin
|
|
|
|
} else {
|
|
|
|
s.BidMargin = defaultMargin
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.AskMargin == 0 {
|
|
|
|
if s.Margin != 0 {
|
|
|
|
s.AskMargin = s.Margin
|
|
|
|
} else {
|
|
|
|
s.AskMargin = defaultMargin
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-09 03:33:34 +00:00
|
|
|
s.hedgeErrorLimiter = rate.NewLimiter(rate.Every(time.Minute), 1)
|
|
|
|
|
2021-03-21 02:44:06 +00:00
|
|
|
// configure sessions
|
|
|
|
sourceSession, ok := sessions[s.SourceExchange]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("source exchange session %s is not defined", s.SourceExchange)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.sourceSession = sourceSession
|
|
|
|
|
|
|
|
makerSession, ok := sessions[s.MakerExchange]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("maker exchange session %s is not defined", s.MakerExchange)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.makerSession = makerSession
|
|
|
|
|
|
|
|
s.sourceMarket, ok = s.sourceSession.Market(s.Symbol)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("source session market %s is not defined", s.Symbol)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.makerMarket, ok = s.makerSession.Market(s.Symbol)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("maker session market %s is not defined", s.Symbol)
|
|
|
|
}
|
|
|
|
|
2021-05-17 12:03:42 +00:00
|
|
|
standardIndicatorSet, ok := s.sourceSession.StandardIndicatorSet(s.Symbol)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%s standard indicator set not found", s.Symbol)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.boll = standardIndicatorSet.BOLL(types.IntervalWindow{
|
|
|
|
Interval: s.BollBandInterval,
|
|
|
|
Window: 21,
|
|
|
|
}, 1.0)
|
|
|
|
|
2021-03-21 02:44:06 +00:00
|
|
|
// restore state
|
2021-03-21 04:43:41 +00:00
|
|
|
instanceID := fmt.Sprintf("%s-%s", ID, s.Symbol)
|
2021-03-22 09:27:07 +00:00
|
|
|
s.groupID = max.GenerateGroupID(instanceID)
|
2021-03-21 02:44:06 +00:00
|
|
|
log.Infof("using group id %d from fnv(%s)", s.groupID, instanceID)
|
|
|
|
|
2021-05-22 09:44:20 +00:00
|
|
|
if err := s.LoadState(); err != nil {
|
|
|
|
return err
|
2021-03-21 02:44:06 +00:00
|
|
|
} else {
|
2021-12-31 17:34:48 +00:00
|
|
|
s.Notify("xmaker: %s position is restored", s.Symbol, s.state.Position)
|
2021-03-21 02:44:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-16 09:58:51 +00:00
|
|
|
if s.makerSession.MakerFeeRate > 0 || s.makerSession.TakerFeeRate > 0 {
|
2021-12-11 11:16:16 +00:00
|
|
|
s.state.Position.SetExchangeFeeRate(types.ExchangeName(s.MakerExchange), types.ExchangeFee{
|
2021-05-16 09:58:51 +00:00
|
|
|
MakerFeeRate: s.makerSession.MakerFeeRate,
|
|
|
|
TakerFeeRate: s.makerSession.TakerFeeRate,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.sourceSession.MakerFeeRate > 0 || s.sourceSession.TakerFeeRate > 0 {
|
2021-12-11 11:16:16 +00:00
|
|
|
s.state.Position.SetExchangeFeeRate(types.ExchangeName(s.SourceExchange), types.ExchangeFee{
|
2021-05-16 09:58:51 +00:00
|
|
|
MakerFeeRate: s.sourceSession.MakerFeeRate,
|
|
|
|
TakerFeeRate: s.sourceSession.TakerFeeRate,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-21 02:44:06 +00:00
|
|
|
s.book = types.NewStreamBook(s.Symbol)
|
2021-05-27 19:13:50 +00:00
|
|
|
s.book.BindStream(s.sourceSession.MarketDataStream)
|
2021-03-21 02:44:06 +00:00
|
|
|
|
2022-01-06 16:10:40 +00:00
|
|
|
s.activeMakerOrders = bbgo.NewLocalActiveOrderBook(s.Symbol)
|
2021-05-27 06:45:06 +00:00
|
|
|
s.activeMakerOrders.BindStream(s.makerSession.UserDataStream)
|
2021-03-21 02:44:06 +00:00
|
|
|
|
|
|
|
s.orderStore = bbgo.NewOrderStore(s.Symbol)
|
2021-05-27 06:45:06 +00:00
|
|
|
s.orderStore.BindStream(s.sourceSession.UserDataStream)
|
|
|
|
s.orderStore.BindStream(s.makerSession.UserDataStream)
|
2021-03-21 02:44:06 +00:00
|
|
|
|
2021-12-26 04:10:10 +00:00
|
|
|
s.tradeCollector = bbgo.NewTradeCollector(s.Symbol, s.state.Position, s.orderStore)
|
2021-12-26 16:12:35 +00:00
|
|
|
|
|
|
|
if s.NotifyTrade {
|
|
|
|
s.tradeCollector.OnTrade(func(trade types.Trade) {
|
|
|
|
s.Notifiability.Notify(trade)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-26 04:10:10 +00:00
|
|
|
s.tradeCollector.OnTrade(func(trade types.Trade) {
|
|
|
|
c := trade.PositionChange()
|
2021-12-31 06:23:02 +00:00
|
|
|
if trade.Exchange == s.sourceSession.ExchangeName {
|
|
|
|
s.state.CoveredPosition.AtomicAdd(c)
|
|
|
|
}
|
|
|
|
|
2021-12-26 04:10:10 +00:00
|
|
|
s.state.ProfitStats.AddTrade(trade)
|
|
|
|
|
|
|
|
if err := s.SaveState(); err != nil {
|
|
|
|
log.WithError(err).Error("save state error")
|
|
|
|
}
|
|
|
|
})
|
2021-12-26 18:59:55 +00:00
|
|
|
s.tradeCollector.OnProfit(func(trade types.Trade, profit fixedpoint.Value, netProfit fixedpoint.Value) {
|
|
|
|
p := bbgo.Profit{
|
|
|
|
Symbol: s.Symbol,
|
|
|
|
Profit: profit,
|
|
|
|
NetProfit: netProfit,
|
|
|
|
TradeAmount: fixedpoint.NewFromFloat(trade.QuoteQuantity),
|
|
|
|
ProfitMargin: profit.DivFloat64(trade.QuoteQuantity),
|
|
|
|
NetProfitMargin: netProfit.DivFloat64(trade.QuoteQuantity),
|
|
|
|
QuoteCurrency: s.state.Position.QuoteCurrency,
|
|
|
|
BaseCurrency: s.state.Position.BaseCurrency,
|
|
|
|
Time: trade.Time.Time(),
|
|
|
|
}
|
|
|
|
s.state.ProfitStats.AddProfit(p)
|
|
|
|
s.Notify(&p)
|
|
|
|
})
|
2021-12-26 04:10:10 +00:00
|
|
|
s.tradeCollector.OnPositionUpdate(func(position *types.Position) {
|
|
|
|
s.Notifiability.Notify(position)
|
|
|
|
})
|
2022-01-09 07:39:59 +00:00
|
|
|
s.tradeCollector.OnRecover(func(trade types.Trade) {
|
|
|
|
s.Notifiability.Notify("Recover trade", trade)
|
|
|
|
})
|
2021-12-26 16:51:57 +00:00
|
|
|
s.tradeCollector.BindStream(s.sourceSession.UserDataStream)
|
|
|
|
s.tradeCollector.BindStream(s.makerSession.UserDataStream)
|
2021-12-26 04:10:10 +00:00
|
|
|
|
2021-03-21 02:44:06 +00:00
|
|
|
s.stopC = make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
2022-01-06 16:14:24 +00:00
|
|
|
posTicker := time.NewTicker(util.MillisecondsJitter(s.HedgeInterval.Duration(), 200))
|
2021-03-21 02:44:06 +00:00
|
|
|
defer posTicker.Stop()
|
|
|
|
|
2022-01-06 16:14:24 +00:00
|
|
|
quoteTicker := time.NewTicker(util.MillisecondsJitter(s.UpdateInterval.Duration(), 200))
|
2021-05-09 12:03:06 +00:00
|
|
|
defer quoteTicker.Stop()
|
|
|
|
|
2021-10-14 00:53:44 +00:00
|
|
|
reportTicker := time.NewTicker(time.Hour)
|
|
|
|
defer reportTicker.Stop()
|
|
|
|
|
2022-01-06 17:03:12 +00:00
|
|
|
tradeScanInterval := 20 * time.Minute
|
|
|
|
tradeScanTicker := time.NewTicker(tradeScanInterval)
|
|
|
|
defer tradeScanTicker.Stop()
|
|
|
|
|
2021-05-10 05:18:57 +00:00
|
|
|
defer func() {
|
|
|
|
if err := s.makerSession.Exchange.CancelOrders(context.Background(), s.activeMakerOrders.Orders()...); err != nil {
|
|
|
|
log.WithError(err).Errorf("can not cancel %s orders", s.Symbol)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-03-21 02:44:06 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-s.stopC:
|
2021-05-09 18:17:19 +00:00
|
|
|
log.Warnf("%s maker goroutine stopped, due to the stop signal", s.Symbol)
|
2021-03-21 02:44:06 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
2021-05-09 18:17:19 +00:00
|
|
|
log.Warnf("%s maker goroutine stopped, due to the cancelled context", s.Symbol)
|
2021-03-21 02:44:06 +00:00
|
|
|
return
|
|
|
|
|
2021-05-09 12:03:06 +00:00
|
|
|
case <-quoteTicker.C:
|
2021-05-12 10:58:20 +00:00
|
|
|
s.updateQuote(ctx, orderExecutionRouter)
|
2021-03-21 02:44:06 +00:00
|
|
|
|
2021-10-14 00:53:44 +00:00
|
|
|
case <-reportTicker.C:
|
|
|
|
s.Notifiability.Notify(&s.state.ProfitStats)
|
|
|
|
|
2022-01-06 17:03:12 +00:00
|
|
|
case <-tradeScanTicker.C:
|
|
|
|
log.Infof("scanning trades from %s ago...", tradeScanInterval)
|
2022-01-09 07:39:59 +00:00
|
|
|
startTime := time.Now().Add(-tradeScanInterval)
|
|
|
|
if err := s.tradeCollector.Recover(ctx, s.sourceSession.Exchange.(types.ExchangeTradeHistoryService), s.Symbol, startTime); err != nil {
|
2022-01-06 17:03:12 +00:00
|
|
|
log.WithError(err).Errorf("query trades error")
|
|
|
|
}
|
|
|
|
|
2021-03-21 02:44:06 +00:00
|
|
|
case <-posTicker.C:
|
2021-12-31 07:13:26 +00:00
|
|
|
// For positive position and positive covered position:
|
|
|
|
// uncover position = +5 - +3 (covered position) = 2
|
|
|
|
//
|
|
|
|
// For positive position and negative covered position:
|
|
|
|
// uncover position = +5 - (-3) (covered position) = 8
|
|
|
|
//
|
|
|
|
// meaning we bought 5 on MAX and sent buy order with 3 on binance
|
|
|
|
//
|
|
|
|
// For negative position:
|
2021-05-30 06:46:48 +00:00
|
|
|
// uncover position = -5 - -3 (covered position) = -2
|
2021-12-26 16:51:57 +00:00
|
|
|
s.tradeCollector.Process()
|
|
|
|
|
2022-01-08 16:35:45 +00:00
|
|
|
position := s.state.Position.GetBase()
|
2021-12-31 07:13:26 +00:00
|
|
|
|
2021-05-30 06:46:48 +00:00
|
|
|
uncoverPosition := position - s.state.CoveredPosition.AtomicLoad()
|
|
|
|
absPos := math.Abs(uncoverPosition.Float64())
|
2021-05-28 16:28:13 +00:00
|
|
|
if !s.DisableHedge && absPos > s.sourceMarket.MinQuantity {
|
2021-12-31 07:26:51 +00:00
|
|
|
log.Infof("%s base position %f coveredPosition: %f uncoverPosition: %f",
|
|
|
|
s.Symbol,
|
|
|
|
position.Float64(),
|
|
|
|
s.state.CoveredPosition.AtomicLoad().Float64(),
|
|
|
|
uncoverPosition.Float64(),
|
|
|
|
)
|
|
|
|
|
2021-05-30 06:46:48 +00:00
|
|
|
s.Hedge(ctx, -uncoverPosition)
|
2021-03-21 02:44:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
s.Graceful.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
close(s.stopC)
|
|
|
|
|
2021-05-22 09:44:20 +00:00
|
|
|
// wait for the quoter to stop
|
2021-05-09 18:17:19 +00:00
|
|
|
time.Sleep(s.UpdateInterval.Duration())
|
2021-05-09 11:04:44 +00:00
|
|
|
|
2022-01-06 16:10:40 +00:00
|
|
|
shutdownCtx, cancelShutdown := context.WithTimeout(context.TODO(), time.Minute)
|
|
|
|
defer cancelShutdown()
|
2021-05-10 05:18:57 +00:00
|
|
|
|
2022-01-06 16:10:40 +00:00
|
|
|
if err := s.activeMakerOrders.GracefulCancel(shutdownCtx, s.makerSession.Exchange); err != nil {
|
|
|
|
log.WithError(err).Errorf("graceful cancel error")
|
2021-05-09 10:48:25 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 09:44:20 +00:00
|
|
|
if err := s.SaveState(); err != nil {
|
2021-03-21 02:44:06 +00:00
|
|
|
log.WithError(err).Errorf("can not save state: %+v", s.state)
|
|
|
|
} else {
|
2021-12-31 07:13:26 +00:00
|
|
|
s.Notify("%s: %s position is saved", ID, s.Symbol, s.state.Position)
|
2021-03-21 02:44:06 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|