Download MetaTrader 5

Problem with incorrect calculation of fractals in the indicator

To add comments, please log in or register
Launch agents on an idle PC. It is profitable!
InfiniteLoop
620
InfiniteLoop 2015.01.31 13:20 

Hi,

I've got a problem with indicator I just wrote and I would be really gratefull for any advice on what the problem is or how to solved it 

Description of indicator:

Function of the Indicator is to display Fractals on different timeframes, not just their own, so for examle: display Fractals  M5 on M1 timeframe.

Indicator first collects data from iFractal for their own timeframes and then calculates shift on timeframe we want to diplay it, in this case M1 timeframe, this is done by checking interval of candles High's or Low's and comparing it to saved price from iFractal, example fractal on M5 covers 5 candles on M1 so it checks which of this 5 candles is the fractal if want to display it on M1 chart, basically it pinpoints location of the fractal on a lower timeframe

Problem is that data get's scrambled, some data get's lost and in the end I get arrays (buffers) of data that are a not complete or correct

Since I just recently switched from mql4 to mql5, I'm not sure where the problem is, but I believe that call of OnCalculate somehow interupts execution from previos call  

// here is the part of the code that collects data from iFractal, this works correctly
// FRACTAL M1
   handle_M1 = iFractals(_Symbol, PERIOD_M1);  
   CopyBuffer(handle_M1, 0, 0, Bars(_Symbol,PERIOD_CURRENT), Fractal_M1_PRICE_U);
   CopyBuffer(handle_M1, 1, 0, Bars(_Symbol,PERIOD_CURRENT), Fractal_M1_PRICE_L); 
   ArraySetAsSeries(Fractal_M1_PRICE_U, true);  
   ArraySetAsSeries(Fractal_M1_PRICE_L, true);  
   
   // FRACTAL M2
   handle_M2 = iFractals(_Symbol, PERIOD_M2);  
   CopyBuffer(handle_M2, 0, 0, Bars(_Symbol,PERIOD_CURRENT), Fractal_M2_PRICE_U);
   CopyBuffer(handle_M2, 1, 0, Bars(_Symbol,PERIOD_CURRENT), Fractal_M2_PRICE_L); 
   ArraySetAsSeries(Fractal_M2_PRICE_U, true);  
   ArraySetAsSeries(Fractal_M2_PRICE_L, true);
// Call to function that calculates shift -> parameters are HIGH or LOW of every candle on M1, buffers for M2 fractals price and date, and Multiply ( 1 M2 candle = 2 M1 candles) and offset, which gives us how many candles we are checking (so for M2 -> 0,1)
// You cna ignore DATE buffer, it's used in different part of indicator and it not part of the problem
// FRACTAL UP & DOWN M2 na M1    
   FRDS(HIGH, TIME, Fractal_M2_PRICE_U, Fractal_M2_DATE_U, 2, 1);      
   FRDS(LOW, TIME, Fractal_M2_PRICE_L, Fractal_M2_DATE_L, 2, 1);

// function works like this, first we copy data from buffer into different array so we can compare data and not rewrite them 
// we loop trough array with fractals, we check which array shift is different from DBL_MAX (not a fractal at  this position)
// we then check that calculation is greater than 0 and lower than number of bars displayed (I have set to 150000 approximately) -> so we don't get array out of range
// then we have second for loop that goes trough HIGH or LOW and compares it to FRACTAL price -> the loop isn't realy needed for M2 fractals (only to possible values), but for higher timeframes it's logical to use loop
// if it finds equal price, then it breaks inside loop since we found the shift of fractal
// AGAIN you can ignore DATE buffer' it's used for different part of indicator and it's not part fo the problem
void FRDS(double &EXTREME[], datetime &TIME[], double &FRACTAL_PRICE[], datetime &FRACTAL_TIME[], int MULTIPLY, int OFFSET)
{  
   double FR[];    
   ArraySetAsSeries(FR,true);   
   ArrayCopy(FR,FRACTAL_PRICE);   
   ArrayFill(FRACTAL_PRICE,0,Bars(_Symbol,PERIOD_CURRENT),DBL_MAX);
   ArrayCopy(FRACTAL_TIME,TIME);   
   ArrayFill(FRACTAL_TIME,0,Bars(_Symbol,PERIOD_CURRENT),0);
   // 0 ==      1970.01.01 00:00:00 
    
   for(int i = 0; i < ArraySize(FR); i++)
   {
      if(FR[i] != DBL_MAX)
      {
         // END & START 
         if(i*MULTIPLY <= 150000 && i*MULTIPLY-OFFSET >= 0)
         { 
            for(int k = i*MULTIPLY-OFFSET; k <= i*MULTIPLY; k++)
            {                                
               if(EXTREME[k] == FR[i])
               {                    
                  FRACTAL_PRICE[k] = FR[i];
                  FRACTAL_TIME[k] =  TIME[i];                  
                  break;
               }                              
            }
         }                  
      }            
   }
}
To add comments, please log in or register