Friday closure

 

I have seen other entries about this, but none seemed to address a solution to what I am seeing.

I want to stop opening new trades on Thursday night at 2300 hours. Then on Friday prior to market close, say by an hour, I want to close all open trades.

Here is my code snippet:

   if (
)
   {
      Print ("true, Day of week is: ", TimeDayOfWeek(TimeCurrent())); // shows as 1 as of today
      if (TimeDayOfWeek(TimeCurrent()) == 5)
      {
         Print ("Friday, Day of week is: ", TimeDayOfWeek(TimeCurrent()));  // does not show up, this is sunday
         StopOpeningTrades = true;
         if (Hour() == 12)
         {
            Print("Its Friday, closing all trades for the week.");   // does not show up, this is sunday
            GLOBALClosingAllOrders();
         }
      }
      
      if (TimeDayOfWeek(TimeCurrent()) == 4 && Hour() == 23)
      {
         Print("Its Thursday, we will not take anymore new trades.");  // does not show up this is sunday
         StopOpeningTrades = true;      
      }
      
      if (TimeDayOfWeek(TimeCurrent()) < 4)
      {
         StopOpeningTrades = false;
      }
      Print ("StopOpeningTrades: ", StopOpeningTrades);   // shows as 0 which is false and is correct if this value is 1 or true 
					    //  then no new trades will be created.
   }
So, here is the problem:
1. Something here has stopped the EA from opening any trades.

2. StopOpeningTrades method is proven and works, I am verifying before and after that it is in the desireable value.

3. GLOBALClosingAllOrders method is proven and works.

4. NoTradeFriday is a boolean, if true, it checks to see if user wants to use this feature, true, then user wants to use it.

Now seperate question:
The time values I am looking for of course is relative to my local time, pacific, however, my servers time appears to be GMT
local time is 3/21/2010 2038hours, charts indicate 3/22/2010 0438 hours. Am I using the right hour values to check for? If not, what should I set them too?
 
Both parts of your code are buggy since Hour() does not return the hour of the current last known server time! It returns the hour of the last known server time at the start of program run. From the documentation (https://docs.mql4.com/dateandtime/Hour):

Returns the hour (0,1,2,..23) of the last known server time by the moment of the program start (this value will not change within the time of the program execution).

You need to use TimeHour(TimeCurrent()) instead of Hour().


Edit: as jjc corrected me bellow, Hour(), Minute() are updated at each start() call... So this would run fine in a EA, but won't work in a script.
 
Try an Expert Advisor that does this in the start() function:

while(!IsStopped()) {
   Comment(StringConcatenate(Seconds(), ",", TimeSeconds(TimeCurrent()), ",", TimeSeconds(TimeLocal()), ",", TimeSeconds(MarketInfo(Symbol(), MODE_TIME)));
   Sleep(1);
}
You'll see that the different functions seem to change at different times and frequencies. The most frequently updating function is TimeLocal() because it doesn't depend on data from the server.
The next most frequently updating function is TimeCurrent().
The other two update less frequently (maybe dependent on an incoming tick) and they update at the same time, but the values are not always the same as each other (from my observation, Seconds() was sometimes greater than TimeSeconds(MarketInfo(Symbol(), MODE_TIME)) by 1 second).
 
gordon:
Both parts of your code are buggy since Hour() does not return the hour of the current last known server time! It returns the hour of the last known server time at the start of program run. From the documentation (https://docs.mql4.com/dateandtime/Hour):
[...]

I'm not quite sure what you think the problem is. Hour() and Minute() return the time at the beginning of the current call to start(). Unless an EA spends a very long time in its start() function, there is no significant difference between the current time versus the values reported by Hour() and Minute(). When the documentation refers to "the start of program run", it clearly means the beginning of the current call to start(). I'm not sure whether this is something slightly garbled in the translation from the Russian, or is an insight into how the Metaquotes developers regard the execution of EAs.

If you want to check this, try running the following EA on any chart. The reported value will update every minute:

int start() {Comment("Minute: " + Minute());}


N.B. Obvious potential difference here between an EA versus a script which runs indefinitely.
 
LEHayes:

Here is my code snippet:[...]

There are three obvious issues with your code/comments in the form you've posted them:

  • The initial if condition appears to be blank. Depending on what (if anything) it's doing, the StopOpeningTrades variable may not be getting set/reset at the correct points.
  • You don't have any handling for the condition where day-of-week is 4 and hour is less than 23. This shouldn't actually matter in practice, but it's still an omission.
  • Your comments refer to variables and conditions such as NoTradeFriday which aren't included in the code.

 

There are comments that explain that NoTradeFriday is a bool that is set by the user to turn on and off the feature.


the alternate condition for handling any time frame other than day 4 and hour = 23, or day 5, is the default normal running condition which it should default to if these conditions are not met. So there is not issue here.

The StopOpeningTrades is very tightly controlled. Only a handful of features are even aloud to touch this control. Each and everyone has been tested and verified to be operating correctly without this new feature.

I believe I have found the control that is the issue and it does not appear to be this feature. I will take note of the Hour() concern, however, based on the debate above, I would like more confirmation as to whether this is an issue or not, it would seem that we have to contradicting opinions on the methods of obtaining the time value, and I would like more direction on this.

I did not get an answer on whether I have the times set correctly or not for Friday closure and Thursday night "stop taking new trades" event. Do I have these hour values set right? Will this work according to the hours I have set? my concern is the variance between GMT and actual times.

The darn thing took trades last night, only on one pair, but in doing so, it showed me where the problem is as far as letting trades occur. I will open a new thread on this.

 
Please see my updated post above about the difference between Seconds(), TimeSeconds(TimeCurrent()), TimeSeconds(TimeLocal()), and TimeSeconds(MarketInfo(Symbol(), MODE_TIME)).
 
jjc:

I'm not quite sure what you think the problem is. Hour() and Minute() return the time at the beginning of the current call to start(). Unless an EA spends a very long time in its start() function, there is no significant difference between the current time versus the values reported by Hour() and Minute(). When the documentation refers to "the start of program run", it clearly means the beginning of the current call to start(). I'm not sure whether this is something slightly garbled in the translation from the Russian, or is an insight into how the Metaquotes developers regard the execution of EAs.

If you want to check this, try running the following EA on any chart. The reported value will update every minute:

int start() {Comment("Minute: " + Minute());}


N.B. Obvious potential difference here between an EA versus a script which runs indefinitely.

Obviously what I think is wrong. I don't remember where I got the idea that it works like that... Must be due to your last comment; I must have tested it in a script and got the wrong idea (and indeed the translated documentation doesn't help). Sorry.

 
Oh, I remember where I got the idea. I tested it in a EA, but... with an endless loop! Not the 'best' way :) https://www.mql5.com/en/forum/122601.
Reason: