69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
package ma1
|
||
|
||
import (
|
||
"github.com/banbox/banbot/config"
|
||
"github.com/banbox/banbot/strat"
|
||
"github.com/banbox/banstagy/com"
|
||
ta "github.com/banbox/banta"
|
||
"math"
|
||
)
|
||
|
||
func Main(pol *config.RunPolicyConfig) *strat.TradeStrat {
|
||
return MainV2_1(pol)
|
||
}
|
||
|
||
var (
|
||
// 这里是默认参数,请尽量在MainVX()中修改,不要在这里修改
|
||
periodBig = 30
|
||
periodSml = 10
|
||
parentRate = 0.7
|
||
periodParBig = int(float64(periodBig) * parentRate)
|
||
periodParSml = int(float64(periodSml) * parentRate)
|
||
sampleNum = 500 // 斜率采样数量
|
||
sampleStep = 2 // 斜率计算间隔
|
||
atrLen = 14
|
||
openOffRate = float64(0) // 开单时,从均线偏移基准ATR的倍数
|
||
goodRate = float64(10) // 收益超过此倍数的ATR时,将止损更新为均线
|
||
badRate = float64(5) // 损失超过此倍数的ATR时,止损退出
|
||
)
|
||
|
||
type MoreType struct {
|
||
*com.Slop
|
||
hitGoods map[int64]bool // 触发goodRate的订单ID,允许每个bar更新止损
|
||
open int // 1开多 -1开空
|
||
}
|
||
|
||
func onStartUp(s *strat.StratJob) {
|
||
s.More = &MoreType{
|
||
Slop: com.NewSlop(sampleNum, 0.25),
|
||
hitGoods: make(map[int64]bool),
|
||
}
|
||
}
|
||
|
||
func onInfoMaCross(s *strat.StratJob, e *ta.BarEnv, pair, tf string) {
|
||
smaSml := ta.SMA(e.Close, periodParSml)
|
||
smaBig := ta.SMA(e.Close, periodParBig)
|
||
x := smaSml.Cross(smaBig)
|
||
more, _ := s.More.(*MoreType)
|
||
if x == 1 || x == -1 {
|
||
more.Dirt = x
|
||
}
|
||
}
|
||
|
||
func onInfoMaSlop(s *strat.StratJob, e *ta.BarEnv, pair, tf string) {
|
||
sma := ta.SMA(e.Close, periodParBig)
|
||
prevSma := sma.Get(sampleStep)
|
||
if e.BarNum%sampleStep > 0 || math.IsNaN(prevSma) {
|
||
return
|
||
}
|
||
more, _ := s.More.(*MoreType)
|
||
curSma := sma.Get(0)
|
||
slop := (curSma - prevSma) / curSma
|
||
//more.Slop.Update(e.BarNum, slop)
|
||
if slop > 0 {
|
||
more.Dirt = 1
|
||
} else {
|
||
more.Dirt = -1
|
||
}
|
||
}
|