Reputation: 43
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
Upvotes: 4
Views: 11379
Reputation: 595
Add this AFTER to the code you use to enter a position:
bought = strategy.position_size[0] > strategy.position_size[1]
entry_price = valuewhen(bought, open, 0)
Upvotes: 1
Reputation: 5927
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!
Upvotes: 3