Skip to content

Commit

Permalink
Merge pull request c9s#813 from zenixls2/feature/drift_study
Browse files Browse the repository at this point in the history
feature: drift study
  • Loading branch information
c9s committed Jul 27, 2022
2 parents 578e4b2 + 84c7c05 commit 4fd571d
Show file tree
Hide file tree
Showing 33 changed files with 1,469 additions and 84 deletions.
80 changes: 80 additions & 0 deletions config/drift.yaml
Original file line number Diff line number Diff line change
@@ -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
91 changes: 91 additions & 0 deletions config/driftBTC.yaml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
4 changes: 4 additions & 0 deletions pkg/bbgo/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions pkg/bbgo/order_executor_general.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bbgo

import (
"context"
"fmt"
"strings"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -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...)
Expand All @@ -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()
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
4 changes: 2 additions & 2 deletions pkg/indicator/alma.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions pkg/indicator/atr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 0 additions & 2 deletions pkg/indicator/boll.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
}

Expand Down
1 change: 0 additions & 1 deletion pkg/indicator/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ import (
var three = fixedpoint.NewFromInt(3)

var zeroTime = time.Time{}

17 changes: 17 additions & 0 deletions pkg/indicator/dema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 4fd571d

Please sign in to comment.