How to access List<> of tick prices for the candle using onBar()

Created at 20 Feb 2022, 12:50
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!
stuart's avatar

stuart

Joined 24.01.2018

How to access List<> of tick prices for the candle using onBar()
20 Feb 2022, 12:50


Hi

How to access List<> of tick prices for the candle using onBar() 

So i dont want to use the onTick and build up a list, but use the MarketData.getTicks() and get the last x amount of tick that were in the Bar just closed. 

I guess i need the tick index, but how can i access it?

 

thank you

 


@stuart
Replies

amusleh
21 Feb 2022, 09:35

Hi,

There is no API feature that allows you to load a bar tick data.

For new real time bars you can record each bar tick data by using OnTick or Calculate method.

For historical bars there is no practical way to do this, you can load the tick data of a symbol and then filter each bar tick data by using its open time but loading all historical bars tick data can get lots of time.

As an example:

using System.Linq;
using cAlgo.API;
using cAlgo.API.Internals;
using System.Collections.Generic;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        private readonly Dictionary<int, Tick[]> _barTicks = new Dictionary<int, Tick[]>();

        protected override void OnStart()
        {
            var ticks = MarketData.GetTicks();

            // This can take very long time based on number of bars and their time frame
            while (ticks[0].Time > Bars[0].OpenTime)
            {
                var loadTicksNumber = ticks.LoadMoreHistory();

                if (loadTicksNumber < 1) break;
            }

            Print(ticks[0].Time, " | ", Bars[0].OpenTime, " | ", ticks.Count, " | ", Bars.Count);

            // For historical bars
            for (int barIndex = 0; barIndex < Bars.Count; barIndex++)
            {
                Tick[] barTicks;

                if (barIndex == Bars.Count - 1)
                {
                    barTicks = ticks.Where(tick => tick.Time >= Bars[barIndex].OpenTime).ToArray();
                }
                else
                {
                    barTicks = ticks.Where(tick => tick.Time >= Bars[barIndex].OpenTime && tick.Time < Bars.OpenTimes[barIndex + 1]).ToArray();
                }

                _barTicks.Add(barIndex, barTicks);
            }

            // Now each bar ticks are stored inside _barTicks dictionary and you can access them with bar index
            Print("All bars ticks are saved on _barTicks");
        }
    }
}

 


@amusleh

stuart
21 Feb 2022, 11:16

RE:

ok thank you for your reply. and also for the code, that gives me a good idea. 

Stuart

 

amusleh said:

Hi,

There is no API feature that allows you to load a bar tick data.

For new real time bars you can record each bar tick data by using OnTick or Calculate method.

For historical bars there is no practical way to do this, you can load the tick data of a symbol and then filter each bar tick data by using its open time but loading all historical bars tick data can get lots of time.

As an example:

using System.Linq;
using cAlgo.API;
using cAlgo.API.Internals;
using System.Collections.Generic;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        private readonly Dictionary<int, Tick[]> _barTicks = new Dictionary<int, Tick[]>();

        protected override void OnStart()
        {
            var ticks = MarketData.GetTicks();

            // This can take very long time based on number of bars and their time frame
            while (ticks[0].Time > Bars[0].OpenTime)
            {
                var loadTicksNumber = ticks.LoadMoreHistory();

                if (loadTicksNumber < 1) break;
            }

            Print(ticks[0].Time, " | ", Bars[0].OpenTime, " | ", ticks.Count, " | ", Bars.Count);

            // For historical bars
            for (int barIndex = 0; barIndex < Bars.Count; barIndex++)
            {
                Tick[] barTicks;

                if (barIndex == Bars.Count - 1)
                {
                    barTicks = ticks.Where(tick => tick.Time >= Bars[barIndex].OpenTime).ToArray();
                }
                else
                {
                    barTicks = ticks.Where(tick => tick.Time >= Bars[barIndex].OpenTime && tick.Time < Bars.OpenTimes[barIndex + 1]).ToArray();
                }

                _barTicks.Add(barIndex, barTicks);
            }

            // Now each bar ticks are stored inside _barTicks dictionary and you can access them with bar index
            Print("All bars ticks are saved on _barTicks");
        }
    }
}

 

 


@stuart