Tradingview Pine 脚本在策略进入时保存收盘价 [英] Tradingview Pine script save close price at time of strategy entry
问题描述
嘿,我正在尝试将 strategy.entry 时的收盘价保存到一个变量中,以便我以后可以使用它来退出.
Hey I'm trying to save the close price at the time of strategy.entry to a variable so I can use it later for an exit.
if condition
strategy.entry("long", true)
buyprice=close
(strategy.exit("exit","long", when = close>buyprice*1.1)
我收到错误:未声明的标识符buyprice"
.据我了解,这意味着该变量在 if 语句之外无效.有没有办法改变这种情况?预先感谢您的帮助
I get the error: Undeclared identifier 'buyprice'
. From what I understand this means that the variable is not valid outside of the if statement. Is there a way to change this? Thanks in advance for your help
推荐答案
这是 是我让它发挥作用的唯一方法.
This is the only way that I could get this to work.
基本上,您在满足多头条件时设置之前的价格,然后在下一阶段从全局变量中检索该值.
Basically, you set the previous price when long condition is met and then retrieve that value from the global variables in the next phase.
//@version=2
...
buyprice=buyprice[1]
golong=...
if golong
buyprice := close
goshort=... or close<=buyprice*0.95
strategy.entry("Long", long=true, when=golong)
strategy.close("Long", when=goshort)
希望这会有所帮助!
这篇关于Tradingview Pine 脚本在策略进入时保存收盘价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!