Error dividing double?

 

Hi, i've this code i'm working on:

   double max_risk = AccountFreeMargin()*Risk/100;
   double sl_points = MathAbs(ObjectGet("P1", OBJPROP_PRICE1)-ObjectGet("P2",OBJPROP_PRICE1))*MathPow(10,Digits);
   double tick_value = MarketInfo(Symbol(), MODE_TICKVALUE);
   double lots = max_risk/(sl_points*tick_value);

 Where P1 and P2 are two HLINE.

 

when I try to print max_risk, sl_points and tick_value (using Comment) everything works fine, but when i try to print "lots" the indicator stops working (anche the 2 HLINE are deleted)

 

Any suggestions? 

 

Thanks 

 
prancesco:

Hi, i've this code i'm working on:

 Where P1 and P2 are two HLINE.

 when I try to print max_risk, sl_points and tick_value (using Comment) everything works fine, but when i try to print "lots" the indicator stops working (anche the 2 HLINE are deleted)

Show the code for the   Comment()   that does this.
 

here it is:


#property indicator_chart_window
//--- input parameters
extern int       Risk = 3;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
    ObjectDelete("P1");
    ObjectDelete("P2");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   int    diff = 0;
   double max_risk = AccountFreeMargin()*Risk/100;
   double sl_points = MathAbs(ObjectGet("P1", OBJPROP_PRICE1)-ObjectGet("P2",OBJPROP_PRICE1))*MathPow(10,Digits);
   double tick_value = MarketInfo(Symbol(), MODE_TICKVALUE);
   double lots = max_risk/sl_points*tick_value;
//----
   Comment("Max Risk$: ", DoubleToStr(max_risk,2) + "\n"+
   "SL Points: ", DoubleToStr(sl_points,0) + "\n" +
   "Tick Value: " + DoubleToStr(tick_value, 4) + "\n" +
   "Round-lot-risk " + sl_points*tick_value + "\n" +
   "Lots to trade: " + DoubleToStr(lots,2) );


   ObjectCreate("P1", OBJ_HLINE, 0, 0, Close + (1/MathPow(10,Digits))*20);
   ObjectSet("P1", OBJPROP_WIDTH, 3);
   ObjectCreate("P2", OBJ_HLINE, 0, 0, Close - (1/MathPow(10,Digits))*20);
   ObjectSet("P2", OBJPROP_COLOR, Green);
   ObjectSet("P2", OBJPROP_WIDTH, 3); 
   diff = 0;

//----
   return(0);
  }
//+------------------------------------------------------------------+
 
prancesco:

here it is:

Check the Experts tab/Journal tab  I suspect you will see a divide by zero error.

When start is first called how is sl_points calculated ?  the Objects don't yet exist ? 

If you ObjectCreate() an Object that already exists you will et an error,  you should check if it already exists,  if it does do not create it simply move it using ObjectSet() or  ObjectMove()

 
Thanks! Moving the Object creation at the beginning of start resolves the problem
Reason: