My function is dividing by zero

 

This isn't really my function. I copied it from somewhere (possibly this forum) quite some time ago, and now I am struggling to understand how to fix it.

What it does:
I determine an amount in currency and I choose between StopLoss or TakeProfit. The function is supposed to return the exact price at which the order will gain or lose the specified amount in currency.

double calcSLTP(string choice, double amount) {
        double SL_in_Currency = amount;
        double TP_in_Currency = amount;

        double tick_size = SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE);
        double pnt = SymbolInfoDouble(Symbol(),SYMBOL_POINT);
        double minfromcurrentprice = SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL) * pnt;
        double loss_for_1_lot = SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE);  //Loss/Gain for a 1 tick move with 1 lot
        double loss_for_lot_size_this_trade = loss_for_1_lot * ordersize;    //Loss/Gain for a 1 tick move with trade lot size
        double SL_ticks = SL_in_Currency / loss_for_lot_size_this_trade;
        double SL_points = MathFloor(SL_ticks * tick_size / pnt) * pnt;
        double TP_ticks = TP_in_Currency / loss_for_lot_size_this_trade;
        double TP_points = MathFloor(TP_ticks * tick_size / pnt) * pnt;

        if      (orderAction1 == OP_SELL)       {SL_points = (-SL_points); TP_points = (-TP_points);}
        double new_sl = Ask - SL_points; 
        double new_tp = Ask + TP_points;
        
        if      (choice == "SL")        {return new_sl;}
        if      (choice == "TP")        {return new_tp;}
        return 0;
}


The problem is in this line which divides by zero:

double SL_ticks = SL_in_Currency / loss_for_lot_size_this_trade;


I am not sure how to fix it. Can someone please advise.

 
whoowl: This isn't really my function. I copied it from somewhere (possibly this forum) quite some time ago, and now I am struggling to understand how to fix it.What it does: I determine an amount in currency and I choose between StopLoss or TakeProfit. The function is supposed to return the exact price at which the order will gain or lose the specified amount in currency. The problem is in this line which divides by zero: I am not sure how to fix it. Can someone please advise.

To fix "divide by zero" you should analyse your code and look at every expression where a division occurs and think about whether the divisor will ever be zero or not.

So ask yourself, will "loss_for_lot_size_this_trade" ever be equal to 0?

Given that "loss_for_lot_size_this_trade = loss_for_1_lot * ordersize", then when either "loss_for_1_lot" or "ordersize" is zero it will fail.

So ask yourself, ...

  • ... will "loss_for_1_lot" ever be equal to 0?
  • ... will "ordersize" ever be equal to 0?

For "loss_for_1_lot", it will be when "SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)" fails to obtain the data, for example when the symbol data has not yet been downloaded or a connection to the server is down.

For "ordersize" , it is unknown because the variable does not appear in your sample code, so it is probably a global variable which you will have to analyse.

So, how to your prevent this?

By detecting when these values are zero and stop calculating the rest, and take action to report to the log or do some kind of recovery.


 
Fernando Carreiro #:

By detecting when these values are zero and stop calculating the rest, and take action to report to the log or do some kind of recovery.


Yes exactly - I find using a ternary expression whenever there is a possible divide by 0 risk is a a convenient way to address this.


Example:

double SL_ticks = (loss_for_lot_size_this_trade != 0)? SL_in_Currency / loss_for_lot_size_this_trade : 0; 
if (SL_ticks == 0) //handle as needed
 
// Set minLot:

double LotSize= ; // Your lot size.
double minLot=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);

if(LotSize<minLot){LotSize=minLot); // Ensure your lot size isn't below the minLot.

Hope this helps.