pine-script - TradingView 帮助使用多个 strategy.exit 调用

标签 pine-script

我在修改 TradingView 策略测试器中的未平仓头寸时遇到了一个奇怪的问题。我先解释一下上下文:

以下行根据我的入场条件打开订单,entryLong .

strategy.entry("ID", strategy.long, comment="L_Entry", when = entryLong)



这很管用,但是,除非您关闭订单,否则您无法赚钱 ;) 因此:

strategy.exit( "L_STOP", "ID", loss = fixedSL * 10)



此行修改未结订单以在价格 fixedSL 处添加止损入口位置下方。在这一点上,我唯一的退出条件是价格触及我的止损,这将始终导致亏损策略。为了解决这个问题,我包括:

if (exitLong) strategy.exit("L_TRAIL", "ID", trail_points = fixedTP * 10, trail_offset = trailSL * 10)



然后在达到设定的利润后添加追踪止损。这样,我可以安全地锁定利润,同时仍然留有增长空间。问题就在这里。每个退出条件都有一个 ID - L_STOPL_Trail (L 代表 Long,bc 这是买入)。我在图表上引用这些 ID 以帮助调试,而且只有 L_STOP曾经似乎关闭订单。这让我相信 L_TRAIL退出条件要么从未满足(不太可能)要么从未设置。我知道 bool 值,exitLong , 设置为 True 并且该行应该正在执行。

我可以通过在单个 strategy.exit 中设置追踪止损和止损来完全避免这个问题。打电话,但看到 很有帮助L_STOP L_TRAIL 打印在屏幕上以说明导致交易退出的原因。当条件满足时,只打印订单的 ID,所以一次调用它只会是 L_STOP 例如,它没有提供关于退出触发器的太多信息。

任何和所有反馈都是有帮助的!如有必要,我还可以包含图表的屏幕截图。

最佳答案

strategy.exit( "L_STOP", "ID", loss = fixedSL * 10)

...

strategy.exit("L_TRAIL", "ID", trail_points = fixedTP * 10, trail_offset = trailSL * 10)

...

I reference these IDs on my charts to help in debugging and only the L_STOP ever appears to close the order. This leads me to believe that the L_TRAIL exit condition is either never met (unlikely) or is never set.


这里的问题是你使用了 strategy.exit()函数两次,两次都设置止损(固定止损和追踪止损)。
但事实并非如此strategy.exit()能行得通。 TradingView的reference说:

"If you use a stop loss and a trailing stop, their order type is 'stop', so only one of them is placed (the one that is supposed to be filled first)."


这将解释为什么您的第二个止损(跟踪止损)没有设置。
您需要做的是重写您的策略代码,以便 strategy.exit() 一站式发送。 .如果没有看到完整的代码,我无法提供很多实用的建议。但也许你可以给你的追踪止损 trail_offset您的正常停止将设置为的值?
我无法从您的问题中得知变量具有哪些值,但也许这会起作用:
stopPrice = exitLong ? strategy.position_avg_price - (trailSL * 10) :
     strategy.position_avg_price - (fixedSL * 10)
     
strategy.exit("L_STOP", "ID", stop=stopPrice)

关于pine-script - TradingView 帮助使用多个 strategy.exit 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51576243/

相关文章:

pine-script - 使用 TradingView 和 Pine 进行源代码控制

types - 如何在 TradingView pinescript 中实现条件长度移动平均线?

pine-script - 我在 Pine Script 中遇到 TradingView 的 'end of line without continuation' 错误

pine-script - 如何计算自 `strategy.entry` 以来的柱数

pine-script - 应该在每次计算时调用该函数以保持一致性,控制台输出?

pine-script - pinescript 获取枢轴高的索引

c# - 将 RSI 从 pinescript 转换为 C#?

pine-script - 如何在TradingView上的Pine中创建自定义系列?

pine-script - 最高 "of the last n days",不是 "n days ago"

pine-script - 为单次入场设置多个止损离场订单