问题
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