我正在尝试创建一个非常简单的 MA 交叉策略(仅做多),我想在固定止损水平上或在满足我的退出条件时退出交易。出于这个原因,我使用 strategy.order 函数来退出我的交易。然而,显然有些问题,因为还启动了一些空头交易......在这里您可以找到代码中最重要的部分:
//strategy conditions & variables
ordersize=floor(strategy.equity/close)
trendfilter = RSI_w > i_RSI_TH ? 1 : 0
longCondition = crossover(shortMA,longMA)
exitCondition = crossover(longMA,shortMA)
stopLoss = float(na)
stopLoss := strategy.position_size[0]>strategy.position_size[1] ? strategy.position_avg_price - (i_atr_mult * atr) : strategy.position_size>0 ? stopLoss[1] : na
// strategy entry & exits
strategy.entry(id="going long", long=true, qty=ordersize, when=longCondition and TimeWindow and trendfilter>0)
strategy.order(id="stop loss", long=false, stop=stopLoss, oca_name='L', oca_type=strategy.oca.cancel, qty=strategy.position_size, when=strategy.position_size > 0 and TimeWindow)
strategy.order(id="exit long", long=false, oca_name='L', oca_type=strategy.oca.cancel, qty=strategy.position_size, when= exitCondition and strategy.position_size > 0 and TimeWindow)
所以在我的逻辑中,第一个 strategy.order 函数只能在 strategy.position_size > 0 时打开交易。这应该避免做空订单,但显然不是。当价格达到 SL 水平时,即使不在未平仓交易中,也会启动空头交易...... 有人能解释一下这是为什么吗???更好的是,我如何调整我的代码以避免这些短条目?这对我很有帮助!!
总结:负责这些短条目的代码行是:
strategy.order(id="stop loss", long=false, stop=stopLoss, oca_name='L', oca_type=strategy.oca.cancel, qty=strategy.position_size, when=strategy.position_size > 0 and TimeWindow)
答案 0 :(得分:1)
第一个订单是止损,第二个订单是市价订单。当exitCondition 为真且价格满足止损条件时,两个订单同时执行,经纪商模拟器无法及时取消其他订单。我建议您在 exitCondition 变为 true 时明确取消止损单。