0

我似乎在我的 pinescript 代码中有一个奇怪的错误,并希望尽可能地获得一些见解。

我有一个 HH/LL 定位指标的私有修改版本,我已将其转化为策略并添加了一些额外的规则、过滤器、TP/SL 等。当 HH 或 LL 出现时,它基本上在看涨和看跌趋势之间波动破碎的。

我的策略在每笔交易中都使用了止损/止盈,我终于设法通过 strategy.close 和 strategy.exit 的组合使其正常工作。

当“趋势”翻转时,所有其他要求都意味着订单打开。90% 的时间运行正确,被正确跟踪并且一切正常(赢或输)。然而,如果 HH 或 LL 被打破(反转趋势)并且下一个柱线完全回撤至断裂线......我的订单已关闭。

在下面显示我的代码,您将看到我尝试“过滤”这些错误或不受欢迎的关闭的多种方法,但都无济于事(我认为是由于回测器的“仅关闭”性质)。

  1. "LetRun" bool 试图完全绕过它
  2. "barssince" 在允许相反的信号结束交易之前尝试有一个最小的交易长度。
  3. 我试图只允许趋势改变关闭功能在“有利可图”的情况下工作(关闭 > avg_price)

作为一个业余爱好者,我曾希望这些是解决该问题的优雅(或至少有效)解决方案,并且对于错误可能存在的其他地方感到困惑,因为似乎没有一个具有预期的效果。

Long = trend == 1 and trend[1] == -1
Short = trend == -1 and trend[1] == 1
if(WaitForConfirm)
    Long := trend[1] == 1 and trend[2] == -1
    Short := trend[1] == -1 and trend[2] == 1
LongEntry = ((not mafilter) or (mafilter and ma>ma2)) and Long
ShortEntry = ((not mafilter) or (mafilter and ma<ma2)) and Short and not LongOnly
LongStop = strategy.position_avg_price * (1 - SL)
ShortStop = strategy.position_avg_price * (1 + SL)
LongTp = strategy.position_avg_price * (1 + TP)
ShortTp = strategy.position_avg_price * (1 - TP)


strategy.entry("buy", strategy.long, when=LongEntry, alert_message="Long Entry")
strategy.exit("buy", limit=LongTp, stop=LongStop, alert_message="Long closed by TP or SL")

strategy.entry("sell", strategy.short, when=ShortEntry, alert_message="Short Entry")
strategy.exit("sell", limit=ShortTp, stop=ShortStop, alert_message="Short closed by TP or SL")

if((barssince(strategy.position_size != strategy.position_size[1]) > minbars) and close > 
strategy.position_avg_price)
    strategy.close("buy", when=Short and not LetRun, alert_message="Trend Changed, close longs")

if((barssince(strategy.position_size != strategy.position_size[1]) > minbars) and close < 
strategy.position_avg_price)
    strategy.close("sell", when=Long and not LetRun, alert_message="Trend changed, close shorts")

即时收盘的例子,离 SL 或 TP 不远,并且没有趋势反转回白色

如果需要任何其他信息,请告诉我。

4

0 回答 0