how to open a trade at the exact time, without using pending order? - page 2

 
William Roeder:

You wait for a tick. Check if the time is 12:05. If not don't open. If so, open. When in doubt, THINK.

Dear William,

Thanks for kindly respect.

 
Milad Yarzamn:

Hi

In Metatrader OnTick() function is called when forming a candle, which does not know what hour or minute the event will take place.

I think you get the current server time using TimeTradeServer (dt_struc)

Then turn it into seconds.

For example:

your clock=12:05:30

current time server=11:01:00

long TimeRemainingInSeconds=((12-11)*60*60)+((05-01)*60)+30;

and call ontimer()  function:

OnTimer(1sec);


and then  in OnTimer function:

if(TimeRemainingInSeconds==0)///////////////////////////At this point the server clock is equal to the clock you want(12:05:30)

  {

     sendorder();

}

else

{

    TimeRemainingInSeconds--;

}





I hope my solution works.

If you have any questions, I'm at your service.

ِDear Milad,

thanks again for your kindly respect. It is my pleasure to have a great friend as you.

 

Use OnTimer Event

int OnInit()
 {
    EventSetTimer(1);
 }

void OnTimer()
{
         MqlDateTime time ;
       TimeToStruct(TimeCurrent(),time) ;
       if(time.hour==12 && time.min==05 && time.sec==00)
        {
         //Send order
        }
}

void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
  }

This is the easiest and most accurate way  to open time orders 

 
datetime targetTime() {string currentdatestr=TimeToString(TimeCurrent(),TIME_DATE); string datetimenow=currentdatestr+" 12:05:00"; return StringToTime(datetimenow);}

void OnTick()
  {
   if(targetTime()==TimeCurrent()){sendOrder();}
  }

hoping a solution of your problem.

 
MikelDavao:

hoping a solution of your problem.

dear Mike,


thanks, I figure out how to solve my problem and your code is so easier and more technically than that I wrote.

 
mh.vasheghany:

dear Mike,


thanks, I figure out how to solve my problem and your code is so easier and more technically than that I wrote.

your Welcome!

 
MikelDavao:

hoping a solution of your problem.

StringToTime() will automatically add the current date when it just receives a time of day. You could shorten your function to:

datetime targetTime() { string timeofday="12:05:00"; return StringToTime(timeofday); }

or even just:

void OnTick()
  {
   if(StringToTime("12:05:00")==TimeCurrent()){sendOrder();}
  }

Note this may trigger several sendOrder(), or even no one at all, depending on the amount of ticks seen during that second.

 
lippmaje:

StringToTime() will automatically add the current date when it just receives a time of day. You could shorten your function to:

or even just:

Note this may trigger several sendOrder(), or even no one at all, depending on the amount of ticks seen during that second.

Im very happy your extended support on this issue, and mostly happy when you trim my code. i am now replacing my old code with this one quoted "datetime targetTime() { string timeofday="12:05:00"; return StringToTime(timeofday); }". YES it is TRUE that -this may trigger several sendOrder() but i am only helping 

how to open a trade at the exact time, without using pending order.  lippmaje-two thumbs up for you!!