Rebuild TrailingStop problem "Freezing while loop"

 

The Algorithm of the Trailingstop only Long Example:



My Try:

for(int i=0;i<limit;i++){

   double currentShortMA=iMA(NULL,0,7,0,MODE_EMA,PRICE_CLOSE,i+1);
   double currentLongMA=iMA(NULL,0,13,0,MODE_EMA,PRICE_CLOSE,i+1);   
   double previousShortMA=iMA(NULL,0,7,0,MODE_EMA,PRICE_CLOSE,i+2);
   double previousLongMA=iMA(NULL,0,13,0,MODE_EMA,PRICE_CLOSE,i+2);

   if(previousShortMA<previousLongMA && currentShortMA>currentLongMA){
      if(Close[i+1]>High[i+2]){
         int artificiallyCounter=i+2;
         lowCheck=Low[artificiallyCounter];        
         bool loopCheck=True;
         while(loopCheck){
            if(lowCheck>Low[i+1]){
               lowCheck=Low[artificiallyCounter+1];
            }else{
               loopCheck=False;
               upStop_1[i+1]=lowCheck;
            }
         }
      }    
   }//if end
}

My Question:

1.Do i have mistakes on the Algorithm of the Trailingstop?

2.Why Is my code alway freezing?

 
Freezes because of array exceeded.
  1. You should stop using the old event handlers and IndicatorCounted() and start using new event handlers.
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016)

  2. double previousLongMA=iMA(NULL,0,13,0,MODE_EMA,PRICE_CLOSE,i+2);
    
          if(Close[i+1]>High[i+2]){
    Your lookback is two.
              How to do your lookbacks correctly #9#14 & #19
 
William Roeder #:
Freezes because of array exceeded.
  1. You should stop using the old event handlers and IndicatorCounted() and start using new event handlers.
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016)

  2. Your lookback is two.
              How to do your lookbacks correctly #9#14 & #19
//---
   int lookback=5;
   int counted = IndicatorCounted();
   
//+------------------------------------------------------------------+
   for(int iBar=Bars-1-MathMax(lookback,counted);iBar>=0;--iBar){
      Print("Hello");
   }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

I did it like in the examples and tried a Hello but its freezing still

 
William Roeder #:
Freezes because of array exceeded.
  1. You should stop using the old event handlers and IndicatorCounted() and start using new event handlers.
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016)

  2. Your lookback is two.
              How to do your lookbacks correctly #9#14 & #19
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   int lookback=5;
   int counted = IndicatorCounted();
   
//+------------------------------------------------------------------+
   for(int iBar = MathMax(lookback, prev_calculated); iBar < Bars; ++iBar){
      Print("Hello");
   }
   return rates_total-1; // Recalculate current bar next tick.
  }
//+------------------------------------------------------------------+

also freezing, I'm going crazy really...