Candle countin' of candles back from the current candle

 

All,

This one is driving me nuts! whilst trying to write an EA to trade the DAX I've discovered several instances of missing candle data or periods where the market is closed during the night. I'm using the 'iclose' to determine a close level from the previous day, but this requires a 'number of candles back from the current as input, but this number varies if there are missing night candles.

 I would like to select the close value of the 6pm candle the previous day without specifying the 'number' of candles back since this varies. Unless there is is a way of counting the candles between the current candle and the one at 6pm the previous day whilst acknowledging that there might be missing candles.

So this could be driven one of two ways,

1. A candle count that counts candles between two different times 

2. A way of using a set time in the iclose command

Any ideas?

 Cheers 

 
timdchambers:

All,

This one is driving me nuts! whilst trying to write an EA to trade the DAX I've discovered several instances of missing candle data or periods where the market is closed during the night. I'm using the 'iclose' to determine a close level from the previous day, but this requires a 'number of candles back from the current as input, but this number varies if there are missing night candles.

 I would like to select the close value of the 6pm candle the previous day without specifying the 'number' of candles back since this varies. Unless there is is a way of counting the candles between the current candle and the one at 6pm the previous day whilst acknowledging that there might be missing candles.

So this could be driven one of two ways,

1. A candle count that counts candles between two different times 

2. A way of using a set time in the iclose command

Any ideas?

 Cheers 

See iBarshift.
 
timdchambers: 

I've discovered several instances of missing candle data or periods where the market is closed during the night.

I would like to select the close value of the 6pm candle the previous day without specifying the 'number' of candles back since this varies.

  1. No ticks, no candles.
  2. Calculate the time, find the bar, get the value.
    #define HR2400 (PERIOD_D1 * 60)  // 86400 = 24 * 3600
    int      TimeOfDay(datetime when=0){      if(when == 0)  when = TimeCurrent();
                                              return( when % HR2400 );            }
    datetime DateOfDay(datetime when=0){      if(when == 0)  when = TimeCurrent();
                                              return( when - TimeOfDay(when) );   }
    //datetime Tomorrow( datetime when=0){      if(when == 0)  when = TimeCurrent();
    //                                          return(DateOfDay(when) + HR2400);   }
    datetime Yesterday(datetime when=0){      if(when == 0)  when = TimeCurrent();
       int iD1 = iBarShift(NULL, PERIOD_D1, DateOfDay(when) - 1);
                                           return( iTime(NULL, PERIOD_D1, iD1) ); }
    //////////
    static const int HR175959 = 18 * 3600 - 1; // 06 PM - 1 sec.
    datetime lastCloseDT    = Yesterday() + HR175959;
    int      lastCloseIndex = iBarShift(NULL,0, lastCloseDT);
    Print("Yesterday Close="+string(Close[lastCloseIndex]);

 

Thanks guys superb responses will work through both of them, think ibarshift is what I've been after, I was searching for things like candle counter or bar counter, thanks WHRoeder for the example.

 Happy New Trading Year to both of you ;o)

 

Hi WHRoeder,

I understand the last bit of code after the ////////

The bits that are new to me and I don't quite understand are:-

 #define, I understand where this goes I've just never used a #define before, please could you explain why its used?

and the other when defining int, double etc I've not seen the use of an 'if' statements or was this just a way of explaining something and I'm being dumb?

Thanks in advance for any help.

Cheers 

 
timdchambers:

 #define, I understand where this goes I've just never used a #define before, please could you explain why its used? 

It has been used to declare a constant, in this case 'HR2400' which has a value of 86400 (PERIOD_D1=1440).

So instead of writing 86400 each time (which may lead to a mistake) you can write HR2400 instead.

This value never changes, so it is a constant. 

Take a look here for an explanation of the #define directive.

 

timdchambers:

and the other when defining int, double etc I've not seen the use of an 'if' statements or was this just a way of explaining something and I'm being dumb?  

 They are just functions, with TimeOfDay() returning an int while DateOfDay() and Yesterday() return datetimes.

Sometimes you might see them written like this:

 

int TimeOfDay(datetime when=0)
  {
   if(when == 0)  when = TimeCurrent();
   return( when % HR2400 );
  }

datetime DateOfDay(datetime when=0)
  {
   if(when == 0)  when = TimeCurrent();
   return( when - TimeOfDay(when) );
  }

datetime Yesterday(datetime when=0)
  {
   if(when == 0)  when = TimeCurrent();
   int iD1 = iBarShift(NULL, PERIOD_D1, DateOfDay(when) - 1);
   return( iTime(NULL, PERIOD_D1, iD1) );
  }

Hope that helps. 

 

Yes that has helped,

So should the three statements defining TimeOfDay, DateOfDay and Yesterday be placed within the 'on Tick' section or in there own function or with the global variables below the #define?

The other bit I don't get is when there is an if statement there is normally an 'else' but in this case:----

so does that mean that if the statement is true the subsequent int and then return lines are processed and there is no 'else'???

datetime Yesterday(datetime when=0)
  {
   if(when == 0)  when = TimeCurrent();
   int iD1 = iBarShift(NULL, PERIOD_D1, DateOfDay(when) - 1);
   return( iTime(NULL, PERIOD_D1, iD1) );
  }
 

ok, I've re-read the posts and put the above statements in as Functions so 2 of the three are working. I've added the results to a comment script to view on screen. The Time and Date of Day are working fine, but the yesterday function is returning 1970.01.01 00:00.

What am I doing wrong? 

datetime Yesterday(datetime when=0)
   {
      if(when == 0)  when = TimeCurrent();
      int iD1 = iBarShift(NULL, PERIOD_D1, DateOfDay(when) - 1);
      return( iTime(NULL, PERIOD_D1, iD1) ); 
   };
 

Hello Tim,

They seem to be working fine for me. Do you want to post up your code and we can take a look?

 For example, this code: 

#define HR2400 (PERIOD_D1 * 60)

int TimeOfDay(datetime when=0)
  {
   if(when == 0)  when = TimeCurrent();
   return( when % HR2400 );
  }

datetime DateOfDay(datetime when=0)
  {
   if(when == 0)  when = TimeCurrent();
   return( when - TimeOfDay(when) );
  }

datetime Yesterday(datetime when=0)
  {
   if(when == 0)  when = TimeCurrent();
   int iD1 = iBarShift(NULL, PERIOD_D1, DateOfDay(when) - 1);
   return( iTime(NULL, PERIOD_D1, iD1) );
  }

 

And then this in OnCalculate():

   printf("TimeOfDay: %s",TimeToStr(TimeOfDay(),TIME_SECONDS));
   printf("DateOfDay: %s",TimeToStr(DateOfDay()));
   printf("Yesterday: %s",TimeToStr(Yesterday()));

 

Generates:

 

(bear in mind my broker closed at midnight last night, so the last tick came in just before then). 

 

Thanks honest_knave for the response, I have now managed to knife and fork the code into my EA, I'm not totally getting the Datetime function but I'm getting there.

My code has been improved massively by the feedback above. I'm just trying to deal with a discrepancy between my indicator and EA on how to deal with non trading days like NYD and Christmas Day, where my code is looking for the close from the previous day I need it to count back the extra day to avoid the non trading days. Any thoughts on how to do this??

Cheers Tim 

 
timdchambers: I'm just trying to deal with a discrepancy between my indicator and EA on how to deal with non trading days like NYD and Christmas Day, where my code is looking for the close from the previous day I need it to count back the extra day to avoid the non trading days. Any thoughts on how to do this??
No you don't. If the market was closed for the entire day (holiday or weekend,) then there are no bars that day. So get the shift for the last candle of yesterday and then the previous close.
double YesterdayClose(datetime when=0)
  {
   datetime  BOD = DateOfDay(when);
   int      iEOD = iBarShift(NULL, 0, BOD - 1); // Last candle yesterday.
   return Close[iEOD];
  }