mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 16:25:16 +00:00
refactor draw on supertrend
This commit is contained in:
parent
3a188aa66a
commit
386ab1f6f3
|
@ -11,9 +11,9 @@ import (
|
|||
"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) {
|
||||
canvas := s.DrawPNL(profit)
|
||||
canvas := DrawPNL(s.InstanceID(), profit)
|
||||
var buffer bytes.Buffer
|
||||
if err := canvas.Render(chart.PNG, &buffer); err != nil {
|
||||
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.RegisterCommand("/cumpnl", "Draw Cummulative PNL(Quote)", func(reply interact.Reply) {
|
||||
canvas := s.DrawCumPNL(cumProfit)
|
||||
canvas := DrawCumPNL(s.InstanceID(), cumProfit)
|
||||
var buffer bytes.Buffer
|
||||
if err := canvas.Render(chart.PNG, &buffer); err != nil {
|
||||
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 {
|
||||
canvas := types.NewCanvas(s.InstanceID())
|
||||
func (s *Strategy) Draw(profit, cumProfit types.Series) error {
|
||||
|
||||
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()
|
||||
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.YAxis = chart.YAxis{
|
||||
ValueFormatter: func(v interface{}) string {
|
||||
|
@ -51,8 +75,8 @@ func (s *Strategy) DrawPNL(profit types.Series) *types.Canvas {
|
|||
return canvas
|
||||
}
|
||||
|
||||
func (s *Strategy) DrawCumPNL(cumProfit types.Series) *types.Canvas {
|
||||
canvas := types.NewCanvas(s.InstanceID())
|
||||
func DrawCumPNL(instanceID string, cumProfit types.Series) *types.Canvas {
|
||||
canvas := types.NewCanvas(instanceID)
|
||||
canvas.PlotRaw("cummulative pnl", cumProfit, cumProfit.Length())
|
||||
canvas.YAxis = chart.YAxis{
|
||||
ValueFormatter: func(v interface{}) string {
|
||||
|
@ -64,28 +88,3 @@ func (s *Strategy) DrawCumPNL(cumProfit types.Series) *types.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")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -540,12 +540,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
|||
}
|
||||
})
|
||||
|
||||
// event trigger order: s.Interval => Interval1m
|
||||
store, ok := session.SerialMarketDataStore(s.Symbol, []types.Interval{s.Interval, types.Interval1m})
|
||||
if !ok {
|
||||
panic("cannot get 1m history")
|
||||
}
|
||||
s.InitDrawCommands(store, &profitSlice, &cumProfitSlice)
|
||||
s.InitDrawCommands(&profitSlice, &cumProfitSlice)
|
||||
|
||||
// Sync position to redis on trade
|
||||
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)
|
||||
|
||||
if s.DrawGraph {
|
||||
s.Draw(store, &profitSlice, &cumProfitSlice)
|
||||
if err := s.Draw(&profitSlice, &cumProfitSlice); err != nil {
|
||||
log.WithError(err).Errorf("cannot draw graph")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user