Trading_time

Author: Copyright 2020, MetaQuotes Software Corp.
Price Data Components
Series array that contains close prices for each bar
Orders Execution
Checks for the total of open orders
0 Views
0 Downloads
0 Favorites
Trading_time
ÿþ//+------------------------------------------------------------------+

//|                                                 Trading_time.mq5 |

//|                        Copyright 2020, MetaQuotes Software Corp. |

//|                              https://www.mql5.com/ru/users/s22aa |

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

#property copyright "Copyright 2020, MetaQuotes Software Corp."

#property link      "https://www.mql5.com/ru/users/s22aa"

#property version   "1.00"



#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\OrderInfo.mqh>



CPositionInfo  m_position;

CTrade         m_trade;

COrderInfo     m_order;



input  int     OpenHour                = 00;          // '0A =0G0;0 B>@3>2;8

input  int     OpenMinute              = 00;          // 8=CB0 =0G0;0 B>@3>2;8

input  int     CloseHour               = 23;          // '0A 7025@H5=8O B>@3>2;8

input  int     CloseMinute             = 59;          // 8=CB0 7025@H5=8O B>@3>2;8

input string   text                    = "@5<O B>@3>2;8 70:>=G8;>AL!";



long currChart,prevChart;

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

int OnInit()

  {

   if(!ObjectCreate(0,"Timer",OBJ_LABEL,0,0,0))

     {

      Print(__FUNCTION__, ": =5 C40;>AL A>740BL B5:AB>2CN <5B:C! >4 >H81:8 = ",GetLastError());

      return(0);

     }

   ObjectSetInteger(0,"Timer",OBJPROP_XDISTANCE,10);

   ObjectSetInteger(0,"Timer",OBJPROP_YDISTANCE,15);

   ObjectSetInteger(0,"Timer",OBJPROP_COLOR,Blue);

   ObjectSetString(0,"Timer",OBJPROP_TEXT,"Trading_time"+": "+IntegerToString(CloseHour)+":"+IntegerToString(CloseMinute));

   ObjectSetInteger(0,"Timer",OBJPROP_FONTSIZE,10);

   return(INIT_SUCCEEDED);

  }

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

void OnDeinit(const int reason)

  {

   prevChart=ChartFirst();

   ObjectsDeleteAll(prevChart,"Text",0,OBJ_LABEL);

   ObjectsDeleteAll(currChart,"Timer",0,OBJ_LABEL);

   int i=0,limit=20;

   while(i<limit)

     {

      currChart=ChartNext(prevChart);

      if(currChart<0)

         break;

      ObjectsDeleteAll(currChart,"Text",0,OBJ_LABEL);

      prevChart=currChart;

     }

  }

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

void OnTick()

  {

   static datetime last_time=0, close_time=0;

   datetime cur_time=iTime(_Symbol,PERIOD_M1,0);



   if(cur_time!=last_time)

     {

      if(IsTimeAllow(OpenHour,OpenMinute,CloseHour,CloseMinute))

        {

         Open_Close(1);

         return;

        }

      Open_Close(0);

     }

   last_time=cur_time;

  }

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

bool IsTimeAllow(int openHour,int openMinute,int closeHour,int closeMinute)

  {

   MqlDateTime dt;

   TimeToStruct(TimeCurrent(),dt);

   double openTimeCode=StringToDouble("1"+IntegerToString(openHour,2,'0')+IntegerToString(openMinute,2,'0'));

   double closeTimeCode=StringToDouble("1"+IntegerToString(closeHour,2,'0')+IntegerToString(closeMinute,2,'0'));

   double currentTimeCode=StringToDouble("1"+IntegerToString(dt.hour,2,'0')+IntegerToString(dt.min,2,'0'));

   return(currentTimeCode>=openTimeCode && currentTimeCode<=closeTimeCode);

  }

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

void Open_Close(int Del)

  {

   prevChart=ChartFirst();

   if(Del)

     {

      ObjectDelete(prevChart,"Text");

     }

   else

     {

      ClosePos();

      DeleteAllOrders();

      Text(currChart);

      prevChart=currChart;

     }



   int i=0,limit=20;

   while(i<limit)

     {

      currChart=ChartNext(prevChart);

      if(currChart<0)

         break;



      if(Del)

        {

         ObjectDelete(currChart,"Text");

         prevChart=currChart;

        }

      else

        {

         ClosePos();

         DeleteAllOrders();

         Text(currChart);

         prevChart=currChart;

        }

     }

  }

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

void ClosePos()

  {

   for(int i=PositionsTotal()-1; i>=0; i--)

      if(m_position.SelectByIndex(i))

         m_trade.PositionClose(m_position.Ticket());

  }

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

void DeleteAllOrders(void)

  {

   for(int i=OrdersTotal()-1; i>=0; i--)

      if(m_order.SelectByIndex(i))

         m_trade.OrderDelete(m_order.Ticket());

  }

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

void Text(long id)

  {

   if(ObjectFind(id,"Text"))

     {

      ObjectDelete(id,"Text");

      if(!ObjectCreate(id,"Text",OBJ_LABEL,0,0,0))

        {

         Print(__FUNCTION__,

               ": =5 C40;>AL A>740BL B5:AB>2CN <5B:C! >4 >H81:8 = ",GetLastError());

         return;

        }

      ObjectSetInteger(id,"Text",OBJPROP_XDISTANCE,200);

      ObjectSetInteger(id,"Text",OBJPROP_YDISTANCE,250);

      ObjectSetInteger(id,"Text",OBJPROP_COLOR,Red);

      ObjectSetInteger(id,"Text",OBJPROP_BACK,false);

      ObjectSetInteger(id,"Text",OBJPROP_FONTSIZE,50);

      ObjectSetInteger(id,"Text",OBJPROP_SELECTABLE,true);

      ObjectSetString(id,"Text",OBJPROP_TEXT,text);

     }

  }

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

Comments