Trend lines, arrows, and text boxes in MultiCharts .NET all have their location set with ChartPoint structures. But what are these ‘structures’ and how do we use them?

What are structures in C#?

A C# structure, struct for short, holds properties, methods, fields (that is, variables), and other members together in a ‘package’ so that they can be stored and manipulated as a group (Liberty & MacDonald, 2009; Stephens, 2014). Because of that structs are primarily used to hold related information (see Sharp, 2013; Stellman & Greene, 2010) as opposed to performing complex actions, for which classes are better suited.

Because structs are value types (Albahari & Albahari, 2012; Stephens, 2014), one ChartPoint struct can be copied to another with the assignment operator (=). That would create a fresh copy, with each struct variable occupying its own memory space, independent from the variable it was copied from (Stellman & Greene, 2010; Sharp, 2013). And so ChartPoint struct variables can be used just like variables of C#’s predefined data types (like int and double).

The ChartPoint structure in MultiCharts .NET

ChartPoint structs are used in MultiCharts .NET to define a point on the chart (PowerLanguage .NET Help, n.d.). They hold both the time (x-axis) and price (y-axis) coordinates of a chart location.

These ChartPoint chart coordinates are used in indicators, strategies, and functions for the placements of trend lines, arrows, and text boxes on the chart (MultiCharts, 2014; PowerLanguage .NET Help, n.d.). The MouseClickArgs’s point field, accessible in the OnMouseEvent() method after a click on the chart, also uses a ChartPoint to specify the mouse click location (see PowerLanguage .NET Help, n.d.).

A ChartPoint variable’s coordinates can apply to a chart’s symbol (like with drawing trend lines on price data) or they can be based on indicator price values (see creating trend lines on indicator values, for example).

How to create a ChartPoint struct variable?

Struct variables are created by defining their type (like ChartPoint), providing an identifier (that is, a for that context unique name like currentBarHigh) and, to initialise the struct, the new keyword followed by calling the struct’s constructor (Sharp, 2013). So, a chart coordinate of the current bar’s high is created as follows:

ChartPoint currentBarHigh = new ChartPoint(Bars.Time[0], Bars.High[0]);

Here we make a ChartPoint struct variable named currentBarHigh that’s set to the time (x-axis) and high (y-axis) coordinate of the current bar (Bars.Time[0] and Bars.High[0]).

ChartPoint variables can be made in three ways (PowerLanguage .NET Help, n.d.):

  • By setting the first argument to a DateTime value and the second to a double (the default approach).
  • Or by only specifying a double price coordinate. With the time coordinate missing, the ChartPoint struct’s DateTime value is set to the equivalent of zero.
  • Or by providing a double variable for the price coordinate and an integer for the bar number, which will act as the time coordinate.

The examples below show these three approaches.

Creating a ChartPoint with a time and price coordinate

A ChartPoint struct variable is made with a DateTime and double value as follows:

ChartPoint timePricePoint = new ChartPoint(Bars.Time[5], Bars.High[5]);

ChartPoint timeAndPrice = new ChartPoint(DateTime.Now, 2089.98);

The first ChartPoint variable created here is set to Bars.Time[5] (which returns the DateTime value of five bars ago) and Bars.High[5] (a double value with the high from five bars ago).

ChartPoint variables are typically based on price bar data. But any valid DateTime and double value can be used, as we see with the timeAndPrice struct variable that’s set to the current computer time (DateTime.Now) and an arbitrary value (2089.98).

Creating a chart point based on only a price

A ChartPoint struct variable can also be created with just a price coordinate, like so:

ChartPoint pricePoint = new ChartPoint(Bars.Close[0]);

ChartPoint priceAverage = new ChartPoint(Bars.Close.Average(10));

Here the pricePoint struct variable is set to the current bar close (Bars.Close[0]). The priceAverage chart location is set to the average close of the last 10 bars, calculated here with the Average() extension method.

Defining a ChartPoint variable with only a price point sets the DateTime time coordinate to the equivalent of zero (see example further below). And so such variables will need to have their Time or BarNumber properties set to another value before the ChartPoint struct is useful. The example further down below shows how to do that.

Creating a ChartPoint with a bar number and price value

The third way to create a ChartPoint variable is with a bar number and price. For instance:

ChartPoint barAndPrice = new ChartPoint(
    Bars.CurrentBarAbsolute(), Bars.Low[0]);

ChartPoint futureBar = new ChartPoint(
    Bars.CurrentBar + 10, Bars.Close[0] * 1.05);

The first statement creates a barAndPrice struct set to the previous bar with Bars.CurrentBarAbsolute(), a method that returns the zero-based bar number (so 1 less than the current bar). This ChartPoint has its price coordinate set to the low of the current bar (Bars.Low[0]).

The futureBar struct variable defines a future chart point by adding 10 to the current bar (Bars.CurrentBar) and five percent to the current bar’s closing price (Bars.Close[0]).

Tip: When drawing a trend line both ChartPoint structs need to hold either DateTime values or bar numbers. A ChartPoint based on DateTime cannot be used together with one that holds a bar number as its time coordinate (MultiCharts Support, personal communication, March 25, 2015).

Retrieving data from a ChartPoint struct in MultiCharts .NET

Once we’ve made a ChartPoint struct variable, its BarNumber, Time, and Price properties can get or set its data (PowerLanguage .NET Help, n.d.). For example:

ChartPoint priceCoordinate = new ChartPoint(Bars.Close[5]);

Output.WriteLine("ChartPoint's Price: {0}", 
    priceCoordinate.Price);

Output.WriteLine("Time: {0} and bar number: {1}",
    priceCoordinate.Time,
    priceCoordinate.BarNumber);

//> ChartPoint's Price: 0,78198
//> Time: 1-1-0001 0:00:00 and bar number:

Here we create a ChartPoint variable set to the close of 5 bars ago. We then output the struct’s properties to the PowerLanguage .NET Editor with Output.WriteLine().

Since we’ve only defined the location’s price coordinate, the Time property returns the default DateTime value (see Microsoft Developer Network, n.d.) while the BarNumber property returns null (which is not printed by Output.WriteLine()).

Changing a ChartPoint struct variable in MultiCharts .NET

After creating a ChartPoint struct variable, we can change its coordinates in two ways: by setting the BarNumber, Time, or Price properties to new values (see PowerLanguage .NET Help, n.d.) or by making a new ChartPoint struct and assigning that to the existing variable. The example below shows how to do both:

ChartPoint chartCoordinate = new ChartPoint();

chartCoordinate.Price = Bars.Close[5];
chartCoordinate.Time  = Bars.Time[5];

Output.WriteLine("Chart point's price: {0} and time: {1}",
    chartCoordinate.Price,
    chartCoordinate.Time);

//> Chart point's price: 0,78198 and time: 24-2-2015 20:35:00

chartCoordinate = new ChartPoint(Bars.Time[10], Bars.Low[10]);

Output.WriteLine("New coordinates: {0} and time: {1}",
    chartCoordinate.Price,
    chartCoordinate.Time);

//> New coordinates: 0,78 and time: 24-2-2015 18:05:00

The chartCoordinate struct variable is created here with a parameterless constructor, which causes all of its fields to be set to the equivalent of zero. To have it hold an actual location, it needs to be set to a time and price coordinate.

We do that by setting its Price and Time properties to values of 5 bars ago, and then output these coordinates with Output.WriteLine(). In the second part of the example we set the struct’s time and price coordinate in a different way by assigning chartCoordinate a new ChartPoint struct that’s set to the time and price of 10 bars ago.

How to copy a struct variable in MultiCharts .NET?

A struct variable can be copied into another struct variable with the assignment operator (=), but this requires that the struct variable on the right side of = is already initialised (Sharp, 2013). The example below shows what this means:

ChartPoint prevHigh = new ChartPoint(Bars.Time[7], Bars.High[7]);
ChartPoint copyOfPrevHigh = prevHigh;

ChartPoint prevLow;
ChartPoint copyOfPrevLow = prevLow;

Here the prevHigh struct is set to the time and high of 7 bars ago. The copyOfPrevHigh struct variable is then assigned the prevHigh ChartPoint variable. Both of these variables are independent from each other — changes made to one will not affect the other (see Sharp, 2013; Stephens, 2014).

In the second part of the example we declare the prevLow struct but not actually create it with C#’s new keyword. We then try to copy this uninitialised struct to another struct variable, which is not allowed (see Sharp, 2013) and so the following error is triggered in the PowerLanguage .NET Editor:

Example of ChartPoint struct error in MultiCharts .NET

For more examples of ChartPoint structs, see creating a trend line, adjusting a line’s location, drawing a line with mouse clicks, and using indicator values for trend lines.

Summary

C#’s structs are a value type that group related information together in a ‘package’. ChartPoint structs in MultiCharts .NET define a chart location (a price and time coordinate) and are primarily used with trend line, text, and arrow drawings. After making a ChartPoint struct variable, its BarNumber, Time, and Price properties can be used to change (or get) its chart coordinate data.

To learn more about C# programming, checkout Kodify's C# tutorials.


References

Albahari, J. & Albahari, B. (2012). C# 5.0 in a Nutshell: The Definitive Reference (5th edition). Sebastopol, CA: O’Reilly Media.

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

Microsoft Developer Network (n.d.). DateTime Structure. Retrieved on June 24, 2015, from https://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx

MultiCharts (2014). MultiCharts .NET Programming Guide (version 1.1). Retrieved from http://www.multicharts.com/downloads/MultiCharts.NET-ProgrammingGuide-v1.1.pdf

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

Sharp, J. (2013). Microsoft Visual C# 2013 Step by Step. Microsoft Press.

Stellman, A. & Greene, J. (2010). Head First C#: A Brain-Friendly Guide (2nd edition). Sebastopol, CA: O’Reilly Media.

Stephens, R. (2014). C# 5.0 Programmer Reference. Indianapolis, IN: John Wiley & Sons.

Visit programming tutorials for more helpful coding articles.