diff --git a/config/drift.yaml b/config/drift.yaml new file mode 100644 index 0000000000..a91314ac1c --- /dev/null +++ b/config/drift.yaml @@ -0,0 +1,80 @@ +--- +persistence: + redis: + host: 127.0.0.1 + port: 6379 + db: 0 + +sessions: + binance: + exchange: binance + futures: false + envVarPrefix: binance + heikinAshi: false + +exchangeStrategies: + +- on: binance + drift: + canvasPath: "./output.png" + symbol: ETHUSDT + # kline interval for indicators + interval: 15m + window: 2 + stoploss: 0.3% + source: close + predictOffset: 2 + # position avg +- takeProfitFactor * atr as take profit price + takeProfitFactor: 1.4 + noTrailingStopLoss: true + # stddev on high/low-source + hlVarianceMultiplier: 0.22 + + generateGraph: true + graphPNLDeductFee: false + graphPNLPath: "./pnl.png" + graphCumPNLPath: "./cumpnl.png" + #exits: + #- roiStopLoss: + # percentage: 0.8% + #- roiTakeProfit: + # percentage: 35% + #- protectiveStopLoss: + # activationRatio: 0.6% + # stopLossRatio: 0.1% + # placeStopOrder: false + #- protectiveStopLoss: + # activationRatio: 5% + # stopLossRatio: 1% + # placeStopOrder: false + #- cumulatedVolumeTakeProfit: + # interval: 5m + # window: 2 + # minQuoteVolume: 200_000_000 + #- protectiveStopLoss: + # activationRatio: 2% + # stopLossRatio: 1% + # placeStopOrder: false + +sync: + userDataStream: + trades: true + filledOrders: true + sessions: + - binance + symbols: + - ETHUSDT + +backtest: + startTime: "2022-01-01" + endTime: "2022-06-18" + symbols: + - ETHUSDT + sessions: [binance] + accounts: + binance: + #makerFeeRate: 0.00001 + #takerFeeRate: 0.00001 + balances: + ETH: 10 + USDT: 5000.0 diff --git a/config/driftBTC.yaml b/config/driftBTC.yaml new file mode 100644 index 0000000000..45d7a70996 --- /dev/null +++ b/config/driftBTC.yaml @@ -0,0 +1,91 @@ +--- +persistence: + redis: + host: 127.0.0.1 + port: 6379 + db: 0 + +sessions: + binance: + exchange: binance + futures: false + envVarPrefix: binance + heikinAshi: false + +exchangeStrategies: + +- on: binance + drift: + canvasPath: "./output.png" + symbol: BTCBUSD + # kline interval for indicators + interval: 15m + window: 2 + stoploss: 0.3% + source: close + predictOffset: 2 + # position avg +- takeProfitFactor * atr as take profit price + takeProfitFactor: 1.2 + noTrailingStopLoss: true + # stddev on high/low-source + hlVarianceMultiplier: 0.27 + + generateGraph: true + graphPNLDeductFee: true + graphPNLPath: "./pnl.png" + graphCumPNLPath: "./cumpnl.png" + exits: + #- roiStopLoss: + # percentage: 0.8% + #- roiTakeProfit: + # percentage: 3% + #- protectiveStopLoss: + # activationRatio: 0.5% + # stopLossRatio: 0.1% + # placeStopOrder: false + - trailingStop: + callbackRate: 1% + # activationRatio is relative to the average cost, + # when side is buy, 1% means lower 1% than the average cost. + # when side is sell, 1% means higher 1% than the average cost. + activationRatio: 3% + # minProfit uses the position ROI to calculate the profit ratio + minProfit: 1% + interval: 1m + side: buy + closePosition: 100% + #- protectiveStopLoss: + # activationRatio: 5% + # stopLossRatio: 1% + # placeStopOrder: false + #- cumulatedVolumeTakeProfit: + # interval: 5m + # window: 2 + # minQuoteVolume: 200_000_000 + #- protectiveStopLoss: + # activationRatio: 2% + # stopLossRatio: 1% + # placeStopOrder: false + +sync: + userDataStream: + trades: true + filledOrders: true + sessions: + - binance + symbols: + - BTCBUSD + +backtest: + startTime: "2022-01-01" + endTime: "2022-06-18" + symbols: + - BTCBUSD + sessions: [binance] + accounts: + binance: + makerFeeRate: 0.000 + takerFeeRate: 0.00075 + balances: + BTC: 10 + BUSD: 5000.0 diff --git a/go.mod b/go.mod index 34bd47ce8d..777a3464d7 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module github.com/c9s/bbgo -go 1.17 +go 1.18 require ( github.com/DATA-DOG/go-sqlmock v1.5.0 @@ -43,6 +43,7 @@ require ( github.com/spf13/viper v1.7.1 github.com/stretchr/testify v1.7.0 github.com/valyala/fastjson v1.5.1 + github.com/wcharczuk/go-chart/v2 v2.1.0 github.com/webview/webview v0.0.0-20210216142346-e0bfdf0e5d90 github.com/x-cray/logrus-prefixed-formatter v0.5.2 github.com/zserge/lorca v0.1.9 @@ -75,6 +76,7 @@ require ( github.com/go-test/deep v1.0.6 // indirect github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect github.com/golang-sql/sqlexp v0.1.0 // indirect + github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect github.com/golang/mock v1.6.0 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/hashicorp/hcl v1.0.0 // indirect @@ -117,6 +119,7 @@ require ( go.opentelemetry.io/otel/trace v0.19.0 // indirect go.uber.org/atomic v1.9.0 // indirect golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect + golang.org/x/image v0.0.0-20200927104501-e162460cd6b5 // indirect golang.org/x/mod v0.5.1 // indirect golang.org/x/net v0.0.0-20220403103023-749bd193bc2b // indirect golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c // indirect diff --git a/go.sum b/go.sum index 96f942c4d3..01b2cdcaa4 100644 --- a/go.sum +++ b/go.sum @@ -182,6 +182,7 @@ github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 h1:au07oEsX2xN0kt github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A= github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EOqtpKwwwHI= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= @@ -516,6 +517,7 @@ github.com/ugorji/go/codec v1.2.3 h1:/mVYEV+Jo3IZKeA5gBngN0AvNnQltEDkR+eQikkWQu0 github.com/ugorji/go/codec v1.2.3/go.mod h1:5FxzDJIgeiWJZslYHPj+LS1dq1ZBQVelZFnjsFGI/Uc= github.com/valyala/fastjson v1.5.1 h1:SXaQZVSwLjZOVhDEhjiCcDtnX0Feu7Z7A1+C5atpoHM= github.com/valyala/fastjson v1.5.1/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY= +github.com/wcharczuk/go-chart/v2 v2.1.0 h1:tY2slqVQ6bN+yHSnDYwZebLQFkphK4WNrVwnt7CJZ2I= github.com/wcharczuk/go-chart/v2 v2.1.0/go.mod h1:yx7MvAVNcP/kN9lKXM/NTce4au4DFN99j6i1OwDclNA= github.com/webview/webview v0.0.0-20210216142346-e0bfdf0e5d90 h1:G/O1RFjhc9hgVYjaPQ0Oceqxf3GwRQl/5XEAWYetjmg= github.com/webview/webview v0.0.0-20210216142346-e0bfdf0e5d90/go.mod h1:rpXAuuHgyEJb6kXcXldlkOjU6y4x+YcASKKXJNUhh0Y= @@ -584,6 +586,7 @@ golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMk golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/image v0.0.0-20200927104501-e162460cd6b5 h1:QelT11PB4FXiDEXucrfNckHoFxwt8USGY1ajP1ZF5lM= golang.org/x/image v0.0.0-20200927104501-e162460cd6b5/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= diff --git a/pkg/bbgo/environment.go b/pkg/bbgo/environment.go index 415bd52de8..c60f056806 100644 --- a/pkg/bbgo/environment.go +++ b/pkg/bbgo/environment.go @@ -453,6 +453,10 @@ func (environ *Environment) SetStartTime(t time.Time) *Environment { return environ } +func (environ *Environment) StartTime() time.Time { + return environ.startTime +} + // SetSyncStartTime overrides the default trade scan time (-7 days) func (environ *Environment) SetSyncStartTime(t time.Time) *Environment { environ.syncStartTime = t diff --git a/pkg/bbgo/order_executor_general.go b/pkg/bbgo/order_executor_general.go index 071e8e10f2..6567171c5f 100644 --- a/pkg/bbgo/order_executor_general.go +++ b/pkg/bbgo/order_executor_general.go @@ -2,6 +2,7 @@ package bbgo import ( "context" + "fmt" "strings" log "github.com/sirupsen/logrus" @@ -102,7 +103,7 @@ func (e *GeneralOrderExecutor) SubmitOrders(ctx context.Context, submitOrders .. createdOrders, err := e.session.Exchange.SubmitOrders(ctx, formattedOrders...) if err != nil { - log.WithError(err).Errorf("can not place orders") + err = fmt.Errorf("can not place orders: %w", err) } e.orderStore.Add(createdOrders...) @@ -113,9 +114,11 @@ func (e *GeneralOrderExecutor) SubmitOrders(ctx context.Context, submitOrders .. // GracefulCancelActiveOrderBook cancels the orders from the active orderbook. func (e *GeneralOrderExecutor) GracefulCancelActiveOrderBook(ctx context.Context, activeOrders *ActiveOrderBook) error { + if activeOrders.NumOfOrders() == 0 { + return nil + } if err := activeOrders.GracefulCancel(ctx, e.session.Exchange); err != nil { - log.WithError(err).Errorf("graceful cancel order error") - return err + return fmt.Errorf("graceful cancel order error: %w", err) } e.tradeCollector.Process() diff --git a/pkg/cmd/builtin.go b/pkg/cmd/builtin.go index 84bfc5647c..2bb81b931f 100644 --- a/pkg/cmd/builtin.go +++ b/pkg/cmd/builtin.go @@ -33,4 +33,5 @@ import ( _ "github.com/c9s/bbgo/pkg/strategy/xmaker" _ "github.com/c9s/bbgo/pkg/strategy/xnav" _ "github.com/c9s/bbgo/pkg/strategy/xpuremaker" + _ "github.com/c9s/bbgo/pkg/strategy/drift" ) diff --git a/pkg/indicator/alma.go b/pkg/indicator/alma.go index 2662852694..ab39a6c8ef 100644 --- a/pkg/indicator/alma.go +++ b/pkg/indicator/alma.go @@ -15,8 +15,8 @@ import ( type ALMA struct { types.SeriesBase types.IntervalWindow // required - Offset float64 // required: recommend to be 5 - Sigma int // required: recommend to be 0.5 + Offset float64 // required: recommend to be 0.5 + Sigma int // required: recommend to be 5 weight []float64 sum float64 input []float64 diff --git a/pkg/indicator/atr.go b/pkg/indicator/atr.go index 53aff531bb..77605ce2fc 100644 --- a/pkg/indicator/atr.go +++ b/pkg/indicator/atr.go @@ -22,6 +22,24 @@ type ATR struct { var _ types.SeriesExtend = &ATR{} +func (inc *ATR) Clone() *ATR { + out := &ATR{ + IntervalWindow: inc.IntervalWindow, + PercentageVolatility: inc.PercentageVolatility[:], + PreviousClose: inc.PreviousClose, + RMA: inc.RMA.Clone().(*RMA), + EndTime: inc.EndTime, + } + out.SeriesBase.Series = out + return out +} + +func (inc *ATR) TestUpdate(high, low, cloze float64) *ATR { + c := inc.Clone() + c.Update(high, low, cloze) + return c +} + func (inc *ATR) Update(high, low, cloze float64) { if inc.Window <= 0 { panic("window must be greater than 0") diff --git a/pkg/indicator/boll.go b/pkg/indicator/boll.go index 9b0a547040..f29fb714a0 100644 --- a/pkg/indicator/boll.go +++ b/pkg/indicator/boll.go @@ -21,7 +21,6 @@ Bollinger Bands Technical indicator guide: //go:generate callbackgen -type BOLL type BOLL struct { - types.SeriesBase types.IntervalWindow // K is the multiplier of Std, generally it's 2 @@ -74,7 +73,6 @@ func (inc *BOLL) LastDownBand() float64 { func (inc *BOLL) Update(value float64) { if inc.SMA == nil { - inc.SeriesBase.Series = inc inc.SMA = &SMA{IntervalWindow: inc.IntervalWindow} } diff --git a/pkg/indicator/const.go b/pkg/indicator/const.go index 4b48f2b998..7764c75dd2 100644 --- a/pkg/indicator/const.go +++ b/pkg/indicator/const.go @@ -9,4 +9,3 @@ import ( var three = fixedpoint.NewFromInt(3) var zeroTime = time.Time{} - diff --git a/pkg/indicator/dema.go b/pkg/indicator/dema.go index 69e75c4f29..bec5823292 100644 --- a/pkg/indicator/dema.go +++ b/pkg/indicator/dema.go @@ -18,6 +18,23 @@ type DEMA struct { UpdateCallbacks []func(value float64) } +func (inc *DEMA) Clone() *DEMA { + out := &DEMA{ + IntervalWindow: inc.IntervalWindow, + Values: inc.Values[:], + a1: inc.a1.Clone(), + a2: inc.a2.Clone(), + } + out.SeriesBase.Series = out + return out +} + +func (inc *DEMA) TestUpdate(value float64) *DEMA { + out := inc.Clone() + out.Update(value) + return out +} + func (inc *DEMA) Update(value float64) { if len(inc.Values) == 0 { inc.SeriesBase.Series = inc diff --git a/pkg/indicator/drift.go b/pkg/indicator/drift.go index f0cc1f7570..1146bfddc1 100644 --- a/pkg/indicator/drift.go +++ b/pkg/indicator/drift.go @@ -15,7 +15,7 @@ type Drift struct { types.IntervalWindow chng *types.Queue Values types.Float64Slice - SMA *SMA + MA types.UpdatableSeriesExtend LastValue float64 UpdateCallbacks []func(value float64) @@ -24,7 +24,9 @@ type Drift struct { func (inc *Drift) Update(value float64) { if inc.chng == nil { inc.SeriesBase.Series = inc - inc.SMA = &SMA{IntervalWindow: types.IntervalWindow{Interval: inc.Interval, Window: inc.Window}} + if inc.MA == nil { + inc.MA = &SMA{IntervalWindow: types.IntervalWindow{Interval: inc.Interval, Window: inc.Window}} + } inc.chng = types.NewQueue(inc.Window) inc.LastValue = value return @@ -36,15 +38,54 @@ func (inc *Drift) Update(value float64) { chng = math.Log(value / inc.LastValue) inc.LastValue = value } - inc.SMA.Update(chng) + inc.MA.Update(chng) inc.chng.Update(chng) if inc.chng.Length() >= inc.Window { stdev := types.Stdev(inc.chng, inc.Window) - drift := inc.SMA.Last() - stdev*stdev*0.5 + drift := inc.MA.Last() - stdev*stdev*0.5 inc.Values.Push(drift) } } +// Assume that MA is SMA +func (inc *Drift) ZeroPoint() float64 { + window := float64(inc.Window) + stdev := types.Stdev(inc.chng, inc.Window) + chng := inc.chng.Index(inc.Window - 1) + /*b := -2 * inc.MA.Last() - 2 + c := window * stdev * stdev - chng * chng + 2 * chng * (inc.MA.Last() + 1) - 2 * inc.MA.Last() * window + + root := math.Sqrt(b*b - 4*c) + K1 := (-b + root)/2 + K2 := (-b - root)/2 + N1 := math.Exp(K1) * inc.LastValue + N2 := math.Exp(K2) * inc.LastValue + if math.Abs(inc.LastValue-N1) < math.Abs(inc.LastValue-N2) { + return N1 + } else { + return N2 + }*/ + return inc.LastValue * math.Exp(window*(0.5*stdev*stdev)+chng-inc.MA.Last()*window) +} + +func (inc *Drift) Clone() (out *Drift) { + out = &Drift{ + IntervalWindow: inc.IntervalWindow, + chng: inc.chng.Clone(), + Values: inc.Values[:], + MA: types.Clone(inc.MA), + LastValue: inc.LastValue, + } + out.SeriesBase.Series = out + return out +} + +func (inc *Drift) TestUpdate(value float64) *Drift { + out := inc.Clone() + out.Update(value) + return out +} + func (inc *Drift) Index(i int) float64 { if inc.Values == nil { return 0 diff --git a/pkg/indicator/ewma.go b/pkg/indicator/ewma.go index 78abde359c..05136f4e49 100644 --- a/pkg/indicator/ewma.go +++ b/pkg/indicator/ewma.go @@ -23,6 +23,21 @@ type EWMA struct { var _ types.SeriesExtend = &EWMA{} +func (inc *EWMA) Clone() *EWMA { + out := &EWMA{ + IntervalWindow: inc.IntervalWindow, + Values: inc.Values[:], + } + out.SeriesBase.Series = out + return out +} + +func (inc *EWMA) TestUpdate(value float64) *EWMA { + out := inc.Clone() + out.Update(value) + return out +} + func (inc *EWMA) Update(value float64) { var multiplier = 2.0 / float64(1+inc.Window) diff --git a/pkg/indicator/fisher.go b/pkg/indicator/fisher.go new file mode 100644 index 0000000000..4cb98f4bc4 --- /dev/null +++ b/pkg/indicator/fisher.go @@ -0,0 +1,58 @@ +package indicator + +import ( + "math" + + "github.com/c9s/bbgo/pkg/types" +) + +//go:generate callbackgen -type FisherTransform +type FisherTransform struct { + types.SeriesBase + types.IntervalWindow + prices *types.Queue + Values types.Float64Slice + + UpdateCallbacks []func(value float64) +} + +func (inc *FisherTransform) Update(value float64) { + if inc.prices == nil { + inc.prices = types.NewQueue(inc.Window) + inc.SeriesBase.Series = inc + } + inc.prices.Update(value) + highest := inc.prices.Highest(inc.Window) + lowest := inc.prices.Lowest(inc.Window) + x := 2*((value-lowest)/(highest-lowest)) - 1 + if x == 1 { + x = 0.9999 + } else if x == -1 { + x = -0.9999 + } + inc.Values.Update(0.5 * math.Log((1+x)/(1-x))) + if len(inc.Values) > MaxNumOfEWMA { + inc.Values = inc.Values[MaxNumOfEWMATruncateSize-1:] + } +} + +func (inc *FisherTransform) Last() float64 { + if inc.Values == nil { + return 0.0 + } + return inc.Values.Last() +} + +func (inc *FisherTransform) Index(i int) float64 { + if inc.Values == nil { + return 0.0 + } + return inc.Values.Index(i) +} + +func (inc *FisherTransform) Length() int { + if inc.Values == nil { + return 0 + } + return inc.Values.Length() +} diff --git a/pkg/indicator/rma.go b/pkg/indicator/rma.go index 4601272a77..413c51bd9b 100644 --- a/pkg/indicator/rma.go +++ b/pkg/indicator/rma.go @@ -25,6 +25,20 @@ type RMA struct { updateCallbacks []func(value float64) } +func (inc *RMA) Clone() types.UpdatableSeriesExtend { + out := &RMA{ + IntervalWindow: inc.IntervalWindow, + Values: inc.Values[:], + counter: inc.counter, + Adjust: inc.Adjust, + tmp: inc.tmp, + sum: inc.sum, + EndTime: inc.EndTime, + } + out.SeriesBase.Series = out + return out +} + func (inc *RMA) Update(x float64) { lambda := 1 / float64(inc.Window) if inc.counter == 0 { diff --git a/pkg/indicator/sma.go b/pkg/indicator/sma.go index d8adc52769..d2209714a3 100644 --- a/pkg/indicator/sma.go +++ b/pkg/indicator/sma.go @@ -40,6 +40,16 @@ func (inc *SMA) Length() int { return inc.Values.Length() } +func (inc *SMA) Clone() types.UpdatableSeriesExtend { + out := &SMA{ + Values: inc.Values[:], + rawValues: inc.rawValues.Clone(), + EndTime: inc.EndTime, + } + out.SeriesBase.Series = out + return out +} + var _ types.SeriesExtend = &SMA{} func (inc *SMA) Update(value float64) { diff --git a/pkg/indicator/util.go b/pkg/indicator/util.go index d676406cf3..722d45a367 100644 --- a/pkg/indicator/util.go +++ b/pkg/indicator/util.go @@ -1,2 +1 @@ package indicator - diff --git a/pkg/statistics/omega.go b/pkg/statistics/omega.go deleted file mode 100644 index 12c0dbc25e..0000000000 --- a/pkg/statistics/omega.go +++ /dev/null @@ -1 +0,0 @@ -package statistics diff --git a/pkg/strategy/drift/strategy.go b/pkg/strategy/drift/strategy.go new file mode 100644 index 0000000000..1c9c4aa750 --- /dev/null +++ b/pkg/strategy/drift/strategy.go @@ -0,0 +1,737 @@ +package drift + +import ( + "bufio" + "context" + "encoding/json" + "errors" + "fmt" + "math" + "os" + "strings" + "sync" + + "github.com/fatih/color" + "github.com/sirupsen/logrus" + "github.com/wcharczuk/go-chart/v2" + + "github.com/c9s/bbgo/pkg/bbgo" + "github.com/c9s/bbgo/pkg/fixedpoint" + "github.com/c9s/bbgo/pkg/indicator" + "github.com/c9s/bbgo/pkg/types" + "github.com/c9s/bbgo/pkg/util" +) + +const ID = "drift" + +var log = logrus.WithField("strategy", ID) +var Four fixedpoint.Value = fixedpoint.NewFromInt(4) +var Three fixedpoint.Value = fixedpoint.NewFromInt(3) +var Two fixedpoint.Value = fixedpoint.NewFromInt(2) +var Delta fixedpoint.Value = fixedpoint.NewFromFloat(0.01) + +func init() { + bbgo.RegisterStrategy(ID, &Strategy{}) +} + +type SourceFunc func(*types.KLine) fixedpoint.Value + +type Strategy struct { + Symbol string `json:"symbol"` + + bbgo.StrategyController + types.Market + types.IntervalWindow + + *bbgo.Environment + *types.Position `persistence:"position"` + *types.ProfitStats `persistence:"profit_stats"` + *types.TradeStats `persistence:"trade_stats"` + + ma types.UpdatableSeriesExtend + stdevHigh *indicator.StdDev + stdevLow *indicator.StdDev + drift *DriftMA + atr *indicator.ATR + midPrice fixedpoint.Value + lock sync.RWMutex + + Source string `json:"source,omitempty"` + TakeProfitFactor float64 `json:"takeProfitFactor"` + StopLoss fixedpoint.Value `json:"stoploss"` + CanvasPath string `json:"canvasPath"` + PredictOffset int `json:"predictOffset"` + HighLowVarianceMultiplier float64 `json:"hlVarianceMultiplier"` + NoTrailingStopLoss bool `json:"noTrailingStopLoss"` + + buyPrice float64 + sellPrice float64 + highestPrice float64 + lowestPrice float64 + + // This is not related to trade but for statistics graph generation + // Will deduct fee in percentage from every trade + GraphPNLDeductFee bool `json:"graphPNLDeductFee"` + GraphPNLPath string `json:"graphPNLPath"` + GraphCumPNLPath string `json:"graphCumPNLPath"` + // Whether to generate graph when shutdown + GenerateGraph bool `json:"generateGraph"` + + ExitMethods bbgo.ExitMethodSet `json:"exits"` + Session *bbgo.ExchangeSession + *bbgo.GeneralOrderExecutor + + getLastPrice func() fixedpoint.Value + getSource SourceFunc +} + +func (s *Strategy) Print(o *os.File) { + f := bufio.NewWriter(o) + defer f.Flush() + b, _ := json.MarshalIndent(s.ExitMethods, " ", " ") + hiyellow := color.New(color.FgHiYellow).FprintfFunc() + hiyellow(f, "------ %s Settings ------\n", s.InstanceID()) + hiyellow(f, "canvasPath: %s\n", s.CanvasPath) + hiyellow(f, "source: %s\n", s.Source) + hiyellow(f, "stoploss: %v\n", s.StopLoss) + hiyellow(f, "takeProfitFactor: %f\n", s.TakeProfitFactor) + hiyellow(f, "predictOffset: %d\n", s.PredictOffset) + hiyellow(f, "exits:\n %s\n", string(b)) + hiyellow(f, "symbol: %s\n", s.Symbol) + hiyellow(f, "interval: %s\n", s.Interval) + hiyellow(f, "window: %d\n", s.Window) + hiyellow(f, "noTrailingStopLoss: %v\n", s.NoTrailingStopLoss) + hiyellow(f, "hlVarianceMutiplier: %f\n", s.HighLowVarianceMultiplier) + hiyellow(f, "\n") +} + +func (s *Strategy) ID() string { + return ID +} + +func (s *Strategy) InstanceID() string { + return fmt.Sprintf("%s-%s", ID, s.Symbol) +} + +func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { + session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{ + Interval: s.Interval, + }) + session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{ + Interval: types.Interval1m, + }) + + if !bbgo.IsBackTesting { + session.Subscribe(types.BookTickerChannel, s.Symbol, types.SubscribeOptions{}) + } + s.ExitMethods.SetAndSubscribe(session, s) +} + +func (s *Strategy) CurrentPosition() *types.Position { + return s.Position +} + +func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error { + order := s.Position.NewMarketCloseOrder(percentage) + if order == nil { + return nil + } + order.Tag = "close" + order.TimeInForce = "" + balances := s.Session.GetAccount().Balances() + baseBalance := balances[s.Market.BaseCurrency].Available + price := s.getLastPrice() + if order.Side == types.SideTypeBuy { + quoteAmount := balances[s.Market.QuoteCurrency].Available.Div(price) + if order.Quantity.Compare(quoteAmount) > 0 { + order.Quantity = quoteAmount + } + } else if order.Side == types.SideTypeSell && order.Quantity.Compare(baseBalance) > 0 { + order.Quantity = baseBalance + } + for { + if s.Market.IsDustQuantity(order.Quantity, price) { + return nil + } + _, err := s.GeneralOrderExecutor.SubmitOrders(ctx, *order) + if err != nil { + order.Quantity = order.Quantity.Mul(fixedpoint.One.Sub(Delta)) + continue + } + return nil + } +} + +func (s *Strategy) SourceFuncGenerator() SourceFunc { + switch strings.ToLower(s.Source) { + case "close": + return func(kline *types.KLine) fixedpoint.Value { return kline.Close } + case "high": + return func(kline *types.KLine) fixedpoint.Value { return kline.High } + case "low": + return func(kline *types.KLine) fixedpoint.Value { return kline.Low } + case "hl2": + return func(kline *types.KLine) fixedpoint.Value { + return kline.High.Add(kline.Low).Div(Two) + } + case "hlc3": + return func(kline *types.KLine) fixedpoint.Value { + return kline.High.Add(kline.Low).Add(kline.Close).Div(Three) + } + case "ohlc4": + return func(kline *types.KLine) fixedpoint.Value { + return kline.Open.Add(kline.High).Add(kline.Low).Add(kline.Close).Div(Four) + } + case "open": + return func(kline *types.KLine) fixedpoint.Value { return kline.Open } + case "": + log.Infof("source not set, use hl2 by default") + return func(kline *types.KLine) fixedpoint.Value { + return kline.High.Add(kline.Low).Div(Two) + } + default: + panic(fmt.Sprintf("Unable to parse: %s", s.Source)) + } +} + +type DriftMA struct { + types.SeriesBase + ma1 types.UpdatableSeries + drift *indicator.Drift + ma2 types.UpdatableSeries +} + +func (s *DriftMA) Update(value float64) { + s.ma1.Update(value) + s.drift.Update(s.ma1.Last()) + s.ma2.Update(s.drift.Last()) +} + +func (s *DriftMA) Last() float64 { + return s.ma2.Last() +} + +func (s *DriftMA) Index(i int) float64 { + return s.ma2.Index(i) +} + +func (s *DriftMA) Length() int { + return s.ma2.Length() +} + +func (s *DriftMA) ZeroPoint() float64 { + return s.drift.ZeroPoint() +} + +func (s *Strategy) initIndicators() error { + s.ma = &indicator.SMA{IntervalWindow: types.IntervalWindow{Interval: s.Interval, Window: 5}} + s.stdevHigh = &indicator.StdDev{IntervalWindow: types.IntervalWindow{Interval: s.Interval, Window: 6}} + s.stdevLow = &indicator.StdDev{IntervalWindow: types.IntervalWindow{Interval: s.Interval, Window: 6}} + s.drift = &DriftMA{ + drift: &indicator.Drift{ + MA: &indicator.SMA{IntervalWindow: s.IntervalWindow}, + IntervalWindow: s.IntervalWindow, + }, + ma1: &indicator.EWMA{ + IntervalWindow: types.IntervalWindow{Interval: s.Interval, Window: 2}, + }, + ma2: &indicator.FisherTransform{ + IntervalWindow: types.IntervalWindow{Interval: s.Interval, Window: 9}, + }, + } + s.drift.SeriesBase.Series = s.drift + s.atr = &indicator.ATR{IntervalWindow: types.IntervalWindow{Interval: s.Interval, Window: 14}} + store, _ := s.Session.MarketDataStore(s.Symbol) + klines, ok := store.KLinesOfInterval(s.Interval) + if !ok { + return errors.New("klines not exists") + } + + for _, kline := range *klines { + source := s.getSource(&kline).Float64() + high := kline.High.Float64() + low := kline.Low.Float64() + s.ma.Update(source) + s.stdevHigh.Update(high - s.ma.Last()) + s.stdevLow.Update(s.ma.Last() - low) + s.drift.Update(source) + s.atr.PushK(kline) + } + return nil +} + +func (s *Strategy) initTickerFunctions(ctx context.Context) { + if s.IsBackTesting() { + s.getLastPrice = func() fixedpoint.Value { + lastPrice, ok := s.Session.LastPrice(s.Symbol) + if !ok { + log.Error("cannot get lastprice") + } + return lastPrice + } + } else { + s.Session.MarketDataStream.OnBookTickerUpdate(func(ticker types.BookTicker) { + bestBid := ticker.Buy + bestAsk := ticker.Sell + + var pricef, stoploss, atr, avg float64 + var price fixedpoint.Value + if util.TryLock(&s.lock) { + if !bestAsk.IsZero() && !bestBid.IsZero() { + s.midPrice = bestAsk.Add(bestBid).Div(Two) + } else if !bestAsk.IsZero() { + s.midPrice = bestAsk + } else { + s.midPrice = bestBid + } + price = s.midPrice + pricef = s.midPrice.Float64() + } else { + return + } + if s.highestPrice > 0 && s.highestPrice < pricef { + s.highestPrice = pricef + } + if s.lowestPrice > 0 && s.lowestPrice > pricef { + s.lowestPrice = pricef + } + + // for trailing stoploss during the realtime + if s.NoTrailingStopLoss { + s.lock.Unlock() + return + } + atr = s.atr.Last() + avg = s.buyPrice + s.sellPrice + stoploss = s.StopLoss.Float64() + exitShortCondition := (avg+atr/2 <= pricef || avg*(1.+stoploss) <= pricef || avg-atr*s.TakeProfitFactor >= pricef || + ((pricef-s.lowestPrice)/pricef > stoploss && (s.sellPrice-s.lowestPrice)/s.sellPrice > 0.01)) && + (s.Position.IsShort() && !s.Position.IsDust(price)) + exitLongCondition := (avg-atr/2 >= pricef || avg*(1.-stoploss) >= pricef || avg+atr*s.TakeProfitFactor <= pricef || + ((s.highestPrice-pricef)/pricef > stoploss && (s.highestPrice-s.buyPrice)/s.buyPrice > 0.01)) && + (!s.Position.IsLong() && !s.Position.IsDust(price)) + if exitShortCondition || exitLongCondition { + if err := s.GeneralOrderExecutor.GracefulCancel(ctx); err != nil { + log.WithError(err).Errorf("cannot cancel orders") + return + } + _ = s.ClosePosition(ctx, fixedpoint.One) + } + s.lock.Unlock() + + }) + s.getLastPrice = func() (lastPrice fixedpoint.Value) { + var ok bool + s.lock.RLock() + if s.midPrice.IsZero() { + lastPrice, ok = s.Session.LastPrice(s.Symbol) + if !ok { + log.Error("cannot get lastprice") + return lastPrice + } + } else { + lastPrice = s.midPrice + } + s.lock.RUnlock() + return lastPrice + } + } + +} + +func (s *Strategy) Draw(time types.Time, priceLine types.SeriesExtend, profit types.Series, cumProfit types.Series, zeroPoints types.Series) { + canvas := types.NewCanvas(s.InstanceID(), s.Interval) + Length := priceLine.Length() + if Length > 300 { + Length = 300 + } + mean := priceLine.Mean(Length) + highestPrice := priceLine.Minus(mean).Abs().Highest(Length) + highestDrift := s.drift.Abs().Highest(Length) + hi := s.drift.drift.Abs().Highest(Length) + ratio := highestPrice / highestDrift + canvas.Plot("upband", s.ma.Add(s.stdevHigh), time, Length) + canvas.Plot("ma", s.ma, time, Length) + canvas.Plot("downband", s.ma.Minus(s.stdevLow), time, Length) + canvas.Plot("drift", s.drift.Mul(ratio).Add(mean), time, Length) + canvas.Plot("driftOrig", s.drift.drift.Mul(highestPrice/hi).Add(mean), time, Length) + canvas.Plot("zero", types.NumberSeries(mean), time, Length) + canvas.Plot("price", priceLine, time, Length) + canvas.Plot("zeroPoint", zeroPoints, time, Length) + f, err := os.Create(s.CanvasPath) + if err != nil { + log.WithError(err).Errorf("cannot create on %s", s.CanvasPath) + return + } + defer f.Close() + if err := canvas.Render(chart.PNG, f); err != nil { + log.WithError(err).Errorf("cannot render in drift") + } + + canvas = types.NewCanvas(s.InstanceID()) + if s.GraphPNLDeductFee { + canvas.PlotRaw("pnl % (with Fee Deducted)", profit, profit.Length()) + } else { + canvas.PlotRaw("pnl %", profit, profit.Length()) + } + f, err = os.Create(s.GraphPNLPath) + if err != nil { + log.WithError(err).Errorf("open pnl") + return + } + defer f.Close() + if err := canvas.Render(chart.PNG, f); err != nil { + log.WithError(err).Errorf("render pnl") + } + + canvas = types.NewCanvas(s.InstanceID()) + if s.GraphPNLDeductFee { + canvas.PlotRaw("cummulative pnl % (with Fee Deducted)", cumProfit, cumProfit.Length()) + } else { + canvas.PlotRaw("cummulative pnl %", cumProfit, cumProfit.Length()) + } + f, err = os.Create(s.GraphCumPNLPath) + if err != nil { + log.WithError(err).Errorf("open cumpnl") + return + } + defer f.Close() + if err := canvas.Render(chart.PNG, f); err != nil { + log.WithError(err).Errorf("render cumpnl") + } +} + +func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error { + instanceID := s.InstanceID() + // Will be set by persistence if there's any from DB + 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) + } + startTime := s.Environment.StartTime() + s.TradeStats.SetIntervalProfitCollector(types.NewIntervalProfitCollector(types.Interval1d, startTime)) + s.TradeStats.SetIntervalProfitCollector(types.NewIntervalProfitCollector(types.Interval1w, startTime)) + + // StrategyController + s.Status = types.StrategyStatusRunning + // Get source function from config input + s.getSource = s.SourceFuncGenerator() + + s.OnSuspend(func() { + _ = s.GeneralOrderExecutor.GracefulCancel(ctx) + }) + + s.OnEmergencyStop(func() { + _ = s.GeneralOrderExecutor.GracefulCancel(ctx) + _ = s.ClosePosition(ctx, fixedpoint.One) + }) + + s.GeneralOrderExecutor = bbgo.NewGeneralOrderExecutor(session, s.Symbol, ID, instanceID, s.Position) + s.GeneralOrderExecutor.BindEnvironment(s.Environment) + s.GeneralOrderExecutor.BindProfitStats(s.ProfitStats) + s.GeneralOrderExecutor.BindTradeStats(s.TradeStats) + s.GeneralOrderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { + bbgo.Sync(s) + }) + s.GeneralOrderExecutor.Bind() + + // Exit methods from config + for _, method := range s.ExitMethods { + method.Bind(session, s.GeneralOrderExecutor) + } + buyPrice := fixedpoint.Zero + sellPrice := fixedpoint.Zero + Volume := fixedpoint.Zero + profit := types.Float64Slice{} + cumProfit := types.Float64Slice{1.} + orderTagHistory := make(map[uint64]string) + s.buyPrice = 0 + s.sellPrice = 0 + s.Session.UserDataStream.OnOrderUpdate(func(order types.Order) { + orderTagHistory[order.OrderID] = order.Tag + }) + modify := func(p fixedpoint.Value) fixedpoint.Value { + return p + } + if s.GraphPNLDeductFee { + fee := fixedpoint.NewFromFloat(0.0004) // taker fee % * 2, for upper bound + modify = func(p fixedpoint.Value) fixedpoint.Value { + return p.Mul(fixedpoint.One.Sub(fee)) + } + } + s.Session.UserDataStream.OnTradeUpdate(func(trade types.Trade) { + tag, ok := orderTagHistory[trade.OrderID] + if !ok { + panic(fmt.Sprintf("cannot find order: %v", trade)) + } + if tag == "close" { + if !buyPrice.IsZero() { + profit.Update(modify(trade.Price.Div(buyPrice)). + Sub(fixedpoint.One). + Mul(trade.Quantity). + Div(Volume). + Add(fixedpoint.One). + Float64()) + cumProfit.Update(cumProfit.Last() * profit.Last()) + Volume = Volume.Sub(trade.Quantity) + if Volume.IsZero() { + buyPrice = fixedpoint.Zero + } + if !sellPrice.IsZero() { + panic("sellprice shouldn't be zero") + } + } else if !sellPrice.IsZero() { + profit.Update(modify(sellPrice.Div(trade.Price)). + Sub(fixedpoint.One). + Mul(trade.Quantity). + Div(Volume). + Neg(). + Add(fixedpoint.One). + Float64()) + cumProfit.Update(cumProfit.Last() * profit.Last()) + Volume = Volume.Add(trade.Quantity) + if Volume.IsZero() { + sellPrice = fixedpoint.Zero + } + if !buyPrice.IsZero() { + panic("buyprice shouldn't be zero") + } + } else { + panic("no price available") + } + } else if tag == "short" { + if buyPrice.IsZero() { + if !sellPrice.IsZero() { + sellPrice = sellPrice.Mul(Volume).Sub(trade.Price.Mul(trade.Quantity)).Div(Volume.Sub(trade.Quantity)) + } else { + sellPrice = trade.Price + } + } else { + profit.Update(modify(trade.Price.Div(buyPrice)).Float64()) + cumProfit.Update(cumProfit.Last() * profit.Last()) + buyPrice = fixedpoint.Zero + Volume = fixedpoint.Zero + sellPrice = trade.Price + } + Volume = Volume.Sub(trade.Quantity) + } else if tag == "long" { + if sellPrice.IsZero() { + if !buyPrice.IsZero() { + buyPrice = buyPrice.Mul(Volume).Add(trade.Price.Mul(trade.Quantity)).Div(Volume.Add(trade.Quantity)) + } else { + buyPrice = trade.Price + } + } else { + profit.Update(modify(sellPrice.Div(trade.Price)).Float64()) + cumProfit.Update(cumProfit.Last() * profit.Last()) + sellPrice = fixedpoint.Zero + buyPrice = trade.Price + Volume = fixedpoint.Zero + } + Volume = Volume.Add(trade.Quantity) + } + s.buyPrice = buyPrice.Float64() + s.highestPrice = s.buyPrice + s.sellPrice = sellPrice.Float64() + s.lowestPrice = s.sellPrice + }) + + if err := s.initIndicators(); err != nil { + log.WithError(err).Errorf("initIndicator failed") + return nil + } + s.initTickerFunctions(ctx) + + dynamicKLine := &types.KLine{} + priceLine := types.NewQueue(300) + zeroPoints := types.NewQueue(300) + stoploss := s.StopLoss.Float64() + + session.MarketDataStream.OnKLineClosed(func(kline types.KLine) { + if s.Status != types.StrategyStatusRunning { + return + } + if kline.Symbol != s.Symbol { + return + } + var driftPred, atr float64 + var drift []float64 + + if !kline.Closed { + return + } + if kline.Interval == types.Interval1m { + if s.NoTrailingStopLoss || !s.IsBackTesting() { + return + } + // for doing the trailing stoploss during backtesting + atr = s.atr.Last() + price := s.getLastPrice() + pricef := price.Float64() + lowf := math.Min(kline.Low.Float64(), pricef) + highf := math.Max(kline.High.Float64(), pricef) + if s.lowestPrice > 0 && lowf < s.lowestPrice { + s.lowestPrice = lowf + } + if s.highestPrice > 0 && highf > s.highestPrice { + s.highestPrice = highf + } + avg := s.buyPrice + s.sellPrice + + exitShortCondition := (avg+atr/2 <= highf || avg*(1.+stoploss) <= highf || avg-atr*s.TakeProfitFactor >= lowf || + ((highf-s.lowestPrice)/pricef > stoploss && (s.sellPrice-s.lowestPrice)/s.sellPrice > 0.01)) && + (s.Position.IsShort() && !s.Position.IsDust(price)) + exitLongCondition := (avg-atr/2 >= lowf || avg*(1.-stoploss) >= lowf || avg+atr*s.TakeProfitFactor <= highf || + ((s.highestPrice-pricef)/pricef > stoploss && (s.highestPrice-s.buyPrice)/s.buyPrice > 0.01)) && + (s.Position.IsLong() && !s.Position.IsDust(price)) + if exitShortCondition || exitLongCondition { + if err := s.GeneralOrderExecutor.GracefulCancel(ctx); err != nil { + log.WithError(err).Errorf("cannot cancel orders") + return + } + _ = s.ClosePosition(ctx, fixedpoint.One) + } + return + } + dynamicKLine.Set(&kline) + + source := s.getSource(dynamicKLine) + sourcef := source.Float64() + priceLine.Update(sourcef) + s.ma.Update(sourcef) + s.drift.Update(sourcef) + zeroPoint := s.drift.ZeroPoint() + zeroPoints.Update(zeroPoint) + s.atr.PushK(kline) + drift = s.drift.Array(2) + ddrift := s.drift.drift.Array(2) + driftPred = s.drift.Predict(s.PredictOffset) + atr = s.atr.Last() + price := s.getLastPrice() + pricef := price.Float64() + lowf := math.Min(kline.Low.Float64(), pricef) + highf := math.Max(kline.High.Float64(), pricef) + lowdiff := s.ma.Last() - lowf + s.stdevLow.Update(lowdiff) + highdiff := highf - s.ma.Last() + s.stdevHigh.Update(highdiff) + avg := s.buyPrice + s.sellPrice + + if !s.IsBackTesting() { + balances := s.Session.GetAccount().Balances() + bbgo.Notify("zeroPoint: %.4f, source: %.4f, price: %.4f, driftPred: %.4f, drift: %.4f, drift[1]: %.4f, atr: %.4f, avg: %.4f", + zeroPoint, sourcef, pricef, driftPred, drift[0], drift[1], atr, avg) + // Notify will parse args to strings and process separately + bbgo.Notify("balances: [Base] %s [Quote] %s", balances[s.Market.BaseCurrency].String(), balances[s.Market.QuoteCurrency].String()) + } + + //shortCondition := (sourcef <= zeroPoint && driftPred <= drift[0] && drift[0] <= 0 && drift[1] > 0 && drift[2] > drift[1]) + //longCondition := (sourcef >= zeroPoint && driftPred >= drift[0] && drift[0] >= 0 && drift[1] < 0 && drift[2] < drift[1]) + //bothUp := ddrift[1] < ddrift[0] && drift[1] < drift[0] + //bothDown := ddrift[1] > ddrift[0] && drift[1] > drift[0] + shortCondition := (ddrift[0] <= 0 || drift[0] <= 0) && driftPred < 0. + longCondition := (ddrift[0] >= 0 || drift[0] >= 0) && driftPred > 0 + exitShortCondition := (avg+atr <= highf || avg*(1.+stoploss) <= highf || avg-atr*s.TakeProfitFactor >= lowf) && + (s.Position.IsShort() && !s.Position.IsDust(fixedpoint.Max(price, source))) && !longCondition && !shortCondition + exitLongCondition := (avg-atr >= lowf || avg*(1.-stoploss) >= lowf || avg+atr*s.TakeProfitFactor <= highf) && + (s.Position.IsLong() && !s.Position.IsDust(fixedpoint.Min(price, source))) && !shortCondition && !longCondition + + if exitShortCondition || exitLongCondition { + if err := s.GeneralOrderExecutor.GracefulCancel(ctx); err != nil { + log.WithError(err).Errorf("cannot cancel orders") + return + } + _ = s.ClosePosition(ctx, fixedpoint.One) + return + } + if shortCondition { + if err := s.GeneralOrderExecutor.GracefulCancel(ctx); err != nil { + log.WithError(err).Errorf("cannot cancel orders") + return + } + baseBalance, ok := s.Session.GetAccount().Balance(s.Market.BaseCurrency) + if !ok { + log.Errorf("unable to get baseBalance") + return + } + source = source.Add(fixedpoint.NewFromFloat(s.stdevHigh.Last() * s.HighLowVarianceMultiplier)) + if source.Compare(price) < 0 { + source = price + } + sourcef = source.Float64() + + if s.Market.IsDustQuantity(baseBalance.Available, source) { + return + } + // Cleanup pending StopOrders + quantity := baseBalance.Available + createdOrders, err := s.GeneralOrderExecutor.SubmitOrders(ctx, types.SubmitOrder{ + Symbol: s.Symbol, + Side: types.SideTypeSell, + Type: types.OrderTypeLimit, + Price: source, + Quantity: quantity, + Tag: "short", + }) + if err != nil { + log.WithError(err).Errorf("cannot place sell order") + return + } + orderTagHistory[createdOrders[0].OrderID] = "short" + } + if longCondition { + if err := s.GeneralOrderExecutor.GracefulCancel(ctx); err != nil { + log.WithError(err).Errorf("cannot cancel orders") + return + } + source = source.Sub(fixedpoint.NewFromFloat(s.stdevLow.Last() * s.HighLowVarianceMultiplier)) + if source.Compare(price) > 0 { + source = price + } + sourcef = source.Float64() + + quoteBalance, ok := s.Session.GetAccount().Balance(s.Market.QuoteCurrency) + if !ok { + log.Errorf("unable to get quoteCurrency") + return + } + if s.Market.IsDustQuantity( + quoteBalance.Available.Div(source), source) { + return + } + quantity := quoteBalance.Available.Div(source) + createdOrders, err := s.GeneralOrderExecutor.SubmitOrders(ctx, types.SubmitOrder{ + Symbol: s.Symbol, + Side: types.SideTypeBuy, + Type: types.OrderTypeLimit, + Price: source, + Quantity: quantity, + Tag: "long", + }) + if err != nil { + log.WithError(err).Errorf("cannot place buy order") + return + } + orderTagHistory[createdOrders[0].OrderID] = "long" + } + }) + + bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { + + defer s.Print(os.Stdout) + + defer fmt.Fprintln(os.Stdout, s.TradeStats.BriefString()) + + if s.GenerateGraph { + s.Draw(dynamicKLine.StartTime, priceLine, &profit, &cumProfit, zeroPoints) + } + + wg.Done() + }) + return nil +} diff --git a/pkg/strategy/ewoDgtrd/strategy.go b/pkg/strategy/ewoDgtrd/strategy.go index 1d2e2aef3f..668cd69e28 100644 --- a/pkg/strategy/ewoDgtrd/strategy.go +++ b/pkg/strategy/ewoDgtrd/strategy.go @@ -15,6 +15,7 @@ import ( "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/indicator" "github.com/c9s/bbgo/pkg/types" + "github.com/c9s/bbgo/pkg/util" ) const ID = "ewo_dgtrd" @@ -114,11 +115,6 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { } } -type UpdatableSeries interface { - types.Series - Update(value float64) -} - // Refer: https://tw.tradingview.com/script/XZyG5SOx-CCI-Stochastic-and-a-quick-lesson-on-Scalping-Trading-Systems/ type CCISTOCH struct { cci *indicator.CCI @@ -180,8 +176,8 @@ func (inc *CCISTOCH) SellSignal() bool { } type VWEMA struct { - PV UpdatableSeries - V UpdatableSeries + PV types.UpdatableSeries + V types.UpdatableSeries } func (inc *VWEMA) Last() float64 { @@ -1010,7 +1006,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se bestAsk := ticker.Sell var midPrice fixedpoint.Value - if tryLock(&s.lock) { + if util.TryLock(&s.lock) { if !bestAsk.IsZero() && !bestBid.IsZero() { s.midPrice = bestAsk.Add(bestBid).Div(types.Two) } else if !bestAsk.IsZero() { diff --git a/pkg/types/float_slice.go b/pkg/types/float_slice.go index 3d53e3bc7e..3632d77b87 100644 --- a/pkg/types/float_slice.go +++ b/pkg/types/float_slice.go @@ -12,6 +12,10 @@ func (s *Float64Slice) Push(v float64) { *s = append(*s, v) } +func (s *Float64Slice) Update(v float64) { + *s = append(*s, v) +} + func (s *Float64Slice) Pop(i int64) (v float64) { v = (*s)[i] *s = append((*s)[:i], (*s)[i+1:]...) diff --git a/pkg/types/indicator.go b/pkg/types/indicator.go index f7767b514d..5c67fbc21b 100644 --- a/pkg/types/indicator.go +++ b/pkg/types/indicator.go @@ -4,7 +4,9 @@ import ( "fmt" "math" "reflect" + "time" + "github.com/wcharczuk/go-chart/v2" "gonum.org/v1/gonum/stat" ) @@ -43,6 +45,15 @@ func (inc *Queue) Length() int { return len(inc.arr) } +func (inc *Queue) Clone() *Queue { + out := &Queue{ + arr: inc.arr[:], + size: inc.size, + } + out.SeriesBase.Series = out + return out +} + func (inc *Queue) Update(v float64) { inc.arr = append(inc.arr, v) if len(inc.arr) > inc.size { @@ -50,7 +61,7 @@ func (inc *Queue) Update(v float64) { } } -var _ SeriesExtend = &Queue{} +var _ UpdatableSeriesExtend = &Queue{} // Float64Indicator is the indicators (SMA and EWMA) that we want to use are returning float64 data. type Float64Indicator interface { @@ -93,6 +104,7 @@ type SeriesExtend interface { Variance(length int) float64 Covariance(b Series, length int) float64 Correlation(b Series, length int, method ...CorrFunc) float64 + AutoCorrelation(length int, lag ...int) float64 Rank(length int) SeriesExtend Sigmoid() SeriesExtend Softmax(window int) SeriesExtend @@ -120,6 +132,24 @@ type UpdatableSeriesExtend interface { Update(float64) } +func Clone(u UpdatableSeriesExtend) UpdatableSeriesExtend { + method, ok := reflect.TypeOf(u).MethodByName("Clone") + if ok { + out := method.Func.Call([]reflect.Value{reflect.ValueOf(u)}) + return out[0].Interface().(UpdatableSeriesExtend) + } + panic("method Clone not exist") +} + +func TestUpdate(u UpdatableSeriesExtend, input float64) UpdatableSeriesExtend { + method, ok := reflect.TypeOf(u).MethodByName("TestUpdate") + if ok { + out := method.Func.Call([]reflect.Value{reflect.ValueOf(u), reflect.ValueOf(input)}) + return out[0].Interface().(UpdatableSeriesExtend) + } + panic("method TestUpdate not exist") +} + // The interface maps to pinescript basic type `series` for bool type // Access the internal historical data from the latest to the oldest // Index(0) always maps to Last() @@ -133,13 +163,10 @@ type BoolSeries interface { // if limit is given, will only sum first limit numbers (a.Index[0..limit]) // otherwise will sum all elements func Sum(a Series, limit ...int) (sum float64) { - l := -1 - if len(limit) > 0 { + l := a.Length() + if len(limit) > 0 && limit[0] < l { l = limit[0] } - if l < a.Length() { - l = a.Length() - } for i := 0; i < l; i++ { sum += a.Index(i) } @@ -150,13 +177,10 @@ func Sum(a Series, limit ...int) (sum float64) { // if limit is given, will only calculate the average of first limit numbers (a.Index[0..limit]) // otherwise will operate on all elements func Mean(a Series, limit ...int) (mean float64) { - l := -1 - if len(limit) > 0 { + l := a.Length() + if len(limit) > 0 && limit[0] < l { l = limit[0] } - if l < a.Length() { - l = a.Length() - } return Sum(a, l) / float64(l) } @@ -183,7 +207,7 @@ func Abs(a Series) SeriesExtend { var _ Series = &AbsResult{} -func Predict(a Series, lookback int, offset ...int) float64 { +func LinearRegression(a Series, lookback int) (alpha float64, beta float64) { if a.Length() < lookback { lookback = a.Length() } @@ -194,7 +218,12 @@ func Predict(a Series, lookback int, offset ...int) float64 { x[i] = float64(i) y[i] = a.Index(i) } - alpha, beta := stat.LinearRegression(x, y, weights, false) + alpha, beta = stat.LinearRegression(x, y, weights, false) + return +} + +func Predict(a Series, lookback int, offset ...int) float64 { + alpha, beta := LinearRegression(a, lookback) o := -1.0 if len(offset) > 0 { o = -float64(offset[0]) @@ -335,6 +364,10 @@ func (a NumberSeries) Length() int { return math.MaxInt32 } +func (a NumberSeries) Clone() NumberSeries { + return a +} + var _ Series = NumberSeries(0) type AddSeriesResult struct { @@ -597,11 +630,11 @@ func Dot(a interface{}, b interface{}, limit ...int) float64 { // if limit is given, will only take the first limit numbers (a.Index[0..limit]) // otherwise will operate on all elements func Array(a Series, limit ...int) (result []float64) { - l := -1 - if len(limit) > 0 { + l := a.Length() + if len(limit) > 0 && l > limit[0] { l = limit[0] } - if l < a.Length() { + if l > a.Length() { l = a.Length() } result = make([]float64, l) @@ -617,13 +650,10 @@ func Array(a Series, limit ...int) (result []float64) { // // notice that the return type is a Float64Slice, which implements the Series interface func Reverse(a Series, limit ...int) (result Float64Slice) { - l := -1 - if len(limit) > 0 { + l := a.Length() + if len(limit) > 0 && l > limit[0] { l = limit[0] } - if l < a.Length() { - l = a.Length() - } result = make([]float64, l) for i := 0; i < l; i++ { result[l-i-1] = a.Index(i) @@ -709,10 +739,8 @@ func PercentageChange(a Series, offset ...int) SeriesExtend { func Stdev(a Series, params ...int) float64 { length := a.Length() - if len(params) > 0 { - if params[0] < length { - length = params[0] - } + if len(params) > 0 && params[0] < length { + length = params[0] } ddof := 0 if len(params) > 1 { @@ -817,6 +845,17 @@ func Correlation(a Series, b Series, length int, method ...CorrFunc) float64 { return runner(a, b, length) } +// similar to pandas.Series.autocorr() function. +// +// The method computes the Pearson correlation between Series and shifted itself +func AutoCorrelation(a Series, length int, lags ...int) float64 { + lag := 1 + if len(lags) > 0 { + lag = lags[0] + } + return Pearson(a, Shift(a, lag), length) +} + // similar to pandas.Series.cov() function with ddof=0 // // Compute covariance with Series @@ -1118,4 +1157,65 @@ func (l *LogisticRegressionModel) Predict(x []float64) float64 { return sigmoid(z + l.Gradient) } +type Canvas struct { + chart.Chart + Interval Interval +} + +func NewCanvas(title string, intervals ...Interval) *Canvas { + valueFormatter := chart.TimeValueFormatter + interval := Interval1m + if len(intervals) > 0 { + interval = intervals[0] + if interval.Minutes() > 24*60 { + valueFormatter = chart.TimeDateValueFormatter + } else if interval.Minutes() > 60 { + valueFormatter = chart.TimeHourValueFormatter + } else { + valueFormatter = chart.TimeMinuteValueFormatter + } + } else { + valueFormatter = chart.IntValueFormatter + } + out := &Canvas{ + Chart: chart.Chart{ + Title: title, + XAxis: chart.XAxis{ + ValueFormatter: valueFormatter, + }, + }, + Interval: interval, + } + out.Chart.Elements = []chart.Renderable{ + chart.LegendLeft(&out.Chart), + } + return out +} + +func (canvas *Canvas) Plot(tag string, a Series, endTime Time, length int) { + var timeline []time.Time + e := endTime.Time() + for i := length - 1; i >= 0; i-- { + shiftedT := e.Add(-time.Duration(i*canvas.Interval.Minutes()) * time.Minute) + timeline = append(timeline, shiftedT) + } + canvas.Series = append(canvas.Series, chart.TimeSeries{ + Name: tag, + YValues: Reverse(a, length), + XValues: timeline, + }) +} + +func (canvas *Canvas) PlotRaw(tag string, a Series, length int) { + var x []float64 + for i := 0; i < length; i++ { + x = append(x, float64(i)) + } + canvas.Series = append(canvas.Series, chart.ContinuousSeries{ + Name: tag, + XValues: x, + YValues: Reverse(a, length), + }) +} + // TODO: ta.linreg diff --git a/pkg/types/indicator_test.go b/pkg/types/indicator_test.go index 830b7b5831..5f103114db 100644 --- a/pkg/types/indicator_test.go +++ b/pkg/types/indicator_test.go @@ -1,9 +1,13 @@ package types import ( + //"os" + "testing" + "time" + "github.com/stretchr/testify/assert" + "github.com/wcharczuk/go-chart/v2" "gonum.org/v1/gonum/stat" - "testing" ) func TestFloat(t *testing.T) { @@ -119,18 +123,18 @@ func TestSigmoid(t *testing.T) { // from https://en.wikipedia.org/wiki/Logistic_regression func TestLogisticRegression(t *testing.T) { - a := []Float64Slice{{0.5, 0.75, 1., 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3., 3.25, 3.5, 4., 4.25, 4.5, 4.75, 5., 5.5}} + a := []Float64Slice{{0.5, 0.75, 1., 1.25, 1.5, 1.75, 1.75, 2.0, 2.25, 2.5, 2.75, 3., 3.25, 3.5, 4., 4.25, 4.5, 4.75, 5., 5.5}} b := Float64Slice{0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1} var x []Series x = append(x, &a[0]) - model := LogisticRegression(x, &b, a[0].Length(), 8000, 0.0009) + model := LogisticRegression(x, &b, a[0].Length(), 90000, 0.0018) inputs := []float64{1., 2., 2.7, 3., 4., 5.} results := []bool{false, false, true, true, true, true} for i, x := range inputs { input := []float64{x} pred := model.Predict(input) - assert.Equal(t, pred > 0.5, results[i]) + assert.Equal(t, pred >= 0.5, results[i]) } } @@ -144,3 +148,23 @@ func TestDot(t *testing.T) { out3 := Dot(3., &a, 2) assert.InDelta(t, out2, out3, 0.001) } + +func TestClone(t *testing.T) { + a := NewQueue(3) + a.Update(3.) + b := Clone(a) + b.Update(4.) + assert.Equal(t, a.Last(), 3.) + assert.Equal(t, b.Last(), 4.) +} + +func TestPlot(t *testing.T) { + ct := NewCanvas("test", Interval5m) + a := Float64Slice{200., 205., 230., 236} + ct.Plot("test", &a, Time(time.Now()), 4) + assert.Equal(t, ct.Interval, Interval5m) + assert.Equal(t, ct.Series[0].(chart.TimeSeries).Len(), 4) + //f, _ := os.Create("output.png") + //defer f.Close() + //ct.Render(chart.PNG, f) +} diff --git a/pkg/types/kline.go b/pkg/types/kline.go index d510b8b482..6906a1c971 100644 --- a/pkg/types/kline.go +++ b/pkg/types/kline.go @@ -71,6 +71,26 @@ type KLine struct { Closed bool `json:"closed" db:"closed"` } +func (k *KLine) Set(o *KLine) { + k.GID = o.GID + k.Exchange = o.Exchange + k.Symbol = o.Symbol + k.StartTime = o.StartTime + k.EndTime = o.EndTime + k.Interval = o.Interval + k.Open = o.Open + k.Close = o.Close + k.High = o.High + k.Low = o.Low + k.Volume = o.Volume + k.QuoteVolume = o.QuoteVolume + k.TakerBuyBaseAssetVolume = o.TakerBuyBaseAssetVolume + k.TakerBuyQuoteAssetVolume = o.TakerBuyQuoteAssetVolume + k.LastTradeID = o.LastTradeID + k.NumberOfTrades = o.NumberOfTrades + k.Closed = o.Closed +} + func (k KLine) GetStartTime() Time { return k.StartTime } diff --git a/pkg/types/omega.go b/pkg/types/omega.go new file mode 100644 index 0000000000..a89649e4f1 --- /dev/null +++ b/pkg/types/omega.go @@ -0,0 +1,28 @@ +package types + +// Determines the Omega ratio of a strategy +// See https://en.wikipedia.org/wiki/Omega_ratio for more details +// +// @param returns (Series): Series of profit/loss percentage every specific interval +// @param returnThresholds(float64): threshold for returns filtering +// @return Omega ratio for give return series and threshold +func Omega(returns Series, returnThresholds ...float64) float64 { + threshold := 0.0 + if len(returnThresholds) > 0 { + threshold = returnThresholds[0] + } else { + threshold = Mean(returns) + } + length := returns.Length() + win := 0.0 + loss := 0.0 + for i := 0; i < length; i++ { + out := threshold - returns.Index(i) + if out > 0 { + win += out + } else { + loss -= out + } + } + return win / loss +} diff --git a/pkg/types/omega_test.go b/pkg/types/omega_test.go new file mode 100644 index 0000000000..5a8fcce030 --- /dev/null +++ b/pkg/types/omega_test.go @@ -0,0 +1,12 @@ +package types + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestOmega(t *testing.T) { + var a Series = &Float64Slice{0.08, 0.09, 0.07, 0.15, 0.02, 0.03, 0.04, 0.05, 0.06, 0.01} + output := Omega(a) + assert.InDelta(t, output, 1, 0.0001) +} diff --git a/pkg/types/seriesbase_imp.go b/pkg/types/seriesbase_imp.go index 0e29aa4e36..41675f2626 100644 --- a/pkg/types/seriesbase_imp.go +++ b/pkg/types/seriesbase_imp.go @@ -121,6 +121,10 @@ func (s *SeriesBase) Correlation(b Series, length int, method ...CorrFunc) float return Correlation(s, b, length, method...) } +func (s *SeriesBase) AutoCorrelation(length int, lag ...int) float64 { + return AutoCorrelation(s, length, lag...) +} + func (s *SeriesBase) Rank(length int) SeriesExtend { return Rank(s, length) } diff --git a/pkg/statistics/sharp.go b/pkg/types/sharp.go similarity index 57% rename from pkg/statistics/sharp.go rename to pkg/types/sharp.go index bf2114249f..f60f00eafd 100644 --- a/pkg/statistics/sharp.go +++ b/pkg/types/sharp.go @@ -1,32 +1,28 @@ -package statistics +package types import ( "math" - - "github.com/c9s/bbgo/pkg/types" ) // Sharpe: Calcluates the sharpe ratio of access returns // +// @param returns (Series): Series of profit/loss percentage every specific interval // @param periods (int): Freq. of returns (252/365 for daily, 12 for monthy) // @param annualize (bool): return annualize sharpe? // @param smart (bool): return smart sharpe ratio -func Sharpe(returns types.Series, periods int, annualize bool, smart bool) float64 { +func Sharpe(returns Series, periods int, annualize bool, smart bool) float64 { data := returns num := data.Length() - if types.Lowest(data, num) >= 0 && types.Highest(data, num) > 1 { - data = types.PercentageChange(returns) - } - divisor := types.Stdev(data, data.Length(), 1) + divisor := Stdev(data, data.Length(), 1) if smart { sum := 0. - coef := math.Abs(types.Correlation(data, types.Shift(data, 1), num-1)) + coef := math.Abs(Correlation(data, Shift(data, 1), num-1)) for i := 1; i < num; i++ { sum += float64(num-i) / float64(num) * math.Pow(coef, float64(i)) } divisor = divisor * math.Sqrt(1.+2.*sum) } - result := types.Mean(data) / divisor + result := Mean(data) / divisor if annualize { return result * math.Sqrt(float64(periods)) } diff --git a/pkg/statistics/sharp_test.go b/pkg/types/sharp_test.go similarity index 84% rename from pkg/statistics/sharp_test.go rename to pkg/types/sharp_test.go index 7d373301c7..e6f90d1c96 100644 --- a/pkg/statistics/sharp_test.go +++ b/pkg/types/sharp_test.go @@ -1,7 +1,6 @@ -package statistics +package types import ( - "github.com/c9s/bbgo/pkg/types" "github.com/stretchr/testify/assert" "testing" ) @@ -17,7 +16,7 @@ print(qx.stats.sharpe(pd.Series([0.01, 0.1, 0.001]), 0, 252, False, False)) print(qx.stats.sharpe(pd.Series([0.01, 0.1, 0.001]), 0, 252, True, False)) */ func TestSharpe(t *testing.T) { - var a types.Series = &types.Float64Slice{0.01, 0.1, 0.001} + var a Series = &Float64Slice{0.01, 0.1, 0.001} output := Sharpe(a, 0, false, false) assert.InDelta(t, output, 0.67586, 0.0001) output = Sharpe(a, 252, false, false) diff --git a/pkg/types/trade_stats.go b/pkg/types/trade_stats.go index ce0281d8cf..7dfd3d1a10 100644 --- a/pkg/types/trade_stats.go +++ b/pkg/types/trade_stats.go @@ -1,30 +1,121 @@ package types import ( + "time" + "gopkg.in/yaml.v3" "github.com/c9s/bbgo/pkg/fixedpoint" ) +type IntervalProfitCollector struct { + Interval Interval `json:"interval"` + Profits *Float64Slice `json:"profits"` + tmpTime time.Time `json:"tmpTime"` +} + +func NewIntervalProfitCollector(i Interval, startTime time.Time) *IntervalProfitCollector { + return &IntervalProfitCollector{Interval: i, tmpTime: startTime, Profits: &Float64Slice{1.}} +} + +// Update the collector by every traded profit +func (s *IntervalProfitCollector) Update(profit *Profit) { + if s.tmpTime.IsZero() { + panic("No valid start time. Did you create IntervalProfitCollector instance using NewIntervalProfitCollector?") + } else { + duration := s.Interval.Duration() + if profit.TradedAt.Before(s.tmpTime.Add(duration)) { + (*s.Profits)[len(*s.Profits)-1] *= 1. + profit.NetProfitMargin.Float64() + } else { + for { + s.Profits.Update(1.) + s.tmpTime = s.tmpTime.Add(duration) + if profit.TradedAt.Before(s.tmpTime.Add(duration)) { + (*s.Profits)[len(*s.Profits)-1] *= 1. + profit.NetProfitMargin.Float64() + break + } + } + } + } +} + +// Get number of profitable traded intervals +func (s *IntervalProfitCollector) GetNumOfProfitableIntervals() (profit int) { + if s.Profits == nil { + panic("profits array empty. Did you create IntervalProfitCollector instance using NewIntervalProfitCollector?") + } + for _, v := range *s.Profits { + if v > 1. { + profit += 1 + } + } + return profit +} + +// Get number of non-profitable traded intervals +// (no trade within the interval or pnl = 0 will be also included here) +func (s *IntervalProfitCollector) GetNumOfNonProfitableIntervals() (nonprofit int) { + if s.Profits == nil { + panic("profits array empty. Did you create IntervalProfitCollector instance using NewIntervalProfitCollector?") + } + for _, v := range *s.Profits { + if v <= 1. { + nonprofit += 1 + } + } + return nonprofit +} + +// Get sharpe value with the interval of profit collected. +// no smart sharpe ON for the calculated result +func (s *IntervalProfitCollector) GetSharpe() float64 { + if s.tmpTime.IsZero() { + panic("No valid start time. Did you create IntervalProfitCollector instance using NewIntervalProfitCollector?") + } + if s.Profits == nil { + panic("profits array empty. Did you create IntervalProfitCollector instance using NewIntervalProfitCollector?") + } + return Sharpe(Minus(s.Profits, 1.), s.Profits.Length(), true, false) +} + +func (s *IntervalProfitCollector) GetOmega() float64 { + return Omega(Minus(s.Profits, 1.)) +} + +func (s IntervalProfitCollector) MarshalYAML() (interface{}, error) { + result := make(map[string]interface{}) + result["Sharpe Ratio"] = s.GetSharpe() + result["Omega Ratio"] = s.GetOmega() + result["Profitable Count"] = s.GetNumOfProfitableIntervals() + result["NonProfitable Count"] = s.GetNumOfNonProfitableIntervals() + return result, nil +} + // TODO: Add more stats from the reference: // See https://www.metatrader5.com/en/terminal/help/algotrading/testing_report type TradeStats struct { - Symbol string `json:"symbol"` - WinningRatio fixedpoint.Value `json:"winningRatio" yaml:"winningRatio"` - NumOfLossTrade int `json:"numOfLossTrade" yaml:"numOfLossTrade"` - NumOfProfitTrade int `json:"numOfProfitTrade" yaml:"numOfProfitTrade"` - GrossProfit fixedpoint.Value `json:"grossProfit" yaml:"grossProfit"` - GrossLoss fixedpoint.Value `json:"grossLoss" yaml:"grossLoss"` - Profits []fixedpoint.Value `json:"profits" yaml:"profits"` - Losses []fixedpoint.Value `json:"losses" yaml:"losses"` - MostProfitableTrade fixedpoint.Value `json:"mostProfitableTrade" yaml:"mostProfitableTrade"` - MostLossTrade fixedpoint.Value `json:"mostLossTrade" yaml:"mostLossTrade"` - ProfitFactor fixedpoint.Value `json:"profitFactor" yaml:"profitFactor"` - TotalNetProfit fixedpoint.Value `json:"totalNetProfit" yaml:"totalNetProfit"` + Symbol string `json:"symbol"` + WinningRatio fixedpoint.Value `json:"winningRatio" yaml:"winningRatio"` + NumOfLossTrade int `json:"numOfLossTrade" yaml:"numOfLossTrade"` + NumOfProfitTrade int `json:"numOfProfitTrade" yaml:"numOfProfitTrade"` + GrossProfit fixedpoint.Value `json:"grossProfit" yaml:"grossProfit"` + GrossLoss fixedpoint.Value `json:"grossLoss" yaml:"grossLoss"` + Profits []fixedpoint.Value `json:"profits" yaml:"profits"` + Losses []fixedpoint.Value `json:"losses" yaml:"losses"` + MostProfitableTrade fixedpoint.Value `json:"mostProfitableTrade" yaml:"mostProfitableTrade"` + MostLossTrade fixedpoint.Value `json:"mostLossTrade" yaml:"mostLossTrade"` + ProfitFactor fixedpoint.Value `json:"profitFactor" yaml:"profitFactor"` + TotalNetProfit fixedpoint.Value `json:"totalNetProfit" yaml:"totalNetProfit"` + IntervalProfits map[Interval]*IntervalProfitCollector `jons:"intervalProfits,omitempty" yaml: "intervalProfits,omitempty"` } func NewTradeStats(symbol string) *TradeStats { - return &TradeStats{Symbol: symbol} + return &TradeStats{Symbol: symbol, IntervalProfits: make(map[Interval]*IntervalProfitCollector)} +} + +// Set IntervalProfitCollector explicitly to enable the sharpe ratio calculation +func (s *TradeStats) SetIntervalProfitCollector(c *IntervalProfitCollector) { + s.IntervalProfits[c.Interval] = c } func (s *TradeStats) Add(profit *Profit) { @@ -33,6 +124,9 @@ func (s *TradeStats) Add(profit *Profit) { } s.add(profit.Profit) + for _, v := range s.IntervalProfits { + v.Update(profit) + } } func (s *TradeStats) add(pnl fixedpoint.Value) { @@ -61,6 +155,24 @@ func (s *TradeStats) add(pnl fixedpoint.Value) { s.ProfitFactor = s.GrossProfit.Div(s.GrossLoss.Abs()) } +// Output TradeStats without Profits and Losses +func (s *TradeStats) BriefString() string { + out, _ := yaml.Marshal(&TradeStats{ + Symbol: s.Symbol, + WinningRatio: s.WinningRatio, + NumOfLossTrade: s.NumOfLossTrade, + NumOfProfitTrade: s.NumOfProfitTrade, + GrossProfit: s.GrossProfit, + GrossLoss: s.GrossLoss, + MostProfitableTrade: s.MostProfitableTrade, + MostLossTrade: s.MostLossTrade, + ProfitFactor: s.ProfitFactor, + TotalNetProfit: s.TotalNetProfit, + IntervalProfits: s.IntervalProfits, + }) + return string(out) +} + func (s *TradeStats) String() string { out, _ := yaml.Marshal(s) return string(out) diff --git a/pkg/strategy/ewoDgtrd/trylock.go b/pkg/util/trylock.go similarity index 53% rename from pkg/strategy/ewoDgtrd/trylock.go rename to pkg/util/trylock.go index f3e6e551a4..5913b76bd9 100644 --- a/pkg/strategy/ewoDgtrd/trylock.go +++ b/pkg/util/trylock.go @@ -1,16 +1,16 @@ //go:build !go1.18 // +build !go1.18 -package ewoDgtrd +package util import "sync" -func tryLock(lock *sync.RWMutex) bool { +func TryLock(lock *sync.RWMutex) bool { lock.Lock() return true } -func tryRLock(lock *sync.RWMutex) bool { +func TryRLock(lock *sync.RWMutex) bool { lock.RLock() return true } diff --git a/pkg/strategy/ewoDgtrd/trylock_18.go b/pkg/util/trylock_18.go similarity index 51% rename from pkg/strategy/ewoDgtrd/trylock_18.go rename to pkg/util/trylock_18.go index 1511766ae3..9e9323789a 100644 --- a/pkg/strategy/ewoDgtrd/trylock_18.go +++ b/pkg/util/trylock_18.go @@ -1,14 +1,14 @@ //go:build go1.18 // +build go1.18 -package ewoDgtrd +package util import "sync" -func tryLock(lock *sync.RWMutex) bool { +func TryLock(lock *sync.RWMutex) bool { return lock.TryLock() } -func tryRLock(lock *sync.RWMutex) bool { +func TryRLock(lock *sync.RWMutex) bool { return lock.TryRLock() }