place order at certain time

 

Hello,

I am Oliver.I try to program my firtst EA. I want to place an order at an certain time. Is it possible to do it easier. I did it like this:

 int res;


int counter = 0;


int s = 0;


int m1 = 59;

int s1 = 00;

int h1 = 15;


void OnTick()

{

int STC=TimeSeconds(TimeCurrent());

int m=TimeMinute(TimeCurrent());

int h=TimeHour(TimeCurrent());


if (counter == 0)

if (h1 == h)

if (m1 == m)

if (s1 == STC)

{ Sell();

counter_adder();}


if (counter == 1)

if (s != STC)

{

counter_reseter();

}

}


void counter_adder()

{

counter = counter+1;

}


void counter_reseter()

{

counter = counter-1;

}


 
You can try that.
#property strict

input datetime TimeOpenOrder = __DATETIME__;
bool res = true;
void OnTick(void)   
{
     if( res )
          if( TimeCurrent() >= TimeOpenOrder )
               if( OrderSend( ... ) )
                    res = false;
}
 
int STC=TimeSeconds(TimeCurrent());
int m=TimeMinute(TimeCurrent());
int h=TimeHour(TimeCurrent());
if (counter == 0
&& h1 == h
&& m1 == m
&& s1 == STC) ..

That assumes that there will be a tick that s1 second. There can be minutes between ticks during the Asian session.

#define WHEN = h1 * 3600 + m1 * 60 + s1
if (counter == 0 && time() >= WHEN) ..
          Find bar of the same time one day ago - Simple Trading Strategies - MQL4 and MetaTrader 4 - MQL4 programming forum
 
Konstantin Nikitin:
You can try that.
Thank you. Very good Idea, but what can I do if I want to have more TimeOpenOrder on the same day?
 
Oliver_Trader :
Thank you. Very good Idea, but what can I do if I want to have more TimeOpenOrder on the same day?

One option

#property strict

datetime TimeOpenOrder[];

void OnInit(void)
{
     int total = 3;
     ArrayResize( TimeOpenOrder, total );
     
     MqlDateTime mqlDateTime;
     
     mqlDateTime.year = 2018;
     mqlDateTime.mon  = 5;
     mqlDateTime.day  = 24;
     mqlDateTime.hour = 12;
     mqlDateTime.min  = 35;
     mqlDateTime.sec  = 14;
     TimeOpenOrder[0] = StructToTime( mqlDateTime );
     
     mqlDateTime.hour = 18;
     mqlDateTime.min  = 05;
     mqlDateTime.sec  = 00;
     TimeOpenOrder[1] = StructToTime( mqlDateTime );
     
     mqlDateTime.day  = 25;
     mqlDateTime.hour = 12;
     mqlDateTime.min  = 10;
     mqlDateTime.sec  = 15;
     TimeOpenOrder[2] = StructToTime( mqlDateTime );

     for(int cnt=total-1; cnt>=0; cnt--)
     {
          if( TimeCurrent() > TimeOpenOrder[cnt] )
                    delTimeOpen( cnt );
     }
}

void OnTick(void)   
{
     int total = ArraySize( TimeOpenOrder )-1;
     for(int cnt=total; cnt>=0; cnt--)
     {
          if( TimeCurrent() >= TimeOpenOrder[cnt] )
               if( OrderSend( ... )
                    delTimeOpen( cnt );
     }
}

void delTimeOpen(const int cnt)
{
     int total = ArraySize( TimeOpenOrder )-1;
     for(int i=cnt; i<total; i++)
          TimeOpenOrder[i] = TimeOpenOrder[i+1];
     
     ArrayResize( TimeOpenOrder, total );
}

It is possible so

void OnInit(void)
{
     int total = 3;
     ArrayResize( TimeOpenOrder, total );
     TimeOpenOrder[0] = StringToTime( "2018.05.24 12:35:14" );
     TimeOpenOrder[1] = StringToTime( "2018.05.24 22:15:00" );
     TimeOpenOrder[2] = StringToTime( "2018.05.25 03:10:05" );

     for(int cnt=total-1; cnt>=0; cnt--)
     {
          if( TimeCurrent() > TimeOpenOrder[cnt] )
                    delTimeOpen( cnt );
     }
}

Or

void OnInit(void)
{
     int total = 3;
     ArrayResize( TimeOpenOrder, total );
     TimeOpenOrder[0] = D'24.05.2018 12:35:14';
     TimeOpenOrder[1] = D'24.05.2018 22:15:00';
     TimeOpenOrder[2] = D'25.05.2018 03:10:05';

     for(int cnt=total-1; cnt>=0; cnt--)
     {
          if( TimeCurrent() > TimeOpenOrder[cnt] )
                    delTimeOpen( cnt );
     }
}
 
Konstantin Nikitin:

One option

It is possible so

Or

Thank you! I understood everything, except of the cnt--.

Can you explain it, please?

     for(int cnt=total-1; cnt>=0; cnt--)
 
Oliver Podolak :

Thank you! I understood everything, except of the cnt--.

Can you explain it, please?

If you do not understand why this check is in OnInit.
It's simple. You can write an array for a long time. And when you restart the expert. All that is in the array earlier retired. And the positions will not take again.
Situations are different. Therefore, insurance does not hurt.

 
Hi,again. I don't know where the error in my program is. I tried the TimeOpenReseter at different times. It worked at 16, 17and 18 O'Clock, but at 00:06 O'Clock it doesn't work. Could you please take a look at it. Thank you!
#property strict
datetime TimeOpenOrder[];
datetime TimeOpenReseterTime[];
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
void OnInit(void)
  {

   int total_TimeOpenReseterTime=1;
   ArrayResize(TimeOpenReseterTime,total_TimeOpenReseterTime);
   TimeOpenReseterTime[0]=StringToTime("00:06:00");

   int total=41;
   ArrayResize(TimeOpenOrder,total);
   TimeOpenOrder[40] = StringToTime("20:10:35");
   TimeOpenOrder[39] = StringToTime("20:10:18");
   TimeOpenOrder[38] = StringToTime("20:09:53");
   TimeOpenOrder[37] = StringToTime("20:09:38");
   TimeOpenOrder[36] = StringToTime("20:09:23");
   TimeOpenOrder[35] = StringToTime("17:39:57");
   TimeOpenOrder[34] = StringToTime("17:39:42");
   TimeOpenOrder[33] = StringToTime("17:39:20");
   TimeOpenOrder[32] = StringToTime("17:37:30");
   TimeOpenOrder[31] = StringToTime("17:37:08");
   TimeOpenOrder[30] = StringToTime("15:03:47");
   TimeOpenOrder[29] = StringToTime("15:03:32");
   TimeOpenOrder[28] = StringToTime("15:03:17");
   TimeOpenOrder[27] = StringToTime("15:03:02");
   TimeOpenOrder[26] = StringToTime("15:02:47");
   TimeOpenOrder[25] = StringToTime("12:43:40");
   TimeOpenOrder[24] = StringToTime("12:43:27");
   TimeOpenOrder[23] = StringToTime("12:43:12");
   TimeOpenOrder[22] = StringToTime("12:42:55");
   TimeOpenOrder[21] = StringToTime("12:42:39");
   TimeOpenOrder[20] = StringToTime("10:15:12");
   TimeOpenOrder[19] = StringToTime("10:12:46");
   TimeOpenOrder[18] = StringToTime("10:10:45");
   TimeOpenOrder[17] = StringToTime("10:08:48");
   TimeOpenOrder[16] = StringToTime("10:06:16");
   TimeOpenOrder[15] = StringToTime("07:35:25");
   TimeOpenOrder[14] = StringToTime("07:34:49");
   TimeOpenOrder[13] = StringToTime("07:33:54");
   TimeOpenOrder[12] = StringToTime("07:33:03");
   TimeOpenOrder[11] = StringToTime("07:32:29");
   TimeOpenOrder[10] = StringToTime("05:14:53");
   TimeOpenOrder[9] = StringToTime("05:14:29");
   TimeOpenOrder[8] = StringToTime("05:14:14");
   TimeOpenOrder[7] = StringToTime("05:13:59");
   TimeOpenOrder[6] = StringToTime("05:13:44");
   TimeOpenOrder[5] = StringToTime("02:37:39");
   TimeOpenOrder[4] = StringToTime("02:37:18");
   TimeOpenOrder[3] = StringToTime("02:36:57");
   TimeOpenOrder[2] = StringToTime("02:36:22");
   TimeOpenOrder[1] = StringToTime("02:36:07");
   TimeOpenOrder[0] = StringToTime("00:12:00");

   for(int cnt=total-1; cnt>=0; cnt--)
     {
      if(TimeCurrent()>TimeOpenOrder[cnt])
         delTimeOpen(cnt);
     }
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick(void)
  {

   if(TimeOpenReseterTime[0]>=TimeCurrent())
     {
      TimeOpenReseter();
     }

   int total=ArraySize(TimeOpenOrder)-1;
   for(int cnt=total; cnt>=0; cnt--)
     {
      if(TimeCurrent()>=TimeOpenOrder[cnt])
         if(OrderSend(Symbol(),OP_SELL,0.1,Bid,3,0,(Bid-0.0003),NULL,0,0,Red))
           {
            delTimeOpen(cnt);
           }
     }
  }
//+------------------------------------------------------------------+
void delTimeOpen(const int cnt)
  {
   int total= ArraySize(TimeOpenOrder)-1;
   for(int i=cnt; i<total; i++)
      TimeOpenOrder[i]=TimeOpenOrder[i+1];

   ArrayResize(TimeOpenOrder,total);
   Comment("Total ",total);
  }
//+------------------------------------------------------------------+
void TimeOpenReseter()
  {
   int total=41;
   Comment("Total: ",total);
   ArrayResize(TimeOpenOrder,total);
   }
//+------------------------------------------------------------------+
 
Oliver Podolak:
Hi,again. I don't know where the error in my program is. I tried the TimeOpenReseter at different times. It worked at 16, 17and 18 O'Clock, but at 00:06 O'Clock it doesn't work. Could you please take a look at it. Thank you!

Couple issues with this code:

  1. OrderSend doesn't return a bool 
  2. Stoploss cannot be lower than the sell price
  3. Lots of repetitive typing!

You can simplify and optimize it by maintaining a simple time pool that constantly replenishes. 

#property strict
#include <stdlib.mqh>
#include <Arrays\ArrayInt.mqh>

CArrayInt g_times;

int OnInit()
{
   init_times();
   return(INIT_SUCCEEDED);
}

void OnTick()
{
   if(g_times.Total() <= 0)
      return; //error
  
   Comment("Next order to be placed at ",TimeToString(g_times[0],TIME_DATE|TIME_SECONDS));
   
   if(TimeCurrent() >= (datetime)g_times[0])
      if(OrderSend(Symbol(),OP_SELL,0.1,Bid,3,NormalizeDouble(Bid+30*_Point,_Digits),0) >= 0)
         g_times.Delete(0); 
         
   if(g_times.Total() <= 0)
      init_times();
}


void init_times()
{
   string t[] = {"20:10:35","20:10:18","20:09:53","20:09:38","20:09:23","17:39:57","17:39:42","17:39:20","17:37:30","17:37:08","15:03:47","15:03:32",
      "15:03:17","15:03:02","15:02:47","12:43:40","12:43:27","12:43:12","12:42:55","12:42:39","10:15:12","10:12:46","10:10:45","10:08:48","10:06:16",
      "07:35:25","07:34:49","07:33:54","07:33:03","07:32:29","05:14:53","05:14:29","05:14:14","05:13:59","05:13:44","02:37:39","02:37:18","02:36:57",
      "02:36:22","02:36:07","00:12:00"
   };
   for(int i=ArraySize(t)-1; i>=0;i--)
   {
      datetime time = StringToTime(t[i]);
      if(time < TimeCurrent())
         time+=PeriodSeconds(PERIOD_D1);
      g_times.Add((int)time);
   }
   g_times.Sort();
   //DEBUG
   for(int i=0;i<g_times.Total();i++)
      Print(TimeToString((datetime)g_times[i],TIME_DATE|TIME_MINUTES|TIME_SECONDS));
}


 
nicholi shen:

Couple issues with this code:

  1. OrderSend doesn't return a bool 
  2. Stoploss cannot be lower than the sell price
  3. Lots of repetitive typing!

You can simplify and optimize it by maintaining a simple time pool that constantly replenishes. 


Thank you! Now it works.
Reason: