Someone asked a similar question with no response and I am not allowed to add to it.
Tradingview Pine script save close price at time of strategy entry
I am trying to build a strategy that will buy multiple times (pyramiding) to average down before closing but I want to check the previous entry price to make sure it's less by a configured percentage.
What I have so far:
lastBuy=0
if (condition)
if (lastBuy==0)
lastBuy=close
strategy.entry("buy", true)
else
if ((close*1.01)<lastBuy)
lastBuy=close
strategy.entry("buy", true)
Each time the code is passed it resets lastBuy back to zero and I never get to check the previous close price. If I don't set this I get undeclared error.
Thanks in advance for any help!
This works for entry price.
entryPrice = valuewhen(strategy.opentrades == 1, strategy.position_avg_price, 0)
How I am saving the entry price to a variable:
bought = strategy.opentrades[0] == 1 and strategy.position_size[0] > strategy.position_size[1]
entry_price = valuewhen(bought, open, 0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With