need help for coding a time window !

 

hi people. im new to this and im looking for a way to make a specific time window to trade in EA.

for example 08:00 to 21:00

with which functions or method can i code it?

 
Birkimson Varzxorani:

hi people. im new to this and im looking for a way to make a specific time window to trade in EA.

for example 08:00 to 21:00

with which functions or method can i code it?

input bool TimeControl = true;                  // Filter time of traiding (true/false)
input string   TradingStart = "08:00";          // Start Trade
input string   TradingEnd = "22:00";            // End Trade 

int StartHours;
int StartMinutes;
int EndHours ;
int EndMinutes;
bool allowOpen;

void OnInit()
{
StartHours = (int) StringToInteger(StringSubstr(TradingStart,0,2));
StartMinutes = (int) StringToInteger(StringSubstr(TradingStart,3,2));
EndHours = (int) StringToInteger(StringSubstr(TradingEnd,0,2));
EndMinutes = (int) StringToInteger(StringSubstr(TradingEnd,3,2));
}

void OnTick()
{
if( ( TimeHour(TimeCurrent())>StartHours || ( TimeHour(TimeCurrent())==StartHours && TimeMinute(TimeCurrent())>=StartMinutes )) &&
 ( TimeHour(TimeCurrent())<EndHours || ( TimeHour(TimeCurrent())==EndHours && TimeMinute(TimeCurrent())<=EndMinutes )) )
{
allowOpen=true;
}
else
{
allowOpen=false;
}
if(!TimeControl)
{
allowOpen=true;
}

}

allowOpen can be included in any if where you want to check if trading hours are true 

I can not guarantee that this is the best solutions but it is a working one :)

 
Nikolay Georgiev:

allowOpen can be included in any if where you want to check if trading hours are true 

I can not guarantee that this is the best solutions but it is a working one :)

wooooow thanks... Nikolay do u hay any solution longer than this?  LooL
 
Birkimson Varzxorani:
wooooow thanks... Nikolay do u hay any solution longer than this?  LooL

Its a little longer due to conversion from string to int of hours and miutes. If you don't like it wait for another :)

 
Nikolay Georgiev:

Its a little longer due to conversion from string to int of hours and miutes. If you don't like it wait for another :)

im just kidding

there are some (int) in your code? the duty of that guys is what?

 
Birkimson Varzxorani:
there are some (int) in your code? the duty of that guys is what?

to convert type and make sure you get an int value

 
Nikolay Georgiev:

to convert type and make sure you get an int value

NIk TimeHour and TimeMinute are functions or what? im in trouble a little to understand that

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
Birkimson Varzxorani:

NIk TimeHour and TimeMinute are functions or what? im in trouble a little to understand that

Don't Use int function for  datetime  use  datetime

datetime   localtime;

// Buy Trade if substring equals "6:00"
   if((PositionsTotal() < 2) && (StringSubstr(hoursAndMinutes,0,5)>"06:00") && (StringSubstr(hoursAndMinutes,0,5) < "23:00"))
    {
      if(signal == "BUY" )
       {
      // Open a buy trade Positions 
      trade.Buy(0.10,NULL,Ask,BSL,BTP,NULL);
       }
      if(signal == "SELL" )
       {
      // Open a buy trade Positions
      trade.Sell(0.10,NULL,Bid,SSL,STP,NULL);
       }        
    } 

please try this and you will get result.

 
Birkimson Varzxorani:

hi people. im new to this and im looking for a way to make a specific time window to trade in EA.

for example 08:00 to 21:00

with which functions or method can i code it?

much easier if you use the time structures of MQL in a function. Returns true if time is ok to trade.

Change the hard coded numbers to variables and the reference time to that of your choice.

bool TimeToTrade()
{
   MqlDateTime dtTimeCurrent;
   TimeCurrent(dtTimeCurrent);
   if(dtTimeCurrent.hour >= 8 && dtTimeCurrent.hour < 21)
      return(true);
   return(false);
}
 
Paul Anscombe:

much easier if you use the time structures of MQL in a function. Returns true if time is ok to trade.

Change the hard coded numbers to variables and the reference time to that of your choice.

it was simple.thanks Paul  .what i needed. but why i cant use mqldatetime and time current in global level to use in other situations?
 
Birkimson Varzxorani:
it was simple.thanks Paul  .what i needed. but why i cant use mqldatetime and time current in global level to use in other situations?

yes you can do that if you wish, but if you are not going to use the variable or structure  elsewhere then it is better to declare inside the function as it makes it more self contained.

you can even pass the trading hours to the function if you like then it is completely independent

Reason: