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

Created at 29 Mar 2013, 20:17
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
TH

thakas

Joined 22.02.2013

How can I get the index position of a specific Datetime?
29 Mar 2013, 20:17


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


@thakas
Replies

lec0456
01 Apr 2013, 01:36

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...

 


@lec0456

Spotware
02 Jan 2014, 11:24

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


@Spotware

pip18
03 Mar 2015, 19:54

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


@pip18

fxfund
12 May 2019, 15:11

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


@fxfund