Download MetaTrader 5
To add comments, please log in or register
Thousands of developers have already published their products in the Market. Join them!
Tim Smith
13
Tim Smith 2015.09.09 22:03 

I have this simple indicator which just adds two objects to the chart to show the spread and margin for currency pair.

It is all fine when first added in (i.e. objects display correctly and are removed when indicator is removed), but when MT4 is restarted two things happen:

1) only one of the objects remains displayed correctly (spread) whilst the other (margin) now appears as grey text just saying "label".

2) also after restart, the two objects seem to have become orphaned, so removing the indicator does not remove the objects.

Any pointers for a novice to help me spot what I'm doing wrong would be greatly appreciated.

Thanks

Tim. 

#property indicator_chart_window

   double myPoint, Spr, tickSize, tickValue, marginRequired, lotValue, Lev;
   int   DisplaySize = 8, Offset_x = 4, Offset_y = 35, Offset_Lev = 20;
   color DisplayColor = Black;
   string Spread="Spread = ";
   string Leverage="Margin % = ";

int init()
   {
      ObjectCreate(Spread,OBJ_LABEL,0,0,0,0,0);
      ObjectSet(Spread, OBJPROP_CORNER, 2);
      ObjectSet(Spread,OBJPROP_XDISTANCE,Offset_x);
      ObjectSet(Spread,OBJPROP_YDISTANCE,Offset_y);
      
      ObjectCreate(Leverage,OBJ_LABEL,0,0,0,0,0);
      ObjectSet(Leverage, OBJPROP_CORNER, 2);
      ObjectSet(Leverage,OBJPROP_XDISTANCE,Offset_x);
      ObjectSet(Leverage,OBJPROP_YDISTANCE,Offset_Lev);
   return(0);
   }

int start()
   {
      if(Digits==5||Digits==3) {myPoint=Point*10;} else {myPoint=Point;}
      Spr=(Ask-Bid)/myPoint;
      ObjectSetText(Spread,"Spread = "+DoubleToStr(Spr,1),DisplaySize, "Arial", DisplayColor);
      
      tickSize        = MarketInfo(Symbol(), MODE_TICKSIZE      );
      tickValue       = MarketInfo(Symbol(), MODE_TICKVALUE     );
      marginRequired  = MarketInfo(Symbol(), MODE_MARGINREQUIRED);
      lotValue        = Close[0]/tickSize * tickValue;
      Lev             = 100/(lotValue/marginRequired);
      ObjectSetText(Leverage,"Margin % = "+DoubleToStr(MathRound(Lev),1)+"%",DisplaySize, "Arial", DisplayColor);     
   return(0);
   }

int deinit()
   {
      ObjectDelete(Spread);
      ObjectDelete(Leverage);
   return(0);
   }
Tim Smith
13
Tim Smith 2015.09.10 22:27  

 Think I've found my answer in the Experts log........

 

  Problem must be in one of these calcs, just got to work out why it calcs fine when adding to a live chart but not on restart of MT4......

lotValue        = Close[0]/tickSize * tickValue;
Lev             = 100/(lotValue/marginRequired);
whroeder1
13630
whroeder1 2015.09.10 22:45  
  1. Check your variables before deviding.
  2. Close[0]/tickSize * tickValue
    is meaningless. There is no value per lot. A change in price * (tickValue / TickSize) is the change in equity per lot.
    1. You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    2. Account Balance * percent = RISK = (OrderOpenPrice - OrderStopLoss)*DIR * OrderLots * DeltaPerlot (Note OOP-OSL includes the SPREAD)
    3. Do NOT use TickValue by itself - DeltaPerlot
    4. You must normalize lots properly and check against min and max.
    5. You must also check FreeMargin to avoid stop out
Tim Smith
13
Tim Smith 2015.09.10 23:06  

Ok, so testing shows marginRequired variable is the problem, therefore this is returning a zero value on start: 

marginRequired  = MarketInfo(Symbol(), MODE_MARGINREQUIRED);

 It seems that for a few seconds on start up it is zero so indicator fails and then won't update itself to correct. Is there a way to fix this - could the initialising of indicator be delayed for a few seconds, or until an event such as first tick refresh of chart?

Tim Smith
13
Tim Smith 2015.09.11 00:00  
Simple if statement to test variables are > 0 did the trick - all working as it should now. Thanks
/
To add comments, please log in or register