MarketSeries Count

Created at 23 Apr 2013, 20:45
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!
supafly's avatar

supafly

Joined 26.12.2012

MarketSeries Count
23 Apr 2013, 20:45


Does MarketSeries.Close[MarketSeries.Close.Count - 2]; represent the second from the end close price? Can you show an algorithm that  e.g takes the last 3 closing prices?

Thanks


@supafly
Replies

supafly
23 Apr 2013, 23:00

Also how does "var = MarketSeries.Close.Count - 2;" differ from "var = MarketSeries.Close[MarketSeries.Close.Count - 2]" ?


@supafly

cAlgo_Fanatic
24 Apr 2013, 10:23

Yes, MarketSeries.Close[MarketSeries.Close.Count - 2]; represents the second from the end close price.

MarketSeries.Close is a DataSeries which is a collection of data like an array. An array, can be thought of as a container that has a list of storage locations. These locations are accessed by their index (position). Indexing starts from zero. For example if the values contained in the MarketSeries.Close are {5,1,9} then MarketSeries.Close[0] = 5, MarketSeries.Close[1] = 1 and MarketSeries.Close[2] = 9.

MarketSeries.Close.Count is the size of the collection. In the above example it is equal to 3. So at the last location, the index is 2.
So, MarketSeries.Close.Count - 1 gives you the last index. The value at that last location is accessed like this: MarketSeries.Close.Count[MarketSeries.Close.Count - 1 ].
To make it more clear to read you can break it into two lines:

var lastIndex = MarketSeries.Close.Count - 1; 

var lastValue = MarketSeries.Close.Count[lastIndex];

To show the last 3 closing prices:

    protected override void OnTick()
    {
        var lastIndex = MarketSeries.Close.Count - 1;
        for (int i = 0; i < 3; i++)
        {
            var index = lastIndex - i;
            var price = MarketSeries.Close[index];
            Print("Closing Price at index {0} is {1}", lastIndex - i, price);
        }
    }




@cAlgo_Fanatic

supafly
24 Apr 2013, 14:00

Thank you very much for this detailed explanation. It has certainly cleared up a few misinterpretations.


@supafly