Skip to content

Commit

Permalink
feature: add plot for series. add autocorrelation. add clone for indi…
Browse files Browse the repository at this point in the history
…cators/series
  • Loading branch information
zenixls2 committed Jul 26, 2022
1 parent 69b45e9 commit c51a994
Show file tree
Hide file tree
Showing 18 changed files with 359 additions and 53 deletions.
41 changes: 21 additions & 20 deletions config/drift.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,24 @@ exchangeStrategies:
# kline interval for indicators
interval: 15m
window: 3
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
stoploss: 2%
#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%
Expand All @@ -53,8 +54,8 @@ backtest:
sessions: [binance]
accounts:
binance:
#makerFeeRate: 0
#takerFeeRate: 0
#makerFeeRate: 0.00001
#takerFeeRate: 0.00001
balances:
ETH: 0.0
ETH: 10.0
USDT: 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
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
1 change: 0 additions & 1 deletion pkg/indicator/cci.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ func (inc *CCI) Length() int {

var _ types.SeriesExtend = &CCI{}


func (inc *CCI) PushK(k types.KLine) {
inc.Update(k.High.Add(k.Low).Add(k.Close).Div(three).Float64())
}
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
18 changes: 18 additions & 0 deletions pkg/indicator/drift.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ func (inc *Drift) Update(value float64) {
}
}

func (inc *Drift) Clone() (out *Drift) {
out = &Drift{
IntervalWindow: inc.IntervalWindow,
chng: inc.chng.Clone(),
Values: inc.Values[:],
SMA: inc.SMA.Clone().(*SMA),
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
Expand Down
16 changes: 16 additions & 0 deletions pkg/indicator/ewma.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ type EWMA struct {

var _ types.SeriesExtend = &EWMA{}

func (inc *EWMA) Clone() *EWMA {
out := &EWMA{
IntervalWindow: inc.IntervalWindow,
Values: inc.Values[:],
LastOpenTime: inc.LastOpenTime,
}
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)

Expand Down
14 changes: 14 additions & 0 deletions pkg/indicator/rma.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions pkg/indicator/sma.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: types.Clone(inc.rawValues).(*types.Queue),
EndTime: inc.EndTime,
}
out.SeriesBase.Series = out
return out
}

var _ types.SeriesExtend = &SMA{}

func (inc *SMA) Update(value float64) {
Expand Down
1 change: 0 additions & 1 deletion pkg/indicator/util.go
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
package indicator

Loading

0 comments on commit c51a994

Please sign in to comment.