//+------------------------------------------------------------------+
//|                                                        # Лот.mq4 |
//|                                     Copyright 2013, Kolosov D.E. |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, Kolosov D.E."

#property indicator_chart_window

extern double risk        =  1;        // Процент риска на сделку
extern int    min_stop    =  100;      // Старые пункты
extern int    min_take    =  100;      // Старые пункты
extern color  colortext   =  Black;
extern int    corner      =  3;
extern int    coord_y     =  12;
extern int    text_size   =  8;

string        name1       =  "lot";
string        name2       =  "loss";
string        name3       =  "prof";
string        name4       =  "lots";
double        n           =  1.0;

int init() 
{
   if (Digits == 3 || Digits == 5) { 
      n = n*10;
      min_stop = min_stop*10;
      min_take = min_take*10;
   }
}

int deinit()
{
   Comment("");
   ObjectDelete(name1);
   ObjectDelete(name2);
   ObjectDelete(name3);
   ObjectDelete(name4);
   return(0);
}

int start()
{
   double Free    = AccountFreeMargin();
   double LotVal  = MarketInfo(Symbol(),MODE_TICKVALUE)/(MarketInfo(Symbol(),MODE_TICKSIZE)/MarketInfo(Symbol(),MODE_POINT)); //стоимость 1 пункта для 1 лота
   double Min_Lot = MarketInfo(Symbol(),MODE_MINLOT);
   double Max_Lot = MarketInfo(Symbol(),MODE_MAXLOT);
   double Step    = MarketInfo(Symbol(),MODE_LOTSTEP);
   double Lot     = MathFloor((Free*risk/100)/(min_stop*LotVal)/Step)*Step;
   if (Lot < Min_Lot) Lot = Min_Lot;
   if (Lot > Max_Lot) Lot = Max_Lot;
   Lot = NormalizeDouble(Lot,2);
      
   string text1, text2, text3, text4, temp;
   double loss, prof, point_price;
   
   text1 = "Лот " + DoubleToStr(Lot,2) + " (при риске " + DoubleToStr(risk,1) + "% и SL = " + min_stop + ")";
   
   loss  = Lot*LotVal*min_stop;
   text2 = "Возможный убыток: " + DoubleToStr(loss,2) + " " + AccountCurrency();
   
   prof  = Lot*LotVal*min_take;
   text3 = "Возможный профит: " + DoubleToStr(prof,2)  + " " + AccountCurrency() + " при уровне ТП: " + min_take + " пунктов";
   
   point_price = Lot*LotVal*n;
   text4 = "Стоимость одного пункта при лоте " + DoubleToStr(Lot,2) + ": " + DoubleToStr(point_price,2) + " " + AccountCurrency();
   
   if (corner == 2 || corner == 3) { temp = text4; text4 = text1; text1 = temp; temp = text3; text3 = text2; text2 = temp; }
   
   SetLabel(name1, text1, colortext, 3, coord_y,      corner, text_size);
   SetLabel(name2, text2, colortext, 3, coord_y + 15, corner, text_size);
   SetLabel(name3, text3, colortext, 3, coord_y + 30, corner, text_size);
   SetLabel(name4, text4, colortext, 3, coord_y + 45, corner, text_size);
   return(0);
}

//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    nm - наименование объекта                                               |
//|    tx - текст                                                              |
//|    cl - цвет метки                                                         |
//|    xd - координата X в пикселах                                            |
//|    yd - координата Y в пикселах                                            |
//|    cr - номер угла привязки        (0 - левый верхний)                     |
//|    fs - размер шрифта              (8 - по умолчанию)                      |
//+----------------------------------------------------------------------------+
void SetLabel(string nm, string tx, color cl, int xd, int yd, int cr, int fs) {
  if (ObjectFind(nm) < 0) ObjectCreate(nm, OBJ_LABEL, 0, 0,0);
  ObjectSetText(nm, tx, fs);
  ObjectSet(nm, OBJPROP_COLOR    , cl);
  ObjectSet(nm, OBJPROP_XDISTANCE, xd);
  ObjectSet(nm, OBJPROP_YDISTANCE, yd);
  ObjectSet(nm, OBJPROP_CORNER   , cr);
  ObjectSet(nm, OBJPROP_FONTSIZE , fs);
}
//+----------------------------------------------------------------------------+