Skip to content

Commit 8315cd8

Browse files
committed
strategy:irr redesign trigger
1 parent f761579 commit 8315cd8

File tree

4 files changed

+164
-153
lines changed

4 files changed

+164
-153
lines changed

config/irr.yaml

+3-17
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,11 @@ exchangeStrategies:
1515
- on: binance
1616
irr:
1717
symbol: BTCBUSD
18-
interval: 1s
19-
window: 120
18+
# in milliseconds(ms)
19+
hftInterval: 1000
20+
# maxima position in USD
2021
amount: 500.0
21-
humpThreshold: 0.000025
2222
# Draw pnl
2323
drawGraph: true
2424
graphPNLPath: "./pnl.png"
2525
graphCumPNLPath: "./cumpnl.png"
26-
27-
backtest:
28-
sessions:
29-
- binance
30-
startTime: "2022-10-09"
31-
endTime: "2022-10-11"
32-
# syncSecKLines: true
33-
symbols:
34-
- BTCBUSD
35-
accounts:
36-
binance:
37-
takerFeeRate: 0.0
38-
balances:
39-
BUSD: 5_000.0

pkg/strategy/irr/draw.go

+25-10
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,41 @@ import (
1111
"github.com/wcharczuk/go-chart/v2"
1212
)
1313

14-
func (s *Strategy) InitDrawCommands(profit, cumProfit types.Series) {
15-
bbgo.RegisterCommand("/pnl", "Draw PNL(%) per trade", func(reply interact.Reply) {
14+
func (s *Strategy) InitDrawCommands(profit, cumProfit, cumProfitDollar types.Series) {
15+
bbgo.RegisterCommand("/rt", "Draw Return Rate(%) Per Trade", func(reply interact.Reply) {
16+
1617
canvas := DrawPNL(s.InstanceID(), profit)
1718
var buffer bytes.Buffer
1819
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))
20+
log.WithError(err).Errorf("cannot render return in irr")
21+
reply.Message(fmt.Sprintf("[error] cannot render return in irr: %v", err))
2122
return
2223
}
23-
bbgo.SendPhoto(&buffer)
24+
go bbgo.SendPhoto(&buffer)
2425
})
25-
bbgo.RegisterCommand("/cumpnl", "Draw Cummulative PNL(Quote)", func(reply interact.Reply) {
26+
bbgo.RegisterCommand("/nav", "Draw Net Assets Value", func(reply interact.Reply) {
27+
2628
canvas := DrawCumPNL(s.InstanceID(), cumProfit)
2729
var buffer bytes.Buffer
2830
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+
log.WithError(err).Errorf("cannot render nav in irr")
32+
reply.Message(fmt.Sprintf("[error] canot render nav in irr: %v", err))
33+
return
34+
}
35+
go bbgo.SendPhoto(&buffer)
36+
37+
})
38+
bbgo.RegisterCommand("/pnl", "Draw Cumulative Profit & Loss", func(reply interact.Reply) {
39+
40+
canvas := DrawCumPNL(s.InstanceID(), cumProfitDollar)
41+
var buffer bytes.Buffer
42+
if err := canvas.Render(chart.PNG, &buffer); err != nil {
43+
log.WithError(err).Errorf("cannot render pnl in irr")
44+
reply.Message(fmt.Sprintf("[error] canot render pnl in irr: %v", err))
3145
return
3246
}
33-
bbgo.SendPhoto(&buffer)
47+
go bbgo.SendPhoto(&buffer)
48+
3449
})
3550
}
3651

@@ -77,7 +92,7 @@ func DrawPNL(instanceID string, profit types.Series) *types.Canvas {
7792

7893
func DrawCumPNL(instanceID string, cumProfit types.Series) *types.Canvas {
7994
canvas := types.NewCanvas(instanceID)
80-
canvas.PlotRaw("cummulative pnl", cumProfit, cumProfit.Length())
95+
canvas.PlotRaw("cumulative pnl", cumProfit, cumProfit.Length())
8196
canvas.YAxis = chart.YAxis{
8297
ValueFormatter: func(v interface{}) string {
8398
if vf, isFloat := v.(float64); isFloat {

pkg/strategy/irr/neg_return_rate.go

+8-16
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,21 @@ type NRR struct {
3030

3131
var _ types.SeriesExtend = &NRR{}
3232

33-
func (inc *NRR) Update(price float64) {
33+
func (inc *NRR) Update(openPrice, closePrice float64) {
3434
if inc.SeriesBase.Series == nil {
3535
inc.SeriesBase.Series = inc
3636
inc.Prices = types.NewQueue(inc.Window)
3737
}
38-
inc.Prices.Update(price)
38+
inc.Prices.Update(closePrice)
3939
if inc.Prices.Length() < inc.Window {
4040
return
4141
}
42-
irr := (inc.Prices.Last() / inc.Prices.Index(inc.Window-1)) - 1
42+
// D0
43+
irr := openPrice - closePrice
44+
// D1
45+
// -1*((inc.Prices.Last() / inc.Prices.Index(inc.Window-1)) - 1)
4346

44-
inc.Values.Push(-irr) // neg ret here
47+
inc.Values.Push(irr) // neg ret here
4548
inc.RankedValues.Push(inc.Rank(inc.RankingWindow).Last() / float64(inc.RankingWindow)) // ranked neg ret here
4649

4750
}
@@ -75,7 +78,7 @@ func (inc *NRR) PushK(k types.KLine) {
7578
return
7679
}
7780

78-
inc.Update(indicator.KLineClosePriceMapper(k))
81+
inc.Update(indicator.KLineOpenPriceMapper(k), indicator.KLineClosePriceMapper(k))
7982
inc.EndTime = k.EndTime.Time()
8083
inc.EmitUpdate(inc.Last())
8184
}
@@ -86,14 +89,3 @@ func (inc *NRR) LoadK(allKLines []types.KLine) {
8689
}
8790
inc.EmitUpdate(inc.Last())
8891
}
89-
90-
//func calculateReturn(klines []types.KLine, window int, val KLineValueMapper) (float64, error) {
91-
// length := len(klines)
92-
// if length == 0 || length < window {
93-
// return 0.0, fmt.Errorf("insufficient elements for calculating VOL with window = %d", window)
94-
// }
95-
//
96-
// rate := val(klines[length-1])/val(klines[length-2]) - 1
97-
//
98-
// return rate, nil
99-
//}

0 commit comments

Comments
 (0)