Skip to content

Commit

Permalink
fix: unlimited length of indicators, add draw elapsed to drift
Browse files Browse the repository at this point in the history
  • Loading branch information
zenixls2 committed Oct 27, 2022
1 parent 493b81f commit b2e867e
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 172 deletions.
1 change: 1 addition & 0 deletions config/driftBTC.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ exchangeStrategies:
graphPNLDeductFee: false
graphPNLPath: "./pnl.png"
graphCumPNLPath: "./cumpnl.png"
graphElapsedPath: "./elapsed.png"
#exits:
# - roiStopLoss:
# percentage: 0.35%
Expand Down
6 changes: 6 additions & 0 deletions pkg/indicator/atr.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"github.com/c9s/bbgo/pkg/types"
)

const MaxNumOfATR = 1000
const MaxNumOfATRTruncateSize = 500

//go:generate callbackgen -type ATR
type ATR struct {
types.SeriesBase
Expand Down Expand Up @@ -73,6 +76,9 @@ func (inc *ATR) Update(high, low, cloze float64) {
inc.RMA.Update(trueRange)
atr := inc.RMA.Last()
inc.PercentageVolatility.Push(atr / cloze)
if len(inc.PercentageVolatility) > MaxNumOfATR {
inc.PercentageVolatility = inc.PercentageVolatility[MaxNumOfATRTruncateSize-1:]
}
}

func (inc *ATR) Last() float64 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/indicator/ewma.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
)

// These numbers should be aligned with bbgo MaxNumOfKLines and MaxNumOfKLinesTruncate
const MaxNumOfEWMA = 5_000
const MaxNumOfEWMATruncateSize = 100
const MaxNumOfEWMA = 1_000
const MaxNumOfEWMATruncateSize = 500

//go:generate callbackgen -type EWMA
type EWMA struct {
Expand Down
6 changes: 6 additions & 0 deletions pkg/indicator/rma.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"github.com/c9s/bbgo/pkg/types"
)

const MaxNumOfRMA = 1000
const MaxNumOfRMATruncateSize = 500

// Running Moving Average
// Refer: https://github.com/twopirllc/pandas-ta/blob/main/pandas_ta/overlap/rma.py#L5
// Refer: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.ewm.html#pandas-dataframe-ewm
Expand Down Expand Up @@ -62,6 +65,9 @@ func (inc *RMA) Update(x float64) {
}

inc.Values.Push(inc.tmp)
if len(inc.Values) > MaxNumOfRMA {
inc.Values = inc.Values[MaxNumOfRMATruncateSize-1:]
}
}

func (inc *RMA) Last() float64 {
Expand Down
3 changes: 3 additions & 0 deletions pkg/indicator/sma.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func (inc *SMA) Update(value float64) {
}

inc.Values.Push(types.Mean(inc.rawValues))
if len(inc.Values) > MaxNumOfSMA {
inc.Values = inc.Values[MaxNumOfSMATruncateSize-1:]
}
}

func (inc *SMA) BindK(target KLineClosedEmitter, symbol string, interval types.Interval) {
Expand Down
6 changes: 6 additions & 0 deletions pkg/indicator/stddev.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"github.com/c9s/bbgo/pkg/types"
)

const MaxNumOfStdev = 600
const MaxNumOfStdevTruncateSize = 300

//go:generate callbackgen -type StdDev
type StdDev struct {
types.SeriesBase
Expand Down Expand Up @@ -49,6 +52,9 @@ func (inc *StdDev) Update(value float64) {

var std = inc.rawValues.Stdev()
inc.Values.Push(std)
if len(inc.Values) > MaxNumOfStdev {
inc.Values = inc.Values[MaxNumOfStdevTruncateSize-1:]
}
}

func (inc *StdDev) PushK(k types.KLine) {
Expand Down
175 changes: 175 additions & 0 deletions pkg/strategy/drift/draw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package drift

import (
"bytes"
"fmt"
"os"

"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/interact"
"github.com/c9s/bbgo/pkg/types"
"github.com/wcharczuk/go-chart/v2"
)

func (s *Strategy) InitDrawCommands(profit, cumProfit types.Series) {
bbgo.RegisterCommand("/draw", "Draw Indicators", func(reply interact.Reply) {
go func() {
canvas := s.DrawIndicators(s.frameKLine.StartTime)
var buffer bytes.Buffer
if err := canvas.Render(chart.PNG, &buffer); err != nil {
log.WithError(err).Errorf("cannot render indicators in drift")
return
}
bbgo.SendPhoto(&buffer)
}()
})

bbgo.RegisterCommand("/pnl", "Draw PNL(%) per trade", func(reply interact.Reply) {
go func() {
canvas := s.DrawPNL(profit)
var buffer bytes.Buffer
if err := canvas.Render(chart.PNG, &buffer); err != nil {
log.WithError(err).Errorf("cannot render pnl in drift")
return
}
bbgo.SendPhoto(&buffer)
}()
})

bbgo.RegisterCommand("/cumpnl", "Draw Cummulative PNL(Quote)", func(reply interact.Reply) {
go func() {
canvas := s.DrawCumPNL(cumProfit)
var buffer bytes.Buffer
if err := canvas.Render(chart.PNG, &buffer); err != nil {
log.WithError(err).Errorf("cannot render cumpnl in drift")
return
}
bbgo.SendPhoto(&buffer)
}()
})

bbgo.RegisterCommand("/elapsed", "Draw Elapsed time for handlers for each kline close event", func(reply interact.Reply) {
go func() {
canvas := s.DrawElapsed()
var buffer bytes.Buffer
if err := canvas.Render(chart.PNG, &buffer); err != nil {
log.WithError(err).Errorf("cannot render elapsed in drift")
return
}
bbgo.SendPhoto(&buffer)
}()
})
}

func (s *Strategy) DrawIndicators(time types.Time) *types.Canvas {
canvas := types.NewCanvas(s.InstanceID(), s.Interval)
Length := s.priceLines.Length()
if Length > 300 {
Length = 300
}
log.Infof("draw indicators with %d data", Length)
mean := s.priceLines.Mean(Length)
highestPrice := s.priceLines.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)
fmt.Printf("%f %f\n", highestPrice, hi)

canvas.Plot("trend", s.trendLine, 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", s.priceLines, time, Length)
return canvas
}

func (s *Strategy) DrawPNL(profit types.Series) *types.Canvas {
canvas := types.NewCanvas(s.InstanceID())
log.Errorf("pnl Highest: %f, Lowest: %f", types.Highest(profit, profit.Length()), types.Lowest(profit, profit.Length()))
length := profit.Length()
if s.GraphPNLDeductFee {
canvas.PlotRaw("pnl % (with Fee Deducted)", profit, length)
} else {
canvas.PlotRaw("pnl %", profit, length)
}
canvas.YAxis = chart.YAxis{
ValueFormatter: func(v interface{}) string {
if vf, isFloat := v.(float64); isFloat {
return fmt.Sprintf("%.4f", vf)
}
return ""
},
}
canvas.PlotRaw("1", types.NumberSeries(1), length)
return canvas
}

func (s *Strategy) DrawCumPNL(cumProfit types.Series) *types.Canvas {
canvas := types.NewCanvas(s.InstanceID())
canvas.PlotRaw("cummulative pnl", cumProfit, cumProfit.Length())
canvas.YAxis = chart.YAxis{
ValueFormatter: func(v interface{}) string {
if vf, isFloat := v.(float64); isFloat {
return fmt.Sprintf("%.4f", vf)
}
return ""
},
}
return canvas
}

func (s *Strategy) DrawElapsed() *types.Canvas {
canvas := types.NewCanvas(s.InstanceID())
canvas.PlotRaw("elapsed time(ms)", s.elapsed, s.elapsed.Length())
return canvas
}

func (s *Strategy) Draw(time types.Time, profit types.Series, cumProfit types.Series) {
canvas := s.DrawIndicators(time)
f, err := os.Create(s.CanvasPath)
if err != nil {
log.WithError(err).Errorf("cannot create on %s", s.CanvasPath)
return
}
if err := canvas.Render(chart.PNG, f); err != nil {
log.WithError(err).Errorf("cannot render in drift")
}
f.Close()

canvas = s.DrawPNL(profit)
f, err = os.Create(s.GraphPNLPath)
if err != nil {
log.WithError(err).Errorf("open pnl")
return
}
if err := canvas.Render(chart.PNG, f); err != nil {
log.WithError(err).Errorf("render pnl")
}
f.Close()

canvas = s.DrawCumPNL(cumProfit)
f, err = os.Create(s.GraphCumPNLPath)
if err != nil {
log.WithError(err).Errorf("open cumpnl")
return
}
if err := canvas.Render(chart.PNG, f); err != nil {
log.WithError(err).Errorf("render cumpnl")
}
f.Close()

canvas = s.DrawElapsed()
f, err = os.Create(s.GraphElapsedPath)
if err != nil {
log.WithError(err).Errorf("open elapsed")
return
}
if err := canvas.Render(chart.PNG, f); err != nil {
log.WithError(err).Errorf("render elapsed")
}
f.Close()
}
Loading

0 comments on commit b2e867e

Please sign in to comment.