Situation
You want to force a recalculation of your strategy or indicator after a certain time period.

Programming example

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

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

        protected override void StartCalc()
        {
            Output.Clear();
        }

        protected override void CalcBar()
        {
            if (Bars.LastBarOnChart)
            {
                Output.WriteLine("Computer time: {0}  -  Bar time: {1}; BarVolume: {2}",
                    DateTime.Now.ToString("dd-MM-yy HH:mm:ss"),
                    Bars.TimeValue.ToString("dd-MM-yy HH:mm:ss"),
                    Bars.TrueVolume().Value);

                // Call the recalculation every 10 seconds
                ExecControl.RecalcLastBarAfter(new TimeSpan(0, 0, 10));
            }
        }

        // Implementation of the recalculation
        protected override void OnRecalcLastBarAfterEvent()
        {
            this.CalcBar();
        }
    }
}

Output of the example

The output will be something like the following:

Computer time: 20-10-13 14:59:51  -  Bar time: 18-10-13 17:30:00; BarVolume: 49684
Computer time: 20-10-13 15:00:01  -  Bar time: 18-10-13 17:30:00; BarVolume: 49684
Computer time: 20-10-13 15:00:11  -  Bar time: 18-10-13 17:30:00; BarVolume: 49684
Computer time: 20-10-13 15:00:21  -  Bar time: 18-10-13 17:30:00; BarVolume: 49684
Computer time: 20-10-13 15:00:31  -  Bar time: 18-10-13 17:30:00; BarVolume: 49684
Computer time: 20-10-13 15:00:41  -  Bar time: 18-10-13 17:30:00; BarVolume: 49684
Computer time: 20-10-13 15:00:51  -  Bar time: 18-10-13 17:30:00; BarVolume: 49684
Computer time: 20-10-13 15:01:01  -  Bar time: 18-10-13 17:30:00; BarVolume: 49684
Computer time: 20-10-13 15:01:11  -  Bar time: 18-10-13 17:30:00; BarVolume: 49684
Computer time: 20-10-13 15:01:21  -  Bar time: 18-10-13 17:30:00; BarVolume: 49684
Computer time: 20-10-13 15:01:32  -  Bar time: 18-10-13 17:30:00; BarVolume: 49684
Computer time: 20-10-13 15:01:42  -  Bar time: 18-10-13 17:30:00; BarVolume: 49684
Computer time: 20-10-13 15:01:52  -  Bar time: 18-10-13 17:30:00; BarVolume: 49684
Computer time: 20-10-13 15:02:02  -  Bar time: 18-10-13 17:30:00; BarVolume: 49684
Computer time: 20-10-13 15:02:12  -  Bar time: 18-10-13 17:30:00; BarVolume: 49684
Computer time: 20-10-13 15:02:22  -  Bar time: 18-10-13 17:30:00; BarVolume: 49684

MultiCharts .NET and script calculations

Indicators and strategies in MultiCharts .NET are recalculated with every incoming tick. During periods were only a few ticks occur forcing a recalculation of (parts of) your indicator or strategy is useful. This can for example be used with submitting an exit order prior to the close to ensure the strategy does not take a position overnight.

The above example has two code segments related to a recalculation. The first is in the if statement code block:

if (Bars.LastBarOnChart)
{
    Output.WriteLine("Computer time: {0}  -  Bar time: {1}; BarVolume: {2}",
        DateTime.Now.ToString("dd-MM-yy HH:mm:ss"),
        Bars.TimeValue.ToString("dd-MM-yy HH:mm:ss"),
        Bars.TrueVolume().Value);

    // Call the recalculation every 10 seconds
    ExecControl.RecalcLastBarAfter(new TimeSpan(0, 0, 10));
}

If the current bar is the last bar on the chart (in which case the Bars.LastBarOnChart property returns true) some information to keep track of the recalculation is written to the PowerLanguage Editor output tab. Next, the RecalcLastBarAfter() method is called. This method takes a TimeSpan structure as an input.

In this example we passed the three parameter overloaded TimeSpan version with a time interval of 0 hours, 0 minutes, and 10 seconds (i.e. TimeSpan(0, 0, 10)) to the RecalcLastBarAfter() method.

An alternative of using new TimeSpan(0, 0, 10) in the code example above is to use TimeSpan.FromSeconds(10). Both will give the same result, namely a time range of 10 seconds.

Implementing the actual recalculation in MultiCharts .NET

Keep in mind that the RecalcLastBarAfter() method does not perform the recalculation but calls the OnRecalcLastBarAfterEvent() method (Fabrice, 2013). We therefore added the following code in lines 32-36:

// Implementation of the recalculation
protected override void OnRecalcLastBarAfterEvent()
{
    this.CalcBar();
}

In the OnRecalcLastBarAfterEvent() method two different types of recalculation can be implemented. First, all methods of the strategy or indicator can be recalculated (including the ones that are only executed once per calculation, such as StartCalc()). In that case we call ExecControl.Recalculate() in the OnRecalcLastBarAfterEvent() method.

Second, a specific method can be recalculated, such as we have done with the CalcBar() override method in the above example. In this case only a recalculation of the last bar is performed.

The this keyword, optional in this example, refers to the current instance of an object (Liberty & MacDonald, 2008) and is used here to explicitly define that the CalcBar() method of the Example_Recalculate class needs to be called.

This example shows the recalculation of an indicator, but the recalculation of a strategy is done in exactly the same way.

References

Fabrice (2013). How to use RecalcLastBarAfter - forum discussion. Retrieved from http://www.multicharts.com/discussion/viewtopic.php?f=19&t=43339#p96376

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

Visit Kodify.net for more helpful coding articles.