Anjimlio2
Pine Script Rookie
Pine Script Rookie
Posts: 1
Joined: April 14th, 2022

Check if a buy order has been placed in de last X candles

Hello,

I would like to know if a buy order has been placed in the last 20 candles
If so, no new buy order may be placed.
If not, a buy order may be placed.

Thank you very much

Shenpai
Pine Script Rookie
Pine Script Rookie
Posts: 6
Joined: April 10th, 2022
Contact: TradingView Profile

Re: Check if a buy order has been placed in de last X candles

this should work if the order placed in the last 20 candles is still open. if it was opened 18 candles ago and closed 10 candles ago this will not detect the order. If it's important to know if there was ANY order, tell me and I try to help.

barsSinceLastEntry()=>
strategy.opentrades > 0 ? (bar_index - strategy.opentrades.entry_bar_index(strategy.opentrades-1)) : na

condition := not (barsSinceLastEntry() < 21)

if (condition)
strategy.entry()

Return to “Pine Script Q&A”