The Command Line can reload price data. But how can we programmatically deal with data gaps caused by an internet connection loss?

Real-time data gaps in MultiCharts .NET price charts

The .rld Command Line command reloads chart price data (MultiCharts Wiki, 2014a) and is executed programmatically with the ChartCommands.CommandLine() method (PowerLanguage .NET Help, n.d.). For more on .rld, see reloading price data with the Command Line.

Data gaps caused by an internet or data feed connection loss are dealt with as follows in MultiCharts .NET (MultiCharts Wiki, 2014b):

  • Tick-based charts have their gap backfilled when the connection is restored and real-time data is received again. These tick-based charts are tick, contract, point (range), change, second, range, Point & Figure, and tick-based Renko charts (MultiCharts Wiki, 2014c).
  • Time-based charts (minute and hour resolutions) have their gap backfilled the next time the data series is loaded (for example when creating a new chart or opening the workspace).

We can, however, programmatically backfill time-based charts immediately after a data gap has occurred.

Examining MultiCharts .NET data gaps during real-time trading

Before discussing the code, let’s see what it does. With a data gap on a time-based chart, like this EUR/USD chart:

Reloading a MultiCharts .NET data gap - before

The chart is reloaded programmatically with data from the data feed to fill the gap:

Data gap in MultiCharts .NET filled

By the way, if you cannot see empty time periods: right-click on the chart, select ‘Format Window’ and click on the ‘X - Time Scale’ tab. Then select ‘Show Empty Periods’:

Empty time periods in MultiCharts .NET

Reloading a price data gap in MultiCharts .NET programmatically

One way to detect data gaps is the following:

protected override void CalcBar()
{
    if (!Bars.LastBarOnChart || Bars.Status != EBarState.Close)
        return;

    TimeSpan timeDiff = Bars.Time[0] - Bars.Time[1];

    if (timeDiff.TotalMinutes > Bars.Info.Resolution.Size &&
        Bars.Time[0].TimeOfDay != 
            Bars.Sessions[0].StartTime.Add(new TimeSpan(0, 1, 0)))
    {
        Output.WriteLine("Time difference with previous bar: {0} minutes.",
            timeDiff.TotalMinutes);

        ChartCommands.CommandLine(".rld int=1day");
    }
}

//> Time difference with previous bar: 6 minutes.

The first if statement executes return to exit the CalcBar() method when the current bar is not (!) the last bar (Bars.LastBarOnChart) or when the script isn’t calculated on bar close (EBarState.Close). The first expression is needed to only deal with recent data gaps, while the second ensures the time difference is calculated correctly.

This time difference is calculated by subtracting the time of the previous bar (Bars.Time[1]) from the current (Bars.Time[0]). When a data gap happens, Bars.Time[1] is the time of the bar before the gap, while Bars.Time[0] is the time of the bar following the data gap (see the first image above). The result of this subtraction is stored in a TimeSpan, which represents an interval of time (Dorman, 2010).

Detecting a data gap in MultiCharts .NET and reloading data

Next, an if statement evaluates two conditions to determine the existence of a data gap. First, the time interval’s length in minutes (timeDiff.TotalMinutes) needs to be larger than the resolution size (which is 1 minute with the chart above). This latter value is returned by Bars.Info.Resolution.Size (PowerLanguage .NET Help, n.d.).

Second, the time of the current bar (returned by DateTime’s TimeOfDay property; Microsoft Developer Network, n.d.) needs to be unequal to the session start time (returned by the StartTime property; PowerLanguage .NET Help, n.d.). This prevents a chart reload when there is a normal time gap between session open and close.

With the Add() method a 1 minute time interval is added to the session start time. This is needed because the closing time of the session’s first bar is the session start time plus the bar’s time duration.

Note that Bars.Sessions[0] provides access to the first session of the week, which works for EUR/USD because each day has the same start time. For symbols with multiple different sessions, the code needs to be adjusted accordingly.

When both if statement expressions are true, the time difference is outputted to the Output Window and the ChartCommands.CommandLine() method is called with the .rld command to reload price data programmatically. An implicit assumption here is that the data feed contains the price data needed to fill the gap. When that isn’t the case, reloading the chart won’t fill the gap.

Summary

The .rld Command Line command reloads a chart’s price data and is programmatically executed with ChartCommands.CommandLine(). By comparing the time difference between the current and previous bar on a time-based chart, a data gap can be determined.

Complete MultiCharts .NET indicator example

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;

namespace PowerLanguage.Indicator
{
    [SameAsSymbol(true)]
    public class Example_ReloadDataGaps : IndicatorObject
    {
        public Example_ReloadDataGaps(object _ctx) : base(_ctx) { }

        protected override void CalcBar()
        {
            if (!Bars.LastBarOnChart || Bars.Status != EBarState.Close)
                return;

            TimeSpan timeDiff = Bars.Time[0] - Bars.Time[1];

            if (timeDiff.TotalMinutes > Bars.Info.Resolution.Size &&
                Bars.Time[0].TimeOfDay != 
                    Bars.Sessions[0].StartTime.Add(new TimeSpan(0, 1, 0)))
            {
                Output.WriteLine("Time difference with previous bar: {0} minutes.",
                    timeDiff.TotalMinutes);

                ChartCommands.CommandLine(".rld int=1day");
            }
        }

        //> Time difference with previous bar: 6 minutes.
    }
}

References

Dorman, S. (2010). Sams Teach Yourself Visual C# 2010 in 24 Hours. Indianapolis, IN (USA): Sams/Pearson Education.

Microsoft Developer Network (n.d.). DateTime.TimeOfDay Property. Retrieved on December 17, 2014, from http://msdn.microsoft.com/en-us/library/system.datetime.timeofday%28v=vs.110%29.aspx

MultiCharts Wiki (2014a, August 18). MultiCharts Work Area: Understanding Command Line. Retrieved on December 16, 2014, from http://www.multicharts.com/trading-software/index.php/MultiCharts_Work_Area#Understanding_Command_Line

MultiCharts Wiki (2014b, May 22). Auto data restore. Retrieved on December 17, 2014, from http://www.multicharts.com/trading-software/index.php/Auto_data_restore

MultiCharts Wiki (2014c, June 11). Tick, Minute and Daily Data. Retrieved on December 17, 2014, from http://www.multicharts.com/trading-software/index.php/Tick,_Minute_and_Daily_Data

PowerLanguage .NET Help (n.d.). Retrieved on November 18, 2014, from http://www.multicharts.com/downloads/PowerLanguage.NET.chm