|
| 1 | +package harmonic |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/c9s/bbgo/pkg/bbgo" |
| 9 | + "github.com/c9s/bbgo/pkg/interact" |
| 10 | + "github.com/c9s/bbgo/pkg/types" |
| 11 | + "github.com/wcharczuk/go-chart/v2" |
| 12 | +) |
| 13 | + |
| 14 | +func (s *Strategy) InitDrawCommands(profit, cumProfit types.Series) { |
| 15 | + bbgo.RegisterCommand("/pnl", "Draw PNL(%) per trade", func(reply interact.Reply) { |
| 16 | + canvas := DrawPNL(s.InstanceID(), profit) |
| 17 | + var buffer bytes.Buffer |
| 18 | + if err := canvas.Render(chart.PNG, &buffer); err != nil { |
| 19 | + log.WithError(err).Errorf("cannot render pnl in drift") |
| 20 | + reply.Message(fmt.Sprintf("[error] cannot render pnl in ewo: %v", err)) |
| 21 | + return |
| 22 | + } |
| 23 | + bbgo.SendPhoto(&buffer) |
| 24 | + }) |
| 25 | + bbgo.RegisterCommand("/cumpnl", "Draw Cummulative PNL(Quote)", func(reply interact.Reply) { |
| 26 | + canvas := DrawCumPNL(s.InstanceID(), cumProfit) |
| 27 | + var buffer bytes.Buffer |
| 28 | + if err := canvas.Render(chart.PNG, &buffer); err != nil { |
| 29 | + log.WithError(err).Errorf("cannot render cumpnl in drift") |
| 30 | + reply.Message(fmt.Sprintf("[error] canot render cumpnl in drift: %v", err)) |
| 31 | + return |
| 32 | + } |
| 33 | + bbgo.SendPhoto(&buffer) |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +func (s *Strategy) Draw(profit, cumProfit types.Series) error { |
| 38 | + |
| 39 | + canvas := DrawPNL(s.InstanceID(), profit) |
| 40 | + f, err := os.Create(s.GraphPNLPath) |
| 41 | + if err != nil { |
| 42 | + return fmt.Errorf("cannot create on path " + s.GraphPNLPath) |
| 43 | + } |
| 44 | + defer f.Close() |
| 45 | + if err = canvas.Render(chart.PNG, f); err != nil { |
| 46 | + return fmt.Errorf("cannot render pnl") |
| 47 | + } |
| 48 | + canvas = DrawCumPNL(s.InstanceID(), cumProfit) |
| 49 | + f, err = os.Create(s.GraphCumPNLPath) |
| 50 | + if err != nil { |
| 51 | + return fmt.Errorf("cannot create on path " + s.GraphCumPNLPath) |
| 52 | + } |
| 53 | + defer f.Close() |
| 54 | + if err = canvas.Render(chart.PNG, f); err != nil { |
| 55 | + return fmt.Errorf("cannot render cumpnl") |
| 56 | + } |
| 57 | + |
| 58 | + return nil |
| 59 | +} |
| 60 | + |
| 61 | +func DrawPNL(instanceID string, profit types.Series) *types.Canvas { |
| 62 | + canvas := types.NewCanvas(instanceID) |
| 63 | + length := profit.Length() |
| 64 | + log.Infof("pnl Highest: %f, Lowest: %f", types.Highest(profit, length), types.Lowest(profit, length)) |
| 65 | + canvas.PlotRaw("pnl %", profit, length) |
| 66 | + canvas.YAxis = chart.YAxis{ |
| 67 | + ValueFormatter: func(v interface{}) string { |
| 68 | + if vf, isFloat := v.(float64); isFloat { |
| 69 | + return fmt.Sprintf("%.4f", vf) |
| 70 | + } |
| 71 | + return "" |
| 72 | + }, |
| 73 | + } |
| 74 | + canvas.PlotRaw("1", types.NumberSeries(1), length) |
| 75 | + return canvas |
| 76 | +} |
| 77 | + |
| 78 | +func DrawCumPNL(instanceID string, cumProfit types.Series) *types.Canvas { |
| 79 | + canvas := types.NewCanvas(instanceID) |
| 80 | + canvas.PlotRaw("cummulative pnl", cumProfit, cumProfit.Length()) |
| 81 | + canvas.YAxis = chart.YAxis{ |
| 82 | + ValueFormatter: func(v interface{}) string { |
| 83 | + if vf, isFloat := v.(float64); isFloat { |
| 84 | + return fmt.Sprintf("%.4f", vf) |
| 85 | + } |
| 86 | + return "" |
| 87 | + }, |
| 88 | + } |
| 89 | + return canvas |
| 90 | +} |
0 commit comments