Price to return to moving average

 
I'm studying MQL. I developed this code below. I'm looking for the following condition: If a trader hits StopLoss, I want EA to expect the price to return in the moving average, but I do not know how to do this in the code.
Anyone can help me?

//+------------------------------------------------------------------+
//|                                                    Exercicio.mq5 |
//|                                                 Tarcísio Allyson |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Tarcísio Allyson"
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade\Trade.mqh>

CTrade trade;

//--- input parameters
input int StopLoss = 3;
input int TakeProfit = 1;
input int MAPeriod = 7;
input int ENVPeriod = 7;
input double ENVCoeficient = 0.15;
input double Volume = 1;
input string hourStart = "09:10";
input string hourEnd = "17:00";
input int EA_Magic = 12345;

//--- other parameters
int envHandle;
int maHandle;
double envUpper[],envLower[],maVal[];
double ask, bid, last;
bool trade_on = true;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(StringToTime(hourStart) <= TimeCurrent() && StringToTime(hourEnd) > TimeCurrent())
   {
      Comment("Ativado");
      ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
      last = SymbolInfoDouble(_Symbol,SYMBOL_LAST);
      
      if(trade_on);
      {
         envHandle = iEnvelopes(_Symbol,_Period,ENVPeriod,0,MODE_EMA,PRICE_CLOSE,ENVCoeficient);
         
         ArraySetAsSeries(envUpper,true);
         ArraySetAsSeries(envLower,true);
         
         CopyBuffer(envHandle,0,0,3,envUpper);
         CopyBuffer(envHandle,1,0,3,envLower);
         
         maHandle = iMA(_Symbol,_Period,MAPeriod,0,MODE_EMA,PRICE_CLOSE);
         
         ArraySetAsSeries(maVal,true);
         
         CopyBuffer(maHandle,0,0,3,maVal);
         
         
         if(last <= envLower[0] && PositionsTotal() == 0)
         {
            trade.Buy(Volume,_Symbol,ask, ask - StopLoss, ask + TakeProfit, "");
         }
         else if(last >= envUpper[0] && PositionsTotal() == 0)
         {
            trade.Sell(Volume,_Symbol,bid, bid + StopLoss, bid - TakeProfit, "");
         }       
      } 
   }
   else Comment("Desativado");
  }
//+------------------------------------------------------------------+
#s3gt_translate_tooltip_mini { display: none !important; }
 
Solved
Reason: