Setting Trading Days

 

Hi all,

I'd like to test my trading strategy on different days of the week. I've 'followed' the instructions here but I don't seem to understand how to use the functions it suggests. Below is my code;

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

input double cLot=25;

input bool Sunday   =true; // Sunday
input bool Monday   =true; // Monday
input bool Tuesday  =true; // Tuesday 
input bool Wednesday=true; // Wednesday
input bool Thursday =true; // Thursday
input bool Friday   =true; // Friday
input bool Saturday =true; // Saturday

bool WeekDays[7];

void WeekDays_Init()
  {
   WeekDays[0]=Sunday;
   WeekDays[1]=Monday;
   WeekDays[2]=Tuesday;
   WeekDays[3]=Wednesday;
   WeekDays[4]=Thursday;
   WeekDays[5]=Friday;
   WeekDays[6]=Saturday;
  }
  
bool WeekDays_Check(datetime aTime)
  {
   MqlDateTime stm;
   TimeToStruct(aTime,stm);
   return(WeekDays[stm.day_of_week]);
  }

int OnInit()
  {      
  return(INIT_SUCCEEDED);
  }
void OnDeinit(const int reason)
{
}

void OnTick()
  {    
///////////////////////////////////////////////////////////////////////////
//Trade Code
    int    digits=(int)SymbolInfoInteger(_Symbol,SYMBOL_DIGITS);
    double point=SymbolInfoDouble(_Symbol,SYMBOL_POINT);
    double bid=SymbolInfoDouble(_Symbol,SYMBOL_BID);
    double ask=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
    double SL=bid-10000*point;
    double TP=bid+7500*point; 

   bool Buy_Condition_1 = (PositionSelect(_Symbol) == false); //check theres no open positions
   if(Buy_Condition_1)
     {
         trade.Buy(cLot,_Symbol,ask,SL,TP,"");
     }
///////////////////////////////////////////////////////////////////////////     
  }

I've tried setting the different input bools to false, with no effect, attempted to reference the 'WeekDays_Check' in an 'if' statement but seem to repeatedly get the syntax wrong.

I was reluctant to post this one as I'm sure I'm painfully close to solving it but at this point I've wasted too many hours staring at it.. Any help?

TIA

MQL5 Programming Basics: Time
MQL5 Programming Basics: Time
  • www.mql5.com
The article focuses on standard MQL5 functions for working with time, as well as programming techniques and practically useful functions for working with time that are required when creating Expert Advisors and indicators. Particular attention is paid to the general theory of time measurement. This article should be of interest primarily to novice MQL5 programmers.
 
JimboDiggity:

Hi all,

I'd like to test my trading strategy on different days of the week. I've 'followed' the instructions here but I don't seem to understand how to use the functions it suggests. Below is my code;

I've tried setting the different input bools to false, with no effect, attempted to reference the 'WeekDays_Check' in an 'if' statement but seem to repeatedly get the syntax wrong.

I was reluctant to post this one as I'm sure I'm painfully close to solving it but at this point I've wasted too many hours staring at it.. Any help?

TIA

You were really close indeed .

Just needed to initialize the week days on oninit 

and to call the check in the trading if check 

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

input double cLot=25;

input bool Sunday   =true; // Sunday
input bool Monday   =true; // Monday
input bool Tuesday  =true; // Tuesday 
input bool Wednesday=true; // Wednesday
input bool Thursday =true; // Thursday
input bool Friday   =true; // Friday
input bool Saturday =true; // Saturday

bool WeekDays[7];

void WeekDays_Init()
  {
   WeekDays[0]=Sunday;
   WeekDays[1]=Monday;
   WeekDays[2]=Tuesday;
   WeekDays[3]=Wednesday;
   WeekDays[4]=Thursday;
   WeekDays[5]=Friday;
   WeekDays[6]=Saturday;
  }
  
bool WeekDays_Check(datetime aTime)
  {
   MqlDateTime stm;
   TimeToStruct(aTime,stm);
   return(WeekDays[stm.day_of_week]);
  }

int OnInit()
  { 
  WeekDays_Init();     
  return(INIT_SUCCEEDED);
  }
void OnDeinit(const int reason)
{
}

void OnTick()
  {    
///////////////////////////////////////////////////////////////////////////
//Trade Code
    int    digits=(int)SymbolInfoInteger(_Symbol,SYMBOL_DIGITS);
    double point=SymbolInfoDouble(_Symbol,SYMBOL_POINT);
    double bid=SymbolInfoDouble(_Symbol,SYMBOL_BID);
    double ask=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
    double SL=bid-10000*point;
    double TP=bid+7500*point; 

   bool Buy_Condition_1 = (PositionSelect(_Symbol) == false); //check theres no open positions
   if(Buy_Condition_1&&WeekDays_Check(TimeTradeServer()))
     {
         trade.Buy(cLot,_Symbol,ask,SL,TP,"");
     }
///////////////////////////////////////////////////////////////////////////     
  }
 
Lorentzos Roussos #:

Just needed to initialize the week days on oninit 

Urghhhh this one hurts. I tried this at the beginning but i guess i must've had some of the other code/the 'if' statement wrong also as i couldn't get it to work, assumed i must've been going down the wrong track and deleted it. Its so frustrating as a novice to know you're so close but still wasting hours on something so simple because you clearly justttt don't yet have the ability to solve it. 

Once again Lorentzos, you're a legend. Many thanks!

 
JimboDiggity #:

Urghhhh this one hurts. I tried this at the beginning but i guess i must've had some of the other code/the 'if' statement wrong also as i couldn't get it to work, assumed i must've been going down the wrong track and deleted it. Its so frustrating as a novice to know you're so close but still wasting hours on something so simple because you clearly justttt don't yet have the ability to solve it. 

Once again Lorentzos, you're a legend. Many thanks!

Don't worry . At first the ideas run faster than their materialization but its normal . The best solution is pen and paper (not the company , notes) , the second best solution is getting away from the screen for a while.

😊


 
Instead of separate inputs
input bool Sunday   =true; // Sunday
input bool Monday   =true; // Monday
input bool Tuesday  =true; // Tuesday 
input bool Wednesday=true; // Wednesday
input bool Thursday =true; // Thursday
input bool Friday   =true; // Friday
input bool Saturday =true; // Saturday

bool WeekDays[7];

void WeekDays_Init()
  {
   WeekDays[0]=Sunday;
   WeekDays[1]=Monday;
   WeekDays[2]=Tuesday;
   WeekDays[3]=Wednesday;
   WeekDays[4]=Thursday;
   WeekDays[5]=Friday;
   WeekDays[6]=Saturday;
  }
  
bool WeekDays_Check(datetime aTime)
  {
   MqlDateTime stm;
   TimeToStruct(aTime,stm);
   return(WeekDays[stm.day_of_week]);
  }
Use a bit mask
input uint dowEnabled=127; // 127 all=[Sunday … Saturday]
bool is_dow_allowed(uint dow){       return 1<<dow & dowEnabled != 0);        }

bool WeekDays_Check(datetime aTime){ return is_dow_allowed(DayOfWeek(aTime)); }
int DayOfWeek(datetime aTime)
  {
   MqlDateTime stm;
   TimeToStruct(aTime,stm);
   return stm.day_of_week;
  }
 
William Roeder #:
Instead of separate inputs
Use a bit mask

That would confuse the users a bit . 

 
William Roeder #:
Instead of separate inputs
Use a bit mask

Thanks for this William. Unfortunately as Lorentzos suggests it has indeed served to confuse me.. I've since done a bit of reading on bitmasks to help but have come away almost none-the-wiser as to their application. I am obviously trying to learn to code MQL5 however, so will save a mental note on this post to attempt it at a later date when I'm a bit more proficient - cheers.

 
Lorentzos Roussos #:

You were really close indeed .

Just needed to initialize the week days on oninit 

and to call the check in the trading if check 


Thank you to everyone for sharing your knowledge. I'm new to this, and it's really helping me to learn.


I'm trying to make an EA specifying the days of the week to trade, and this post was exactly what I needed.

However, no matter I change this area,

input bool Sunday   =true; // Sunday
input bool Monday   =true; // Monday
input bool Tuesday  =true; // Tuesday 
input bool Wednesday=true; // Wednesday
input bool Thursday =true; // Thursday
input bool Friday   =true; // Friday
input bool Saturday =true; // Saturday

the result of

WeekDays_Check(TimeTradeServer())

is always showing "true".


Could somebody give me an advise/hint to this situation???

Thank you so much in advance.

 
KeiRuSa #: Could somebody give me an advise/hint to this situation???

We can't see your broken code.

 
William Roeder #:

We can't see your broken code.

input bool Sunday   =false; // Sunday
input bool Monday   =false; // Monday
input bool Tuesday  =false; // Tuesday 
input bool Wednesday=true; // Wednesday
input bool Thursday =false; // Thursday
input bool Friday   =false; // Friday
input bool Saturday =false; // Saturday

bool WeekDays[7];

void WeekDays_Init()
  {
   WeekDays[0]=Sunday;
   WeekDays[1]=Monday;
   WeekDays[2]=Tuesday;
   WeekDays[3]=Wednesday;
   WeekDays[4]=Thursday;
   WeekDays[5]=Friday;
   WeekDays[6]=Saturday;
  }

bool WeekDays_Check(datetime aTime)
  {
   MqlDateTime stm;
   TimeToStruct(aTime,stm);
   return(WeekDays[stm.day_of_week]);
  }  


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

   WeekDays_Init();
     
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

   if(WeekDays_Check(TimeTradeServer()))
      {
      Comment("Trade day");
      }
   else
      {
      Comment("Not Trade day");
      }
      
  }

Thank you and sorry for my late reply.


I'm always getting the comment "Trade day" even though I want this only on the Wednesdays in this situation.

Obviously I'm not understanding something...

 
KeiRuSa #:

Thank you and sorry for my late reply.


I'm always getting the comment "Trade day" even though I want this only on the Wednesdays in this situation.

Obviously I'm not understanding something...


The code that you included is correct, try showing us a screenshot of your EA settings and chart.