//+------------------------------------------------------------------+
//|                                              Money Managment.mq4 |
//|                                   Copyright © 2010, Cisco Press. |
//|                                        error.detection@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Cisco Press."
#property link      "error.detection@gmail.com"

#property indicator_chart_window
#property indicator_buffers 2
extern color indicator_clr1= Gold;
extern color indicator_clr2= Aqua;

bool MM = TRUE;
extern int Risk     = 10,
           StopLoss = 25;
extern int Order.No=1;
extern int Position =1;
double MinLot = 0.1;
int g_leverage_316;
double gd_200;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectDelete("lot");
   ObjectDelete("lot1");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
  DisplayText("Lot","Allow Lots Per "+Order.No +" trade with Risk "+DoubleToStr(Risk,0)+"% :","Arial",12,indicator_clr1,50,10,Position);
  DisplayText("Lot1",DoubleToStr(Lots(),2),"Arial",12,indicator_clr2,10,10,Position);
//----
   return(0);
  }
//+------------------------------------------------------------------+
double Lots() {
   int li_0;
   int spread = MarketInfo(Symbol(),MODE_SPREAD);
   int sl = StopLoss;
   double ld_4 = NormalizeDouble(MarketInfo(Symbol(), MODE_LOTSTEP), 2);
   double tickvalue = MarketInfo(Symbol(), MODE_TICKVALUE);
   if (ld_4 == 0.01) li_0 = 2;
   else li_0 = 1;
   g_leverage_316 = AccountLeverage();   
   if (MM) gd_200 = NormalizeDouble(AccountFreeMargin()*Risk/100/(1000/g_leverage_316)/(sl*tickvalue), li_0);
   else gd_200 = MinLot;
   double ld_12 = NormalizeDouble(MarketInfo(Symbol(), MODE_MINLOT), 2);
   double ld_20 = NormalizeDouble(MarketInfo(Symbol(), MODE_MAXLOT), 2);
   if (gd_200 < ld_12) gd_200 = ld_12;
   if (gd_200 > ld_20) gd_200 = ld_20;
   return (gd_200);
}
//+------------------------------------------------------------------+
void DisplayText(string objname, string objtext, string fontname, int fontsize, int clr, int x, int y,int Cor)
   {
      ObjectCreate(objname,OBJ_LABEL,0,0,0);
      ObjectSetText(objname,objtext,fontsize,fontname,clr);
      ObjectSet(objname,OBJPROP_CORNER,Cor);
      ObjectSet(objname,OBJPROP_XDISTANCE,x);
      ObjectSet(objname,OBJPROP_YDISTANCE,y);
   }
//+------------------------------------------------------------------+   