How to open order

 
enum ordertype
  {
   buy=1,   // Buy
   sell=2,  // Sell
   notrade=0,// No Order
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
enum rtype
  {
   r1,// R1
   r2,// R2
   r3,// R3
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+


extern   ordertype   condition01;

extern   rtype   resistance=1;   //Resistance
extern   int   magic=1234;   // Magic Number


double   lot;
int tick;
int numcondition1;

datetime time1;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

      lot=1;

      
      count();

             
        
       if(condition01==1 && resistancevalue(resistance)>High[0] && Time[0]>time1)
          
         {tick=OrderSend(Symbol(),OP_BUY,lot,Ask,5,0,0,"numcondition1",magic,0,clrGreen);
         time1=Time[0];}
        
         
count();
      if(condition01==2 && resistancevalue(resistance)<High[0] && Time[0]>time1)
         {tick=OrderSend(Symbol(),OP_SELL,lot,Bid,5,0,0,"numcondition1",magic,0,clrGreen);
         time1=Time[0];}
         
if(OrdersTotal()>0)
        {
         for(int i=OrdersTotal()-1;i>=0;i--)
           {
            tick=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
            if(OrderProfit()>=1) 
               tick=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,clrWhite);
            if(OrderProfit()<=2)
               tick=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,clrWhite);
           }

        }
}
      
      
//+------------------------------------------------------------------+


double resistancevalue(int newresistance)
  {
   double pivot=(High[0]+Low[0]+Close[0])/3;
   if(resistance==1) return 2*pivot-Low[0];
   if(resistance==2) return pivot+High[0]-Low[0];
   else return High[0]+2*(pivot-Low[0]);


  }
  
  
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void   count()
  {
   numcondition1=0; 
   
    for(int i=OrdersTotal()-1;i>=0;i--)
     {
      tick=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

      if(OrderComment()=="numcondition1" && OrderMagicNumber()==magic && OrderSymbol()==Symbol())
         numcondition1++;


     }}

Good morning,

I'm writing an Expert Advisor for MT4, but I need to open the order at the closure of the candle and not immediately when the condition meet. There is a   function to do it? Or how I can write it? Can someone please help me?

What I wrote it sends the order immediately when the condition are satisfied, but how can I modify it to send the oreder at the closure of the candle?


Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
Trader1529:

Good morning,

I'm writing an Expert Advisor for MT4, but I need to open the order at the closure of the candle and not immediately when the condition meet. There is a   function to do it? Or how I can write it? Can someone please help me?

What I wrote it sends the order immediately when the condition are satisfied, but how can I modify it to send the oreder at the closure of the candle?


I didn't read your codes, but it is the answer for what you asked.

You can't  get the exact closure  of the bar, but you can check for opening of the next bar.

Therefor you need to check your conditions based on the bar 1 and not the bar 0 which is the current bar.

define a variable in the global area (after your externs/inputs and before OnInit() ) 

datetime  newBar=0;// 0 means 1970,01,01 00:00:00  

then in the OnTick () 

if(newBar <Time[0])
        {
        //check for your condition in the bar 1 and open the trade if your conditions are satisfied.

        
        //at the bottom of OnTick:
        newBar=Time[0];
        }

 

You may need to change it sometimes .

 
if(newBar <Time[0])
Don't use less than, use not equals. A clock mis-set and then corrected could stop your EA indeterminately. (Happened 8 years ago.)
 
William Roeder:
Don't use less than, use not equals. A clock mis-set and then corrected could stop your EA indeterminately. (Happened 8 years ago.)

Thanks a lot William Roeder

Perfect !