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,""); } /////////////////////////////////////////////////////////////////////////// }
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!
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; } |
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.
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.
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...
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.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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