简体   繁体   English

Pine Script:开盘策略后退出策略 X 柱

[英]Pine Script: Exit strategy X bars after opening strategy

I am trying to close a strategy either after it crosses above the MA or after 50 bars, whichever comes first我试图在策略超过 MA 之后或在 50 根柱之后关闭策略,以先到者为准

I have been trying to look into and it seems "barstate.isconfirmed" or "barsince" function should be used.我一直在尝试调查,似乎应该使用“barstate.isconfirmed”或“barsince”function。

Currently I have the below, what do I need to add to include closing after 50 bars?目前我有以下内容,我需要添加什么以包括在 50 个柱后关闭?

strategy.close("buy",when = close[0]>basis 

You can use the strategy.opentrades.entry_bar_index built-in variable to get the bar index of the open trade.您可以使用strategy.opentrades.entry_bar_index内置变量来获取未平仓交易的柱线索引。 Then compare it with the current bar index.然后将其与当前柱形指数进行比较。

n = 50
open_idx = strategy.opentrades.entry_bar_index(strategy.opentrades-1)
diff_idx = bar_index - open_idx

if (diff_idx >= n)
    strategy.close("buy")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM