Download MetaTrader 5
To add comments, please log in or register
1000 paid orders are executed in Freelance service. Sign up!
Ernest Klokow
1069
Ernest Klokow 2015.03.05 08:06 

I have a strange thing happening which I cannot figure out. If I run the following code

    for (i=1; i<=bars; i++) 
   {   
   closePrice = iClose(NULL,timeFrame,i);
   openPrice = iOpen(NULL,timeFrame,i);
   if (closePrice > openPrice)
   change = change + (closePrice - openPrice);
   else
   change = change + (openPrice - closePrice);
   if (CalcPoint == 0.01)
   change = NormalizeDouble(change,3);
   if (CalcPoint == 0.0001)
   change = NormalizeDouble(change,5);
}


I get a result for the variable "change" as follows:

0    09:51:31    2015.01.06 00:10  Scalper2 EURUSD,M1: Change is 0.00094

If I now multiply "change" with 100000

 for (i=1; i<=bars; i++) 
   {   
   closePrice = iClose(NULL,timeFrame,i);
   openPrice = iOpen(NULL,timeFrame,i);
   if (closePrice > openPrice)
   change = change + (closePrice - openPrice);
   else
   change = change + (openPrice - closePrice);
   if (CalcPoint == 0.01)
   change = NormalizeDouble(change,3) * 1000;
   if (CalcPoint == 0.0001)
   change = NormalizeDouble(change,5) * 100000;
}

I should get 94, but look what I get

0    10:00:23    2015.01.06 00:13  Scalper2 EURUSD,M1: Change is -9.223372036854776e+018

Can anyone tell me why this happens?

Carl Schreiber
5953
Carl Schreiber 2015.03.05 08:44  

May be you should place the "* 100000" outside the loop?

for (i=1; i<=bars; i++) 
   {   
   closePrice = iClose(NULL,timeFrame,i);
   openPrice = iOpen(NULL,timeFrame,i);
   if (closePrice > openPrice)
   change = change + (closePrice - openPrice);
   else
   change = change + (openPrice - closePrice);
}
if (CalcPoint == 0.01)
   change = NormalizeDouble(change,3) * 1000;
if (CalcPoint == 0.0001)
   change = NormalizeDouble(change,5) * 100000;

Ernest Klokow
1069
Ernest Klokow 2015.03.05 08:50  
Fantastic! Thanks a lot Gooly. Your advice solved the problem!
Keith Watford
Moderator
7947
Keith Watford 2015.03.05 09:05  

I know it is a small piece of code, but

   if (closePrice > openPrice)
   change = change + (closePrice - openPrice);
   else
   change = change + (openPrice - closePrice);

 Can be replaced with

   change += MathAbs(openPrice - closePrice);
Ernest Klokow
1069
Ernest Klokow 2015.03.05 09:14  
Thanks Gumrai! That is far more efficient. I will use that approach in future - starting now with this code.
/
To add comments, please log in or register