Small Moneymanagement-Indicator ruined by new MQL4

 

Hey coders,

I made a small Moneymanagement indicator which worked fine for me. But since the new MQL4 it causes problems I can't handle.

When you drag this indicator to your chart some numbers are printed on the very right side. The first number means the Stoploss in pips and the second is the lot-size. In the options you can choose the risk. The other number are the Stoploss Levels. But when I change the currency pair the numbers are gone. Instead I see several times the word "Label". And when I look to the indicator-list, my indicator is also gone. So, in summary, after changing the currency pair, the indicator is gone and left Labels instead of numbers.

I am totallly confused. This indicator used to work without any problem. Help is very appreciated.

Here is the code:

#property indicator_chart_window
//--- input parameters
extern int  Risk=2;
extern int SL_Levels=20;
int multi, exp, 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;
   exp     = 0;
   if (Digits==5 || Digits==3)
   {
      _modifySpread = 0.1;
      multi=10;
      exp=1;
   }
//----
   SL = MathFloor((iATR(NULL, PERIOD_D1, 7, 0)*MathPow(10,Digits-exp)+1)*0.1);
//----
   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;
   for(i=1; i<=SL_Levels; i++)
   {
      lots=MoneyRisk/SL/(MarketInfo(Symbol(), MODE_TICKVALUE)*multi);
      ObjectSetText("SL"+i, SL+ " : "+DoubleToStr(lots, 2), 10, "Calibri", Red);
      k=k+15;
      SL = SL+MathFloor(((iATR(NULL, PERIOD_D1, 7, 0)*MathPow(10,Digits-exp)+1)*0.1)/2);     
   }
   SL = MathFloor((iATR(NULL, PERIOD_D1, 7, 0)*MathPow(10,Digits-exp)+1)*0.1);
   ObjectSetText("Spread", DoubleToStr(Spread,1), 10, "Calibri", Red);
   WindowRedraw();
//----
   return(0);
  }
//+------------------------------------------------------------------+
 

first you cant use "exp" for an int it's reserved

second you express the "SL"

SL = MathFloor((iATR(NULL, PERIOD_D1, 7, 0)*MathPow(10,Digits-exp)+1)*0.1);

in the init section, bad idea

because if you get Error 4066 the "SL" = 0

then you get zero divide on "lots"

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

and the indicator stops and you have to reload the indicator

 
I understand. Just changed the variable exp but can you tell me how to check or avoid error 4066? I read that it has to do with the fact that MetaTrader updates only the quotes of the current chart and timeframe. But what would you suggest to do to avoid 4066?
 

obviously you can't avoid the update ;-) but you have to avoid a situation of "zero divide"

so you have to check if one of the variables equals to 0.... in case avoid zero divide

and because you can not stop an indicator you have to figure out something

example:

SL = MathFloor((iATR(NULL, PERIOD_D1, 7, 0)*MathPow(10,Digits-exp)+1)*0.1);
if (SL == 0) // because error 4066 (for example)
   {
   SL = Someting;
   }
lots=MoneyRisk/SL/(MarketInfo(Symbol(), MODE_TICKVALUE)*multi);
 

You are right.. I can't avoid it. :-)

What I meant is, can I wait until the update is completed before going on with the SL-calculation? The problem is that I can't give SL a random value because then the whole indicator is useless. So I would like to check for error 4066 and if it occurs, I would like to wait somehow and check again before going on.

 

By the way, I have the same problem with this indicator. But why wasn't it a problem in the "old" MetaTrader? Here I always receive a zero for the ATR(5). WHY?!?! That never happened before.. I hate this new MetaTrader...!

int start()
//----
  {
   double Spread = MarketInfo(Symbol(), MODE_SPREAD);
   double ATR = iATR(NULL, PERIOD_D1, 5, 0);
   int exp1 = 0;
   if (Digits==5 || Digits==3)
   {
      Spread=Spread*0.1;
      exp1 = 1;
   }
   Comment  (
            "ADR(5): ",DoubleToStr(ATR*MathPow(10,Digits-exp1),1),
            " || TODAY: ",(iHigh(NULL, PERIOD_D1, 0)-iLow(NULL, PERIOD_D1, 0))*MathPow(10,Digits-exp1),
            " || ", DoubleToStr(((iHigh(NULL, PERIOD_D1, 0)-iLow(NULL, PERIOD_D1, 0))*MathPow(10,Digits-exp1)/(ATR*MathPow(10,Digits-exp1))*100), 2), "%",
            "\n" +
            "SPREAD: ", Spread,
            " || RATIO: ",DoubleToStr((Spread/ATR)*MathPow(10,-(Digits-exp1-2)), 2),"%"
            );
//----
   return(0);
  }
 
What do you mean zero? I print all of the comment and ATR(5) shows output bigger than zero ... above 50 in fact ...
 

I think I found the problem! It is definitely an updating problem. When I now change currencies, the indicator works. But then I opened a currency-pair I had not open yet today. Then I got this message:


Open a pair you have not yet opened today. Switch to whatever timeframe you like, except D1. Then you drag the indicator to your chart. The result will be an error like above. It seems to me that the ATR(5) of the daily chart is not correct because of missing DAILY DATA. Opening a "fresh" chart in the D1 timeframe causes no problems at all. And when you switch to another timeframe then it is also fine because the ATR(5) is correct. I noticed that I can switch to any pair as long as I stay in the D1 chart. But switching to other pairs in other timeframes than D1 causes trouble as long as MetaTrader has not received the daily data. So, my question is now, how can I get the daily data that my ATR-calculation is correct when I open a new chart in a timeframe which is not D1?
 

You have to put something like

if(number==0)dothis; else dothis; 

in ShowSpread.mq4


Just search the line where this zero divide occurs.

 
mar:

I think I found the problem! It is definitely an updating problem. When I now change currencies, the indicator works. But then I opened a currency-pair I had not open yet today. Then I got this message:


Open a pair you have not yet opened today. Switch to whatever timeframe you like, except D1. Then you drag the indicator to your chart. The result will be an error like above. It seems to me that the ATR(5) of the daily chart is not correct because of missing DAILY DATA. Opening a "fresh" chart in the D1 timeframe causes no problems at all. And when you switch to another timeframe then it is also fine because the ATR(5) is correct. I noticed that I can switch to any pair as long as I stay in the D1 chart. But switching to other pairs in other timeframes than D1 causes trouble as long as MetaTrader has not received the daily data. So, my question is now, how can I get the daily data that my ATR-calculation is correct when I open a new chart in a timeframe which is not D1?
qjol already said you where the problem is. Why don't you follow or at least check what people advised you if you are requesting help ?
 

angevoyageur, I think you did not see this posting from me:

mar 2014.04.02 08:16 #

You are right.. I can't avoid it. :-)

"What I meant is, can I wait until the update is completed before going on with the SL-calculation? The problem is that I can't give SL a random value because then the whole indicator is useless. So I would like to check for error 4066 and if it occurs, I would like to wait somehow and check again before going on."

There you can see that described that I can't follow the advice and because I came to that solution you can also see that I definitely checked what qjol advised me.