Corrections to Pine script (TradingView)

Job ID: 37230714

Budget: £10 – £20 GBP

I am creating a simple backtesting strategy with Pine for TradingView with following rules on M15 timeframe. I have already developed the script but it is not executing my trades correctly, is it needs some adjustment:

for Up-trend:

1) an UP-TREND is true when the following conditions are met:
a) high[1] > high[2] and high[2] > high[3]
b) close[1] > 50 EMA (not just on crossover but at all times when this is true)

2) when UP-TREND is true, place a BUY-LIMIT on high[1] (i.e. the most recent closed candle). the trade should be executed IMMEDIATELY when the buy-limit is touched (not after the candle is closed!).

3) exit conditions are SL = low[1] and TP = SL amount in pips (i.e. 1:1 RR)

4) only ONE trade is possible at a time

for Down-trend:

1) a DOWN-TREND is true when the following conditions are met:
a) low[1] > low[2] and low[2] > low[3]
b) close[1] < 50 EMA (not just on crossover but at all times when this is true)

2) when DOWN-TREND is true, place a SELL-LIMIT on low[1] (i.e. the most recent closed candle). the trade should be executed IMMEDIATELY when the sell-limit is touched (not after the candle is closed!).

3) exit conditions are SL = high[1] and TP = SL amount in pips (i.e. 1:1 RR)

4) only ONE trade is possible at a time

I have provided the script but it is not executing trades correctly. Please modify so trades are executed as described above and importantly as soon as the buy/sell-limit is touched - not a bar close afterwards!

I have attached a diagram to show the above logic to help you understand better.

Thank you.
Related categories: Pine Script Trading