refactor draw on supertrend

This commit is contained in:
Fredrik 2022-09-11 09:48:08 +02:00
parent 3a188aa66a
commit 386ab1f6f3
2 changed files with 36 additions and 40 deletions

View File

@ -11,9 +11,9 @@ import (
"github.com/wcharczuk/go-chart/v2" "github.com/wcharczuk/go-chart/v2"
) )
func (s *Strategy) InitDrawCommands(store *bbgo.SerialMarketDataStore, profit, cumProfit types.Series) { func (s *Strategy) InitDrawCommands(profit, cumProfit types.Series) {
bbgo.RegisterCommand("/pnl", "Draw PNL(%) per trade", func(reply interact.Reply) { bbgo.RegisterCommand("/pnl", "Draw PNL(%) per trade", func(reply interact.Reply) {
canvas := s.DrawPNL(profit) canvas := DrawPNL(s.InstanceID(), profit)
var buffer bytes.Buffer var buffer bytes.Buffer
if err := canvas.Render(chart.PNG, &buffer); err != nil { if err := canvas.Render(chart.PNG, &buffer); err != nil {
log.WithError(err).Errorf("cannot render pnl in drift") log.WithError(err).Errorf("cannot render pnl in drift")
@ -23,7 +23,7 @@ func (s *Strategy) InitDrawCommands(store *bbgo.SerialMarketDataStore, profit, c
bbgo.SendPhoto(&buffer) bbgo.SendPhoto(&buffer)
}) })
bbgo.RegisterCommand("/cumpnl", "Draw Cummulative PNL(Quote)", func(reply interact.Reply) { bbgo.RegisterCommand("/cumpnl", "Draw Cummulative PNL(Quote)", func(reply interact.Reply) {
canvas := s.DrawCumPNL(cumProfit) canvas := DrawCumPNL(s.InstanceID(), cumProfit)
var buffer bytes.Buffer var buffer bytes.Buffer
if err := canvas.Render(chart.PNG, &buffer); err != nil { if err := canvas.Render(chart.PNG, &buffer); err != nil {
log.WithError(err).Errorf("cannot render cumpnl in drift") log.WithError(err).Errorf("cannot render cumpnl in drift")
@ -34,10 +34,34 @@ func (s *Strategy) InitDrawCommands(store *bbgo.SerialMarketDataStore, profit, c
}) })
} }
func (s *Strategy) DrawPNL(profit types.Series) *types.Canvas { func (s *Strategy) Draw(profit, cumProfit types.Series) error {
canvas := types.NewCanvas(s.InstanceID())
canvas := DrawPNL(s.InstanceID(), profit)
f, err := os.Create(s.GraphPNLPath)
if err != nil {
return fmt.Errorf("cannot create on path " + s.GraphPNLPath)
}
defer f.Close()
if err = canvas.Render(chart.PNG, f); err != nil {
return fmt.Errorf("cannot render pnl")
}
canvas = DrawCumPNL(s.InstanceID(), cumProfit)
f, err = os.Create(s.GraphCumPNLPath)
if err != nil {
return fmt.Errorf("cannot create on path " + s.GraphCumPNLPath)
}
defer f.Close()
if err = canvas.Render(chart.PNG, f); err != nil {
return fmt.Errorf("cannot render cumpnl")
}
return nil
}
func DrawPNL(instanceID string, profit types.Series) *types.Canvas {
canvas := types.NewCanvas(instanceID)
length := profit.Length() length := profit.Length()
log.Errorf("pnl Highest: %f, Lowest: %f", types.Highest(profit, length), types.Lowest(profit, length)) log.Infof("pnl Highest: %f, Lowest: %f", types.Highest(profit, length), types.Lowest(profit, length))
canvas.PlotRaw("pnl %", profit, length) canvas.PlotRaw("pnl %", profit, length)
canvas.YAxis = chart.YAxis{ canvas.YAxis = chart.YAxis{
ValueFormatter: func(v interface{}) string { ValueFormatter: func(v interface{}) string {
@ -51,8 +75,8 @@ func (s *Strategy) DrawPNL(profit types.Series) *types.Canvas {
return canvas return canvas
} }
func (s *Strategy) DrawCumPNL(cumProfit types.Series) *types.Canvas { func DrawCumPNL(instanceID string, cumProfit types.Series) *types.Canvas {
canvas := types.NewCanvas(s.InstanceID()) canvas := types.NewCanvas(instanceID)
canvas.PlotRaw("cummulative pnl", cumProfit, cumProfit.Length()) canvas.PlotRaw("cummulative pnl", cumProfit, cumProfit.Length())
canvas.YAxis = chart.YAxis{ canvas.YAxis = chart.YAxis{
ValueFormatter: func(v interface{}) string { ValueFormatter: func(v interface{}) string {
@ -64,28 +88,3 @@ func (s *Strategy) DrawCumPNL(cumProfit types.Series) *types.Canvas {
} }
return canvas return canvas
} }
func (s *Strategy) Draw(store *bbgo.SerialMarketDataStore, profit, cumProfit types.Series) {
canvas := s.DrawPNL(profit)
f, err := os.Create(s.GraphPNLPath)
if err != nil {
log.WithError(err).Errorf("cannot create on path " + s.GraphPNLPath)
return
}
defer f.Close()
if err = canvas.Render(chart.PNG, f); err != nil {
log.WithError(err).Errorf("cannot render pnl")
return
}
canvas = s.DrawCumPNL(cumProfit)
f, err = os.Create(s.GraphCumPNLPath)
if err != nil {
log.WithError(err).Errorf("cannot create on path " + s.GraphCumPNLPath)
return
}
defer f.Close()
if err = canvas.Render(chart.PNG, f); err != nil {
log.WithError(err).Errorf("cannot render cumpnl")
}
}

View File

@ -540,12 +540,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
} }
}) })
// event trigger order: s.Interval => Interval1m s.InitDrawCommands(&profitSlice, &cumProfitSlice)
store, ok := session.SerialMarketDataStore(s.Symbol, []types.Interval{s.Interval, types.Interval1m})
if !ok {
panic("cannot get 1m history")
}
s.InitDrawCommands(store, &profitSlice, &cumProfitSlice)
// Sync position to redis on trade // Sync position to redis on trade
s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
@ -656,7 +651,9 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
defer s.AccumulatedProfitReport.Output(s.Symbol) defer s.AccumulatedProfitReport.Output(s.Symbol)
if s.DrawGraph { if s.DrawGraph {
s.Draw(store, &profitSlice, &cumProfitSlice) if err := s.Draw(&profitSlice, &cumProfitSlice); err != nil {
log.WithError(err).Errorf("cannot draw graph")
}
} }
} }