Zero divide Error - even after declaring fix

Robocop01
24
Robocop01  

Hi 

I have a Zero Divide Error popping up in 2 areas of my code base, the just of the calculation is to calculate gradient. 

The differences between the Run (x axis, int bar position) is calculated and when that difference is 0, the algorithm automatically changes the variable to 1. 

However I am still getting the Zero Divide error.

***

Vladimir Karputov
Moderator
240825
Vladimir Karputov  
Robocop01 :

Hi 

I have a Zero Divide Error popping up in 2 areas of my code base, the just of the calculation is to calculate gradient. 

The differences between the Run (x axis, int bar position) is calculated and when that difference is 0, the algorithm automatically changes the variable to 1. 

However I am still getting the Zero Divide error.

***

Insert the code correctly: use the button Code

Lorentzos Roussos
25452
Lorentzos Roussos  
Robocop01:

Hi 

I have a Zero Divide Error popping up in 2 areas of my code base, the just of the calculation is to calculate gradient. 

The differences between the Run (x axis, int bar position) is calculated and when that difference is 0, the algorithm automatically changes the variable to 1. 

However I am still getting the Zero Divide error.

***

Hello ,try and compile the project file that contains the library as well (not just the library) and what mr Karputov said

Robocop01
24
Robocop01  
//Arrays and Initialization
//1M
   double HighPos1MArray[5];
   for(e1M=0; e1M<4; e1M++)
     {
      HighPos1MArray[0]= High1M2;
      HighPos1MArray[1]= High1M5;
      HighPos1MArray[2]= High1M13;
      HighPos1MArray[3]= High1M34;
      HighPos1MArray[4]= High1M55;
      double DeltaHghPos1MArray[4];
      for(v1M=0; v1M<3; v1M++)
        {
         //DeltaHghPos1MArray[v1M]=((HighPos1MArray[0])-(HighPos1MArray[v1M+1]));
         DeltaHghPos1MArray[0]= ((HighPos1MArray[1]-HighPos1MArray[0]));
         if (DeltaHghPos1MArray[0]==0)
            {
             DeltaHghPos1MArray[0]=1;
            }
         DeltaHghPos1MArray[1]= ((HighPos1MArray[2]-HighPos1MArray[0]));
         if (DeltaHghPos1MArray[1]==0)
            {
             DeltaHghPos1MArray[1]=1;
            }
         DeltaHghPos1MArray[2]= ((HighPos1MArray[3]-HighPos1MArray[0]));
         if (DeltaHghPos1MArray[2]==0)
            {
             DeltaHghPos1MArray[2]=1;
            }
         DeltaHghPos1MArray[3]= ((HighPos1MArray[4]-HighPos1MArray[0]));
         if (DeltaHghPos1MArray[3]==0)
            {
             DeltaHghPos1MArray[3]=1;
            }
        }
     }
   double LowPos1MArray[5];
   for(f1M=0; f1M<4; f1M++)-
     {
      LowPos1MArray[0]= Low1M2;
      LowPos1MArray[1]= Low1M5;
      LowPos1MArray[2]= Low1M13;
      LowPos1MArray[3]= Low1M34;
      LowPos1MArray[4]= Low1M55;
      double DeltaLowPos1MArray[4];
      for(u1M=0; u1M<3; u1M++)
        {
//         (LowPos1MArray[1])-(f1M+1);
         DeltaLowPos1MArray[0]= ((LowPos1MArray[1]-LowPos1MArray[0]));
         if (DeltaLowPos1MArray[0]==0)
            {
             DeltaLowPos1MArray[0]=1;
            }
         DeltaLowPos1MArray[1]= ((LowPos1MArray[2]-LowPos1MArray[0]));
         if (DeltaLowPos1MArray[1]==0)
            {
             DeltaLowPos1MArray[1]=1;
            }
         DeltaLowPos1MArray[2]= ((LowPos1MArray[3]-LowPos1MArray[0]));
         if (DeltaLowPos1MArray[2]==0)
            {
             DeltaLowPos1MArray[2]=1;
            }
         DeltaLowPos1MArray[3]= ((LowPos1MArray[4]-LowPos1MArray[0]));
         if (DeltaLowPos1MArray[3]==0)
            {
             DeltaLowPos1MArray[3]=1;
            }
        }
     }
   double HighPrc1MArray[5];
   for(i1M=0; i1M<4; i1M++)
     {
      HighPrc1MArray[0]= High1M2Prc;
      HighPrc1MArray[1]= High1M5Prc;
      HighPrc1MArray[2]= High1M13Prc;
      HighPrc1MArray[3]= High1M34Prc;
      HighPrc1MArray[4]= High1M55Prc;
      double DeltaHghPrc1MArray[4];
      for(z1M=0; z1M<3; z1M++)
        {
//         (HghPrc1MArray[1])-(i1M+1);

         DeltaHghPrc1MArray[0]= ((HighPrc1MArray[1]-HighPrc1MArray[0]));
         DeltaHghPrc1MArray[1]= ((HighPrc1MArray[2]-HighPrc1MArray[0]));
         DeltaHghPrc1MArray[2]= ((HighPrc1MArray[3]-HighPrc1MArray[0]));
         DeltaHghPrc1MArray[3]= ((HighPrc1MArray[4]-HighPrc1MArray[0]));
        }
     }
   double HighMFI1MArray[5];
   for(b1M=0; b1M<4; b1M++)
     {
      HighMFI1MArray[0]= High1MMFI2;
      HighMFI1MArray[1]= High1MMFI5;
      HighMFI1MArray[2]= High1MMFI13;
      HighMFI1MArray[3]= High1MMFI34;
      HighMFI1MArray[4]= High1MMFI55;
      double DeltaHghMFI1MArray[4];
      for(y1M=0; y1M<3; y1M++)
        {
//         (HighMFI1MArray[1])-(b1M+1);
         DeltaHghMFI1MArray[0]= ((HighMFI1MArray[1]-HighMFI1MArray[0]));
         DeltaHghMFI1MArray[1]= ((HighMFI1MArray[2]-HighMFI1MArray[0]));
         DeltaHghMFI1MArray[2]= ((HighMFI1MArray[3]-HighMFI1MArray[0]));
         DeltaHghMFI1MArray[3]= ((HighMFI1MArray[4]-HighMFI1MArray[0]));
        }
     }
   double LowPrc1MArray[5];
   for(c1M=0; c1M<4; c1M++)
     {
      LowPrc1MArray[0]= Low1M2Prc;
      LowPrc1MArray[1]= Low1M5Prc;
      LowPrc1MArray[2]= Low1M13Prc;
      LowPrc1MArray[3]= Low1M34Prc;
      LowPrc1MArray[4]= Low1M55Prc;
      double DeltaLowPrc1MArray[4];
      for(x1M=0; x1M<3; x1M++)
        {
//         (LowPrc1MArray[1])-(c1M+1);
         DeltaLowPrc1MArray[0]= ((LowPrc1MArray[1]-LowPrc1MArray[0]));
         DeltaLowPrc1MArray[1]= ((LowPrc1MArray[2]-LowPrc1MArray[0]));
         DeltaLowPrc1MArray[2]= ((LowPrc1MArray[3]-LowPrc1MArray[0]));
         DeltaLowPrc1MArray[3]= ((LowPrc1MArray[4]-LowPrc1MArray[0]));
        }
     }
   double LowMFI1MArray[5];
   for(d1W=0; d1W<4; d1W++)
     {
      LowMFI1MArray[0]= Low1MMFI2;
      LowMFI1MArray[1]= Low1MMFI5;
      LowMFI1MArray[2]= Low1MMFI13;
      LowMFI1MArray[3]= Low1MMFI34;
      LowMFI1MArray[4]= Low1MMFI55;
      double DeltaLowMFI1MArray[4];
      for(w1M=0; w1M<3; w1M++)
        {
//         (LowMFI1MArray[1])-(d1M+1);
         DeltaLowMFI1MArray[0]= ((LowMFI1MArray[1]-LowMFI1MArray[0]));
         DeltaLowMFI1MArray[1]= ((LowMFI1MArray[2]-LowMFI1MArray[0]));
         DeltaLowMFI1MArray[2]= ((LowMFI1MArray[3]-LowMFI1MArray[0]));
         DeltaLowMFI1MArray[3]= ((LowMFI1MArray[4]-LowMFI1MArray[0]));
        }
     }
   double DeltaHghPrc1MArray[4];
   double DeltaHghPos1MArray[4];
   double HighPriceSlope1MArray[4];
   for(ContainerHighPriceSlope1MArray=0; ContainerHighPriceSlope1MArray<3; ContainerHighPriceSlope1MArray++)
     {
      HighPriceSlope1MArray[0]=DeltaHghPrc1MArray[0]/DeltaHghPos1MArray[0];
      HighPriceSlope1MArray[1]=DeltaHghPrc1MArray[1]/DeltaHghPos1MArray[1];
      HighPriceSlope1MArray[2]=DeltaHghPrc1MArray[2]/DeltaHghPos1MArray[2];
      HighPriceSlope1MArray[3]=DeltaHghPrc1MArray[3]/DeltaHghPos1MArray[3];
     }
   double DeltaHghMFI1MArray[4];
   double HighMFISlope1MArray[4];
   for(ContainerHighMFISlope1MArray=0; ContainerHighMFISlope1MArray<3; ContainerHighMFISlope1MArray++)
     {
      HighMFISlope1MArray[0]=(DeltaHghMFI1MArray[0]/DeltaHghPos1MArray[0]);
      HighMFISlope1MArray[1]=(DeltaHghMFI1MArray[1]/DeltaHghPos1MArray[1]);
      HighMFISlope1MArray[2]=(DeltaHghMFI1MArray[2]/DeltaHghPos1MArray[2]);
      HighMFISlope1MArray[3]=(DeltaHghMFI1MArray[3]/DeltaHghPos1MArray[3]);
     }
   double DeltaLowPrc1MArray[4];
   double DeltaLowPos1MArray[4];
   double LowPriceSlope1MArray[4];
   for(ContainerLowPriceSlope1MArray=0; ContainerLowPriceSlope1MArray<3; ContainerLowPriceSlope1MArray++)
     {
      LowPriceSlope1MArray[0]=DeltaLowPrc1MArray[0]/DeltaLowPos1MArray[0];
      LowPriceSlope1MArray[1]=DeltaLowPrc1MArray[1]/DeltaLowPos1MArray[1];
      LowPriceSlope1MArray[2]=DeltaLowPrc1MArray[2]/DeltaLowPos1MArray[2];
      LowPriceSlope1MArray[3]=DeltaLowPrc1MArray[3]/DeltaLowPos1MArray[3];
     }

Thank you for the input 


the full file is a bit large, please find the attached as requested 

Paul Anscombe
5691
Paul Anscombe  

Robocop01
:

 

   double DeltaHghPrc1MArray[4];
   double DeltaHghPos1MArray[4];
   double HighPriceSlope1MArray[4];
   for(ContainerHighPriceSlope1MArray=0; ContainerHighPriceSlope1MArray<3; ContainerHighPriceSlope1MArray++)
     {
      HighPriceSlope1MArray[0]=DeltaHghPrc1MArray[0]/DeltaHghPos1MArray[0];
      HighPriceSlope1MArray[1]=DeltaHghPrc1MArray[1]/DeltaHghPos1MArray[1];
      HighPriceSlope1MArray[2]=DeltaHghPrc1MArray[2]/DeltaHghPos1MArray[2];
      HighPriceSlope1MArray[3]=DeltaHghPrc1MArray[3]/DeltaHghPos1MArray[3];
     }
   double DeltaHghMFI1MArray[4];
   double HighMFISlope1MArray[4];
   for(ContainerHighMFISlope1MArray=0; ContainerHighMFISlope1MArray<3; ContainerHighMFISlope1MArray++)
     {
      HighMFISlope1MArray[0]=(DeltaHghMFI1MArray[0]/DeltaHghPos1MArray[0]);
      HighMFISlope1MArray[1]=(DeltaHghMFI1MArray[1]/DeltaHghPos1MArray[1]);
      HighMFISlope1MArray[2]=(DeltaHghMFI1MArray[2]/DeltaHghPos1MArray[2]);
      HighMFISlope1MArray[3]=(DeltaHghMFI1MArray[3]/DeltaHghPos1MArray[3]);
     }
   double DeltaLowPrc1MArray[4];
   double DeltaLowPos1MArray[4];
   double LowPriceSlope1MArray[4];
   for(ContainerLowPriceSlope1MArray=0; ContainerLowPriceSlope1MArray<3; ContainerLowPriceSlope1MArray++)
     {
      LowPriceSlope1MArray[0]=DeltaLowPrc1MArray[0]/DeltaLowPos1MArray[0];
      LowPriceSlope1MArray[1]=DeltaLowPrc1MArray[1]/DeltaLowPos1MArray[1];
      LowPriceSlope1MArray[2]=DeltaLowPrc1MArray[2]/DeltaLowPos1MArray[2];
      LowPriceSlope1MArray[3]=DeltaLowPrc1MArray[3]/DeltaLowPos1MArray[3];


you are declaring arrays then using them to divide with before you assign any value to them..



Robocop01
24
Robocop01  
Paul Anscombe:

you are declaring arrays then using them to divide with before you assign any value to them..



Apologies Paul, the allocations are done further up. 


I'll just post the full source 

//Monthly
//Highs
   double High1M2 = iHighest(_Symbol,PERIOD_MN1,2);
   double High1M5 = iHighest(_Symbol, PERIOD_MN1,5);
   double High1M13 = iHighest(_Symbol, PERIOD_MN1,Container13);
   double High1M34 = iHighest(_Symbol, PERIOD_MN1,Container34);
   double High1M55= iHighest(_Symbol, PERIOD_MN1,Container55);
   double High1M2Prc = iHigh(_Symbol, PERIOD_MN1, High1M2);
   double High1M5Prc = iHigh(_Symbol, PERIOD_MN1, High1M5);
   double High1M13Prc = iHigh(_Symbol, PERIOD_MN1, High1M13);
   double High1M34Prc = iHigh(_Symbol, PERIOD_MN1, High1M34);
   double High1M55Prc = iHigh(_Symbol, PERIOD_MN1, High1M55);
   double High1MMFI2 = iMFI(_Symbol, PERIOD_MN1,2, High1M2);
   double High1MMFI5 = iMFI(_Symbol, PERIOD_MN1,5, High1M5);
   double High1MMFI13 = iMFI(_Symbol, PERIOD_MN1, 13, High1M13);
   double High1MMFI34 = iMFI(_Symbol, PERIOD_MN1,34, High1M34);
   double High1MMFI55 = iMFI(_Symbol, PERIOD_MN1,89, High1M55);
//Lows
   double Low1M2 = iLowest(_Symbol, PERIOD_W1,2);
   double Low1M5 = iLowest(_Symbol, PERIOD_W1,5);
   double Low1M13 = iLowest(_Symbol, PERIOD_W1,Container13);
   double Low1M34 = iLowest(_Symbol, PERIOD_W1,Container34);
   double Low1M55= iLowest(_Symbol, PERIOD_W1,Container55);
   double Low1M2Prc = iLow(_Symbol, PERIOD_W1, Low1M2);
   double Low1M5Prc = iLow(_Symbol, PERIOD_W1, Low1M5);
   double Low1M13Prc = iLow(_Symbol, PERIOD_W1, Low1M13);
   double Low1M34Prc = iLow(_Symbol, PERIOD_W1, Low1M34);
   double Low1M55Prc = iLow(_Symbol, PERIOD_W1, Low1M55);
   double Low1MMFI2 = iMFI(_Symbol, PERIOD_W1,13, Low1M2);
   double Low1MMFI5 = iMFI(_Symbol, PERIOD_W1, 13, Low1M5);
   double Low1MMFI13 = iMFI(_Symbol, PERIOD_W1, 13, Low1M13);
   double Low1MMFI34 = iMFI(_Symbol, PERIOD_W1, 13, Low1M34);
   double Low1MMFI55 = iMFI(_Symbol, PERIOD_W1, 13, Low1M55);
Robocop01
24
Robocop01  
It seems like the code exceeds the 65000 character limit
Paul Anscombe
5691
Paul Anscombe  
Robocop01:

Apologies Paul, the allocations are done further up. 


No, the arrays I higlighted are being declared but not assigned before you use them.

You need to put values into the arrays before doing the calculations.

The extra source you just posted does not have those arrays in, and even if it did you are making a new declaration so it would override any previous declaration or fail to compile if they were in the same function.

double DeltaLowPrc1MArray[4];   // this is a declaration the array will be created without assigned values or worse random values.

DeltaLowPrc1MArray[0] = 55.55;  // this is an assignment and places a value into the array in index position [0]


Robocop01
24
Robocop01  

Ok I see that makes sense, so I would have to perform these calculations privately and directly after declaring the variables that make up the calculation?

If I define these variables privately, how do I call the info on a higher level for further data processing (Straight line projection after calculation of the regression)?

Paul Anscombe
5691
Paul Anscombe  
Robocop01:

Ok I see that makes sense, so I would have to perform these calculations privately and directly after declaring the variables that make up the calculation?

If I define these variables privately, how do I call the info on a higher level for further data processing (Straight line projection after calculation of the regression)?

I do not know what you mean by privately.

All you have to do is ensure that any variables you use in calculations have been assigned values before you do the calculation

You should also check for zero divide errors before doing the divide

The checks a trading robot must pass before publication in the Market
The checks a trading robot must pass before publication in the Market
  • www.mql5.com
Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.