Tick TimeStamp Backtesting Indicator

Created at 10 Apr 2022, 18:17
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!
XA

Xammo

Joined 23.06.2021

Tick TimeStamp Backtesting Indicator
10 Apr 2022, 18:17


Have seen all the other posts about this and the recommendation is to use Server.Time to get the tick time when backtesting but this is not accurate and totally fails if the backtest is sped up more than 1x

Is there any way to get the actual tick timestamp when backtesting as is possible when using the indicator live?

Following abbreviated code/approach works live

public class MarketDataSample : Indicator
    {
        private Ticks ticks;
       
        protected override void Initialize()
        {
            ticks = MarketData.GetTicks(SymbolName);
            ticks.Tick += Event_Tick;
        }
        public override void Calculate(int index)
        {
        }
        public void Event_Tick(TicksTickEventArgs Args)
        {
             var tick_LastValue_Time = Args.Ticks.LastTick.Time;
        }
    }
}

but when backtesting the .LastTick is the tick at the very end of the chart (the bit that exists 'in time' but can't be backtested) and getting the total tick count var total_Ticks = Args.Ticks.Count and using Args.Ticks.Last(total_Ticks).Time or  Args.Ticks.Last(0).Time just gets the first and last tick in that unbacktestable series... can't find how to get the actual tick that just occurred in backtesting...

Any ideas/other approaches how could make this work please?

Thanks


@Xammo
Replies

amusleh
11 Apr 2022, 12:33

Hi,

We are aware of this issue and it's fixed on cTrader 4.2.

But it works if you use ticks inside cBot itself.

Here is an example:

using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        private Ticks _ticks;

        protected override void OnStart()
        {
            // Getting a symbol ticks data
            _ticks = MarketData.GetTicks(SymbolName);
            // Subscribing to upcoming ticks
            _ticks.Tick += Ticks_Tick;
        }

        private void Ticks_Tick(TicksTickEventArgs obj)
        {
            // Printing Last tick inside Ticks collection
            Print("Bid: {0} | Ask: {1} | Time: {2:dd/MM/yyyy HH:mm:ss}", obj.Ticks.LastTick.Bid, obj.Ticks.LastTick.Ask, obj.Ticks.LastTick.Time);
        }
    }
}

Result:

11/04/2022 09:24:02.489 | Reloaded
11/04/2022 09:23:57.208 | Reloaded
11/04/2022 09:23:53.693 | Reloaded
29/03/2022 23:05:20.667 | Backtesting was stopped
29/03/2022 23:05:20.667 | Bid: 1.10928 | Ask: 1.10928 | Time: 29/03/2022 23:05:20
29/03/2022 23:05:20.412 | Bid: 1.10928 | Ask: 1.1093 | Time: 29/03/2022 23:05:20
29/03/2022 23:05:18.068 | Bid: 1.10929 | Ask: 1.1093 | Time: 29/03/2022 23:05:18
29/03/2022 23:05:14.467 | Bid: 1.10929 | Ask: 1.10931 | Time: 29/03/2022 23:05:14
29/03/2022 23:05:14.263 | Bid: 1.10929 | Ask: 1.1093 | Time: 29/03/2022 23:05:14
29/03/2022 23:05:10.745 | Bid: 1.10927 | Ask: 1.10929 | Time: 29/03/2022 23:05:10
29/03/2022 23:05:10.503 | Bid: 1.10926 | Ask: 1.10929 | Time: 29/03/2022 23:05:10
29/03/2022 23:05:07.679 | Bid: 1.10927 | Ask: 1.10929 | Time: 29/03/2022 23:05:07
29/03/2022 23:05:07.477 | Bid: 1.10927 | Ask: 1.10928 | Time: 29/03/2022 23:05:07
29/03/2022 23:05:04.379 | Bid: 1.10927 | Ask: 1.10929 | Time: 29/03/2022 23:05:04
29/03/2022 23:05:04.178 | Bid: 1.10931 | Ask: 1.10933 | Time: 29/03/2022 23:05:04
29/03/2022 23:05:02.903 | Bid: 1.10935 | Ask: 1.10936 | Time: 29/03/2022 23:05:02
29/03/2022 23:04:54.160 | Bid: 1.10935 | Ask: 1.10935 | Time: 29/03/2022 23:04:54
29/03/2022 23:04:53.863 | Bid: 1.10934 | Ask: 1.10934 | Time: 29/03/2022 23:04:53
29/03/2022 23:04:53.655 | Bid: 1.10933 | Ask: 1.10933 | Time: 29/03/2022 23:04:53
29/03/2022 23:04:52.772 | Bid: 1.10931 | Ask: 1.10932 | Time: 29/03/2022 23:04:52
29/03/2022 23:04:49.771 | Bid: 1.10931 | Ask: 1.10931 | Time: 29/03/2022 23:04:49
29/03/2022 23:04:49.270 | Bid: 1.1093 | Ask: 1.1093 | Time: 29/03/2022 23:04:49
29/03/2022 23:04:48.812 | Bid: 1.10931 | Ask: 1.10931 | Time: 29/03/2022 23:04:48
29/03/2022 23:04:48.610 | Bid: 1.10932 | Ask: 1.10932 | Time: 29/03/2022 23:04:48
29/03/2022 23:04:43.949 | Bid: 1.10933 | Ask: 1.10933 | Time: 29/03/2022 23:04:43
29/03/2022 23:04:43.684 | Bid: 1.10934 | Ask: 1.10934 | Time: 29/03/2022 23:04:43
29/03/2022 23:04:43.480 | Bid: 1.10936 | Ask: 1.10936 | Time: 29/03/2022 23:04:43
29/03/2022 23:04:36.142 | Bid: 1.10938 | Ask: 1.10938 | Time: 29/03/2022 23:04:36
29/03/2022 23:04:35.273 | Bid: 1.10938 | Ask: 1.10939 | Time: 29/03/2022 23:04:35
29/03/2022 23:04:25.332 | Bid: 1.10938 | Ask: 1.10938 | Time: 29/03/2022 23:04:25
29/03/2022 23:04:24.781 | Bid: 1.10938 | Ask: 1.10939 | Time: 29/03/2022 23:04:24
29/03/2022 23:04:23.446 | Bid: 1.10938 | Ask: 1.10938 | Time: 29/03/2022 23:04:23
29/03/2022 23:04:22.167 | Bid: 1.10939 | Ask: 1.1094 | Time: 29/03/2022 23:04:22
29/03/2022 23:04:21.958 | Bid: 1.10939 | Ask: 1.10939 | Time: 29/03/2022 23:04:21
29/03/2022 23:04:21.558 | Bid: 1.10939 | Ask: 1.1094 | Time: 29/03/2022 23:04:21

 

 


@amusleh

Xammo
11 Apr 2022, 14:13

RE:

Great ok that's good to know and yeh I hadn't tried it in the cBot itself (ran out of steam trying from the indicator and decided to post on here) - will give it a go/wait for 4.2 many thanks

Seemed like it should have been in the Bars. collection which gets triggered in the Calculate method every tick already (IsLastBar) like the Tick.Volume already available ----->

on public override void Calculate(int index)
{
     double tick_Volume_LastValue = Bars.TickVolumes.LastValue;

     DateTime tick_Time_LastValue = Bars.TickTimes.LastValue; //<-------- would be good
}

 


@Xammo