Why are my GetTicks not synchrone with the Bars

Created at 02 Jul 2020, 12:16
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!
SH

Shares4us

Joined 01.04.2020

Why are my GetTicks not synchrone with the Bars
02 Jul 2020, 12:16


I got a bit stuck here, probably something very obvious but:
Why are the bid prices not always within the Bar High-Low range./Why am i not retreiving the right date

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TestIndicator : Indicator
    {

        private readonly string Version = "1.0";
        ///////////////////////////////////////
        // Test
        ///////////////////////////////////////
        // Changed 1.0 : 
        //  02-07-2020 initial setup 
        ///////////////////////////////////////
        //
        #region class Globals
        Ticks SymbolTicks;// Symbol SymbolPrices;

        int IndexSkip = 0;
        int  LastIndexSearched;        
        #endregion class Globals

        #region Events
        protected override void Initialize()
        {
            LastIndexSearched = -1;
        
            SymbolTicks = MarketData.GetTicks(SymbolName);
        
            Print(this.ToString() + ":" + Version);          
        }

 
       public override void Calculate(int index)
        {
            if(index!=IndexSkip) return;
            IndexSkip = index + 10;
            
            DateTime UsedTime = Bars.OpenTimes.LastValue;
            double TimedBid = FillBid(UsedTime);
            Chart.DrawText(this.ToString() + index, "' " + index+"\n" + TimedBid + "\n" + FilledTime + "\n" + UsedTime, UsedTime, TimedBid, Color.Yellow);
        }
        #endregion Events

        #region Private Methods
        DateTime FilledTime;
        double FillBid(DateTime dt)
        {
            for (int i = LastIndexSearched + 1; i < SymbolTicks.Count; i++)
            {
                if (SymbolTicks[i].Time >= dt)
                {
                    LastIndexSearched = i;
                    FilledTime=SymbolTicks[i].Time;
                    return SymbolTicks[i].Bid;
                }
            }
           return double.NaN;
        }
        #endregion Private Methods
    }
}

Anyone a Clue?


@Shares4us
Replies

Shares4us
02 Jul 2020, 12:28

RE: Solved it

solved it loaded extra Ticks worked

          int Loaded=SymbolTicks.Count;
            while(SymbolTicks[0].Time > Bars.Last(50).OpenTime && Loaded>0)
                Loaded = SymbolTicks.LoadMoreHistory();

So again:
Extracting the problem from the source and focussing to ask the right question often solves the problem ;-)


@Shares4us

PanagiotisCharalampous
02 Jul 2020, 12:36

Hi ctid956028,

The mistake in your code is here

for (int i = LastIndexSearched + 1; i < SymbolTicks.Count; i++)
            {

You seem to assume that there is a correlation between the bar index and the ticks index but there isn't. The correct should be the following

 for (int i = 0; i < SymbolTicks.Count; i++)
            {

You also need to make sure that you have tick data that goes back to the first loaded bar and beyond. So in the OnStart() method, you need to add the following code 

            while (SymbolTicks[0].Time > Bars.OpenTimes.First())
            {
                SymbolTicks.LoadMoreHistory();
            }

Here is the complete working code

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using System.Linq;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TestIndicator : Indicator
    {

        private readonly string Version = "1.0";
        ///////////////////////////////////////
        // Test
        ///////////////////////////////////////
        // Changed 1.0 : 
        //  02-07-2020 initial setup 
        ///////////////////////////////////////
        //
        #region class Globals
        Ticks SymbolTicks;
        // Symbol SymbolPrices;
        int IndexSkip = 0;
        int LastIndexSearched;
        #endregion class Globals

        #region Events
        protected override void Initialize()
        {
            LastIndexSearched = -1;

            SymbolTicks = MarketData.GetTicks(SymbolName);
            while (SymbolTicks[0].Time > Bars.OpenTimes.First())
            {
                SymbolTicks.LoadMoreHistory();
            }

            Print(this.ToString() + ":" + Version);
        }


        public override void Calculate(int index)
        {
            if (index != IndexSkip)
                return;
            IndexSkip = index + 10;

            DateTime UsedTime = Bars.OpenTimes.LastValue;
            double TimedBid = FillBid(UsedTime);
            Chart.DrawText(index.ToString(), this.ToString() + index + "' " + index + "\n" + TimedBid + "\n" + UsedTime, UsedTime, TimedBid, Color.Yellow);
        }
        #endregion Events

        #region Private Methods
        double FillBid(DateTime dt)
        {
            for (int i = 0; i < SymbolTicks.Count; i++)
            {
                if (SymbolTicks[i].Time >= dt)
                {
                    LastIndexSearched = i;
                    return SymbolTicks[i].Bid;
                }
            }
            return double.NaN;
        }
        #endregion Private Methods
    }
}

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

Shares4us
02 Jul 2020, 13:00

RE:

Thanks for the input, As it looks our solutions are almost similar (loading history)

On the correlation:
 LastIndexSearched is filled with the ticks index 
 I use it to eliminate loops that are in the past (speeding up!)
  As my understanding is that the index from the ticks counts sequentially from 0 to count-1;

 


@Shares4us

GridSurfer
03 Jul 2020, 09:38

Thank you for the example.

I was looking everywhere to find some/any expample on how to use the Tick history.
Maybe I am missing something, but it would be great if there were short examples here or there in the API reference on how to use the function, event or interface.


@GridSurfer