//+------------------------------------------------------------------+
//| Cкрипт выставляет рыночный ордер, в зависимости от того куда     |
//| кинуть, если выше цены то Byu ниже Sell и от него сразу он       |
//| выставляет сетку лимит ордеров.                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013, Хлыстов Владимир"
#property link      "cmillion@narod.ru"
#property show_inputs

//--------------------------------------------------------------------
extern int     Stoploss          = 1000,     //стоплосс ордеров
               Takeprofit        = 0;     //тейкпрофит ордеров
extern double  Lot               = 0.01;   //
extern int     Orders            = 1;     //кол-во лимитных ордеров
extern int     STEP              = 1500;     //шаг лимитных ордеров
extern int     Magic             = 0;     //уникальный номер ордеров этого советника
extern bool    comment           = False;  //выводить информацию на экран
//--------------------------------------------------------------------
int start()
{
   double SL,TP;
   double Price = NormalizeDouble(WindowPriceOnDropped(),Digits);
   string txt=StringConcatenate("Скрипт выставления Limit ордера ",DoubleToStr(Price,Digits)," старт ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));
   RefreshRates();
   int i;
   
   if(Price>Ask) 
   {
      Price = NormalizeDouble(Ask,Digits);
      if (Takeprofit!=0) TP = NormalizeDouble(Price + Takeprofit * Point,Digits); else TP=0;
      if (Stoploss!=0)   SL = NormalizeDouble(Price - Stoploss   * Point,Digits); else SL=0;
      if (OrderSend(Symbol(),OP_BUY,Lot,Price,0,SL,TP,"BUY",Magic,0,CLR_NONE )==-1)
           txt = StringConcatenate(txt,"\nВыставлен BUY ",DoubleToStr(Price,Digits));
      else txt = StringConcatenate(txt,"\nОшибка ",GetLastError()," выставления BUY");
      for (i = 0; i < Orders; i++)
      {
         Price = NormalizeDouble(Price-STEP*Point,Digits);
         if (Takeprofit!=0) TP = NormalizeDouble(Price + Takeprofit * Point,Digits); else TP=0;
         if (Stoploss!=0)   SL = NormalizeDouble(Price - Stoploss   * Point,Digits); else SL=0;
         if (OrderSend(Symbol(),OP_BUYLIMIT,Lot,Price,0,SL,TP,"LimitOrders",Magic,0,CLR_NONE )==-1)
              txt = StringConcatenate(txt,"\nВыставлен BUYLIMIT ",DoubleToStr(Price,Digits));
         else txt = StringConcatenate(txt,"\nОшибка ",GetLastError()," выставления BUYLIMIT ");
      }
   }
   else
   if(Price<Bid) 
   {
      Price = NormalizeDouble(Bid,Digits);
      if (Takeprofit!=0) TP = NormalizeDouble(Price - Takeprofit * Point,Digits); else TP=0;
      if (Stoploss!=0)   SL = NormalizeDouble(Price + Stoploss   * Point,Digits); else SL=0;
      if (OrderSend(Symbol(),OP_SELL,Lot,Price,0,SL,TP,"SELL",Magic,0,CLR_NONE)!=-1)
           txt = StringConcatenate(txt,"\nВыставлен SELL ",DoubleToStr(Price,Digits));
      else txt = StringConcatenate(txt,"\nОшибка ",GetLastError()," выставления SELL");
      for (i = 0; i < Orders; i++)
      {
         Price = NormalizeDouble(Price+STEP*Point,Digits);
         if (Takeprofit!=0) TP = NormalizeDouble(Price - Takeprofit * Point,Digits); else TP=0;
         if (Stoploss!=0)   SL = NormalizeDouble(Price + Stoploss   * Point,Digits); else SL=0;
         if (OrderSend(Symbol(),OP_SELLLIMIT,Lot,Price,0,SL,TP,"LimitOrders",Magic,0,CLR_NONE)!=-1)
              txt = StringConcatenate(txt,"\nВыставлен SELLLIMIT ",DoubleToStr(Price,Digits));
         else txt = StringConcatenate(txt,"\nОшибка ",GetLastError()," выставления SELLLIMIT ");
      }
   }
   if (comment) Comment(txt,"\nСкрипт закончил свою работу ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));
   return(0);
}
//--------------------------------------------------------------------

