Open trade in the last second, is it possible?

 

Hello..

I'm having some troubles trying to adjust an EA to open the trade at the last second of the end of the candle once the other conditions are met.

I've read about the Seconds() function and all, but wasn't able to perform this task with success.

I wonder if this is posible, or i'm losing my time trying to do this..

Thanks in advance for your help.

 

The problem is that you never know which is the last tick.

Only if you have got the first netxt-candle-tick you know the prev. tick has been the last one.

Can you use a percentage of the candle like if a candle is 90% 'full'?

if ( (double)(TimeCurrent()-Time[0])/(double)PeriodSeconds() > 0.9 )

or in the last 10 seconds:

if ( (TimeCurrent()-Time[0])>= PeriodSeconds() - 10 )

But this will fail if no tick are coming in.

Or you can use the OnTimer() but with this is a different approach: Read about OnTimer() with EventSetTimer() and EventKillTimeer() but I don't know how precise the timer is after a couple of days!!

 
gooly:

The problem is that you never know which is the last tick.

Only if you have got the first netxt-candle-tick you know the prev. tick has been the last one.

Can you use a percentage of the candle like if a candle is 90% 'full'?

or in the last 10 seconds:

But this will fail if no tick are coming in.

Or you can use the OnTimer() but with this is a different approach: Read about OnTimer() with EventSetTimer() and EventKillTimeer() but I don't know how precise the timer is after a couple of days!!

Ok Gooly, thanks for your help, i'll read about this OnTimer().

If i use this percentage, and have no ticks until the end of the candle, will it open in the first tick of the first next candle?

And couldn't I specify like:

input double seconds = 59;

if (PeriodSeconds()==seconds)

   ? 


Or something like, even if had or haven't any ticks it will open the order exactly at the 59 second..

 
input double seconds = 59;

if (PeriodSeconds()==seconds)
That will always be false PeriodSeconds - MQL4 Documentation

 
One option to explore is using a millisecond timer to check TimeGMT() or TimeLocal()... both of which aren't tied to an incoming tick.
 
honest_knave:
One option to explore is using a millisecond timer to check TimeGMT() or TimeLocal()... both of which aren't tied to an incoming tick.

So would its usage be like this?

input int seconds = 59;

int init()
   {
      EventSetTimer(TimeLocal());
      return(0);
   }
int deinit()
   {
      EventKillTimer();
      return(0);
   }
int start()
   {
   }
void OnTimer()
   {
      EventSetTimer(seconds);  || or || EventSetMillisecondTimer(59000);  
   }
 

Please bear in mind the following is not very efficient, but it may suit your needs.

You can add in additional checks (minutes etc).

If you opt for a millisecond timer (rather than a second timer), you are more likely to catch the start of the 59th second but you must balance accuracy with the additional processing load. Plus you'll need extra code to avoid it firing multiple times during the same 59th second.

Just an idea.

#property strict

input int seconds=59;

int OnInit()
  {
   EventSetTimer(1);
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   EventKillTimer();
  }

void OnTick()
  {
  }

void OnTimer() 
  {
   if(TimeSeconds(TimeLocal())==seconds)
     { // do what you need to do on the last second of each minute
     }
  }

 

Alternatively, you can try to catch an inbound tick during the 59th second and then simply set a 60 second timer from then on.  This method has 2 flaws: (1) it requires the 59th second to be caught during an inbound tick. (2) there may be some slippage over time(?). Not sure about that last point. You may want to code in a recalibration periodically.

#property strict

input int seconds=59;

int OnInit()
  {
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   EventKillTimer();
  }

void OnTick()
  {
   static bool TimerSet=false;
   if(!TimerSet && TimeSeconds(TimeLocal())==seconds)
     {
      TimerSet=true;
      EventSetTimer(60);
      // do what you need to do on the 59th second
     }
  }

void OnTimer()
  {
    // do what you need to do on the last second of each minute
  }
 
honest_knave:

Please bear in mind the following is not very efficient, but it may suit your needs.

You can add in additional checks (minutes etc).

If you opt for a millisecond timer (rather than a second timer), you are more likely to catch the start of the 59th second but you must balance accuracy with the additional processing load. Plus you'll need extra code to avoid it firing multiple times during the same 59th second.

Just an idea.

 

Alternatively, you can try to catch an inbound tick during the 59th second and then simply set a 60 second timer from then on.  This method has 2 flaws: (1) it requires the 59th second to be caught during an inbound tick. (2) there may be some slippage over time(?). Not sure about that last point. You may want to code in a recalibration periodically.

 

 

Very nice, cleared a lot for me.
It can also be set a Sleep function of 'n' seconds to avoid the multiple firing... 

 
gviali:

Very nice, cleared a lot for me.
It can also be set a Sleep function of 'n' seconds to avoid the multiple firing... 

Or use loop to count opening orders and set limit based on that.
 
honest_knave:

Alternatively, you can try to catch an inbound tick during the 59th second and then simply set a 60 second timer from then on.  This method has 2 flaws: (1) it requires the 59th second to be caught during an inbound tick. (2) there may be some slippage over time(?). Not sure about that last point. You may want to code in a recalibration periodically.

The problem is that one cannot be sure that a tick comes in within the 59th second so that OnTick is executed to set the Eventtimer.

This could be better?

   if ( IsNewBar ) {
      int tmeLeft = PeriodSeconds() - 1 - (TimeCurrent()-Time[0]);
      EventSetTimer(tmeLeft);
   }
 

Hi gooly, that might be a nice refinement.

It would probably be best to kill the timer from within OnTimer(), and let it get reset on the next new bar.

Reason: