Dividing 2 long values(both above 0) return 0 !

 

Hi ,

I have an issue, I am taking tick volume data and getting percent rates. The problem is that when i take the bullish and bearish tick data(both much above 0) and divide them and after that *100 , i get 0. I printed out everything and i find no logical reason for that. The data is taken with the MQL5 DOM - the processes are managed within the onbookevent handler.

  double lastAsk=0;
  double lastBid=0;
  double lastTotal=0;
  long lastBear=0;
  long lastBull=0;
  long lastBoth=0;
   if(symbol==_Symbol)
   {
   bool getBook=MarketBookGet(symbol,book);
   if(getBook)
     {
      int size=ArraySize(book);
      for(int i=0;i<size;i++)
        {
         if(book[i].type==1)
           {
            ask_size++;
            ArrayResize(Askdata,ask_size);
            Askdata[ask_index].volume_real=book[i].volume_real;
            Askdata[ask_index].time=TimeCurrent();
            Askdata[ask_index].volume=book[i].volume;
            ask_index++;
            lastAsk+=book[i].volume_real;
            lastTotal+=book[i].volume_real;
            lastBull+=book[i].volume;
            lastBoth+=book[i].volume;
           }
         else if(book[i].type==2)
           {
            bid_size++;
            ArrayResize(Biddata,bid_size);
            Askdata[bid_index].volume_real=book[i].volume_real;
            Askdata[bid_index].time=TimeCurrent();
            Biddata[bid_index].volume=book[i].volume;
            bid_index++;
            lastBid+=book[i].volume_real;
            lastTotal+=book[i].volume_real;
            lastBear+=book[i].volume;
            lastBoth+=book[i].volume;
           }
         else 
          {Print("type == ",book[i].type);}  

        }
     }
   else
     {
      Print("Could not get contents of the symbol DOM ",Symbol());
     }
      totalAskvolumeBar1=0;
      totalBidvolumeBar1=0;
      totalAskvolumeBar2=0;
      totalBidvolumeBar2=0;
      totalAskTickBar1=0;
      totalBidTickBar1=0;
      totalAskTickBar2=0;
      totalBidTickBar2=0;

      for(int i=ArraySize(Biddata)-1;i>=0;i--)
        {
         if(Biddata[i].time>TimeCurrent()-Bar1Time*60)
           {
            totalBidvolumeBar1+=Biddata[i].volume_real;
           }
         if(Biddata[i].time>TimeCurrent()-Bar1Time*60)
           {
            totalBidTickBar1+=Biddata[i].volume;
           }

         if(Biddata[i].time>TimeCurrent()-Bar2Time*60)
           {
            totalBidvolumeBar2+=Biddata[i].volume_real;
           }
         if(Biddata[i].time>TimeCurrent()-Bar2Time*60)
           {
            totalBidTickBar2+=Biddata[i].volume;
           }

         if(Biddata[i].time<TimeCurrent()-Bar2Time*60)
           {break;}
        }
      for(int i=ArraySize(Askdata)-1;i>=0;i--)
        {
         if(Askdata[i].time>TimeCurrent()-Bar1Time*60)
           {
            totalAskvolumeBar1+=Askdata[i].volume_real;
           }
         if(Askdata[i].time>TimeCurrent()-Bar1Time*60)
           {
            totalAskTickBar1+=Askdata[i].volume;
           }

         if(Askdata[i].time>TimeCurrent()-Bar2Time*60)
           {
            totalAskvolumeBar2+=Askdata[i].volume_real;
           }
         if(Askdata[i].time>TimeCurrent()-Bar2Time*60)
           {
            totalAskTickBar2+=Askdata[i].volume;
           }

         if(Askdata[i].time<TimeCurrent()-Bar2Time*60)
           {break;}
        }
      double totalVolumeBar1 = totalAskvolumeBar1+totalBidvolumeBar1;
      double totalVolumeBar2 = totalAskvolumeBar2+totalBidvolumeBar2;
      double Bar1AskPercent = (totalAskvolumeBar1/totalVolumeBar1)*100;
      double Bar1BidPercent = (totalBidvolumeBar1/totalVolumeBar1)*100;
      double Bar2AskPercent = (totalAskvolumeBar2/totalVolumeBar2)*100;
      double Bar2BidPercent = (totalBidvolumeBar2/totalVolumeBar2)*100;
      double CurAskPercent = (lastAsk/lastTotal)*100;
      double CurBidPercent = (lastBid/lastTotal)*100;

      long totalTickBar1 = totalAskTickBar1+totalBidTickBar1;
      long totalTickBar2 = totalAskTickBar2+totalBidTickBar2;
      double Bar1AskTickPercent = (double)(totalAskTickBar1/totalTickBar1)*100;
      double Bar1BidTickPercent = (double)(totalBidTickBar1/totalTickBar1)*100;
      double Bar2AskTickPercent = (double)(totalAskTickBar2/totalTickBar2)*100;
      double Bar2BidTickPercent = (double)(totalBidTickBar2/totalTickBar2)*100;
      double CurAskTickPercent = (double)(lastBull/lastBoth)*100;
      double CurBidTickPercent = (double)(lastBear/lastBoth)*100;
      Print(lastBear," ",lastBoth," ",CurBidTickPercent);

Thats the code.

I hope someone will have a solution!

Thanks

 
Stanislav Ivanov:

Hi ,

I have an issue, I am taking tick volume data and getting percent rates. The problem is that when i take the bullish and bearish tick data(both much above 0) and divide them and after that *100 , i get 0. I printed out everything and i find no logical reason for that. The data is taken with the MQL5 DOM - the processes are managed within the onbookevent handler.

Thats the code.

I hope someone will have a solution!

Thanks

The problem with your code is that you are doing integer math with an implicit cast to double. You need to cast int/long values to double before you divide them. Better yet use a function. 

void OnStart()
{
   long x = 1; 
   long y = 2;
   Print(percentage(x, y));     
}

template<typename T1, typename T2>
double percentage(T1 numerator, T2 denominator)
{
   return double(numerator) / double(denominator) * 100.0;
}
 
Stanislav Ivanov: divide them and after that *100 , i get 0. I printed out everything and i find no logical reason for that.
On MT4 v434, division quotient don't give floating point values(Bug??) - MQL4 and MetaTrader 4 - MQL4 programming forum 2012.09.18
 
nicholi shen:

The problem with your code is that you are doing integer math with an implicit cast to double. You need to cast int/long values to double before you divide them. Better yet use a function. 

thanks a lot , worked !!

Reason: