策略。

发布于 2025-01-22 00:46:46 字数 917 浏览 3 评论 0原文

我对此非常陌生,我想知道如何添加策略。此外,X bars之后的功能。

因此,当信号朝相反的方向(我目前实施)或x bar之后,我想退出长/短交易。

事先感谢您的任何帮助。非常感谢。

目前,我的进入和退出条件如下:


strategy.entry("Long", strategy.long, when=buy_1 or buy_2,
 alert_message="{{strategy.order.action}} for {{ticker}} at {{strategy.order.price}}")
strategy.close("Long", when=sell_1 or sell_2, comment="Text for Exit here")
 
// strategy.exit("Exit Long", from_entry="Long", limit=tpLevel_long, stop=slLevel_long, comment="")

// Entry and exit logic for short

if sell_1 or sell_2
    alert(message="Add Short text here")

strategy.entry("Short", strategy.short, when=sell_1 or sell_2, 
 alert_message="{{strategy.order.action}} for {{ticker}} at {{strategy.order.price}}")
strategy.close("Short", when=buy_1 or buy_2, comment="Text for Exit here")

// strategy.exit("Exit Short", from_entry="Short", limit=tpLevel_short, stop=slLevel_short, comment="")```

I'm quite new to this and I was wondering how it's possible to add a strategy.close function additionally after x bars.

So, I'd like to exit the long/short trades either when there comes a signal in the opposite direction (as I have implemented it currently) or after x bars.

Thanks in advance for any help. Much appreciated.

Right now I have the entry and exit conditions as following:


strategy.entry("Long", strategy.long, when=buy_1 or buy_2,
 alert_message="{{strategy.order.action}} for {{ticker}} at {{strategy.order.price}}")
strategy.close("Long", when=sell_1 or sell_2, comment="Text for Exit here")
 
// strategy.exit("Exit Long", from_entry="Long", limit=tpLevel_long, stop=slLevel_long, comment="")

// Entry and exit logic for short

if sell_1 or sell_2
    alert(message="Add Short text here")

strategy.entry("Short", strategy.short, when=sell_1 or sell_2, 
 alert_message="{{strategy.order.action}} for {{ticker}} at {{strategy.order.price}}")
strategy.close("Short", when=buy_1 or buy_2, comment="Text for Exit here")

// strategy.exit("Exit Short", from_entry="Short", limit=tpLevel_short, stop=slLevel_short, comment="")```

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

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

发布评论

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

评论(2

饮惑 2025-01-29 00:46:46

将其添加到您的策略中:

barsSinceLastEntry() =>
    strategy.opentrades > 0 ? bar_index - strategy.opentrades.entry_bar_index(strategy.opentrades - 1) : na

// Close the long position after 10 bars. 
if barsSinceLastEntry() >= 10
    strategy.close_all(comment="Close")

Add this to your strategy:

barsSinceLastEntry() =>
    strategy.opentrades > 0 ? bar_index - strategy.opentrades.entry_bar_index(strategy.opentrades - 1) : na

// Close the long position after 10 bars. 
if barsSinceLastEntry() >= 10
    strategy.close_all(comment="Close")
皓月长歌 2025-01-29 00:46:46

您可以使用stragity.opentrades.entry_bar_index内置变量以获取条目的条索引。然后根据当前条索引添加支票。

entry_idx = strategy.opentrades.entry_bar_index
diff = bar_index - entry_idx

if (diff > 10)
    strategy.close("Long")

You can use the strategy.opentrades.entry_bar_index built-in variable to get the bar index of the entry. Then add a check based on current bar index.

entry_idx = strategy.opentrades.entry_bar_index
diff = bar_index - entry_idx

if (diff > 10)
    strategy.close("Long")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文