CTA策略之商品期货简易马丁格尔

Author: Hukybo, Created: 2021-04-23 17:52:44, Updated: 2021-07-20 11:31:06

三、策略实现

``````/*backtest
start: 2015-06-01 00:00:00
end: 2021-04-01 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
*/

unit = 1
profits = 50
bei = 2

function main() {
exchange.SetContractType("RM000")
while (true) {
let depth = exchange.GetDepth();
if (!depth) return;
let bid = depth.Bids[0].Price;
let position = exchange.GetPosition()
if (position.length == 0) {
let redom = Math.random()
unit = 1
if (redom < 0.5) {
exchange.SetDirection("sell")
exchange.Sell(bid, unit, "开空")
}
if (redom > 0.5) {
}
}
if (position.length > 0) {
let type = position[0].Type;
let profit = position[0].Profit;
let amount = position[0].Amount;
if (type == 0) {
if (profit > profits) {
exchange.Sell(bid, amount, "多头止盈，当前盈利：" + profit)
unit = 1
}
if (profit < -profits) {
unit = unit * bei
}
}
if (type == 1) {
if (profit > profits) {
exchange.SetDirection("closesell")
unit = 1
}
if (profit < -profits) {
unit = unit * bei
exchange.SetDirection("sell")
exchange.Sell(bid, unit, "空头加仓，当前盈利：" + profit)
}
}
}
Sleep(1000 * 60 * 60 * 24)
}
}
``````

四、策略回测

• 回测开始日期：2015-06-01
• 回测结束日期：2021-04-01
• 数据品种：菜粕指数
• 数据周期：日线
• 滑点：开平仓各2跳

六、总结

More

ooo 1.1倍当止盈时压根回不了本阿，还有意义么？

Hukybo 这里只是一个类比，意思就是倍数越高，风险越大。

Hukybo 2倍才能回本，意义在于降低风险