You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.6 KiB
63 lines
1.6 KiB
package strategy |
|
|
|
import ( |
|
"math" |
|
"sig-pub/pkg/types" |
|
"sig-pub/pkg/zlog" |
|
) |
|
|
|
// MeanReversionV1 |
|
type MeanReversionV1 struct { |
|
IIntervalSigStrategy |
|
rate float64 |
|
rate2 float64 |
|
} |
|
|
|
func (s *MeanReversionV1) New() ISigStrategy { |
|
return &MeanReversionV1{} |
|
} |
|
|
|
func (s *MeanReversionV1) Meta() StrategyMeta { |
|
return StrategyMeta{ |
|
Name: "MeanReversionV1", |
|
Desc: "均值回归策略v1", |
|
Input: []types.InputArg{ |
|
{Name: "rate", Type: types.InputTypeUFloat, Desc: "上线影线与基线比例"}, |
|
{Name: "rate2", Type: types.InputTypeUFloat, Desc: "上线影线之间比例"}, |
|
}, |
|
} |
|
} |
|
|
|
func (s *MeanReversionV1) Init(input types.Input) (err error) { // 校验参数, 并根据参数初始化策略 |
|
s.rate = input.Float("rate") |
|
s.rate2 = input.Float("rate2") |
|
return |
|
} |
|
|
|
func (s *MeanReversionV1) CandlePeriods(ctx IIntervalSigStrategyContext) (iss *types.IntervalState[int16]) { |
|
iss = types.NewIntervalState[int16]() |
|
iss.Set(types.Interval5m, 1) |
|
iss.Set(types.Interval15m, 2) |
|
iss.Set(types.Interval30m, 2) |
|
return |
|
} |
|
|
|
func (s *MeanReversionV1) Update(ctx IIntervalSigStrategyContext) (side types.Side) { |
|
// O 109744.8 H 110600 L 109507.5 C 109686.8 |
|
k0 := ctx.Get("5m", 0) |
|
open, close, high, low := k0.OpenF64(), k0.CloseF64(), k0.HighF64(), k0.LowF64() |
|
base := math.Abs(open - close) |
|
rup := (high - max(open, close)) / base |
|
rdown := (min(open, close) - low) / base |
|
|
|
if k0.Ts == 1761833700000 { |
|
zlog.Debugf("base=%.4f, rup=%.4f, rdown=%.4f", base, rup, rdown) |
|
} |
|
if rup > s.rate && rup/rdown > s.rate2 { |
|
return types.SideLong |
|
} |
|
if rdown > s.rate && rdown/rup > s.rate2 { |
|
return types.SideShort |
|
} |
|
return |
|
}
|
|
|