Unexpected Calculation Results

 

Hi

I am getting some unexpected results with what i expected to be a simple function. I am trying to check if a value is above a set percentage but am getting unexpected results at the percentage calculation

bool  isUpTrend(double &MAData[],MqlRates &IUTRates[],int percentRequired,int IUTNewestCandle,int IUTOldestCandle)
{
int IUTCounter=0;
for(int IUT=IUTNewestCandle;IUT<IUTOldestCandle;IUT++)
  {
   if(IUTRates[IUT].open>MAData[IUT])
     {
     //Print("Rates open ",IUTRates[IUT].open," MAData ",MAData[IUT]);
      IUTCounter++;
     }
   if(IUT==IUTOldestCandle-1)
    {
    double IUTPercent=0;
    IUTPercent=(IUTCounter/(IUTOldestCandle-IUTNewestCandle))*100;
    Print("IUTCounter ",IUTCounter, " Candles Checked ",IUTOldestCandle-IUTNewestCandle," Percent ",IUTPercent," Count/Candles ",IUTCounter/(IUTOldestCandle-IUTNewestCandle));
    if(IUTPercent>percentRequired)
      {
       return true;
      }
    }
  }
return false;
}

This is Printing the following:   IUTCounter 37 Candles Checked 150 Percent 0.0 Count/Candles 0

I cant understand why i am getting correct values for the counter and amount of candles checked but the calculation does not work.

Any help would be very much appreciated.


Documentation on MQL5: Integration / MetaTrader for Python / order_calc_margin
Documentation on MQL5: Integration / MetaTrader for Python / order_calc_margin
  • www.mql5.com
order_calc_margin - MetaTrader for Python - Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Debugging MQL5 Programs
Debugging MQL5 Programs
  • www.mql5.com
This article is intended primarily for the programmers who have already learned the language but have not fully mastered the program development yet. It reveals some debugging techniques and presents a combined experience of the author and many other programmers.
 
  • Read the documentation on typecasting; (especially the examples)
  • Try:

IUTPercent=((double)IUTCounter/(IUTOldestCandle-IUTNewestCandle))*100;

100.0 instead of 100 might work as well to force integers being cast to doubles _before_ calculation.

Documentation on MQL5: Language Basics / Data Types / Typecasting
Documentation on MQL5: Language Basics / Data Types / Typecasting
  • www.mql5.com
Typecasting - Data Types - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Haruto Rat #:
  • Read the documentation on typecasting; (especially the examples)
  • Try:

100.0 instead of 100 might work as well to force integers being cast to doubles _before_ calculation.

Thanks very much for your reply. I didn't realise that using integers in calculations would cause this issue. I understand now. Thanks again
Reason: