Mql4 - trade 10 mins before the close of current candle

 
I have coded an ea that trades at the close of the current candle. However my system is traded on the daily chart. The spreads are much higher at the open of a new bar. Therefore I would like to perform all my trading actions before the candle closes.
Any 
 
samsgtw: Therefore I would like to perform all my trading actions before the candle closes.
  1. So do it. Bar ends at Time[0] + PeriodSeconds().

  2. You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.
              No free help 2017.04.21

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum 2018.05.12

    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting).
              No free help 2017.04.21

 
samsgtw:
I have coded an ea that trades at the close of the current candle. However my system is traded on the daily chart. The spreads are much higher at the open of a new bar. Therefore I would like to perform all my trading actions before the candle closes.
Any 

You can adapt this code to your own Expert program.


   
   double i;
   int m,s,k;
   m=Time[0]+Period()*60-CurTime();
   i=m/60.0;
   s=m%60;
   m=(m-m%60)/60;
    Comment( m + " minutes " + s + " seconds left to bar end");
   if(m==50) // Time to Bar Closing
   { 
   // Your Open Codes..
   
   }
 
Mehmet Bastem:

You can adapt this code to your own Expert program.

Why didn't you use PeriodSeconds() like William did?

   m=Time[0]+Period()*60-CurTime();

This does not work for MQL5.

William Roeder

You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.

Agree. Please provide a sample of your code.

 
William Roeder:
  1. So do it. Bar ends at Time[0] + PeriodSeconds().

  2. You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.
              No free help 2017.04.21

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum 2018.05.12

    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting).
              No free help 2017.04.21

i thought it would be a simple solution thats only a couple of lines of code so someone would be glad to help me out. But i will share my attempt

 
samsgtw:
I have coded an ea that trades at the close of the current candle. However my system is traded on the daily chart. The spreads are much higher at the open of a new bar. Therefore I would like to perform all my trading actions before the candle closes.
Any 

Please let me be some sort of intrusive on your idea. 


A better idea, would be you check the spread values, since you want to avoid high spreads which happens on the end of the day.

Check the spread of the previous candles, on a timeframe inferior than D1. Calculate a median/average value for reference.. act on the current D1 candle Zero as usual, and only make operations if the spread is about the same median/average values you have calculated. (E.g.: If spread is like about 40% above the average spread values, do not let the EA place orders.. etc..)

It is more guaranteed this way, than work based on time, time of the candle, since the broker has no default for increasing the spread.. it is a decision at the broker will, at any time.

 
rrocchi:

Please let me be some sort of intrusive on your idea. 


A better idea, would be you check the spread values, since you want to avoid high spreads which happens on the end of the day.

Check the spread of the previous candles, on a timeframe inferior than D1. Calculate a median/average value for reference.. act on the current D1 candle Zero as usual, and only make operations if the spread is about the same median/average values you have calculated. (E.g.: If spread is like about 40% above the average spread values, do not let the EA place orders.. etc..)

It is more guaranteed this way, than work based on time, time of the candle, since the broker has no default for increasing the spread.. it is a decision at the broker will, at any time.

Interesting idea, thanks :D

 
here is my attempt
input string tradetime = "23:50:00";           //Trade Entry Time

void OnTick()
  {
      datetime time_current = TimeCurrent();
      if(time_current = tradetime)
      {
         OnBar();
         //OnBar = function to check indicators and enter trade
      }
The OnBar function should be called once every day at 23:50:00
But it is being called every twenty minutes when i test the EA
 
William Roeder:
  1. So do it. Bar ends at Time[0] + PeriodSeconds().

  2. You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.
              No free help 2017.04.21

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum 2018.05.12

    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting).
              No free help 2017.04.21

here is my attempt
input string tradetime = "23:50:00";           //Trade Entry Time

void OnTick()
  {
      datetime time_current = TimeCurrent();
      if(time_current = tradetime)
      {
         OnBar();
         //OnBar = function to check indicators and enter trade
      }
The OnBar function should be called once every day at 23:50:00
But it is being called every twenty minutes when i test the EA
 
samsgtw:
here is my attempt The OnBar function should be called once every day at 23:50:00
But it is being called every twenty minutes when i test the EA

You cannot compare TYPE string to TYPE datetime 


Compare different data TYPE is possible only in Perl language ;)


Documentation about how to compare datetime is below:

dateandtime

Documentation on MQL5: Date and Time
Documentation on MQL5: Date and Time
  • www.mql5.com
Date and Time - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Reason: