Setting Trading Times - page 2

 
JimboDiggity #: As mentioned i used your code but will repost..
Where did you put the functions, that I provided a link to TWICE? (In #2 and #5).
 
William Roeder #:
Where did you put the functions, that I provided a link to TWICE? (In #2 and #5).

You could post the links another hundred times but yet i think its painfully obvious i'd be no nearer to understanding how to solve the issue. As far as i can tell you've posted numerous functions on those links and i have no clear idea which relate to my project, and better still all the ones I've tried seem to return a whole bunch more errors, which is why i modified my code in the first place. 

Clearly, i'm very new to coding in MQL5, and clearly i have a long way to go. If you don't want to help then its completely understood, but circle jerking me with links (that i had already mentioned i didn't understand in my FIRST reply to you), helps nobody.

 
JimboDiggity #:

Hi Lorentzos, thanks for your detailed explanation. Reading it however makes me think i'm perhaps looking at the back testing data incorrectly..

To be fully clear on my issue; i am trying to back test my strategy, and it needs to trade within the window 9:30am-9:45am UTC-5. I am currently in UTC+4, and my broker provides data UTC+2.

I guess the question is, when running a back test where does the data come from? Is it provided by MetaTrader in local times to the market/symbol, or does it come from my broker? I.e. to run a back test on the US30.Cash will i need to adjust the code you provided to local UTC-5 times (i.e. its provided from my broker and therefore DST will also need to be adjusted for), or can i code it as you describe and it will understand it is for the UTC-5 timezone because that's the timezone of the market (US30)?

A bit wordy but hopefully that's clear..?

Hmm yeah i see .

So you will need to adjust your trading window for the DST . I imagine a timezone shift changes the "range" of an event , if i'm not mistaken.

The Tester holds the broker time and you can turn that into your time with an offset (and set the offset to 0 and use local time when running the program live)

The DST though remains .

There could be a solution if it can return the gmt time then (in the test) which means with a couple of tweaks on the trading window

you would be set , although very vigilant to test right times and operate on right times. 

 
Lorentzos Roussos #:

Hmm yeah i see .

So you will need to adjust your trading window for the DST . I imagine a timezone shift changes the "range" of an event , if i'm not mistaken.

The Tester holds the broker time and you can turn that into your time with an offset (and set the offset to 0 and use local time when running the program live)

The DST though remains .

There could be a solution if it can return the gmt time then (in the test) which means with a couple of tweaks on the trading window

you would be set , although very vigilant to test right times and operate on right times. 

Urgh well that's a massive pain. I'd actually already cracked the trading window issue with my original code (adjusted from William's suggestion, and by cracked i mean for if i was in the local time zone - its useless outside of it :/), although i have little doubt that your's is better than anything i've come up with thus far. I think my only option is to continue back testing in prorealcode (which handles all time zones issues for you) and just use MQL5 for execution.

On that note, would you happen to see any obvious flaws in my code (the long "full" code i posted) that would prevent it from executing any orders? Even if the time zone issue is still  a mess, i would have thought it would do some trades in a back test (even though the results would be meaningless!) yet it doesn't do a single one, regardless of the time period i test it over.

TIA

 
JimboDiggity #:

Urgh well that's a massive pain. I'd actually already cracked the trading window issue with my original code (adjusted from William's suggestion), although i have little doubt that your's is better than anything i've come up with thus far. I think my only option is to continue back testing in prorealcode (which handles all time zones issues for you) and just use MQL5 for execution.

On that note, would you happen to see any obvious flaws in my code (the long "full" code i posted) that would prevent it from executing any orders? Even if the time zone issue is still  a mess, i would have thought it would do some trades in a back test (even though the results would be meaningless!) yet it doesn't do a single one, regardless of the time period i test it over.

TIA

It can be done actually this way 

  • If you know the time zone of your broker
  • Find the time shift days of that time zone (and kinda do the same thing but with day of the year instead of seconds)
  • And define the "trading window" in gmt hours and minutes then it would work .
  • (and then you would keep the times in gmt for live operation or shift to local time)

But yeah it would be really handy if the rates had a time_gmt component that is true.

Edit : 

Looking at your code i set the Lot to 0.01 and the symbol to EURUSD for a test 

Make sure you are running on a timeframe that can "render" or "enter" your time window , i tested on M1

And additionally the time check is not working 

replace it with this : (after you include the new function in the code)

bool Buy_Condition_1 = isThisTimeInMyTimeWindow(TimeCurrent(),9,30,9,45); //checks trading time

Set the example prints to false because they are annoying too . 

Enjoy 

😊


 
Lorentzos Roussos #:

It can be done actually this way 

  • If you know the time zone of your broker
  • Find the time shift days of that time zone (and kinda do the same thing but with day of the year instead of seconds)
  • And define the "trading window" in gmt hours and minutes then it would work .
  • (and then you would keep the times in gmt for live operation or shift to local time)

But yeah it would be really handy if the rates had a time_gmt component that is true.

By time shift days are you referring to the days when the clocks go back/forward? If so then i think that's where i tap-out. I tend to back test on many different time periods, use walk forward analysis etc and so if i have to faff about coding in each DST change it's just going to be more hassle than i have the motivation or, frankly, the technical no-how to do. 

It seems like its a rather gaping floor in the software however to not provide an easier method to adjust for this, assuming i'm not the only person in the world trying to back test data from a different time zone of course. Being new to it i don't know but am assuming its primarily used for execution as opposed to back testing?

 
Lorentzos Roussos #:

It can be done actually this way 

  • If you know the time zone of your broker
  • Find the time shift days of that time zone (and kinda do the same thing but with day of the year instead of seconds)
  • And define the "trading window" in gmt hours and minutes then it would work .
  • (and then you would keep the times in gmt for live operation or shift to local time)

But yeah it would be really handy if the rates had a time_gmt component that is true.

Edit : 

Looking at your code i set the Lot to 0.01 and the symbol to EURUSD for a test 

Make sure you are running on a timeframe that can "render" or "enter" your time window , i tested on M1

And additionally the time check is not working 

replace it with this : (after you include the new function in the code)

Set the example prints to false because they are annoying too . 

Enjoy 

😊


ah great i'll give it a go! don't expect a hasty response however, i'll likely be hacking around at it until tomorrow some time (i wish i were joking)
 
JimboDiggity #:

By time shift days are you referring to the days when the clocks go back/forward? If so then i think that's where i tap-out. I tend to back test on many different time periods, use walk forward analysis etc and so if i have to faff about coding in each DST change it's just going to be more hassle than i have the motivation or, frankly, the technical no-how to do. 

It seems like its a rather gaping floor in the software however to not provide an easier method to adjust for this, assuming i'm not the only person in the world trying to back test data from a different time zone of course. Being new to it i don't know but am assuming its primarily used for execution as opposed to back testing?

Yeah , all it had to have was keep a gmt copy of time which maybe it does and a more experienced coder will mention it , but so far i have not seen it

 
JimboDiggity #:
ah great i'll give it a go! don't expect a hasty response however, i'll likely be hacking around at it until tomorrow some time (i wish i were joking)

Great . Good evening 

 
Lorentzos Roussos #:

Great . Good evening 

Hi Lorentzos, finally got around to trying out your code and low and behold i again can't seem to get it to work again.. To be completely clear the exact code i am trying is below, and i am testing on EURUSD with 0.1 lot size and M1 bars as suggested. 


Would you mind trying the code yourself and seeing if it produces trades? At this point i'm starting to think there is a configuration setting wrong in my MT5 as i just cant see how it would not work.. *weep*

input char   Lot=0.1;

double p_close;
int STP, TKP;

double CheckHigh;
double CheckLow;
double Distance;

string symbol="EURUSD";

#include <Trade/Trade.mqh>
CTrade trade;

//Trading times

bool isThisTimeInMyTimeWindow(datetime this_time,//the first thing you need which time is checked 
                                   int from_hours,//we are keeping it simple here 24 h format tho
                                   int from_minutes,//
                                   int to_hours,//hour to , 24 h format again
                                   int to_minutes){
  bool example_prints=true;
  if(example_prints)
    {
    Print("------");
    Print("Received Time "+TimeToString(this_time,TIME_DATE|TIME_MINUTES|TIME_SECONDS));
    Print("---to be tested from ("+IntegerToString(from_hours)+":"+IntegerToString(from_minutes)+":00)");
    Print("------------------to ("+IntegerToString(to_hours)+":"+IntegerToString(to_minutes)+":00)");
    }
  
  int days_in_time=(int)MathFloor(((double)this_time)/86400.0);     
  if(example_prints){Print("Days in time : "+IntegerToString(days_in_time));}              

  int seconds_in_this_time=((int)this_time)-(days_in_time*86400);
  if(example_prints){Print("Left seconds in this time : "+IntegerToString(seconds_in_this_time));}
  int seconds_from=from_hours*3600+from_minutes*60;
  int seconds_to=to_hours*3600+to_minutes*60;
  if(example_prints)
    {
    Print("Seconds From : "+IntegerToString(seconds_from));
    Print("Seconds To   : "+IntegerToString(seconds_to));
    }
  
    if(seconds_to>=seconds_from){
      if(example_prints){Print("Normal Comparison Used");}
      if(seconds_in_this_time>=seconds_from&&seconds_in_this_time<=seconds_to){
      if(example_prints){Print("Valid");}
      return(true); 
      }
    }

    else{ 
      if(example_prints){Print("Weird Comparison Used");}
    
    if((seconds_in_this_time>=seconds_from&&seconds_in_this_time<=86400)||(seconds_in_this_time>=0&&seconds_in_this_time<=seconds_to)){
    if(example_prints){Print("Valid");}
    return(true);
    }}

if(example_prints){Print("NOT Valid");}
return(false);
}
int OnInit()
  {
  //tests 
    bool is_valid_time=isThisTimeInMyTimeWindow(TimeLocal(),9,30,9,45);//09:30 to 09:45
         //is_valid_time=isThisTimeInMyTimeWindow(TimeLocal(),13,0,21,0);//13:00 to 21:00
         //is_valid_time=isThisTimeInMyTimeWindow(TimeLocal(),23,0,4,0);//23:00 to 04:00 
    
  return(INIT_SUCCEEDED);
  }
  
void OnDeinit(const int reason)
{}

void OnTick()
  {        
    int    digits=(int)SymbolInfoInteger(symbol,SYMBOL_DIGITS);
    double point=SymbolInfoDouble(symbol,SYMBOL_POINT);
    double bid=SymbolInfoDouble(symbol,SYMBOL_BID);
    double SL=bid-500*point;
    double TP=bid+500*point; 
    
   bool Buy_Condition_1 = isThisTimeInMyTimeWindow(TimeCurrent(),9,30,9,45);
   bool Buy_Condition_2 = (PositionSelect(symbol) == false);

   if(Buy_Condition_1)
     {    
      if(Buy_Condition_2)
         {     
         trade.Buy(Lot,symbol,0.0,SL,TP,"");
         }
     }
  }
         

 
Reason: