Having the strategy trade only once between 4 & 6 PM

 

Hi,

I wasted like 5 hours in a row trying to find out how to have the system open and close a position only ONCE whitin a time interval, example from 4 to 6 PM if the condition is met.

Tried many pieces of code, I have herebelow what I believe could be the key, but I am feeling that I will waste many hours more before finding the solution:

for(i=OrdersHistoryTotal();i>=0;i--)

{
  if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==True)
   {if(OrderCloseTime()>(TimeCurrent()-10*Period()*250))
      TradeAllowed=false; 
       else TradeAllowed=true;
       }

Basically I want the system to check the last closing, if the last closing is still between 4 and 6 pm, or very near, then tradeallowed=false.

If last time it traded was long time ago if(OrderCloseTime()>(TimeCurrent()-10*Period()*250) then trade again.

It works only for the first trade no matter what i try..

Please help.

May thanks.

 

int start()

{

if(Hour()<4||Hour()>=6)return(0);

....

}

 
Roger wrote >>

int start()

{

if(Hour()<4||Hour()>=6)return(0);

....

}

Thank you roger, that's how I have started my code.

Having the system trade whithin a timeframe is fine, but stopping him from trading more than x times, lets say 1 time for the sake of being simple is what I am looking for.

Basically if the timeframe is lets say 6 hours, the system will trade every time the condition is met during those 6 hours and not only one time. I am trying to found out a way to have it trade only once during that timeframe.

So in my code above the system try to find out what is the last trade, assess the trading time, and then delay the next trade to a further time, which should be after the 6 hours for example.

Any more thoughts on that ?

Thanks.

 

After a test, this seems to work....




bool check_open_trade_in_the_period(int hour_ini, int hour_end)
{
   if(TimeDayOfWeek(OrderOpenTime())==TimeDayOfWeek(TimeCurrent()))  // if it is today 
      if(TimeMonth(OrderOpenTime())==TimeMonth(TimeCurrent()))       // if it is current month 
         if(TimeYear(OrderOpenTime())==TimeYear(TimeCurrent()))      // if it is current year
              if(TimeHour(OrderOpenTime())>=hour_ini && TimeHour(OrderOpenTime())<=hour_end)  // if it is in the time interval
                  return(true);  // trade has been opened in the period, today

   return(false); // trade not opened in the period
}


bool can_I_trade(int hour_ini, int hour_end)
{
   // see if there has been a closed trade in the period...
   for(int i=OrdersHistoryTotal()-1;i>=0;i--)
   {      
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))  
         continue;
      if(check_open_trade_in_the_period(hour_ini, hour_end))   
         return(false); // got a trade in history      
   }
   
   // see if there there is a open trade in the period...
   for(i=OrdersTotal()-1;i>=0;i--)
   {      
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))   
         continue;
      if(check_open_trade_in_the_period(hour_ini, hour_end))   
         return(false); // there is an open trade      
   }  
   // There is no closed nor open trade in the period...
   // Now, check if it is time to trade...
   if(Hour()<hour_ini || Hour()>hour_end)
      return(false);   // ... it isn't. Let's go for a drink.
 
   // It is time to trade
   return(true); // let's try a trade....  
}
 

int start()
{

   if(can_I_trade(16, 18))
   {
       // ADD YOUR PROFITABLE TRADE RULES HERE...
   }
}



Enjoy it.

 
inquisiteur:

[...] So in my code above the system try to find out what is the last trade, assess the trading time, and then delay the next trade to a further time, which should be after the 6 hours for example.

Any more thoughts on that ?

It's a similar question to https://www.mql5.com/en/forum/123277, with the same range of options in terms of easy solutions (i.e. record the last trade time in memory) versus harder but more reliable solutions (i.e. check the MT4 order history).


Abstract_mind's suggestion takes the harder route and should work for you - with a small number of fairly pedantic caveats:


  • There's no check that orders are from your EA. In other words, the rules for new trades by your EA potentially get blocked by other EAs, or by manual orders. The code will not trade if there is any order at all during the time period, regardless of who/what placed it. There ought ideally to be a check that the magic number of open or historic orders matches the magic number which you are using for your EA. In addition, if there's any chance that your EA will be used on more than one chart at once, then your EA should compare the symbol of open/historic orders against the symbol which the EA is trading. Unless, that is, it's desirable that your EA on e.g. a EURUSD chart doesn't trade if another copy of your EA on e.g. EURCHF has taken a trade during your 4pm-to-6pm window.
  • The code does not distinguish between filled versus deleted historic orders. This may not matter in real life, but if your EA places a pending order and then deletes it before it is filled, the code will then block subsequent trading during the 4pm-to-6pm window.
  • The check of "TimeHour(OrderOpenTime())<=hour_end)" allows trading until the end of hour_end (e.g. 18:59, not 18:00). Similarly, Roger's suggestion trades up until 06:59 (am, not pm).
  • As far as I can see, "TimeDayOfWeek(OrderOpenTime())==TimeDayOfWeek(TimeCurrent())" blocks trading on the same day of the week (e.g. Mondays), not the same day of the month (i.e. today). The code will potentially not take a trade if today is Tuesday and the system took a trade on a previous Tuesday of the current month. I think the code should use TimeDay(), not TimeDayOfWeek().
 

I'll mention this once.

Some people in this forum don't really help, and whenever they see contributions, instead of providing constructive feedback/critic, they try to pull down these altruist contributions. This kind of behavior will only affect the guys who really need help.

The suggestions just made are quite easy to include in the offered approach. But for me, this thread is over.

 

Abstract - I read this post and my interpretation is that you and jjc are BOTH helping the poster.

I've dealt with jjc offline and find him most helpful - check his other posts and I think you might change your mind.

In fact if you re-read jjc's post, I think he's being complimentary to your suggestion for the most part.


CB

 
abstract_mind wrote >>

After a test, this seems to work....

Enjoy it.

I have wanted to thank both abstract_mind and jjc for helping me out, will be able to try this code on the afternoon. Providing ready to use code is of course way more helpful, but any constructive insight with regards to time interpretation is welcome too.

thanks again.

 
I found JJC's post helpful as well. Very accurate and to the point.
 

My overreaction was because of a previous comment from a guy who complained about my (bad) coding style.

Sorry JJC.

 
abstract_mind:



My overreaction was because of a previous comment from a guy who complained about my (bad) coding style.

Sorry JJC.

i have meant it in a general way, not related to anyone special. and there was NO complaining about anybody in my post, but about the fact of bad readable code in general.

but you are right,i shouldnt care about this, i can read my code, LOL. even after years without comments inside.

looks like your are a little bit sensitive, which is in general is a good talent, but often someone may feel offended, inspite of the fact, that noone hasnt offended him/her.