Small Moneymanagement-Indicator ruined by new MQL4 - page 2

 
@deysmacro: I know where the line is which causes the division by zero. It is because I need the daily average range of the last five days. Later in the indicator I divide something by that ATR and as long as the data is not updated ATR is zero and that is the problem. I need to be sure that the daily data of at least the last 5 days is available. Only then the ATR will be >0 and the indicator works.
 
mar:
@deysmacro: I know where the line is which causes the division by zero. It is because I need the daily average range of the last five days. Later in the indicator I divide something by that ATR and as long as the data is not updated ATR is zero and that is the problem. I need to be sure that the daily data of at least the last 5 days is available. Only then the ATR will be >0 and the indicator works.

Put available data into GV and retrieve it. That way, when there is data coming or there is differences in data, just update GV.

Maybe ATR works that way.


People always said "Make it work first, then optimize the code."

Sometimes to make it work, the line of codes are very long but once it is working, you can start to trim the code. :)

 

Maybe it sounds stupid, but what is GV?

 
Global Variables
 

Of course... :-)

Something else I can't explain: The following code is a small modification of my first code I posted. It is also a Moneymanagement indicator but I removed the ATR. It only prints stop loss levels from 5 - 100 pips in 5-pip-steps. So, there is no ATR-calculation at all. When you drag this indicator to your chart it works fine. Every pair, every timeframe. No problems. But when you OPEN the MetaTrader it causes a division by zero error and again only "Label" is printed instead of the stop loss levels.

The error is caused in this line:

lots=MoneyRisk/SL/(MarketInfo(Symbol(), MODE_TICKVALUE)*multi);

I printed ALL variables of this line and I came to the conclusion that AccountBalance() is zero and MarketInfo(Symbol(), MODE_TICKVALUE) is zero. So obviously when you open the MetaTrader, no data at all is available. This never happened in the old MetaTrader. So I have no idea what to do...

Here is the code:

#property indicator_chart_window
//--- input parameters
extern int  Risk=5;
extern int SL_Levels=20;
int multi, exp1, SL, i, k;
double MoneyRisk, Spread, modifySpread, lots;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   k=0;
   for(i=1; i<=SL_Levels; i++)
   {
      if (ObjectFind("SL"+i) == -1)
      {
         ObjectCreate("SL"+i, OBJ_LABEL, 0, 0, 0);
         ObjectSet("SL"+i, OBJPROP_CORNER, 1);
         ObjectSet("SL"+i, OBJPROP_XDISTANCE, 3);
         ObjectSet("SL"+i, OBJPROP_YDISTANCE, k);
         k=k+15;    
      }
   }
   if (ObjectFind("Spread") == -1)
   {
      ObjectCreate("Spread", OBJ_LABEL, 0, 0, 0);
      ObjectSet("Spread", OBJPROP_CORNER, 3);
      ObjectSet("Spread", OBJPROP_XDISTANCE, 3);
      ObjectSet("Spread", OBJPROP_YDISTANCE, 1);
   }
   
//-- 4 / 5 Digit Settings --
   multi   = 1;
   exp1     = 0;
   modifySpread = 1;
   if (Digits==5 || Digits==3)
   {
      modifySpread = 0.1;
      multi=10;
      exp1=1;
   }
//----
   SL = 10;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   for(i=1; i<=SL_Levels; i++)
   {
      ObjectDelete("SL"+i);
   }
   ObjectDelete("Spread");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//----
   Spread     = MarketInfo(Symbol(), MODE_SPREAD)*modifySpread;
   MoneyRisk  = AccountBalance()*Risk*0.01;
   k=0;
   Print(Spread);
   Print(AccountBalance());
   Print(MoneyRisk);
   Print(MarketInfo(Symbol(), MODE_TICKVALUE));
   Print(SL);
   Print(multi);
   for(i=1; i<=SL_Levels; i++)
   {
      lots=MoneyRisk/SL/(MarketInfo(Symbol(), MODE_TICKVALUE)*multi);
      ObjectSetText("SL"+i, SL+ " : "+DoubleToStr(lots, 2), 10, "Calibri", White);
      k=k+15;
      SL = SL+5;     
   }
   SL = 10;
   ObjectSetText("Spread", Spread, 10, "Calibri", White);

   WindowRedraw();
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
lots=MoneyRisk/SL/(MarketInfo(Symbol(), MODE_TICKVALUE)*multi);
You don't know the basic of Math calculation orders? Straight away I know what is the problem. Your Math calculation orders.
 

Could you be a little more precise? What exactly is the problem with them (because it always worked..)?


I think there is still the problem that AccountBalance() and other data I need from MetaTrader are not available right after starting the Platform.

 

Another example:

#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//----
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
//----
  {
   Comment  (AccountBalance());
//----
   return(0);
  }
//+------------------------------------------------------------------+
If you open the MetaTrader, the result will be zero! And it stays zero until you change the timeframe or pair. I think this is exactly the problem of the division by zero error. Don't you also think?
 
mar:

I printed ALL variables of this line and I came to the conclusion that AccountBalance() is zero and MarketInfo(Symbol(), MODE_TICKVALUE) is zero. So obviously when you open the MetaTrader, no data at all is available. This never happened in the old MetaTrader. So I have no idea what to do...

Here is the code:

If you are sure that this is the cause of the problem, add this just after Start()

if( MarketInfo(Symbol(), MODE_SPREAD)==0 || AccountBalance() == 0 )
   return(0);
 

Hey GumRai,

I tested it with the indicator above. The AccountBalance stays zero even if I let the MetaTrader run for some minutes. Only until I changed something manually (pair, timeframe) the correct AccountBalance is displayed. And if I use your solution I have no problems with the division by zero error but the indicator will never do its work because AccountBalance stays zero.

I am wondering if I am the only person who has this problem? I think it is a general data-import issue of the new MetaTrader...

Reason: