Here ya go — Here it is for you. I left out the MM, because I am not very good at coding that yet. Someone else can add it. Also, it works at the beginning of every bar, not within bars like you ask, if I did that the price can cross the MA ...
I've added the ATR and done some backtesting, with and without ATR. The ATR causes significant loss unless it's period is set as high as 22, in which case it caused the EA to make VERY slightly more money in the optimized period. This is only with ...