Zero divide error

Cheddarbob
36

Hey guys, having a tough time sorting this one out, been at it a couple days now lol..

No matter which way i right the if statement i always end up with a zero divide, the error messages always seem to point back to the initial calculation itself (buyriskreward or sellriskreward).

I guess i should mention im just trying to create a risk to reward filter, anything above 1:1.5 i want to take a trade, any help is greatly appreciated!

here's the code

 //FOR BUYS
         int buyStopLoss= iLowest(NULL,0,MODE_LOW,CandlesBack,1);
         int buyTPlevel= iHighest(NULL,0,MODE_CLOSE,TPCandlesBack,1);
         double buyriskreward = ((Close[buyTPlevel]) - Ask) / (Ask - (Low[buyStopLoss]));

      //FOR SELLS
               int sellStopLoss= iHighest(NULL,0,MODE_HIGH,CandlesBack,1);
               int sellTPlevel= iLowest(NULL,0,MODE_CLOSE,TPCandlesBack,1);
               double sellriskreward = ((High[sellStopLoss]) - Bid) / (Bid - (Close[sellTPlevel]));
    
      

      if(buyriskreward >=1.5 && GapTwo > SmoothedMATen && ParaSar < Bid && OrdersTotal()==0)
        if(buyriskreward == 0)
        {
        Print("Error message:",GetLastError());
        ResetLastError();
        }
        else
         {
          EnterTrade(OP_BUY);        
          }
          

      if(sellriskreward >= 1.5 && Gaptwo < SmoothedMATen && ParaSar > Ask && OrdersTotal()==0)
        if(sellriskreward == 0)
        {
        Print("Error message:",GetLastError());
        ResetLastError();
        }
        else
           {
            EnterTrade(OP_SELL);
           }
           
Alain Verleyen
42817
mql4 question on MT4/mql4 section of the forum please.
Paul Anscombe
6644
Cheddarbob:

Hey guys, having a tough time sorting this one out, been at it a couple days now lol..

No matter which way i right the if statement i always end up with a zero divide, the error messages always seem to point back to the initial calculation itself (buyriskreward or sellriskreward).

I guess i should mention im just trying to create a risk to reward filter, anything above 1:1.5 i want to take a trade, any help is greatly appreciated!

here's the code

use the debugger and set a break point before the calculation line. Then you can examine the values of the variables and understand where your problem is.

Cheddarbob
36
Cheddarbob  
Paul Anscombe #:

use the debugger and set a break point before the calculation line. Then you can examine the values of the variables and understand where your problem is.

Awesome I never even thought of using that, I’m a noob 🤷‍♂️. Thanks!
Cheddarbob
36
Cheddarbob  
Paul Anscombe #:

use the debugger and set a break point before the calculation line. Then you can examine the values of the variables and understand where your problem is.

Do the risk reward calculations seem right to you? I worked it out on a whiteboard an it all seems fine, it seems to work a lot of the time but once and a while I get that zero divide error, problem for me is I can’t think of a different way to calculate risk vs reward without using division..
Mehmet Bastem
2753
Mehmet Bastem  
Cheddarbob:

Hey guys, having a tough time sorting this one out, been at it a couple days now lol..

No matter which way i right the if statement i always end up with a zero divide, the error messages always seem to point back to the initial calculation itself (buyriskreward or sellriskreward).

I guess i should mention im just trying to create a risk to reward filter, anything above 1:1.5 i want to take a trade, any help is greatly appreciated!

here's the code

 //FOR BUYS
         int buyStopLoss= iLowest(NULL,0,MODE_LOW,CandlesBack,1);
         int buyTPlevel= iHighest(NULL,0,MODE_CLOSE,TPCandlesBack,1);
double buyriskreward =0;
double sellriskreward=0;
if((Ask - (Low[buyStopLoss])>0
         double buyriskreward = ((Close[buyTPlevel]) - Ask) / (Ask - (Low[buyStopLoss]));

      //FOR SELLS
               int sellStopLoss= iHighest(NULL,0,MODE_HIGH,CandlesBack,1);
               int sellTPlevel= iLowest(NULL,0,MODE_CLOSE,TPCandlesBack,1);
if((Bid - (Close[sellTPlevel]))>0
               sellriskreward = ((High[sellStopLoss]) - Bid) / (Bid - (Close[sellTPlevel]));
    
      

      if(buyriskreward >=1.5 && GapTwo > SmoothedMATen && ParaSar < Bid && OrdersTotal()==0)
        if(buyriskreward == 0)
        {
        Print("Error message:",GetLastError());
        ResetLastError();
        }
        else
         {
          EnterTrade(OP_BUY);        
          }
          

      if(sellriskreward >= 1.5 && Gaptwo < SmoothedMATen && ParaSar > Ask && OrdersTotal()==0)
        if(sellriskreward == 0)
        {
        Print("Error message:",GetLastError());
        ResetLastError();
        }
        else
           {
            EnterTrade(OP_SELL);
           }
Cheddarbob
36
Cheddarbob  
Mehmet Bastem #:
oh awesome! i cannot wait to try this out, thank you!
Todd Peter Gilbey
420
Todd Peter Gilbey  
Yes, I've come to learn and whenever you have an IF/ELSE statement, ALWAYS, print an error code with a GetLastError() function in the ELSE brackets, it will save your sanity! Hope you got it sorted :)  

Also, if you put any number into a standard calculator and divide by zero, watch what happens :) 

All the best, 
Cheddarbob
36
Cheddarbob  
Todd Peter Gilbey #:
Yes, I've come to learn and whenever you have an IF/ELSE statement, ALWAYS, print an error code with a GetLastError() function in the ELSE brackets, it will save your sanity! Hope you got it sorted :)  

Also, if you put any number into a standard calculator and divide by zero, watch what happens :) 

All the best, 

whoa, i didn't know that would happen! cool!, ah i havent gotten it sorted yet but i've got a few idea's ive found from looking around, going to write them all down and slowly go through them, noob problems haha. thanks for the feedback

Cheddarbob
36
Cheddarbob  
update: i figured it out! I implemented  Mehmet Bastem's code, initially i was just doing it wrong, then i messed around a bit, reversed the sellriskreward calculation and it all came together, thanks very much everyone for the help!!
Mehmet Bastem
Mehmet Bastem
  • 2021.06.24
  • www.mql5.com
Trader's profile