Download MetaTrader 5

Open trade in the last second, is it possible?

To add comments, please log in or register
Looking for like-minded people? You will find lots of them on the Forum!
Gabriel Viali
205
Gabriel Viali 2015.02.04 06:13 

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.

Carl Schreiber
6581
Carl Schreiber 2015.02.04 07:48  

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!!

Gabriel Viali
205
Gabriel Viali 2015.02.04 16:07  
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..

whroeder1
14457
whroeder1 2015.02.04 19:39  
input double seconds = 59;

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

honest_knave
Moderator
2153
honest_knave 2015.02.04 20:20  
One option to explore is using a millisecond timer to check TimeGMT() or TimeLocal()... both of which aren't tied to an incoming tick.
Gabriel Viali
205
Gabriel Viali 2015.02.04 21:39  
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);  
   }
honest_knave
Moderator
2153
honest_knave 2015.02.04 22:55  

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
  }
Gabriel Viali
205
Gabriel Viali 2015.02.05 02:14  
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... 

SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2015.02.05 02:31  
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.
Carl Schreiber
6581
Carl Schreiber 2015.02.05 09:43  
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);
   }
honest_knave
Moderator
2153
honest_knave 2015.02.05 11:23  

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.

12
To add comments, please log in or register