Ok. Let's move step by step. You will need a loop searching for the closest swing high/low on the MA loking bak to 10 candles. However, I have a previous question. What if there is nor such swing in 10 candles back? What if the last 10 candles are showing a continuous up/down of the MA value?
D