Getindex tick

Created at 20 May 2022, 23:13
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!
KI

Kisses

Joined 05.04.2022

Getindex tick
20 May 2022, 23:13


Hi, 

I am trying to get an index number of a particular tick data to access bid and ask information (based on DateTime of another tick dataset) using the getindexbytime - but this method seems only applicable for MarketData.GetBars and not MarketData.GetTicks correct? If so, is there any workaround to get this? Really appreciate any helps or suggestion on this.

Thanks!


@Kisses
Replies

amusleh
23 May 2022, 08:56

Hi,

There is no GetIndexByTime method for Ticks series, because several ticks can occur per second and it's hard to match them against a fixed time, but if you want to you can create your own as an extension method to Ticks:

using System;
using cAlgo.API;

namespace cAlgo
{
    [Indicator(AccessRights = AccessRights.None)]
    public class NewIndicator2 : Indicator
    {
        protected override void Initialize()
        {
            var ticks = MarketData.GetTicks();

            var index = ticks.GetIndexByTime(Bars.OpenTimes.LastValue);

            Print(index);
        }

        public override void Calculate(int index)
        {
        }
    }

    public static class TickGetIndexByTimeExtension
    {
        public static int GetIndexByTime(this Ticks ticks, DateTime time)
        {
            for (var i = 0; i < ticks.Count; i++)
            {
                if (ticks[i].Time == time)
                {
                    return i;
                }
            }

            return -1;
        }
    }
}

 


@amusleh