CloseAllTime

Author: Vladimir Khlistov
Price Data Components
Series array that contains open time of each bar
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
0 Views
0 Downloads
0 Favorites
CloseAllTime
ÿþ//+------------------------------------------------------------------+

#property copyright "Vladimir Khlistov"

#property link      "http://cmillion.ru"

#property version "1.0"

#property strict

#property description "!>25B=8: 70:@K205B 2A5 A45;:8 8 >B;>65==K5 >@45@0 :064K9 45=L 2 7040==>95 2@5<O."

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

extern string time_close = "23:55";

int   attempts   = 10;    //:>;-2> ?>?KB>: 70:@KB8O

int   slippage    = 5;

int nn;

datetime timeclose=0;

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

void OnTick()

{  

   if (TimeCurrent()<StrToTime(time_close))

   {

      Comment("0:@KB85 G5@57 ",TimeToStr(StrToTime(time_close)-TimeCurrent(),TIME_SECONDS));

      return;

   }

   if (timeclose==0)

   {

      timeclose=StrToTime(time_close);

      return;

   }

   else

   {

      if (TimeCurrent()<timeclose)

      {

         Comment("!53>4=O C65 1K;> 70:@KB85 2 ",TimeToStr(StrToTime(time_close),TIME_SECONDS));

         return;

      }

   }

   if (Digits==5 || Digits==3) slippage=30;

   bool error=true;

   int j,DIGITS,OT,Ticket;

   double ASK,BID;

   Comment("Start ",TimeToString(timeclose));

   while(true)

   {

      for (j = OrdersTotal()-1; j >= 0; j--)

      {

         if (OrderSelect(j, SELECT_BY_POS))

         {

            DIGITS = MarketInfo(OrderSymbol(),MODE_DIGITS);

            ASK = NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),DIGITS);

            BID = NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID),DIGITS);

            OT = OrderType();

            Ticket=OrderTicket();

            if (OrderOpenTime()>timeclose) continue;

            if (OT==OP_BUY) 

            {

               error=OrderClose(Ticket,OrderLots(),BID,slippage,Red);

               if (error) Comment("0:@KB >@45@ N ",Ticket,"  ?@81K;L ",OrderProfit(),

                                  "     ",TimeToStr(TimeCurrent(),TIME_SECONDS));

            }

            if (OT==OP_SELL) 

            {

               error=OrderClose(Ticket,OrderLots(),ASK,slippage,Red);

               if (error) Comment("0:@KB >@45@ N ",Ticket,"  ?@81K;L ",OrderProfit(),

                                  "     ",TimeToStr(TimeCurrent(),TIME_SECONDS));

            }

            if (OT>1) 

               if (OrderDelete(Ticket))

                  Comment("@45@ ",Ticket," CA?5H=> C40;5= ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));

               else

                  Comment("@45@ ",Ticket," error C40;5=8O ",GetLastError());

            

            if (!error) 

            {

               error = GetLastError();

               if (error<2) continue;

               if (error==129) 

               {  Comment("5?@028;L=0O F5=0 ",TimeToStr(TimeCurrent(),TIME_SECONDS));

                  Sleep(5000);

                  RefreshRates();

                  continue;

               }

               if (error==146) 

               {

                  j++;

                  if (IsTradeContextBusy()) Sleep(2000);

                  continue;

               }

               Comment("error ",error," 70:@KB8O >@45@0 N ",Ticket,"     ",TimeToStr(TimeCurrent(),TIME_SECONDS));

            }

         }

      }

      int n=0;

      for (j = OrdersTotal()-1; j >= 0; j--)

      {

         if (OrderSelect(j, SELECT_BY_POS))

         {

            if (OrderOpenTime()<timeclose) n++;

         }

      }

      if (n==0) 

      {

         timeclose=StrToTime(time_close)+1440*60;

         break;

      }

      nn++;

      if (nn>attempts) {Comment("5 C40;>AL 70:@KBL 2A5 A45;:8, >AB0;>AL 5I5 ",n);break;}

      Sleep(1000);

      RefreshRates();

   }

}

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



Comments