Situation
You want to know how to access price and time data of the current bar for the primary data series in MultiCharts .NET.

Programming example

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

namespace PowerLanguage.Indicator
{
    [SameAsSymbol(true)]
    public class Example_AccessCurrentPriceBar : IndicatorObject
    {
        bool calculatedOnce;

        public Example_AccessCurrentPriceBar(object _ctx) : base(_ctx) { }

        protected override void StartCalc()
        {
            calculatedOnce = false;
        }

        protected override void CalcBar()
        {
            if (Bars.LastBarOnChart && !calculatedOnce)
            {
                Output.WriteLine(Bars.Info.Name + " - Closing time of bar: " + 
                    Bars.TimeValue.ToString("d-M-yyyy HH:mm:ss"));

                Output.WriteLine("Open: {0}, High: {1}, Low: {2}, Close: {3}",
                    Bars.OpenValue,
                    Bars.HighValue,
                    Bars.LowValue,
                    Bars.CloseValue);
                
                calculatedOnce = true;
            }
        }
    }
}

Output of the programming example

When applied to the EuroStoxx 50 future, the indicator prints to the Output Window something like the following:

EX# - Closing time of bar: 16-5-2014 22:00:00
Open: 3157, High: 3163, Low: 3156, Close: 3162

And applied to the EUR/USD symbol, the output is like:

EUR/USD - Closing time of bar: 16-5-2014 23:00:00
Open: 1,36949, High: 1,36949, Low: 1,36927, Close: 1,36927

Accessing price data in MultiCharts .NET

Price data, visually represented with price bars, can be accessed in an indicator or trading strategy regardless of the MultiCharts .NET environment (meaning, a price chart, real-time market scanner, or backtest portfolio) we are working in (MultiCharts, 2013).

Programmatically accessing price bars of the primary data series is done with the Bars property (MultiCharts, 2013). Current bar data can be retrieved with this Bars property in the StartCalc(), CalcBar(), and StopCalc() override methods for price data (see MultiCharts, 2013), while referencing historical price bars with Bars is limited to the CalcBar() method.

Overview of the price and time data Bars properties

The Bars property provides access to the following price and time related data (MultiCharts, 2013; PowerLanguage .NET Help, n.d.):

Bars property Description
Bars.Open[0] or Bars.OpenValue Returns the opening price of the current bar.
Bars.High[0] or Bars.HighValue Returns the high price of the current bar.
Bars.Low[0] or Bars.LowValue Returns the low price of the current bar.
Bars.Close[0] or Bars.CloseValue Returns the closing price of the current bar.

When the current bar is updated in real-time, it returns the most recent price.
Bars.Time[0] or Bars.TimeValue Returns a DateTime value with the closing time of the current bar.
Bars.BarUpdateTime Returns the time (a DateTime value) of the last update of the current bar if the Bar Magnifier is on.

When the Bar Magnifier is off, it returns the time of the current bar (the equivalent of Bars.TimeValue).
Bars.LastBarTime Returns a DateTime value with the time of the last bar.

Let’s look at the example to see how price and time data can be accessed.

Programming example: attribute setting and variable declaration

The programming example begins with the SameAsSymbol attribute:

[SameAsSymbol(true)]

Setting the SameAsSymbol attribute to true plots the indicator on the main price chart instead of on a separate sub-chart.

Then a Boolean variable (calculatedOnce) is declared in line 11. This variable will be used to track if the indicator has already been calculated once.

StartCalc() for once every calculation cycle

Next in the programming example is the StartCalc() override method:

protected override void StartCalc()
{
    calculatedOnce = false;
}

The StartCalc() method is called once before each calculation (MultiCharts, 2013). In it the calculatedOnce variable is set to false. This resets the variable with each calculation cycle so that the information is outputted once with each calculation.

Outputting the price and time data

Then we arrive at the CalcBar() method:

protected override void CalcBar()
{
    if (Bars.LastBarOnChart && !calculatedOnce)
    {
        Output.WriteLine(Bars.Info.Name + " - Closing time of bar: " + 
            Bars.TimeValue.ToString("d-M-yyyy HH:mm:ss"));

        Output.WriteLine("Open: {0}, High: {1}, Low: {2}, Close: {3}",
            Bars.OpenValue,
            Bars.HighValue,
            Bars.LowValue,
            Bars.CloseValue);
        
        calculatedOnce = true;
    }
}

The if statement (line 22) verifies two conditions. First, if the current bar is the last bar of the data series (in which case Bars.LastBarOnChart will return true). Second, the calculatedOnce variable coupled with the logical not operator (!) needs to evaluate to false.

When both of those expressions are true, the body of the if statement (lines 24-33) is executed. This code block consists out of three parts.

First, in lines 24-25, the symbol (Bars.Info.Name) and the closing time (Bars.TimeValue) are outputted. This latter is formatted to a string so that the date and time are displayed in a readable format. With the use of the + operator, the parts are combined into a string that is passed into the Output.WriteLine() method to print the text to the Output Window.

Second, lines 27-31 output the open (Bars.OpenValue), high (Bars.HighValue), low (Bars.LowValue), and close (Bars.CloseValue) of the current bar.

The third and final part sets the calculatedOnce Boolean variable to true. This ensures that the data is outputted only once.

Key points:

  • The Bars property provides access to the price data of the primary data series;
  • Price data for the current bar can be retrieved with either the properties that end with “Value” (e.g., Bars.CloseValue) or by using an index of zero (e.g., Bars.Close[0]).
References

MultiCharts (2013). MultiCharts .NET Programming Guide (version 1.0). Retrieved from http://www.multicharts.com/downloads/MultiCharts.NET-ProgrammingGuide-v1.0.pdf

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