How can I get the index position of a specific Datetime?

29 Mar 2013, 20:17How can I get the index position of a specific Datetime?#1
thakasposts: 6since: 22 Feb 2013

Is it any function or an other way to get immediatly the index position of a specific Datetime?

In MT4 i use the function:

iBarShift(NULL,0, time)

So if I want to get for any bar the close price of the 1st bar of the day I use the following code:

datetime CurrentTime  = Time[j];                                                                              // Current Time
int      BarsCurrentTime = iBarShift(NULL,0, CurrentTime);                                // Bars Current Time

datetime TimeStartOfDay  = CurrentTime - CurrentTime % 86400;                  // Time Start of Day
int             BarsStartOfDay = iBarShift(NULL,0, TimeStartOfDay);                       // Bars Start of Day

MaCounter  =  BarsStartOfDay - BarsCurrentTime;

close = iClose(NULL, 0, MaCounter )

 

In cAlgo I can get the first bar of the Day for any bar with the next code:

MarketSeries.OpenTime[index].Date

But how can I convert the Datetime to index position ?

 

Thanks in Advance

01 Apr 2013, 01:36#2
lec0456posts: 487since: 14 Nov 2012

The index is just :

MarketSeries.Close.Count-1
And the index value of:
MarketSeries.OpenTime[index].Date

is the same variable index used to get the open time you are requesting

 

hope this helps buy maybe I am not understanding you question...

 

02 Jan 2014, 11:24#3
cTrader Teamposts: 3380since: 23 Sep 2013

You can now access it easier with the GetIndexByTime or GetIndexByExactTime methods:

DateTime date = MarketSeries.OpenTime.LastValue.Date;
var firstBarIndex = MarketSeries.OpenTime.GetIndexByTime(date);

 

/api/reference/timeseries/getindexbytime-9d11


TRADERS FIRST™ Vote for your favorite features: http://vote.spotware.com/
03 Mar 2015, 19:54#4
pip18posts: 1since: 03 Mar 2015

How do I get the close at a specific time each day, i.e., 1700 hours or 0230 hours?

12 May 2019, 15:11#5
FXROBposts: 1since: 06 Sep 2018

                         var dateTime = new DateTime(2019, 5, 10, 17, 0, 0);
            Print(MarketSeries.Open[MarketSeries.OpenTime.GetIndexByTime(dateTime)]);