GetIndexByDate

Created at 30 Oct 2013, 20:14
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!
jhtrader's avatar

jhtrader

Joined 15.10.2013 Blocked

GetIndexByDate
30 Oct 2013, 20:14


Using GetIndexByDate here: /forum/whats-new/1463#3

The following function is supposed to return the index of the of the series for a given time.

Question 1 - In using the function you need to pass in the time. 

how would you do this for the following scenarios:

A. The most recent bar

B  The second most recent bar.

C. The first bar of the previous day

D. The last bar of the previous day

private int GetIndexByDate(MarketSeries series, DateTime time)
        {
            for (int i = series.High.Count - 1; i > 0; i--)
            {
                if (time == series.OpenTime[i])
                    return i;
            }
            return -1;
        }

 

Question 2 - When using this method

(is this the way to use it??)

 var lastIndex = MarketSeries.High.Count - 1;

var index = GetIndexByDate(seriesX, MarketSeries.OpenTime[lastIndex]);

the index would be -1 and then 7125 in the following bar itteration and then osccilate between -1 and 712x.. why does this happen?

The explanation that:

Different market series (different timeframes) will have different total count and the indices for one market series will not correspond to that of another marketSeries.  I can understand that but why the -1??

I am consistent in the use of the same marketseries.  I pass the series into the function, I use the  var lastIndex = MarketSeries.High.Count - 1;

as a way to find the last index on all series is this correct?

 

 

 

 

 

 

 


Replies

Spotware
04 Nov 2013, 12:23

The property Count of any series is the total number of elements contained in it. Since series are zero based (the first index is zero), the last element index is Count - 1.
For example, series.High.Count - 1 is the last (current) index of the series. So, you do not need the GetIndexByDate method to get the last index of any series. Dido for the second to last index, that will be series.High.Count - 2.

To find the first bar of the previous day index you can use code similar to this:

        private int GetFirstIndexOfPrevousDay(MarketSeries series)
        {
            var previousDay = Server.Time.AddDays(-1).Date;

            for (int i = 0; i < series.High.Count - 1; i++)
                if ( series.OpenTime[i].Date == previousDay)
                    return i;
            return -1;
        }

Similarly you can code the last bar of the previous day:

        private int GetFirstPrevousDayIndex(MarketSeries series)
        {
            var previousDay = Server.Time.AddDays(-1).Date;

            for (int i = series.High.Count - 1; i > 0; i--)
                if ( series.OpenTime[i].Date == previousDay)
                    return i;
            return -1;
        }

 

Question 2

The method returns -1 if the series does not contain the OpenTime parameter. 


@Spotware