How to open order

 
count();
      if(condition01==1)
         {
         tick=OrderSend(Symbol(),OP_BUY,lot,Ask,5,0,0,"condition1",magic,0,clrGreen);
         time1=Time[0];
         }

Good morning,

I'm writing an Expert Advisor, 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, but how can I modify it to send it at the closure of the candle?


 
Trader1529:

Good morning,

I'm writing an Expert Advisor, 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, but how can I modify it to send it at the closure of the candle?


Hey there. Try adding if(IsNewBar) condition. Here are the parameters. Since the beginning of a new candle is the end of an old one this should work.
      static datetime Old_Time;
      datetime New_Time[1];
      bool IsNewBar=false;

      // copying the last bar time to the element New_Time[0]
      int copied=CopyTime(_Symbol,_Period,0,1,New_Time);
      if(copied>0) // ok, the data has been copied successfully
        {
         if(Old_Time!=New_Time[0]) // if old time isn't equal to new bar time
           {
            IsNewBar=true;   // if it isn't a first call, the new bar has appeared
            if(MQL5InfoInteger(MQL5_DEBUGGING))
               Print("We have new bar here ",New_Time[0]," old time was ",Old_Time);
            Old_Time=New_Time[0];            // saving bar time
           }
        }
      else
        {
         Alert("Error in copying historical times data, error =",GetLastError());
         ResetLastError();
         return;
        }
      if(IsNewBar)
 
royzen:
Hey there. Try adding if(IsNewBar) condition. Here are the parameters. Since the beginning of a new candle is the end of an old one this should work.

Thank you Royzen, but it doesn't work

 
Trader1529:

Good morning,

I'm writing an Expert Advisor, 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, but how can I modify it to send it at the closure of the candle?


static datetime prevTime=0;

       datetime lastTime[1];
       if (CopyTime(_Symbol,_Period,0,1,lastTime)==1 && prevTime!=lastTime[0])
          {
               prevTime=lastTime[0];
              
               
          }

try this

 
Kenneth Parling:

try this

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++;


     }}

  
//+------------------------------------------------------------------+

This is my expert advisor coded in MQL4.

How can I send the order at the opening of the new candle? Where I have to write your code? 

 

I am new to programming and MQL5 and I've been studying MQL5 but right now I need to learn how to send order. I've used the MQL5 documentation but I still can't get it right. Please can anyone assist me, thanks.

 
mike5n :

I am new to programming and MQL5 and I've been studying MQL5 but right now I need to learn how to send order. I've used the MQL5 documentation but I still can't get it right. Please can anyone assist me, thanks.

Example: at the moment of the birth of a new bar, a position of 1.0 lot is opened.

How to start with MQL5
How to start with MQL5
  • 2020.03.05
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
Reason: