55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package trend
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/banbox/banbot/config"
|
|
"github.com/banbox/banbot/core"
|
|
"github.com/banbox/banbot/orm/ormo"
|
|
"github.com/banbox/banbot/strat"
|
|
ta "github.com/banbox/banta"
|
|
"math"
|
|
"math/rand"
|
|
)
|
|
|
|
func randCut(p *config.RunPolicyConfig) *strat.TradeStrat {
|
|
slRate := p.Def("slRate", 3, core.PNorm(1, 3))
|
|
return &strat.TradeStrat{
|
|
WarmupNum: 100,
|
|
OnBar: func(s *strat.StratJob) {
|
|
e := s.Env
|
|
c := e.Close.Get(0)
|
|
atr := ta.ATR(e.High, e.Low, e.Close, 30)
|
|
slRange := atr.Get(0) * slRate
|
|
odList := s.GetOrders(0)
|
|
if len(odList) > 0 {
|
|
for _, od := range odList {
|
|
sl := od.GetStopLoss()
|
|
if od.Short {
|
|
if sl.Price > c+slRange {
|
|
od.SetStopLoss(&ormo.ExitTrigger{
|
|
Price: c + slRange,
|
|
})
|
|
}
|
|
} else if sl.Price < c-slRange {
|
|
od.SetStopLoss(&ormo.ExitTrigger{
|
|
Price: c - slRange,
|
|
})
|
|
}
|
|
}
|
|
} else {
|
|
openRate := 0.015 * c / atr.Get(0)
|
|
if openRate < 1 {
|
|
openRate = math.Sqrt(openRate)
|
|
}
|
|
isShort := rand.Float64() > 0.5
|
|
s.OpenOrder(&strat.EnterReq{
|
|
Tag: fmt.Sprintf("%v", isShort),
|
|
Short: isShort,
|
|
CostRate: openRate,
|
|
StopLossVal: slRange,
|
|
})
|
|
}
|
|
},
|
|
}
|
|
}
|