init
This commit is contained in:
68
trend/ma1/common.go
Normal file
68
trend/ma1/common.go
Normal file
@@ -0,0 +1,68 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
63
trend/ma1/v1.go
Normal file
63
trend/ma1/v1.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package ma1
|
||||
|
||||
import (
|
||||
"github.com/banbox/banbot/config"
|
||||
"github.com/banbox/banbot/strat"
|
||||
"github.com/banbox/banstagy/com"
|
||||
ta "github.com/banbox/banta"
|
||||
)
|
||||
|
||||
func MainV1(pol *config.RunPolicyConfig) *strat.TradeStrat {
|
||||
periodSml = 300
|
||||
periodBig = 1800
|
||||
parentRate = 0.5
|
||||
periodParBig = int(float64(periodBig) * parentRate)
|
||||
periodParSml = int(float64(periodSml) * parentRate)
|
||||
return &strat.TradeStrat{
|
||||
Version: 1,
|
||||
WarmupNum: periodBig,
|
||||
MinTfScore: 0.75,
|
||||
//DrawDownExit: true,
|
||||
OnStartUp: onStartUp,
|
||||
OnPairInfos: com.MakePairInfos(sampleNum*sampleStep + periodParBig),
|
||||
OnInfoBar: onInfoMaCross,
|
||||
OnBar: func(s *strat.StratJob) {
|
||||
e := s.Env
|
||||
sml := ta.SMA(e.Close, 10)
|
||||
big := ta.SMA(e.Close, 50)
|
||||
x := sml.Cross(big)
|
||||
|
||||
smaSml := ta.SMA(e.Close, periodSml)
|
||||
smaBig := ta.SMA(e.Close, periodBig)
|
||||
crx := smaSml.Cross(smaBig)
|
||||
|
||||
more, _ := s.More.(*MoreType)
|
||||
if crx == 1 || crx == -1 {
|
||||
closeTag := "x_short"
|
||||
if crx < 0 {
|
||||
closeTag = "x_long"
|
||||
}
|
||||
_ = s.CloseOrders(&strat.ExitReq{
|
||||
Tag: closeTag,
|
||||
Dirt: crx * -1,
|
||||
})
|
||||
if crx == more.Dirt {
|
||||
// 交叉信号和大环境一致
|
||||
more.open = crx
|
||||
}
|
||||
}
|
||||
|
||||
if more.open != 0 && more.open == -x {
|
||||
more.open = 0
|
||||
openTag := "long"
|
||||
if x > 0 {
|
||||
openTag = "short"
|
||||
}
|
||||
_ = s.OpenOrder(&strat.EnterReq{
|
||||
Tag: openTag,
|
||||
Short: x > 0,
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
80
trend/ma1/v2.1.go
Normal file
80
trend/ma1/v2.1.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package ma1
|
||||
|
||||
import (
|
||||
"github.com/banbox/banbot/config"
|
||||
"github.com/banbox/banbot/strat"
|
||||
ta "github.com/banbox/banta"
|
||||
"math"
|
||||
)
|
||||
|
||||
/*
|
||||
针对15m的参数
|
||||
*/
|
||||
func MainV2_1(pol *config.RunPolicyConfig) *strat.TradeStrat {
|
||||
periodSml = 120
|
||||
periodBig = 800
|
||||
parentRate = 0.5
|
||||
periodParBig = int(float64(periodBig) * parentRate)
|
||||
periodParSml = int(float64(periodSml) * parentRate)
|
||||
return &strat.TradeStrat{
|
||||
Version: 1,
|
||||
WarmupNum: sampleNum*sampleStep + periodBig,
|
||||
MinTfScore: 0.75,
|
||||
//DrawDownExit: true,
|
||||
OnStartUp: onStartUp,
|
||||
//OnPairInfos: onPairInfos,
|
||||
//OnInfoBar: onInfoMaSlop,
|
||||
OnBar: func(s *strat.StratJob) {
|
||||
e := s.Env
|
||||
sml := ta.SMA(e.Close, 30)
|
||||
big := ta.SMA(e.Close, 70)
|
||||
x := sml.Cross(big)
|
||||
|
||||
smaSml := ta.SMA(e.Close, periodSml)
|
||||
smaBig := ta.SMA(e.Close, periodBig)
|
||||
crx := smaSml.Cross(smaBig)
|
||||
|
||||
more, _ := s.More.(*MoreType)
|
||||
if crx == 1 || crx == -1 {
|
||||
closeTag := "x_short"
|
||||
if crx < 0 {
|
||||
closeTag = "x_long"
|
||||
}
|
||||
_ = s.CloseOrders(&strat.ExitReq{
|
||||
Tag: closeTag,
|
||||
Dirt: crx * -1,
|
||||
})
|
||||
more.open = crx
|
||||
//if crx == more.Dirt {
|
||||
// 交叉信号和大环境一致
|
||||
//openTag := "long"
|
||||
//if crx < 0 {
|
||||
// openTag = "short"
|
||||
//}
|
||||
//_ = s.OpenOrder(&strategy.EnterReq{
|
||||
// Tag: openTag,
|
||||
// Short: crx < 0,
|
||||
//})
|
||||
//}
|
||||
}
|
||||
prevSma := smaBig.Get(sampleStep)
|
||||
if e.BarNum%sampleStep == 0 && !math.IsNaN(prevSma) {
|
||||
curSma := smaBig.Get(0)
|
||||
more.Slop.Update(e.BarNum, (curSma-prevSma)/curSma)
|
||||
}
|
||||
|
||||
x = -x
|
||||
if more.open != 0 && more.open == x && more.open*more.Dirt >= 0 {
|
||||
more.open = 0
|
||||
openTag := "long"
|
||||
if x < 0 {
|
||||
openTag = "short"
|
||||
}
|
||||
_ = s.OpenOrder(&strat.EnterReq{
|
||||
Tag: openTag,
|
||||
Short: x < 0,
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
84
trend/ma1/v2.go
Normal file
84
trend/ma1/v2.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package ma1
|
||||
|
||||
import (
|
||||
"github.com/banbox/banbot/config"
|
||||
"github.com/banbox/banbot/strat"
|
||||
"github.com/banbox/banstagy/com"
|
||||
ta "github.com/banbox/banta"
|
||||
)
|
||||
|
||||
/*
|
||||
针对15m的参数
|
||||
*/
|
||||
func MainV2(pol *config.RunPolicyConfig) *strat.TradeStrat {
|
||||
periodSml = 120
|
||||
periodBig = 750
|
||||
parentRate = 0.5
|
||||
periodParBig = int(float64(periodBig) * parentRate)
|
||||
periodParSml = int(float64(periodSml) * parentRate)
|
||||
return &strat.TradeStrat{
|
||||
Version: 1,
|
||||
WarmupNum: periodBig,
|
||||
MinTfScore: 0.75,
|
||||
//DrawDownExit: true,
|
||||
OnStartUp: onStartUp,
|
||||
OnPairInfos: com.MakePairInfos(sampleNum*sampleStep + periodParBig),
|
||||
OnInfoBar: onInfoMaCross,
|
||||
OnBar: func(s *strat.StratJob) {
|
||||
e := s.Env
|
||||
sml := ta.SMA(e.Close, 30)
|
||||
big := ta.SMA(e.Close, 70)
|
||||
x := sml.Cross(big)
|
||||
|
||||
smaSml := ta.SMA(e.Close, periodSml)
|
||||
smaBig := ta.SMA(e.Close, periodBig)
|
||||
crx := smaSml.Cross(smaBig)
|
||||
|
||||
more, _ := s.More.(*MoreType)
|
||||
if crx == 1 || crx == -1 {
|
||||
closeTag := "x_short"
|
||||
if crx < 0 {
|
||||
closeTag = "x_long"
|
||||
}
|
||||
_ = s.CloseOrders(&strat.ExitReq{
|
||||
Tag: closeTag,
|
||||
Dirt: crx * -1,
|
||||
})
|
||||
if crx == more.Dirt {
|
||||
// 交叉信号和大环境一致
|
||||
more.open = crx
|
||||
//openTag := "long"
|
||||
//if crx < 0 {
|
||||
// openTag = "short"
|
||||
//}
|
||||
//_ = s.OpenOrder(&strategy.EnterReq{
|
||||
// Tag: openTag,
|
||||
// Short: crx < 0,
|
||||
//})
|
||||
}
|
||||
}
|
||||
x = -x
|
||||
if more.open != 0 && more.open == x {
|
||||
more.open = 0
|
||||
openTag := "long"
|
||||
if x < 0 {
|
||||
openTag = "short"
|
||||
}
|
||||
_ = s.OpenOrder(&strat.EnterReq{
|
||||
Tag: openTag,
|
||||
Short: x < 0,
|
||||
})
|
||||
}
|
||||
},
|
||||
//GetDrawDownExitRate: func(s *strategy.StratJob, od *ormo.InOutOrder, maxChg float64) float64 {
|
||||
// if maxChg > 3 {
|
||||
// return 0.2
|
||||
// } else if maxChg > 1 {
|
||||
// return 0.25
|
||||
// } else if maxChg > 0.5 {
|
||||
// return 0.3
|
||||
// }
|
||||
// return 0
|
||||
//},
|
||||
}
|
||||
}
|
||||
74
trend/ma1/v3.go
Normal file
74
trend/ma1/v3.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package ma1
|
||||
|
||||
import (
|
||||
"github.com/banbox/banbot/config"
|
||||
"github.com/banbox/banbot/strat"
|
||||
"github.com/banbox/banstagy/com"
|
||||
ta "github.com/banbox/banta"
|
||||
)
|
||||
|
||||
/*
|
||||
针对1h的参数
|
||||
*/
|
||||
func MainV3(pol *config.RunPolicyConfig) *strat.TradeStrat {
|
||||
periodSml = 28
|
||||
periodBig = 160
|
||||
parentRate = 0.75
|
||||
periodParBig = int(float64(periodBig) * parentRate)
|
||||
periodParSml = int(float64(periodSml) * parentRate)
|
||||
return &strat.TradeStrat{
|
||||
Version: 1,
|
||||
WarmupNum: periodBig,
|
||||
MinTfScore: 0.75,
|
||||
//DrawDownExit: true,
|
||||
OnStartUp: onStartUp,
|
||||
OnPairInfos: com.MakePairInfos(sampleNum*sampleStep + periodParBig),
|
||||
OnInfoBar: onInfoMaCross,
|
||||
OnBar: func(s *strat.StratJob) {
|
||||
e := s.Env
|
||||
sml := ta.SMA(e.Close, 7)
|
||||
big := ta.SMA(e.Close, 24)
|
||||
x := sml.Cross(big)
|
||||
|
||||
smaSml := ta.SMA(e.Close, periodSml)
|
||||
smaBig := ta.SMA(e.Close, periodBig)
|
||||
crx := smaSml.Cross(smaBig)
|
||||
|
||||
more, _ := s.More.(*MoreType)
|
||||
if crx == 1 || crx == -1 {
|
||||
closeTag := "x_short"
|
||||
if crx < 0 {
|
||||
closeTag = "x_long"
|
||||
}
|
||||
_ = s.CloseOrders(&strat.ExitReq{
|
||||
Tag: closeTag,
|
||||
Dirt: crx * -1,
|
||||
})
|
||||
if crx == more.Dirt {
|
||||
// 交叉信号和大环境一致
|
||||
more.open = crx
|
||||
//openTag := "long"
|
||||
//if crx < 0 {
|
||||
// openTag = "short"
|
||||
//}
|
||||
//_ = s.OpenOrder(&strategy.EnterReq{
|
||||
// Tag: openTag,
|
||||
// Short: crx < 0,
|
||||
//})
|
||||
}
|
||||
}
|
||||
x = -x
|
||||
if more.open != 0 && more.open == x {
|
||||
more.open = 0
|
||||
openTag := "long"
|
||||
if x < 0 {
|
||||
openTag = "short"
|
||||
}
|
||||
_ = s.OpenOrder(&strat.EnterReq{
|
||||
Tag: openTag,
|
||||
Short: x < 0,
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user