如何设置动态盈利,但是Pinescript V4中的静态停止损失?

发布于 2025-01-24 12:37:55 字数 1313 浏览 2 评论 0 原文

我在Pinescript V4中的停止功能遇到了一些问题。 TP有效,但由于某种原因,停止行动不起作用。附件是代码片段,也是该代码应该在哪里的图片。

Stop should be at the highest high of the last 4 bars (including the current bar)

longstop = lowest(low, 4)
shortstop = highest(high, 4)

if longcond
    strategy.entry("long", strategy.long, 5, stop = longstop)
strategy.exit("TP", "long", limit = EMA)
if shortcond
    strategy.entry("short", strategy.short, 5, stop = shortstop)
strategy.exit("TP", "short", limit = EMA)

Update

If我在if块中添加了额外的stragity.exit()函数,那么即使它应该以限制价格关闭Example

 if shortcond
    strategy.entry("short", strategy.short, 5)
    strategy.exit("SL", "short", stop = shortstop)
strategy.exit("TP", "short", limit = EMA)

If I add the stop to the current strategy.exit() line, then the stop price is continually updated at each bar for the lowest/highest of the past我不想要的4个酒吧。我希望在入口栏上设定和静态停止价格。

if longcond
    strategy.entry("long", strategy.long, 5)
 
strategy.exit("TP", "long", limit = EMA, stop = longstop)

我希望TP是动态的(更新每个栏是EMA价格),但我希望SL是静态的(最近4个bar中的最低/最高价格)

I've been having some problems with the stop function in Pinescript v4. The TP works but for some reason, the stop isn't working. Attached is the code snippet and a picture of where it should be.

Stop should be at the highest high of the last 4 bars (including the current bar)

longstop = lowest(low, 4)
shortstop = highest(high, 4)

if longcond
    strategy.entry("long", strategy.long, 5, stop = longstop)
strategy.exit("TP", "long", limit = EMA)
if shortcond
    strategy.entry("short", strategy.short, 5, stop = shortstop)
strategy.exit("TP", "short", limit = EMA)

Update

If I add an extra strategy.exit() function within the if block, then it will only trigger when it hits the stop price even when it should have closed at the limit price Example

 if shortcond
    strategy.entry("short", strategy.short, 5)
    strategy.exit("SL", "short", stop = shortstop)
strategy.exit("TP", "short", limit = EMA)

If I add the stop to the current strategy.exit() line, then the stop price is continually updated at each bar for the lowest/highest of the past 4 bars which I don't want. I want the stop price to be set and static at the entry bar.

if longcond
    strategy.entry("long", strategy.long, 5)
 
strategy.exit("TP", "long", limit = EMA, stop = longstop)

I want the TP to be dynamic (updates every bar to be the EMA price) but I want the SL to be static (the lowest/highest price within the last 4 bars)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

软糖 2025-01-31 12:37:55

limit strategy.exit()的参数用于获取利润。如果要设置停止损失,请使用 stop 参数。

limit argument of the strategy.exit() is used for take profits. If you want to set a stop loss, use the stop argument.

迷荒 2025-01-31 12:37:55

我能够通过添加一个条件来计算以来的条件来修复它,从而切换到V5。

shortstop = ta.highest(5)
longstop = ta.lowest(5)

strategy.entry("long", strategy.long, 50, when = longcond)
longbarsSinceEntry = bar_index - strategy.opentrades.entry_bar_index(0)
strategy.exit("exit", "long", stop = longstop[longbarsSinceEntry], limit = EMA)


strategy.entry("short", strategy.short, 50,  when = shortcond)
shortbarsSinceEntry = bar_index - strategy.opentrades.entry_bar_index(0)
strategy.exit("exit", "short", stop = shortstop[shortbarsSinceEntry], limit = EMA)

I was able to fix it by adding a condition counting the number of bars since entry and switching to v5.

shortstop = ta.highest(5)
longstop = ta.lowest(5)

strategy.entry("long", strategy.long, 50, when = longcond)
longbarsSinceEntry = bar_index - strategy.opentrades.entry_bar_index(0)
strategy.exit("exit", "long", stop = longstop[longbarsSinceEntry], limit = EMA)


strategy.entry("short", strategy.short, 50,  when = shortcond)
shortbarsSinceEntry = bar_index - strategy.opentrades.entry_bar_index(0)
strategy.exit("exit", "short", stop = shortstop[shortbarsSinceEntry], limit = EMA)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文