Have the open price of candle at specific hour.

 

Hi guys,

i have a new question, simple i think.

So when for example it's 15:00, i want to know the open price of the candle which time was 9:00.

if (TC >= 15:00)
{
    if (Openprice < iclose(symbol(), period_H1, 1)
    {
       Action
    }
}
So how to write code to obtain the Openprice of the candle of pre-defined time hour?
 

Work out the time as a datetime variable and use it with iBarshift() to get the bar number,  when you have the bar number use that with Open[] or iOpen()

 
RaptorUK:

Work out the time as a datetime variable and use it with iBarshift() to get the bar number,  when you have the bar number use that with Open[] or iOpen()


yes not bad idea :)

Thanks you i will use it !

 

And is possible to do that:

   int Bar;
   double OpenPrice;
   
   int B(k);
   {
      switch(HeureSH[k])
      {
         case 0: Bar= iBarShift(Symbol(), PERIOD_H1, StrToTime("23:00"));
         break;
            
         case 1: Bar= iBarShift(Symbol(), PERIOD_H1, StrToTime("09:00"));
         break;
            
         case 1: Bar= iBarShift(Symbol(), PERIOD_H1, StrToTime("15:00"));
         break;
            
         case 3: Bar= iBarShift(Symbol(), PERIOD_H1, StrToTime("19:00"));
         break;
      }
   }
   OpenPrice= iOpen(Symbol(), PERIOD_H1, B);


HeureSH is arraysize with the four hours

Cause if it's a specific hours the bar have to be different, ithough create a switch to have only one line, not four différents lines.

Oh is it correct? is the code is writed correctly?

 
Kane59:

And is possible to do that:


HeureSH is arraysize with the four hours

Cause if it's a specific hours the bar have to be different, ithough create a switch to have only one line, not four différents lines.

Oh is it correct? is the code is writed correctly?

I assume you have a copy/paste error as you have case 1 twice . . .  so I'll assume the second case 1 should be case 2

For this to work the values stored in the  HeureSH[]  array are 1, 2, 3 or 4 ,  is that correct ?  they can only be int type values. no strings or doubles . . .

In your iOpen() call  I assume B  should be Bar ?  or did you mean it to be B(int) ?  if you want to use the function in that call you have to make the function return the correct value.


You will do yourself, and everyone else, a big favour if you use meaningful variable and function names . . .  B, k and Bar are not very descriptive. 

 

Oh it's an error from me, i copied my code but i don't know what i wrote....... I resolved so.

In my iopen, i writed B which is "Bar" cause time is different, so Bar is different too.


Ah yes strings values will do some problems, i think it's the problem, so i will try  to change.

 
  int B(k);
   {
      switch(HeureSH[k])
      {
         case 0: Bar= iBarShift(Symbol(), PERIOD_H1, StrToTime("23:00"));
         break;
            
         case 1: Bar= iBarShift(Symbol(), PERIOD_H1, StrToTime("09:00"));
         break;
            
         case 1: Bar= iBarShift(Symbol(), PERIOD_H1, StrToTime("15:00"));
         break;
            
         case 3: Bar= iBarShift(Symbol(), PERIOD_H1, StrToTime("19:00"));
         break;
      }
   }
   OpenPrice= iOpen(Symbol(), PERIOD_H1, B);
This won't work. 1) iBarShift assigned to Bar but iOpen uses B. 2) StrToTime("23:00") will return today date at 23:00 which will always be in the future (or current H1 bar.)
#define HR2400      86400           // 24 * 3600
datetime TimeOfDay(datetime when){  return( when % HR2400          );       }
datetime DateOfDay(datetime when){  return( when - TimeOfDay(when) );       }
  int B(k);
   {
    #define HR0900 23400    // 9*3600
    #define HR1500 54000
    #define HR1900 68400
    #define HR2300 59800
    int times[]={ HR2300, HR0900, HR1500, HR1900 };
    datetime    now = TimeCurrent(),
                Bod = DateOfDay(now),
                want= Bod + times[HeureSH[k]];
    if (want > now){    // Yesterdays?
        datetime prevTradingDate = iTime(Symbol(), PERIOD_D1, 1);
        want = prevTradingDate + times[HeureSH[k]];
    }
   int iBar= iBarShift(Symbol(), PERIOD_H1, want);
   OpenPrice= iOpen(Symbol(), PERIOD_H1, iBar);
   }
 

Hi WHRoeder,

I think you code is more efficient than mine and get if time value is yesterday or today. I'm agree: 23:00, with my code, is always the futur or current when it's too late...


Thank you so much ! It's exactly i searched.

 

Hi WHRoeader,

for information:

you said ibarshift was assigned to Bar, iOpen to B. Ok

But the function B(k), what is this function? why is it here?

In my code, I placed it cause wanted a relation beetween arraysize of strings times (HeuresSH) and the differents StrToTime. It's was false so.


You used it again, why?

Regards :)

 

Yes, i forgot to say, HeureSH[] is a arraySize of four differents hours in string values.

 

Ok, i inserted your code in my EA and it works perfectly ! I smilled when i see the EA execute like i wanted !!!

Very thanks very very ......

It's on backtest cause market is closed...

But there is a problem:

Ea Open correctly positions, but buy are never closed and sell immediatelly closed when opened.

My strat is:

In same time, the EA have to open Sell OR Buy if parameters are true. There is not SL or TP.

And in same time, it has to loop and close all trade if next functions are true:

if(OrdersTotal()>0)
            { 
               for(int i=OrdersTotal()-1; i>=0; i--)
               {
                  if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
                  {
                     if(OrderMagicNumber() == MagicSH && OrderOpenTime() > iTime(Symbol(), PERIOD_H1, 1))
                     {
                        if(OrderType() == OP_BUY)
                        {
                           OrderClose(OrderTicket(),LotsSH, bid, 300, CLR_NONE);
                        }
                        if(OrderType() == OP_SELL)
                        {
                           OrderClose(OrderTicket(),LotsSH, ask, 300, CLR_NONE);
I have to add a profit filter, but i will work about this. Without that it have to close Buy, but never closed, and it have to let sell run but sell are closed immediately. Is there a wrong in the last code?
Reason: