//+------------------------------------------------------------------+
//|                                                   Sniper Lot.mq4 |
//|                                          Copyright 2014, Kolosov |
//|                                                   investlabfx.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, Kolosov"
#property link      "investlabfx.ru"
#property version   "1.00"
#property strict
#property indicator_chart_window

//+------------------------------------------------------------------+
//| Parameters                                                       |
//+------------------------------------------------------------------+
extern double  sl_r        =  10.0;          // Стоплосс разгонного ордера
extern int     magic       =  0;             // Мэгик (если нет, то 0)
extern int     show_type   =  0;             // Тип отображения
extern string  type_1      =  "";            // 0 - Вслед за ценой
extern string  type_2      =  "";            // 1 - Как коментарий
extern string  type_3      =  "";            // 2 - В выбранном углу экрана
extern int     corner      =  3;             // Угол привязки текста
extern int     coord_y     =  12;            // Координата Y
extern int     otstup      =  5;             // Отступ в барах
extern int     text_size   =  8;             // Размер шрифта
extern color   colortext   =  clrBlack;      // Цвет текста

string         name        =  "sniper_lot";
string         text        =  "";
double         n           =  1.0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   if (Digits == 3 || Digits == 5) n *= 10;
   
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   Comment("");
   ObjectDelete(name);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{  
   double tu = razgon();
   
   //if (n == 10) tu /= n;
   
   text = "Разгон " + DoubleToStr(tu,2);
   
   if (show_type == 0) {
      SetText(name, text, colortext, TimeCurrent(), SymbolInfoDouble(Symbol(),SYMBOL_BID), text_size);
   }
   if (show_type == 1) 
      Comment(text);
   if (show_type == 2)
      SetLabel(name, text, colortext, 3, coord_y, corner, text_size);
   
   return(rates_total);
}
//+------------------------------------------------------------------+

// Расчёт пунктов
double Pips()
{
   double temp = 0.0;
   
   for (int i = 0; i < OrdersTotal(); i++) {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol() == Symbol()) {
            if (OrderMagicNumber() == magic) {
               if (OrderType() <= 1) {
                  temp += (OrderProfit() / OrderLots() / MarketInfo( OrderSymbol(), MODE_TICKVALUE )) / n;
               }
            }
         }
      }
   }
   
   return (temp);
}
// Лот ордера
double Lot()
{
   double temp = 0.0;
   for (int i = 0; i < OrdersTotal(); i++) {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {
         if (OrderSymbol()      != Symbol()) continue;
         if (OrderMagicNumber() != magic)    continue;
         if (OrderType()         > 1)        continue;
         
         temp += OrderLots();   
      }
   } 
   return (temp);
}
// Лот разгона
double razgon()
{
   double lotb = 0.0;
   double lots = Lot();
   double tp = Pips() + sl_r;
   
   lotb = lots * tp / sl_r;
   
   if (lotb < lots) lotb = lots;
   
   int b = 0, s = 0;
   
   for (int i = OrdersTotal()-1; i >= 0; i--) {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {
         if (OrderSymbol() != Symbol() && OrderMagicNumber() != magic) continue;
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == magic) {
            if (OrderType() == OP_BUY)  b++;
            if (OrderType() == OP_SELL) s++;
         }
      }
   }
   
   if (b != 0 && s != 0) lotb = 0.0;
   
   return (lotb);
}
//+------------------------------------------------------------------+
//|  Параметры:                                                      |
//|    nm - наименование объекта                                     |
//|    tx - текст                                                    |
//|    cl - цвет метки                                               |
//|    xd - координата X в пикселах                                  |
//|    yd - координата Y в пикселах                                  |
//|    cr - номер угла привязки        (0 - левый верхний)           |
//|    fs - размер шрифта              (8 - по умолчанию)            |
//+------------------------------------------------------------------+
bool SetText(string nm, string tx, color cl, datetime time, double price, int fs) 
{
   time += otstup*Period()*60;
   //--- сбросим значение ошибки
   ResetLastError();
   //--- создадим объект "Текст"
   if (ObjectFind(nm) < 0) {
      ObjectCreate(0, nm, OBJ_TEXT, 0, time, price);
   } 
   
   ObjectMove(0, nm, 0, time, price);
     
   //--- установим текст
   ObjectSetString(0, nm, OBJPROP_TEXT, tx);
   
   //--- установим размер шрифта
   ObjectSetInteger(0, nm, OBJPROP_FONTSIZE, fs);
   
   //--- установим угол наклона текста
   ObjectSetDouble(0, nm, OBJPROP_ANGLE, 0.0);
   
   //--- установим способ привязки
   ObjectSetInteger(0, nm, OBJPROP_ANCHOR, ANCHOR_LEFT_LOWER);
   
   //--- установим цвет 
   ObjectSetInteger(0, nm, OBJPROP_COLOR, cl);
   
   //--- отобразим на переднем (false) или заднем (true) плане
   ObjectSetInteger(0, nm, OBJPROP_BACK, false);
   
   //--- включим (true) или отключим (false) режим перемещения объекта мышью
   ObjectSetInteger(0, nm, OBJPROP_SELECTABLE, false);
   ObjectSetInteger(0 ,nm, OBJPROP_SELECTED,   false);
   
   return (true);
}
//+------------------------------------------------------------------+
//|  Параметры:                                                      |
//|    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);
}
//+------------------------------------------------------------------+