//+------------------------------------------------------------------+
//|                                             LotCalculator v1.mq4 |
//|                                                    Skyline, 2008 |
//|                                      http://www.forexpertutti.it |
//+------------------------------------------------------------------+
#property copyright "Skyline, 2008"
#property link      "http://www.forexpertutti.it"
#property indicator_separate_window

//---- input parameters
extern double  Risk       = 1.5;
extern double  StopLoss   = 20;
extern color   LabelColor = Black;

string IndicatorName = "LotCalculator";

//int nDigits;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   IndicatorShortName(IndicatorName);

   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   Comment("");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   //----
        double Lot = normalize_volume(AccountEquity()*Risk/10000/StopLoss/MarketInfo(Symbol(), MODE_TICKVALUE));
        
        // Risk label  
        ObjectCreate("LotCalc1", OBJ_LABEL, WindowFind(IndicatorName), 0, 0);
        ObjectSetText("LotCalc1","RISK : ", 16, "Century Gothic", LabelColor);
        ObjectSet("LotCalc1", OBJPROP_CORNER, 0);
        ObjectSet("LotCalc1", OBJPROP_XDISTANCE, 100);
        ObjectSet("LotCalc1", OBJPROP_YDISTANCE, 1);
        
        ObjectCreate("LotCalc2", OBJ_LABEL, WindowFind(IndicatorName), 0, 0);
        ObjectSetText("LotCalc2",DoubleToStr(Risk,1)+" %",16, "Century Gothic Bold ", Blue);
        ObjectSet("LotCalc2", OBJPROP_CORNER, 0);
        ObjectSet("LotCalc2", OBJPROP_XDISTANCE, 180);
        ObjectSet("LotCalc2", OBJPROP_YDISTANCE, 1);
        
        // StopLoss label
        ObjectCreate("LotCalc3", OBJ_LABEL, WindowFind(IndicatorName), 0, 0);
        ObjectSetText("LotCalc3","STOPLOSS : ", 16, "Century Gothic ", LabelColor);
        ObjectSet("LotCalc3", OBJPROP_CORNER, 0);
        ObjectSet("LotCalc3", OBJPROP_XDISTANCE, 300);
        ObjectSet("LotCalc3", OBJPROP_YDISTANCE, 1);
        
        ObjectCreate("LotCalc4", OBJ_LABEL, WindowFind(IndicatorName), 0, 0);
        ObjectSetText("LotCalc4",DoubleToStr(StopLoss,0),16, "Century Gothic Bold", Red);
        ObjectSet("LotCalc4", OBJPROP_CORNER, 0);
        ObjectSet("LotCalc4", OBJPROP_XDISTANCE, 450);
        ObjectSet("LotCalc4", OBJPROP_YDISTANCE, 1);        
        
        // Lot label
        ObjectCreate("LotCalc5", OBJ_LABEL, WindowFind(IndicatorName), 0, 0);
        ObjectSetText("LotCalc5","LOT : ", 15, "Century Gothic", LabelColor);
        ObjectSet("LotCalc5", OBJPROP_CORNER, 0);
        ObjectSet("LotCalc5", OBJPROP_XDISTANCE, 550);
        ObjectSet("LotCalc5", OBJPROP_YDISTANCE, 1);
        
        ObjectCreate("LotCalc6", OBJ_LABEL, WindowFind(IndicatorName), 0, 0);
        ObjectSetText("LotCalc6",DoubleToStr(Lot,2),16, "Century Gothic Bold", Green );
        ObjectSet("LotCalc6", OBJPROP_CORNER, 0);
        ObjectSet("LotCalc6", OBJPROP_XDISTANCE, 650);
        ObjectSet("LotCalc6", OBJPROP_YDISTANCE, 1);

   return(0);
  }
  
double normalize_volume(double lots)
{
  // Adjust trade volume to broker
  double step   = MarketInfo(Symbol(), MODE_LOTSTEP);
  double min    = MarketInfo(Symbol(), MODE_MINLOT);
  double max    = MarketInfo(Symbol(), MODE_MAXLOT);
  if(max > 0)
    lots = MathMin(lots, max);
  if(step > 0)
    double result = MathFloor(lots/step)*step;
  else
    result = 0;
  if(result < min)
    if(min>0)
      return(min);
    else
      return(lots);
  else if(result > 0)
    return(result);
  else
    return(lots);
}  

//+------------------------------------------------------------------+