static public method core.findDate

Brief

Searches in the price stream for the specified date.

Declaration
Lua
number core.findDate (stream, date, precise)

Parameters
stream

A tick or bar stream to search the date inside.

date

The date and time as a number. That number means the number of days past midnight December 30, 1899 (also known as the OLE date/time format).

precise

The boolean flag. If flag is true the method returns the index of exact date found. If flag is false the method returns the index of the exact date or of the nearest previous date if exact date is not found.

Returns

The index of the bar if date is found or -1 in case the date is not found.

Details

The function searches the date in O(log2(stream:size())) operations.

Example: Find the oldest bar in the source which belongs to the same trading day [hide]

   function FindOldest(source, period)
       local dayoffset = core.host:execute("getTradingDayOffset");
       local second = 1 / 86400;       -- 1 second constant
       local day, day1;
       local date = source:date(period);
 
       day = core.getcandle("D1", date, dayoffset, 0);
       -- using non-precise search because the first smaller timeframe candle can absent
       -- in this case the non-precise search can return the last candle of the previous day
       local index = core.findDate(source, day, false);
       if index == -1 then
           -- the date is not found at all.
           -- check whether the first date belongs to the same trading day
           day1 = core.getcandle("D1", source:date(source:first()), dayoffset, 0);
           if math.abs(day - day1) < second then
               return source:first();
           else
               return -1;
           end
       end
 
       day1 = core.getcandle("D1", source:date(index), dayoffset, 0);
       if math.abs(day - day1) < second then
           return index;       -- the same day
       else
           return index + 1;   -- the candle belongs to previous day, so take the next candle
       end
   end

back