Stop loss hunting script

 

MQL is so complicated. I need help.

I need a script that will execute a stop loss at a set price if that stop loss has been hit two more more times within the last 5 minutes.

For example, (this is a long position), wick of candle hits 1.9000 but open and close of the candle are at 1.8999 and 1.8997 respectively.

My stoploss is at 1.9000. I want to verify that this isn't a long wick. I want a second wick or second opening price to verify that 1.9000 has been reached or exceeded (only if this has occured once before in the last 5 one minute candles.

 

You need an integer array, a toggle, a counter and two functions. On every tick activate a function to check if the elapsed time from the earliest time in your array exceeded you specified timeframe (5 minutes). If so, decrease the counter by one (unless it's zero) delete the earliest array time and shift the other values back in the array.

Your toggle will be on or off depending on wether your're above or below your target, and use the second function to set it. On that function, check on every tick if the price exceeds your target and previous toggle position is "off". If so, increase counter, add current time to array, and switch toggle to "on".

When your counter = [target occurences within specified timeframe], you met your condition.

This approach will work regardless of how many occurences you're monitoring. If you only care about two occurences, you might be able to simplify.

Then there is also the possibility that the level will be breached and the price keeps moving. You'll want to check if a new bar is formed with the toggle switch in the "on" position, and then you might want to exit.

Reason: