How to treat candles (chart) by seconds

 

Hello folks, 


Is there anyone have tried this? Could anyone show me a part of an example code?

I'm coding an EA using 5 minutes chart and depending of the conditions, it will open new orders. As a result all the orders time are opened by multiple of 5 minutes: 0,5, 10, 15...

But now, I'm trying to deal with seconds, I mean instead of open a new order exactly at 05 minutes, it will open at 10:04:55 (10 hours, 4 minutes and 55 seconds).


The following is an example of conditions:


void OpenOrder()

  {
     
   //HighTrade
   if(iClose(Symbol(),PERIOD_M5,1)>iOpen(Symbol(),(PERIOD_M5 - 5 Seconds),1))    //iOpen should be exactly the price of the 5 minutes, but iClose should be: (5 minutes - 5 seconds) = "10:04:55"
      {
       BuyTrade();
      } 

   }

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

 
  1. Why did you post your MT4 question in the Root / MT5 Systems section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Guilherme Mendonca: As a result all the orders time are opened by multiple of 5 minutes:

    Nope. They open when you open them. Only if you test for a  new bar will they be close to the beginning of 5 minutes bar.

  3. Guilherme Mendonca:
     if(iClose(Symbol(),PERIOD_M5,1)>iOpen(Symbol(),(PERIOD_M5 - 5 Seconds),1)) 
    Your posted code makes no sense. There is no open 5 seconds before the end of a candle, and 5 - 5 Seconds is meaningless. You can wait until the current time is Time[0]+PeriodSeconds()-5 and then check current market price vs. the previous candle. You also assume that there will be a tick in the last five seconds of a candle:
    What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific.) requires knowledge of when your broker stops and starts (not necessary the same as the market.)
              "Free-of-Holes" Charts - MQL4 Articles 20 June 2006
              No candle if open = close ? - MQL4 programming forum 2010.06.06
 
William Roeder:
  1. Why did you post your MT4 question in the Root / MT5 Systems section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Nope. They open when you open them. Only if you test for a  new bar will they be close to the beginning of 5 minutes bar.

  3. Your posted code is moronic. There is no open 5 seconds before the end of a candle, and 5 - 5 Seconds is meaningless. You can wait until the current time is Time[0]+PeriodSeconds()-5 and then check current market price vs. the previous candle. You also assume that there will be a tick in the last five seconds of a candle:

I don't know why almost every post here, there are someone offending the other person because he judges this is a "stupid question".

1. This is not a MT4 question. I'm using MT5.

2. Of course.

3. I didn't posted a MORONIC "code", I posted AN EXAMPLE. At the market that I'm trading, 5 seconds make a BIG diference, from the opened to closed market, there is always dozens of trades per second. MT5 it is not used only on forex market. 
"Time[0]+PeriodSeconds()-5" can be an idea. But Time[0] should always be reseted for each new 5 minutes bar. After that, the EA can count Time[0] + (4 minutes and 55 seconds), after that check the conditions. If is true, will open a new order.

 
William Roeder:
  1. Why did you post your MT4 question in the Root / MT5 Systems section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Nope. They open when you open them. Only if you test for a  new bar will they be close to the beginning of 5 minutes bar.

  3. Your posted code is moronic. There is no open 5 seconds before the end of a candle, and 5 - 5 Seconds is meaningless. You can wait until the current time is Time[0]+PeriodSeconds()-5 and then check current market price vs. the previous candle. You also assume that there will be a tick in the last five seconds of a candle:

Thank you for your idea.

I implemented some code and it is working as I wanted.

This is the part of the code:


void OnTick()
  {
  
   datetime newbar=NewBar();

   if(TimeCurrent()>=newbar+294 && TimeCurrent()<=newbar+301)
        {
         OpenOrder();
        }

  }


//+------------------------------------------------------------------+
datetime NewBar()
  {

   datetime time[];
   CopyTime(_Symbol,PERIOD_M5,0,1,time);
   if(time[0]!=lasttime)
     {
      lasttime=TimeCurrent();
     }

   return lasttime;
  
  } 
//+------------------------------------------------------------------+
 
Guilherme Mendonca:

Thank you for your idea.

I implemented some code and it is working as I wanted.

This is the part of the code:


Can I make a small suggestion? you use TimeCurrent() three times in this piece of code. It could be that the time changes from one second to the next precisely during the execution of this code. In that case are not all three evaluations identical. I don't know if this would affect your intention. My suggested alternative (adding and using parameter timenow):

void OnTick()
  {
   datetime timenow = TimeCurrent();
   datetime newbar=NewBar(timenow);

   if(timenow>=newbar+294 && timenow<=newbar+301)
        {
         OpenOrder();
        }

  }


//+------------------------------------------------------------------+
datetime NewBar(datetime now)
  {

   datetime time[];
   CopyTime(_Symbol,PERIOD_M5,0,1,time);
   if(time[0]!=lasttime)
     {
      lasttime=now;
     }

   return lasttime;
  
  } 
//+------------------------------------------------------------------+
 
WindmillMQL:

Can I make a small suggestion? you use TimeCurrent() three times in this piece of code. It could be that the time changes from one second to the next precisely during the execution of this code. In that case are not all three evaluations identical. I don't know if this would affect your intention. My suggested alternative (adding and using parameter timenow):

Hi!


You are right, thank you for your answer and to share your knowledge.

Your code should works better.