Tradingview Pine script save close price at time of strategy entry

对着背影说爱祢 提交于 2019-12-10 16:34:45

问题


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) 

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


回答1:


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)

Hope this helps!



来源:https://stackoverflow.com/questions/48958139/tradingview-pine-script-save-close-price-at-time-of-strategy-entry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!