BarOpened Event occasionally fires randomly

Created at 24 May 2022, 12:32
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!
CO

couscousmckoy

Joined 07.02.2018

BarOpened Event occasionally fires randomly
24 May 2022, 12:32


Morning - 

I have a cBot that relies on the BarOpened event firing once (and only once!) as each bar is closed and a new one opened.  Although this is true about 95% of the time, it can not be relied on as it occasionally fires unpredictably midway through the life of that candle.  I setup a test to see how bad the problem was.  20 pairs on 1hr timeframe logging when the BarOpened event fires.  I ran it for approximately 20 hrs continuously and of the 434 log records, 16 were erroneous (i.e. event fired for a second time between 10 seconds and 58 minutes after the original (correct) bar open event was logged).  This doesn't seem to be the same as the BarOpened event firing on every tick problem that was reported last year, but it is annoying and means I am forced to store and check a 'bar last opened' variable to double check that the BarOpened event is legitimate. Please let me know if I'm misunderstanding anything here.

Thanks

Erroneous results (logs for correct BarOpened events removed):

Test code:

using System;
using System.Linq;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class MisfiringOnBarOpen : Robot
    {

        protected override void OnStart()
        {
            Bars bars1 = MarketData.GetBars(TimeFrame.Hour, "AUDUSD");
            Bars bars2 = MarketData.GetBars(TimeFrame.Hour, "USDJPY");
            Bars bars3 = MarketData.GetBars(TimeFrame.Hour, "USDCAD");
            Bars bars4 = MarketData.GetBars(TimeFrame.Hour, "NZDUSD");
            Bars bars5 = MarketData.GetBars(TimeFrame.Hour, "EURUSD");
            Bars bars6 = MarketData.GetBars(TimeFrame.Hour, "GBPUSD");
            Bars bars7 = MarketData.GetBars(TimeFrame.Hour, "EURGBP");
            Bars bars8 = MarketData.GetBars(TimeFrame.Hour, "AUDJPY");
            Bars bars9 = MarketData.GetBars(TimeFrame.Hour, "GBPJPY");
            Bars bars10 = MarketData.GetBars(TimeFrame.Hour, "EURAUD");
            Bars bars11 = MarketData.GetBars(TimeFrame.Hour, "XAUUSD");
            Bars bars12 = MarketData.GetBars(TimeFrame.Hour, "UK100");
            Bars bars13 = MarketData.GetBars(TimeFrame.Hour, "US2000");
            Bars bars14 = MarketData.GetBars(TimeFrame.Hour, "USDCHF");
            Bars bars15 = MarketData.GetBars(TimeFrame.Hour, "EURJPY");
            Bars bars16 = MarketData.GetBars(TimeFrame.Hour, "EURCHF");
            Bars bars17 = MarketData.GetBars(TimeFrame.Hour, "CADJPY");
            Bars bars18 = MarketData.GetBars(TimeFrame.Hour, "US500");
            Bars bars19 = MarketData.GetBars(TimeFrame.Hour, "US30");
            Bars bars20 = MarketData.GetBars(TimeFrame.Hour, "SpotCrude");

            bars1.BarOpened += Bars_BarOpened;
            bars2.BarOpened += Bars_BarOpened;
            bars3.BarOpened += Bars_BarOpened;
            bars4.BarOpened += Bars_BarOpened;
            bars5.BarOpened += Bars_BarOpened;
            bars6.BarOpened += Bars_BarOpened;
            bars7.BarOpened += Bars_BarOpened;
            bars8.BarOpened += Bars_BarOpened;
            bars9.BarOpened += Bars_BarOpened;
            bars10.BarOpened += Bars_BarOpened;
            bars11.BarOpened += Bars_BarOpened;
            bars12.BarOpened += Bars_BarOpened;
            bars13.BarOpened += Bars_BarOpened;
            bars14.BarOpened += Bars_BarOpened;
            bars15.BarOpened += Bars_BarOpened;
            bars16.BarOpened += Bars_BarOpened;
            bars17.BarOpened += Bars_BarOpened;
            bars18.BarOpened += Bars_BarOpened;
            bars19.BarOpened += Bars_BarOpened;
            bars20.BarOpened += Bars_BarOpened;
        }

        private void Bars_BarOpened(BarOpenedEventArgs obj)
        {
            Print("1Hr BarOpened Event Fired for: {0} - candle index: {1} - candle opentime: {2}", obj.Bars.SymbolName, obj.Bars.Count, obj.Bars.LastBar.OpenTime.ToLocalTime());
        }

    }
}


@couscousmckoy
Replies

amusleh
25 May 2022, 09:03

Hi,

Based on your posted screenshot I don't see any duplicate event trigger for any of the symbols, am I missing something?


@amusleh

couscousmckoy
25 May 2022, 10:13

RE:

Hi amusleh

Thanks for the prompt response. The entire data set was 434 records long for my test so I removed all the valid BarOpened event logs (even those where there was subsequently a later refire of the same bar).  I've still removed the records that were correct with no subsequent erroneous refire, but here are those records with the original correct event logged plus the subsequent erroneous entry. Look at the log entry times for each pair.

Thanks


@couscousmckoy

amusleh
26 May 2022, 09:29 ( Updated at: 21 Dec 2023, 09:22 )

RE: RE:

couscousmckoy said:

Hi amusleh

Thanks for the prompt response. The entire data set was 434 records long for my test so I removed all the valid BarOpened event logs (even those where there was subsequently a later refire of the same bar).  I've still removed the records that were correct with no subsequent erroneous refire, but here are those records with the original correct event logged plus the subsequent erroneous entry. Look at the log entry times for each pair.

Thanks

Hi,

Can you tell me on which version of cTrader you tested this? and was it back test data or live data?


@amusleh

couscousmckoy
26 May 2022, 10:03

RE: RE: RE:

Hi

Ctrader version:  4.1.19.50832

Live Data

Thanks

 

 


@couscousmckoy