strategy/linregmaker: parameter of check main trend interval

This commit is contained in:
Andy Cheng 2022-11-24 16:51:37 +08:00
parent 41e27a8e38
commit 8c57dec793
2 changed files with 40 additions and 17 deletions

View File

@ -43,15 +43,18 @@ exchangeStrategies:
interval: 1d
window: 60
# reverseInterval
reverseInterval: 4h
# fastLinReg
fastLinReg:
interval: 1m
window: 20
window: 30
# slowLinReg
slowLinReg:
interval: 1m
window: 60
window: 120
# allowOppositePosition
allowOppositePosition: true
@ -61,7 +64,7 @@ exchangeStrategies:
# neutralBollinger
neutralBollinger:
interval: "5m"
interval: "15m"
window: 21
bandWidth: 2.0
@ -120,8 +123,8 @@ exchangeStrategies:
window: 20
dynamicQuantityLinRegScale:
byPercentage:
# exp means we want to use exponential scale, you can replace "exp" with "linear" for linear scale
exp:
# log means we want to use log scale, you can replace "log" with "linear" for linear scale
linear:
# from lower band -100% (-1) to upper band 100% (+1)
domain: [ -1, 1 ]
# when in down band, holds 1.0 by maximum
@ -134,8 +137,8 @@ exchangeStrategies:
window: 20
dynamicQuantityLinRegScale:
byPercentage:
# exp means we want to use exponential scale, you can replace "exp" with "linear" for linear scale
exp:
# log means we want to use log scale, you can replace "log" with "linear" for linear scale
linear:
# from lower band -100% (-1) to upper band 100% (+1)
domain: [ 1, -1 ]
# when in down band, holds 1.0 by maximum
@ -145,4 +148,4 @@ exchangeStrategies:
exits:
# roiStopLoss is the stop loss percentage of the position ROI (currently the price change)
- roiStopLoss:
percentage: 20%
percentage: 30%

View File

@ -45,6 +45,10 @@ type Strategy struct {
// All the opposite trend position will be closed upon the trend change
ReverseEMA *indicator.EWMA `json:"reverseEMA"`
// ReverseInterval is the interval to check trend reverse against ReverseEMA. Close price of this interval crossing
// the ReverseEMA triggers main trend change.
ReverseInterval types.Interval `json:"reverseInterval"`
// mainTrendCurrent is the current long-term trend
mainTrendCurrent types.Direction
// mainTrendPrevious is the long-term trend of previous kline
@ -158,6 +162,11 @@ func (s *Strategy) Validate() error {
return errors.New("reverseEMA must be set")
}
// Use interval of ReverseEMA if ReverseInterval is omitted
if s.ReverseInterval == "" {
s.ReverseInterval = s.ReverseEMA.Interval
}
if s.FastLinReg == nil {
return errors.New("fastLinReg must be set")
}
@ -177,6 +186,11 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
// Initialize ReverseEMA
s.ReverseEMA = s.StandardIndicatorSet.EWMA(s.ReverseEMA.IntervalWindow)
// Subscribe for ReverseInterval
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
Interval: s.ReverseInterval,
})
// Subscribe for LinRegs
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
Interval: s.FastLinReg.Interval,
@ -443,15 +457,8 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
_ = s.ClosePosition(ctx, fixedpoint.NewFromFloat(1.0))
})
// Main interval
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.Interval, func(kline types.KLine) {
// StrategyController
if s.Status != types.StrategyStatusRunning {
return
}
_ = s.orderExecutor.GracefulCancel(ctx)
// Check trend reversal
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.ReverseInterval, func(kline types.KLine) {
// closePrice is the close price of current kline
closePrice := kline.GetClose()
// priceReverseEMA is the current ReverseEMA price
@ -464,6 +471,19 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
} else if closePrice.Compare(priceReverseEMA) < 0 {
s.mainTrendCurrent = types.DirectionDown
}
}))
// Main interval
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.Interval, func(kline types.KLine) {
// StrategyController
if s.Status != types.StrategyStatusRunning {
return
}
_ = s.orderExecutor.GracefulCancel(ctx)
// closePrice is the close price of current kline
closePrice := kline.GetClose()
// Trend reversal
if s.mainTrendCurrent != s.mainTrendPrevious {