strategy: refactor oneliner to irr

This commit is contained in:
austin362667 2022-09-20 10:32:41 +08:00
parent d7b9721224
commit beb13449cb
5 changed files with 14 additions and 57 deletions

View File

@ -17,9 +17,9 @@ import (
_ "github.com/c9s/bbgo/pkg/strategy/fmaker"
_ "github.com/c9s/bbgo/pkg/strategy/funding"
_ "github.com/c9s/bbgo/pkg/strategy/grid"
_ "github.com/c9s/bbgo/pkg/strategy/irr"
_ "github.com/c9s/bbgo/pkg/strategy/kline"
_ "github.com/c9s/bbgo/pkg/strategy/marketcap"
_ "github.com/c9s/bbgo/pkg/strategy/oneliner"
_ "github.com/c9s/bbgo/pkg/strategy/pivotshort"
_ "github.com/c9s/bbgo/pkg/strategy/pricealert"
_ "github.com/c9s/bbgo/pkg/strategy/pricedrop"

View File

@ -1,4 +1,4 @@
package oneliner
package irr
import (
"time"
@ -66,31 +66,6 @@ func (inc *NRR) Length() int {
return len(inc.Values)
}
func (inc *NRR) CalculateAndUpdate(allKLines []types.KLine) {
if len(inc.Values) == 0 {
for _, k := range allKLines {
inc.PushK(k)
}
inc.EmitUpdate(inc.Last())
} else {
k := allKLines[len(allKLines)-1]
inc.PushK(k)
inc.EmitUpdate(inc.Last())
}
}
func (inc *NRR) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
if inc.Interval != interval {
return
}
inc.CalculateAndUpdate(window)
}
func (inc *NRR) Bind(updater indicator.KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}
func (inc *NRR) BindK(target indicator.KLineClosedEmitter, symbol string, interval types.Interval) {
target.OnKLineClosed(types.KLineWith(symbol, interval, inc.PushK))
}

View File

@ -1,6 +1,6 @@
// Code generated by "callbackgen -type NRR"; DO NOT EDIT.
package oneliner
package irr
import ()

View File

@ -1,4 +1,4 @@
package oneliner
package irr
import (
"bytes"
@ -32,10 +32,6 @@ func init() {
bbgo.RegisterStrategy(ID, &Strategy{})
}
type IntervalWindowSetting struct {
types.IntervalWindow
}
type Strategy struct {
Environment *bbgo.Environment
Symbol string `json:"symbol"`
@ -101,16 +97,6 @@ type Strategy struct {
TrailingCallbackRate []float64 `json:"trailingCallbackRate"`
TrailingActivationRatio []float64 `json:"trailingActivationRatio"`
DriftFilterNeg float64 `json:"driftFilterNeg"`
DriftFilterPos float64 `json:"driftFilterPos"`
DDriftFilterNeg float64 `json:"ddriftFilterNeg"`
DDriftFilterPos float64 `json:"ddriftFilterPos"`
buyPrice float64 `persistence:"buy_price"`
sellPrice float64 `persistence:"sell_price"`
highestPrice float64 `persistence:"highest_price"`
lowestPrice float64 `persistence:"lowest_price"`
// This is not related to trade but for statistics graph generation
// Will deduct fee in percentage from every trade
GraphPNLDeductFee bool `json:"graphPNLDeductFee"`
@ -202,20 +188,22 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
method.Bind(session, s.orderExecutor)
}
store, _ := session.MarketDataStore(s.Symbol)
kLineStore, _ := s.session.MarketDataStore(s.Symbol)
s.nrr = &NRR{IntervalWindow: types.IntervalWindow{Window: 2, Interval: s.Interval}, RankingWindow: s.Window}
s.nrr.Bind(store)
s.nrr.BindK(s.session.MarketDataStream, s.Symbol, s.Interval)
if klines, ok := kLineStore.KLinesOfInterval(s.nrr.Interval); ok {
s.nrr.LoadK((*klines)[0:])
}
//startTime := s.Environment.StartTime()
//s.TradeStats.SetIntervalProfitCollector(types.NewIntervalProfitCollector(types.Interval1h, startTime))
// queued first signal as its initial process
//alphaLst := types.NewQueue(2)
s.session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.Interval, func(kline types.KLine) {
// transformed to [0~1] which divided equally
//alpha := s.nrr.RankedValues.Last()
// ts_rank(): transformed to [0~1] which divided equally
// queued first signal as its initial process
// important: delayed signal in order to submit order at current kline close (a.k.a. next open while in production)
// instead of right in current kline open
// alpha-weighted assets (inventory and capital)
targetBase := s.QuantityOrAmount.CalculateQuantity(kline.Close).Mul(fixedpoint.NewFromFloat(s.nrr.RankedValues.Index(1)))
@ -230,9 +218,6 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.orderExecutor.OpenPosition(context.Background(), bbgo.OpenPositionOptions{Quantity: diffQty.Abs(), Short: true, MarketOrder: true})
}
// important: delayed signal in order to submit order at current kline close (a.k.a. next open while in production)
// instead of right in current kline open
//alphaLst.Update(alpha)
}))
bbgo.RegisterCommand("/draw", "Draw Indicators", func(reply interact.Reply) {
@ -374,10 +359,7 @@ func (s *Strategy) DrawIndicators(time types.Time) *types.Canvas {
hi := s.alpha.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)
canvas.Plot("drift", s.alpha.Mul(ratio).Add(mean), time, Length)
canvas.Plot("alpha", s.alpha.Mul(ratio).Add(mean), time, Length)
canvas.Plot("driftOrig", s.alpha.Mul(highestPrice/hi).Add(mean), time, Length)
canvas.Plot("zero", types.NumberSeries(mean), time, Length)
canvas.Plot("price", s.priceLines, time, Length)