How to send orders "on time" in the future

 

Hi, my question is about how can I send an order on a specific time in the future. I mean: let's suppose now it's 20.30 (8.30 pm) and I'd like to put a buy order to go long on 22.30 (10.30 pm), may be because some news will come on that time. Let's even suppose that I'll not be in front of my computer at 22.30, so now (20.30) I have to run some kind of program, and leave the pc on.. so that at 22.30 the order will be send.

Sorry, I'm not an expert and I tried with this script:

int start()

{

while (Hour()>=22 && Minute()>=30)

{

Sleep(1000);

}

OrderSend("EURUSD",OP_BUY,1,Ask,3,Ask-0.05,Ask+0.0007);

return(0);

}

But it seems that it doesn't work: No order is sent!

So I tried another way:

bool entrato;

int start()

{

entrato=0;

attendi();

return(0);

}

//------------------------------------------------------------------

void attendi()

{

Sleep(1000);

if (entrato==0)

{

opera();

}

return;

}

//------------------------------------------------------------------

void opera()

{

if (entrato==0 && Hour()>=22 && Minute()>=30)

{

OrderSend("EURUSD",OP_BUY,1,Ask,3,Ask-0.05,Ask+0.0007);

entrato=1;

}

attendi();

return;

}

But it seems that it doesn't work as well! (stack overflow message, I guess to many functions opened one inside the other..)

How can I do this? Is it correct to make a script? How have I to write it?

Please help me, thanks so much!!

Bye

 

Here.

You must attach it to the chart as an expert advisor (EA), not a script. That way, the EA will be activated in every tick that arrives to your terminal.

Instead of constant values, you can define external variables that you can define when you drag&drop the EA, and replace below.


extern int _hour=22;
extern int _minute=30;





#include <stdlib.mqh>
//+------------------------------------------------------------------+
//| TRADE AT A TIME                                   |
//+------------------------------------------------------------------+

void process_buy()
{
   int ticket=OrderSend(Symbol(),OP_BUY,1.0,Ask,3,0,0,"expert comment",255,0,CLR_NONE);
   if(ticket<1)
     {
      int error=GetLastError();
      Print("Error = ",ErrorDescription(error));
      return;
     }
   OrderPrint();
}

int start()
{
   // your code here...
   bool buy_condition=false;
   int number_of_longs=0;
   // assign true to your buy condition when your buy rule is fired 
   if((Hour()==22 && Minute()>=30) && number_of_longs==0)  //--> just to illustrate
        buy_condition=true;
   
   if(buy_condition)
      process_buy();
   
   return(0);
}
 

Thanks so much!!!!

only 2 questions:

1 - I think you never put the variable: number_of_longs different to zero. may be it's ok to put number_of_longs=1 after an else in the process_buy, isn't it'

2 - How can I stop an Expert? I do not find anywhere the list of the active Experts and I do not find anywhere how to stop them...

can you please help me?

thanks so much!!

 

Here again.

The EA only stops if you dettach it from the chart or you delete the very chart.

Anyway as it is now, it only opens one trade, regardless it continues working.

It only opens another if previous one is closed.

enjoy it.

//+------------------------------------------------------------------+
//| TRADE AT A TIME                                   |
//+------------------------------------------------------------------+


int CountLongs()
{
   int buys=0;
   
   for(int i=0;i<OrdersTotal();i++)    {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) 
         break;            
      if(OrderSymbol()==Symbol() )       
      if(OrderType()==OP_BUY)         
         buys++;   
   }    
   return(buys);   
}



void process_buy()
{
   int ticket=OrderSend(Symbol(),OP_BUY,1.0,Ask,3,0,0,"expert comment",255,0,CLR_NONE);
   if(ticket<1)
     {
      int error=GetLastError();
      Print("Error = ",ErrorDescription(error));
      return;
     }
   OrderPrint();
}



int start()
{
   // your code here...
   bool buy_condition=false;
   int number_of_longs=CountLongs();
   
   // you can assign true to your buy condition when your trade rule is fired    
   // if moving averagew, rsi, ss, or whatever... signals a buy 
        buy_condition=true;
   
   if(Hour()==22 && Minute()>=30 ) 
   if(buy_condition)
   if(number_of_longs==0)
      process_buy();
   
   return(0);
}
 
Here's an EA which will do what you want. https://www.mql5.com/en/code/8929
 

you're GREAT!

Thanks so much!!

Reason: