MultiCharts .NET indicators require less calculations with skipping identical ticks. But calculating an indicator only once per bar would be even more efficient. So, how can we set that programmatically?

The UpdateOnEveryTick attribute: only calculate an indicator on bar close

C# class attributes can be thought of as post-its notes (Gishu, 2010), modifying how the class operates (Liberty & MacDonald, 2009). In MultiCharts .NET, such attributes change a script’s settings or features.

One of these settings is ‘Update on every tick’, set programmatically with the UpdateOnEveryTick attribute (see PowerLanguage .NET Help, n.d.). With this option enabled, the indicator is calculated on the current bar with each incoming real-time tick; when disabled, the indicator is only calculated on the close of each bar (MultiCharts Wiki, 2012, 2014).

The ‘Update on every tick’ setting only affects real-time calculations: on historical data the indicator’s code is always executed once for each completed bar (MultiCharts Wiki, 2012, 2014). A complex indicator that’s suitable for calculating once per bar will therefore perform considerably better when this option is disabled on real-time data.

Setting the UpdateOnEveryTick attribute in MultiCharts .NET

To calculate an indicator only on bar close, set the attribute to false:

[UpdateOnEveryTick(false)]
public class Example_UpdateOnEveryTick : IndicatorObject
{
    // Indicator code
}

And set the attribute to true to calculate the script on every tick:

[UpdateOnEveryTick(true)]
public class Example_UpdateOnEveryTick : IndicatorObject
{
    // Indicator code
}

Indicators calculate on each real-time tick by default (MultiCharts Wiki, 2012), and this also applies when UpdateOnEveryTick is omitted from the code.

Example: UpdateOnEveryTick’s effect on script processing

How this attribute affects an indicator is illustrated with the example below:

[UpdateOnEveryTick(true), SameAsSymbol(true), SkipIdenticalTicks(false)]
public class Example_UpdateOnEveryTick : IndicatorObject
{
    public Example_UpdateOnEveryTick(object _ctx) : base(_ctx) { }

    protected override void CalcBar()
    {
        if (Environment.IsRealTimeCalc == false)
            return;

        if (Bars.Status == EBarState.Close)
        {
            Output.WriteLine("#{0} - {1} - Closing price.",
                Bars.CurrentBar,
                Bars.Close[0]);
        }
        else if (Bars.Status == EBarState.Inside)
        {
            Output.WriteLine("#{0} - {1} - Price inside the bar.",
              Bars.CurrentBar,
              Bars.Close[0]);
        }
        else if (Bars.Status == EBarState.Open)
        {
            Output.WriteLine("#{0} - {1} - The bar's open.",
                Bars.CurrentBar,
                Bars.Close[0]);
        }
    }
}

We start with applying three attributes: UpdateOnEveryTick set to true ensures that real-time intra-bar ticks are processed, SameAsSymbol displays the indicator on a data series, and SkipIdenticalTicks set to false prevents skipping identical ticks.

In the CalcBar() method an if statement evaluates the Environment.IsRealTimeCalc property, which returns true when the script’s calculations are based on real-time data (PowerLanguage .NET Help, n.d.). When there’s no real-time data, it returns false and the return keyword is executed to prematurely exit the CalcBar() method. That way the code below return is only processed when thee are real-time prices.

Then an if-else statement evaluates the bar status of the primary data series (Bars.Status). Three possible values for the current tick’s status are evaluated: a price update that’s the last of the bar (EBarState.Close), inside the bar (EBarState.Inside), or that’s the bar opening tick (EBarState.Open).

Each of those three potential Bars.Status values output information to the PowerLanguage .NET Editor that contains the current bar number (Bars.CurrentBar) and the bar’s current price that the script was calculated on (Bars.Close[0]).

Output of MultiCharts .NET’s UpdateOnEveryTick enabled or disabled

When the indicator has UpdateOnEveryTick set to false, the output is as follows:

//> #4570 - 93,639 - Closing price.
//> #4571 - 93,646 - Closing price.
//> #4572 - 93,652 - Closing price.
//> #4573 - 93,647 - Closing price.
//> #4574 - 93,649 - Closing price.
//> #4575 - 93,648 - Closing price.
//> #4576 - 93,659 - Closing price.
//> #4577 - 93,652 - Closing price.
//> #4578 - 93,647 - Closing price.
//> #4579 - 93,636 - Closing price.

And with the attribute set to true the following is outputted (edited for brevity):

//> #4551 - 93,643 - Price inside the bar.
//> #4551 - 93,643 - Price inside the bar.
//> #4551 - 93,643 - Price inside the bar.
//> #4551 - 93,643 - Closing price.
//> #4552 - 93,642 - The bar's open.
//> #4552 - 93,645 - Price inside the bar.
//> #4552 - 93,647 - Price inside the bar.
//> #4552 - 93,649 - Price inside the bar.
//> #4552 - 93,649 - Closing price.
//> #4553 - 93,652 - The bar's open.
//> #4553 - 93,651 - Price inside the bar.
//> #4553 - 93,649 - Price inside the bar.
//> #4553 - 93,649 - Price inside the bar.
//> #4553 - 93,650 - Closing price.
//> #4554 - 93,651 - The bar's open.
//> #4554 - 93,652 - Price inside the bar.
//> #4554 - 93,653 - Price inside the bar.
//> #4554 - 93,653 - Price inside the bar.
//> #4554 - 93,652 - Price inside the bar.
//> #4554 - 93,651 - Price inside the bar.
//> #4554 - 93,651 - Closing price.

The output shows that the indicator is calculated once per bar when the UpdateOnEveryTick attribute is set to false. When the attribute is set to true, opening and intra-bar ticks are also processed.

Setting ‘Update on every tick’ manually in the PowerLanguage .NET Editor

The ‘Update on every tick’ option can also be set manually in the PowerLanguage .NET Editor. To do so, right-click somewhere in the editor’s code window and select ‘Properties’:

MultiCharts .NET UpdateOnEveryTick - code editor

Then enable or disable the ‘Update on every tick’ setting in the ‘Indicator Properties’ window:

MultiCharts .NET UpdateOnEveryTick - enable or disable manually

Note that this is the same window in which we also set the ‘Same As Symbol’ and ‘Skip Identical Ticks’ options manually.

Specifying ‘Update on every tick’ manually in MultiCharts .NET

Once an indicator is added to the chart or Scanner/Watchlist, ‘Update on every tick’ can be changed for that specific instance of the indicator.

To do that, right-click on the indicator’s name in the Status Line and select ‘Format [script name]‘. Or, when using the Scanner/Watchlist, right-click in the indicator’s column and select ‘Format Study’.

Then go to the ‘Properties’ tab and enable or disable the ‘Update on every tick’ option:

MultiCharts .NET UpdateOnEveryTick - manual indicator

Other MultiCharts .NET attributes that can be used with indicators include enabling mouse click processing with MouseEvents and preventing intra-bar generated drawings from being removed with RecoverDrawings.

Summary

Indicators are updated on every real-time tick by default, a setting that can be changed with the UpdateOnEveryTick attribute. An indicator is only calculated on bar close when this attribute is set to false, which reduces the script’s CPU usage.


References

Gishu (2012, April 20). How do attribute classes work? StackOverflow discussion. Retrieved on January 26, 2015, from http://stackoverflow.com/a/2677354/531257

Liberty, J. & MacDonald, B. (2009). Learning C# 3.0: Master the Fundamentals of C# 3.0. Sebastopol, CA: O’Reilly Media.

MultiCharts Wiki (2012, February 23). How Scripts Work. Retrieved on January 26, 2015, from http://www.multicharts.com/trading-software/index.php/How_Scripts_Work

MultiCharts Wiki (2014, September 15). Indicator Settings: Update on Every Tick. Retrieved on January 26, 2015, from http://www.multicharts.com/trading-software/index.php/Indicator_Settings#Update_on_Every_Tick

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