2022-09-20 02:32:41 +00:00
|
|
|
package irr
|
2022-09-19 06:45:13 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
2022-10-04 06:59:09 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/data/tsv"
|
2022-09-19 06:45:13 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/datatype/floats"
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
|
|
"github.com/c9s/bbgo/pkg/indicator"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2022-10-11 13:49:41 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-10-17 13:34:55 +00:00
|
|
|
"go.uber.org/atomic"
|
2022-10-04 06:59:09 +00:00
|
|
|
"os"
|
|
|
|
"sync"
|
2022-09-19 06:45:13 +00:00
|
|
|
)
|
|
|
|
|
2022-09-28 09:07:13 +00:00
|
|
|
const ID = "irr"
|
2022-09-19 06:45:13 +00:00
|
|
|
|
|
|
|
var one = fixedpoint.One
|
|
|
|
var zero = fixedpoint.Zero
|
2022-10-11 13:49:41 +00:00
|
|
|
var Fee = 0.000 // taker fee % * 2, for upper bound
|
2022-09-19 06:45:13 +00:00
|
|
|
|
|
|
|
var log = logrus.WithField("strategy", ID)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
bbgo.RegisterStrategy(ID, &Strategy{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type Strategy struct {
|
|
|
|
Environment *bbgo.Environment
|
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
Market types.Market
|
|
|
|
|
|
|
|
types.IntervalWindow
|
|
|
|
|
|
|
|
// persistence fields
|
|
|
|
Position *types.Position `persistence:"position"`
|
|
|
|
ProfitStats *types.ProfitStats `persistence:"profit_stats"`
|
|
|
|
TradeStats *types.TradeStats `persistence:"trade_stats"`
|
|
|
|
|
|
|
|
activeOrders *bbgo.ActiveOrderBook
|
|
|
|
|
|
|
|
ExitMethods bbgo.ExitMethodSet `json:"exits"`
|
|
|
|
|
|
|
|
session *bbgo.ExchangeSession
|
|
|
|
orderExecutor *bbgo.GeneralOrderExecutor
|
|
|
|
|
|
|
|
bbgo.QuantityOrAmount
|
2022-10-11 13:49:41 +00:00
|
|
|
|
2022-10-17 14:26:33 +00:00
|
|
|
Interval int `json:"hftInterval"`
|
|
|
|
NR bool `json:"NR"`
|
|
|
|
MR bool `json:"MR"`
|
2022-10-11 13:49:41 +00:00
|
|
|
|
|
|
|
// for back-test
|
|
|
|
Nrr *NRR
|
2022-10-17 13:34:55 +00:00
|
|
|
Ma *indicator.SMA
|
|
|
|
// realtime book ticker to submit order
|
|
|
|
obBuyPrice *atomic.Float64
|
|
|
|
obSellPrice *atomic.Float64
|
|
|
|
// for negative return rate
|
|
|
|
openPrice float64
|
|
|
|
closePrice float64
|
|
|
|
rtNr *types.Queue
|
|
|
|
// for moving average reversion
|
|
|
|
rtMaFast *types.Queue
|
|
|
|
rtMaSlow *types.Queue
|
|
|
|
rtMr *types.Queue
|
|
|
|
|
|
|
|
// for final alpha (Nr+Mr)/2
|
|
|
|
rtWeight *types.Queue
|
|
|
|
stopC chan struct{}
|
2022-09-19 06:45:13 +00:00
|
|
|
|
|
|
|
// StrategyController
|
|
|
|
bbgo.StrategyController
|
|
|
|
|
2022-10-04 06:59:09 +00:00
|
|
|
AccountValueCalculator *bbgo.AccountValueCalculator
|
|
|
|
|
|
|
|
// whether to draw graph or not by the end of backtest
|
|
|
|
DrawGraph bool `json:"drawGraph"`
|
|
|
|
GraphPNLPath string `json:"graphPNLPath"`
|
|
|
|
GraphCumPNLPath string `json:"graphCumPNLPath"`
|
|
|
|
|
|
|
|
// for position
|
|
|
|
buyPrice float64 `persistence:"buy_price"`
|
|
|
|
sellPrice float64 `persistence:"sell_price"`
|
|
|
|
highestPrice float64 `persistence:"highest_price"`
|
|
|
|
lowestPrice float64 `persistence:"lowest_price"`
|
|
|
|
|
|
|
|
// Accumulated profit report
|
|
|
|
AccumulatedProfitReport *AccumulatedProfitReport `json:"accumulatedProfitReport"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// AccumulatedProfitReport For accumulated profit report output
|
|
|
|
type AccumulatedProfitReport struct {
|
|
|
|
// AccumulatedProfitMAWindow Accumulated profit SMA window, in number of trades
|
|
|
|
AccumulatedProfitMAWindow int `json:"accumulatedProfitMAWindow"`
|
|
|
|
|
|
|
|
// IntervalWindow interval window, in days
|
|
|
|
IntervalWindow int `json:"intervalWindow"`
|
|
|
|
|
|
|
|
// NumberOfInterval How many intervals to output to TSV
|
|
|
|
NumberOfInterval int `json:"NumberOfInterval"`
|
|
|
|
|
|
|
|
// TsvReportPath The path to output report to
|
|
|
|
TsvReportPath string `json:"tsvReportPath"`
|
|
|
|
|
|
|
|
// AccumulatedDailyProfitWindow The window to sum up the daily profit, in days
|
|
|
|
AccumulatedDailyProfitWindow int `json:"accumulatedDailyProfitWindow"`
|
|
|
|
|
|
|
|
// Accumulated profit
|
|
|
|
accumulatedProfit fixedpoint.Value
|
|
|
|
accumulatedProfitPerDay floats.Slice
|
|
|
|
previousAccumulatedProfit fixedpoint.Value
|
|
|
|
|
|
|
|
// Accumulated profit MA
|
|
|
|
accumulatedProfitMA *indicator.SMA
|
|
|
|
accumulatedProfitMAPerDay floats.Slice
|
|
|
|
|
|
|
|
// Daily profit
|
|
|
|
dailyProfit floats.Slice
|
|
|
|
|
|
|
|
// Accumulated fee
|
|
|
|
accumulatedFee fixedpoint.Value
|
|
|
|
accumulatedFeePerDay floats.Slice
|
|
|
|
|
|
|
|
// Win ratio
|
|
|
|
winRatioPerDay floats.Slice
|
|
|
|
|
|
|
|
// Profit factor
|
|
|
|
profitFactorPerDay floats.Slice
|
|
|
|
|
|
|
|
// Trade number
|
|
|
|
dailyTrades floats.Slice
|
|
|
|
accumulatedTrades int
|
|
|
|
previousAccumulatedTrades int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AccumulatedProfitReport) Initialize() {
|
|
|
|
if r.AccumulatedProfitMAWindow <= 0 {
|
|
|
|
r.AccumulatedProfitMAWindow = 60
|
|
|
|
}
|
|
|
|
if r.IntervalWindow <= 0 {
|
|
|
|
r.IntervalWindow = 7
|
|
|
|
}
|
|
|
|
if r.AccumulatedDailyProfitWindow <= 0 {
|
|
|
|
r.AccumulatedDailyProfitWindow = 7
|
|
|
|
}
|
|
|
|
if r.NumberOfInterval <= 0 {
|
|
|
|
r.NumberOfInterval = 1
|
|
|
|
}
|
|
|
|
r.accumulatedProfitMA = &indicator.SMA{IntervalWindow: types.IntervalWindow{Interval: types.Interval1d, Window: r.AccumulatedProfitMAWindow}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AccumulatedProfitReport) RecordProfit(profit fixedpoint.Value) {
|
|
|
|
r.accumulatedProfit = r.accumulatedProfit.Add(profit)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AccumulatedProfitReport) RecordTrade(fee fixedpoint.Value) {
|
|
|
|
r.accumulatedFee = r.accumulatedFee.Add(fee)
|
|
|
|
r.accumulatedTrades += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AccumulatedProfitReport) DailyUpdate(tradeStats *types.TradeStats) {
|
|
|
|
// Daily profit
|
|
|
|
r.dailyProfit.Update(r.accumulatedProfit.Sub(r.previousAccumulatedProfit).Float64())
|
|
|
|
r.previousAccumulatedProfit = r.accumulatedProfit
|
|
|
|
|
|
|
|
// Accumulated profit
|
|
|
|
r.accumulatedProfitPerDay.Update(r.accumulatedProfit.Float64())
|
|
|
|
|
|
|
|
// Accumulated profit MA
|
|
|
|
r.accumulatedProfitMA.Update(r.accumulatedProfit.Float64())
|
|
|
|
r.accumulatedProfitMAPerDay.Update(r.accumulatedProfitMA.Last())
|
|
|
|
|
|
|
|
// Accumulated Fee
|
|
|
|
r.accumulatedFeePerDay.Update(r.accumulatedFee.Float64())
|
|
|
|
|
|
|
|
// Win ratio
|
|
|
|
r.winRatioPerDay.Update(tradeStats.WinningRatio.Float64())
|
|
|
|
|
|
|
|
// Profit factor
|
|
|
|
r.profitFactorPerDay.Update(tradeStats.ProfitFactor.Float64())
|
|
|
|
|
|
|
|
// Daily trades
|
|
|
|
r.dailyTrades.Update(float64(r.accumulatedTrades - r.previousAccumulatedTrades))
|
|
|
|
r.previousAccumulatedTrades = r.accumulatedTrades
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output Accumulated profit report to a TSV file
|
|
|
|
func (r *AccumulatedProfitReport) Output(symbol string) {
|
|
|
|
if r.TsvReportPath != "" {
|
|
|
|
tsvwiter, err := tsv.AppendWriterFile(r.TsvReportPath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer tsvwiter.Close()
|
|
|
|
// Output symbol, total acc. profit, acc. profit 60MA, interval acc. profit, fee, win rate, profit factor
|
|
|
|
_ = tsvwiter.Write([]string{"#", "Symbol", "accumulatedProfit", "accumulatedProfitMA", fmt.Sprintf("%dd profit", r.AccumulatedDailyProfitWindow), "accumulatedFee", "winRatio", "profitFactor", "60D trades"})
|
|
|
|
for i := 0; i <= r.NumberOfInterval-1; i++ {
|
|
|
|
accumulatedProfit := r.accumulatedProfitPerDay.Index(r.IntervalWindow * i)
|
|
|
|
accumulatedProfitStr := fmt.Sprintf("%f", accumulatedProfit)
|
|
|
|
accumulatedProfitMA := r.accumulatedProfitMAPerDay.Index(r.IntervalWindow * i)
|
|
|
|
accumulatedProfitMAStr := fmt.Sprintf("%f", accumulatedProfitMA)
|
|
|
|
intervalAccumulatedProfit := r.dailyProfit.Tail(r.AccumulatedDailyProfitWindow+r.IntervalWindow*i).Sum() - r.dailyProfit.Tail(r.IntervalWindow*i).Sum()
|
|
|
|
intervalAccumulatedProfitStr := fmt.Sprintf("%f", intervalAccumulatedProfit)
|
|
|
|
accumulatedFee := fmt.Sprintf("%f", r.accumulatedFeePerDay.Index(r.IntervalWindow*i))
|
|
|
|
winRatio := fmt.Sprintf("%f", r.winRatioPerDay.Index(r.IntervalWindow*i))
|
|
|
|
profitFactor := fmt.Sprintf("%f", r.profitFactorPerDay.Index(r.IntervalWindow*i))
|
|
|
|
trades := r.dailyTrades.Tail(60+r.IntervalWindow*i).Sum() - r.dailyTrades.Tail(r.IntervalWindow*i).Sum()
|
|
|
|
tradesStr := fmt.Sprintf("%f", trades)
|
|
|
|
|
|
|
|
_ = tsvwiter.Write([]string{fmt.Sprintf("%d", i+1), symbol, accumulatedProfitStr, accumulatedProfitMAStr, intervalAccumulatedProfitStr, accumulatedFee, winRatio, profitFactor, tradesStr})
|
|
|
|
}
|
|
|
|
}
|
2022-09-19 06:45:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
|
|
|
|
if !bbgo.IsBackTesting {
|
2022-10-17 13:34:55 +00:00
|
|
|
session.Subscribe(types.AggTradeChannel, s.Symbol, types.SubscribeOptions{})
|
2022-10-11 13:49:41 +00:00
|
|
|
session.Subscribe(types.BookTickerChannel, s.Symbol, types.SubscribeOptions{})
|
2022-09-19 06:45:13 +00:00
|
|
|
}
|
2022-10-17 13:34:55 +00:00
|
|
|
//session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
|
2022-09-19 06:45:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) ID() string {
|
|
|
|
return ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) InstanceID() string {
|
|
|
|
return fmt.Sprintf("%s:%s", ID, s.Symbol)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
|
|
|
|
var instanceID = s.InstanceID()
|
|
|
|
|
|
|
|
if s.Position == nil {
|
|
|
|
s.Position = types.NewPositionFromMarket(s.Market)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.ProfitStats == nil {
|
|
|
|
s.ProfitStats = types.NewProfitStats(s.Market)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.TradeStats == nil {
|
|
|
|
s.TradeStats = types.NewTradeStats(s.Symbol)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2022-10-17 13:34:55 +00:00
|
|
|
_ = s.orderExecutor.ClosePosition(ctx, fixedpoint.One)
|
2022-09-19 06:45:13 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// initial required information
|
|
|
|
s.session = session
|
|
|
|
|
2022-10-04 06:59:09 +00:00
|
|
|
// Set fee rate
|
|
|
|
if s.session.MakerFeeRate.Sign() > 0 || s.session.TakerFeeRate.Sign() > 0 {
|
|
|
|
s.Position.SetExchangeFeeRate(s.session.ExchangeName, types.ExchangeFee{
|
|
|
|
MakerFeeRate: s.session.MakerFeeRate,
|
|
|
|
TakerFeeRate: s.session.TakerFeeRate,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-19 06:45:13 +00:00
|
|
|
s.orderExecutor = bbgo.NewGeneralOrderExecutor(session, s.Symbol, ID, instanceID, s.Position)
|
|
|
|
s.orderExecutor.BindEnvironment(s.Environment)
|
|
|
|
s.orderExecutor.BindProfitStats(s.ProfitStats)
|
|
|
|
s.orderExecutor.BindTradeStats(s.TradeStats)
|
|
|
|
|
2022-10-04 06:59:09 +00:00
|
|
|
// AccountValueCalculator
|
|
|
|
s.AccountValueCalculator = bbgo.NewAccountValueCalculator(s.session, s.Market.QuoteCurrency)
|
|
|
|
|
|
|
|
// Accumulated profit report
|
|
|
|
if bbgo.IsBackTesting {
|
|
|
|
if s.AccumulatedProfitReport == nil {
|
|
|
|
s.AccumulatedProfitReport = &AccumulatedProfitReport{}
|
|
|
|
}
|
|
|
|
s.AccumulatedProfitReport.Initialize()
|
|
|
|
s.orderExecutor.TradeCollector().OnProfit(func(trade types.Trade, profit *types.Profit) {
|
|
|
|
if profit == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.AccumulatedProfitReport.RecordProfit(profit.Profit)
|
|
|
|
})
|
|
|
|
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, types.Interval1d, func(kline types.KLine) {
|
|
|
|
s.AccumulatedProfitReport.DailyUpdate(s.TradeStats)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
// For drawing
|
|
|
|
profitSlice := floats.Slice{1., 1.}
|
|
|
|
price, _ := session.LastPrice(s.Symbol)
|
2022-09-19 06:45:13 +00:00
|
|
|
initAsset := s.CalcAssetValue(price).Float64()
|
2022-10-04 06:59:09 +00:00
|
|
|
cumProfitSlice := floats.Slice{initAsset, initAsset}
|
2022-10-17 13:34:55 +00:00
|
|
|
profitDollarSlice := floats.Slice{0, 0}
|
|
|
|
cumProfitDollarSlice := floats.Slice{0, 0}
|
2022-10-04 06:59:09 +00:00
|
|
|
|
|
|
|
s.orderExecutor.TradeCollector().OnTrade(func(trade types.Trade, profit fixedpoint.Value, netProfit fixedpoint.Value) {
|
|
|
|
if bbgo.IsBackTesting {
|
|
|
|
s.AccumulatedProfitReport.RecordTrade(trade.Fee)
|
|
|
|
}
|
|
|
|
|
|
|
|
// For drawing/charting
|
|
|
|
price := trade.Price.Float64()
|
|
|
|
if s.buyPrice > 0 {
|
|
|
|
profitSlice.Update(price / s.buyPrice)
|
|
|
|
cumProfitSlice.Update(s.CalcAssetValue(trade.Price).Float64())
|
|
|
|
} else if s.sellPrice > 0 {
|
|
|
|
profitSlice.Update(s.sellPrice / price)
|
|
|
|
cumProfitSlice.Update(s.CalcAssetValue(trade.Price).Float64())
|
|
|
|
}
|
2022-10-17 13:34:55 +00:00
|
|
|
profitDollarSlice.Update(profit.Float64())
|
|
|
|
cumProfitDollarSlice.Update(profitDollarSlice.Sum())
|
2022-10-04 06:59:09 +00:00
|
|
|
if s.Position.IsDust(trade.Price) {
|
|
|
|
s.buyPrice = 0
|
|
|
|
s.sellPrice = 0
|
|
|
|
s.highestPrice = 0
|
|
|
|
s.lowestPrice = 0
|
|
|
|
} else if s.Position.IsLong() {
|
|
|
|
s.buyPrice = price
|
|
|
|
s.sellPrice = 0
|
|
|
|
s.highestPrice = s.buyPrice
|
|
|
|
s.lowestPrice = 0
|
|
|
|
} else {
|
|
|
|
s.sellPrice = price
|
|
|
|
s.buyPrice = 0
|
|
|
|
s.highestPrice = 0
|
|
|
|
s.lowestPrice = s.sellPrice
|
|
|
|
}
|
2022-09-19 06:45:13 +00:00
|
|
|
})
|
2022-10-04 06:59:09 +00:00
|
|
|
|
2022-10-17 13:34:55 +00:00
|
|
|
s.InitDrawCommands(&profitSlice, &cumProfitSlice, &cumProfitDollarSlice)
|
2022-10-11 13:49:41 +00:00
|
|
|
|
2022-09-19 06:45:13 +00:00
|
|
|
s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
|
2022-10-03 10:45:24 +00:00
|
|
|
bbgo.Sync(ctx, s)
|
2022-09-19 06:45:13 +00:00
|
|
|
})
|
|
|
|
s.orderExecutor.Bind()
|
|
|
|
s.activeOrders = bbgo.NewActiveOrderBook(s.Symbol)
|
|
|
|
|
2022-10-11 13:49:41 +00:00
|
|
|
//back-test only, because 1s delayed a lot
|
2022-10-17 13:34:55 +00:00
|
|
|
//kLineStore, _ := s.session.MarketDataStore(s.Symbol)
|
|
|
|
//s.Nrr = &NRR{IntervalWindow: types.IntervalWindow{Window: 2, Interval: s.Interval}, RankingWindow: s.Window}
|
|
|
|
//s.Nrr.BindK(s.session.MarketDataStream, s.Symbol, s.Interval)
|
|
|
|
//if klines, ok := kLineStore.KLinesOfInterval(s.Nrr.Interval); ok {
|
|
|
|
// s.Nrr.LoadK((*klines)[0:])
|
|
|
|
//}
|
|
|
|
//s.Ma = &indicator.SMA{IntervalWindow: types.IntervalWindow{Window: s.Window, Interval: s.Interval}}
|
|
|
|
//s.Ma.BindK(s.session.MarketDataStream, s.Symbol, s.Interval)
|
|
|
|
//if klines, ok := kLineStore.KLinesOfInterval(s.Ma.Interval); ok {
|
|
|
|
// s.Ma.LoadK((*klines)[0:])
|
|
|
|
//}
|
2022-09-19 06:45:13 +00:00
|
|
|
|
2022-10-17 13:34:55 +00:00
|
|
|
s.rtNr = types.NewQueue(100)
|
2022-09-19 06:45:13 +00:00
|
|
|
|
2022-10-17 13:34:55 +00:00
|
|
|
s.rtMaFast = types.NewQueue(1)
|
|
|
|
s.rtMaSlow = types.NewQueue(5)
|
|
|
|
s.rtMr = types.NewQueue(100)
|
2022-10-11 13:49:41 +00:00
|
|
|
|
2022-10-17 13:34:55 +00:00
|
|
|
s.rtWeight = types.NewQueue(100)
|
2022-10-11 13:49:41 +00:00
|
|
|
|
2022-10-17 13:34:55 +00:00
|
|
|
currentRoundTime := int64(0)
|
|
|
|
previousRoundTime := int64(0)
|
2022-10-11 13:49:41 +00:00
|
|
|
|
2022-10-17 13:34:55 +00:00
|
|
|
currentTradePrice := 0.
|
|
|
|
previousTradePrice := 0.
|
|
|
|
|
|
|
|
if !bbgo.IsBackTesting {
|
2022-10-11 13:49:41 +00:00
|
|
|
|
|
|
|
s.session.MarketDataStream.OnBookTickerUpdate(func(bt types.BookTicker) {
|
2022-10-17 13:34:55 +00:00
|
|
|
// quote order book price
|
|
|
|
s.obBuyPrice = atomic.NewFloat64(bt.Buy.Float64())
|
|
|
|
s.obSellPrice = atomic.NewFloat64(bt.Sell.Float64())
|
2022-10-11 13:49:41 +00:00
|
|
|
})
|
2022-10-17 13:34:55 +00:00
|
|
|
|
|
|
|
s.session.MarketDataStream.OnAggTrade(func(trade types.Trade) {
|
|
|
|
// rounding to 1000 milliseconds if hftInterval is set to 1000
|
|
|
|
currentRoundTime = trade.Time.UnixMilli() % int64(s.Interval)
|
|
|
|
currentTradePrice = trade.Price.Float64()
|
|
|
|
if currentRoundTime < previousRoundTime {
|
|
|
|
|
|
|
|
s.openPrice = s.closePrice
|
|
|
|
// D0 strategy can use now data
|
|
|
|
// D1 strategy only use previous data (we're here)
|
|
|
|
s.closePrice = previousTradePrice
|
|
|
|
//log.Infof("Previous Close Price: %f", s.closePrice)
|
|
|
|
//log.Infof("Previous Open Price: %f", s.openPrice)
|
|
|
|
log.Infof("Now Open Price: %f", currentTradePrice)
|
|
|
|
s.orderExecutor.CancelNoWait(ctx)
|
|
|
|
// calculate real-time Negative Return
|
|
|
|
s.rtNr.Update(s.openPrice - s.closePrice)
|
|
|
|
// calculate real-time Negative Return Rank
|
|
|
|
rtNrRank := 0.
|
|
|
|
if s.rtNr.Length() >= 100 {
|
|
|
|
rtNrRank = s.rtNr.Rank(s.rtNr.Length()).Last() / float64(s.rtNr.Length())
|
|
|
|
}
|
|
|
|
// calculate real-time Mean Reversion
|
|
|
|
s.rtMaFast.Update(s.closePrice)
|
|
|
|
s.rtMaSlow.Update(s.closePrice)
|
|
|
|
s.rtMr.Update((s.rtMaSlow.Mean() - s.rtMaFast.Mean()) / s.rtMaSlow.Mean())
|
|
|
|
// calculate real-time Mean Reversion Rank
|
|
|
|
rtMrRank := 0.
|
|
|
|
if s.rtMr.Length() >= 100 {
|
|
|
|
rtMrRank = s.rtMr.Rank(s.rtMr.Length()).Last() / float64(s.rtMr.Length())
|
|
|
|
}
|
2022-10-17 14:26:33 +00:00
|
|
|
alpha := 0.
|
|
|
|
if s.NR && s.MR {
|
|
|
|
alpha = (rtNrRank + rtMrRank) / 2
|
|
|
|
} else if s.NR && !s.MR {
|
|
|
|
alpha = rtNrRank
|
|
|
|
} else if !s.NR && s.MR {
|
|
|
|
alpha = rtMrRank
|
|
|
|
}
|
|
|
|
s.rtWeight.Update(alpha)
|
2022-10-17 14:14:06 +00:00
|
|
|
//rtWeightRank := 0.
|
|
|
|
//if s.rtWeight.Length() >= 100 {
|
|
|
|
// rtWeightRank = s.rtWeight.Rank(s.rtWeight.Length()).Last() / float64(s.rtWeight.Length())
|
|
|
|
//}
|
|
|
|
log.Infof("Alpha: %f/1.0", s.rtWeight.Last())
|
|
|
|
s.rebalancePosition(s.obBuyPrice.Load(), s.obSellPrice.Load(), s.rtWeight.Last())
|
2022-10-17 13:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
previousRoundTime = currentRoundTime
|
|
|
|
previousTradePrice = currentTradePrice
|
2022-10-11 13:49:41 +00:00
|
|
|
})
|
|
|
|
}
|
2022-09-19 06:45:13 +00:00
|
|
|
|
2022-10-03 08:01:08 +00:00
|
|
|
bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) {
|
2022-09-19 06:45:13 +00:00
|
|
|
defer wg.Done()
|
2022-10-04 06:59:09 +00:00
|
|
|
// Output accumulated profit report
|
|
|
|
if bbgo.IsBackTesting {
|
|
|
|
defer s.AccumulatedProfitReport.Output(s.Symbol)
|
|
|
|
|
|
|
|
if s.DrawGraph {
|
|
|
|
if err := s.Draw(&profitSlice, &cumProfitSlice); err != nil {
|
|
|
|
log.WithError(err).Errorf("cannot draw graph")
|
|
|
|
}
|
|
|
|
}
|
2022-10-11 13:49:41 +00:00
|
|
|
} else {
|
|
|
|
close(s.stopC)
|
2022-10-04 06:59:09 +00:00
|
|
|
}
|
2022-09-19 06:45:13 +00:00
|
|
|
_, _ = fmt.Fprintln(os.Stderr, s.TradeStats.String())
|
|
|
|
_ = s.orderExecutor.GracefulCancel(ctx)
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) CalcAssetValue(price fixedpoint.Value) fixedpoint.Value {
|
|
|
|
balances := s.session.GetAccount().Balances()
|
|
|
|
return balances[s.Market.BaseCurrency].Total().Mul(price).Add(balances[s.Market.QuoteCurrency].Total())
|
|
|
|
}
|
2022-10-11 13:49:41 +00:00
|
|
|
|
2022-10-17 13:34:55 +00:00
|
|
|
func (s *Strategy) rebalancePosition(bestBid, bestAsk float64, w float64) {
|
|
|
|
// alpha-weighted assets (inventory and capital)
|
|
|
|
position := s.orderExecutor.Position()
|
|
|
|
p := fixedpoint.NewFromFloat((bestBid + bestAsk) / 2)
|
2022-10-11 13:49:41 +00:00
|
|
|
|
2022-10-17 13:34:55 +00:00
|
|
|
targetBase := s.QuantityOrAmount.CalculateQuantity(p).Mul(fixedpoint.NewFromFloat(w))
|
2022-10-11 13:49:41 +00:00
|
|
|
|
2022-10-17 13:34:55 +00:00
|
|
|
// to buy/sell quantity
|
|
|
|
diffQty := targetBase.Sub(position.Base)
|
|
|
|
log.Infof("Target Position Diff: %f", diffQty.Float64())
|
2022-10-11 13:49:41 +00:00
|
|
|
|
2022-10-17 13:34:55 +00:00
|
|
|
// ignore small changes
|
|
|
|
if diffQty.Abs().Float64() < 0.0005 {
|
|
|
|
return
|
|
|
|
}
|
2022-10-11 13:49:41 +00:00
|
|
|
|
2022-10-17 13:34:55 +00:00
|
|
|
if diffQty.Sign() > 0 {
|
|
|
|
_, err := s.orderExecutor.SubmitOrders(context.Background(), types.SubmitOrder{
|
|
|
|
Symbol: s.Symbol,
|
|
|
|
Side: types.SideTypeBuy,
|
|
|
|
Quantity: diffQty.Abs(),
|
|
|
|
Type: types.OrderTypeLimit,
|
|
|
|
Price: fixedpoint.NewFromFloat(bestBid),
|
|
|
|
Tag: "irr re-balance: buy",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err)
|
|
|
|
}
|
|
|
|
} else if diffQty.Sign() < 0 {
|
|
|
|
_, err := s.orderExecutor.SubmitOrders(context.Background(), types.SubmitOrder{
|
|
|
|
Symbol: s.Symbol,
|
|
|
|
Side: types.SideTypeSell,
|
|
|
|
Quantity: diffQty.Abs(),
|
|
|
|
Type: types.OrderTypeLimit,
|
|
|
|
Price: fixedpoint.NewFromFloat(bestAsk),
|
|
|
|
Tag: "irr re-balance: sell",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err)
|
2022-10-11 13:49:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|