What is best code for taking trades only in first 5 seconds of current/new candle

 

Can anyone tell what is the best coding for taking trades only in the first 5 (or x) seconds of the current/new candle?


Any help aprreciated!


Thanks.

 
  1. learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem.
  2. What will you do if there are no ticks during the first x seconds? "Free-of-Holes" Charts - MQL4 Articles
 

I try to learn, spent always every free hour I have to learn more.

Paying a coder is not a problem but I prefer learning myself by getting good tips from developers who have past my stadium ;-)


Nature problem: when Indi give signal after first x seconds of new bar, signal must be ignored.


I read different things about it. Possibility of use OnTimer and also that it is not possible.

Could I continue with the sample source or is that not gonna work?


Thank you for your time.

#property strict
#define magicnumber 123

input double lots=1; // Lot:
input int sleep=30000; // Sleep, in milliseconds:

int OnInit()
  {
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
  }

void OnTick()
  {
   static datetime LastBar=0;
   if(Time[0]!=LastBar)
     {
      LastBar=Time[0];
      int tmeLeft=PeriodSeconds()-1 -(int)(TimeCurrent()-Time[0]);
      EventSetTimer(tmeLeft);
     }
  }

void OnTimer()
  {
   int order;
   double customindarrow0 = iCustom(Symbol(),0,"cia",0,0);
   double customindarrow1 = iCustom(Symbol(),0,"cia1",1,0);

   if(customindarrow0!=0.0)
     {
      order=OrderSend(Symbol(),OP_BUY,lots,Ask,0,0,0,"custom buy arrow",magicnumber,0,Blue);
      Sleep(sleep);
      if(order==0)
        {
         Print("Error Opening Buy Order: ",GetLastError());
        }
      else if(order>0)
        {
         Print("Buy Order Opened");
        }
     }
   if(customindarrow1!=0.0)
     {
      order=OrderSend(Symbol(),OP_SELL,lots,Bid,0,0,0,"custom sell arrow",magicnumber,0,Red);
      Sleep(sleep);
      if(order==0)
        {
         Print("Error Opening Sell Order: ",GetLastError());
        }
      else if(order>0)
        {
         Print("Sell Order Opened");
        }
     }
   EventKillTimer();
  }
 
 order=OrderSend(Symbol(),OP_BUY,lots,Ask,0,0,0,"custom buy arrow",magicnumber,0,Blue);
      Sleep(sleep);
      if(order==0)
        {
         Print("Error Opening Buy Order: ",GetLastError());
What does OrderSend return when it fails?
 
digisoft:

Can anyone tell what is the best coding for taking trades only in the first 5 (or x) seconds of the current/new candle?


Any help aprreciated!


Thanks.

Maybe:

if(TimeCurrent()<=Time[0]+X)
 {

 }
 
If you are trying to guarantee that the broker will fill the placed order in the first 5 seconds of the new bar, you are fighting a losing battle.  From what I have heard, that is hit or miss in the best of situations.  If you are only trying to send the order within the first 5 seconds of the new bar, this could possibly work.
Reason: