When we create trading strategies, code indicators, or program functions in the PowerLanguage .NET Editor we occasionally program the same code over and over again. That code, however, can also be put into a DLL for easy code reuse and maintenance. But how do we use DLLs in the PowerLanguage .NET Editor?

The PowerLanguage .NET Editor and DLL references

We create and edit indicators, trading strategies, and functions primarily in the PowerLanguage .NET Editor (MultiCharts, 2014). It’s true that we can also code scripts in other programs and have MultiCharts .NET automatically compile the scripts for us (MultiCharts, 2014), but things like exporting, importing, and removing scripts are only possible in the PowerLanguage .NET Editor.

What we also need to use the PowerLanguage .NET Editor exclusively for is adding references to external assemblies (MultiCharts, 2014). An assembly is an application or class library, and such libraries are compiled into a .dll (dynamic-link library) file (Albahari & Albahari, 2012). And so an assembly is just a compiled version of an individual project (Sempf, Sphar, & Davis, 2010).

Those libraries can contain one or more classes (Sempf et al., 2010; Stellman & Greene, 2010) and their purpose is to be called upon (referenced) by an application or other library (Albahari & Albahari, 2012). The benefits of using DLLs are easy code reuse (Troelsen, 2012) plus that we can write DLLs in another language than C# and still use them in C# (Henry MultiCharts, 2013b; Sempf et al., 2010). Now let’s see how to make a simple DLL library and then use this assembly in a MultiCharts .NET script.

Programming a simple DLL in Visual Studio Community

In this tutorial we create a DLL in Visual Studio Community, the Visual Studio version that’s free for individual developers (other versions of Visual Studio should work similarly).

To get begin, start Visual Studio Community 2013 and click on ‘File’, point to ‘New’, and choose ‘Project’ (or press Ctrl + Shift + N):

Creating a new project in Visual Studio

This opens the ‘New Project’ window. Here we select ‘Class Library’ from the Visual C# templates and set the .NET Framework version to 4.0 (the version that’s currently also used by MultiCharts .NET). In the ‘Name’ field we give this library the ‘HelperMethods’ name and below that we can optionally choose a location. After specifying these settings we press ‘OK’:

Creating a class library in Visual Studio

Now our project is created in Visual Studio Community. In the ‘Solution Explorer’ (if you don’t have this window, go to the ‘View’ menu and then ‘Solution Explorer’; Ctrl + Alt + L) we can see the following files in the project:

Solution Explorer window in Visual Studio

Coding a class library in Visual Studio

The file in which we need to code, Class1.cs, is already opened and currently empty:

New class file in Visual Studio

We’re going to rename this class to Calc and add two methods to it: Add() and Subtract(). Both take in two parameters and, as their name implies, add these double values or subtract them from each other. The C# code for this looks as follows:

using System;

namespace HelperMethods
{
    public class Calc
    {
        public static double Add(double x, double y)
        {
            return (x + y);
        }

        public static double Subtract(double x, double y)
        {
            return (x - y);
        }
    }
}

After typing this in, we click on the ‘Build’ menu item and select ‘Build Solution’ (Ctrl + Shift + B) in order to make a DLL from our project:

Build class library solution in Visual Studio
Tip: If your DLL targets a specific platform (which we don’t do in this tutorial), note that 32-bit DLLs only work with the 32-bit version of MultiCharts .NET while 64-bit DLLs are required for MultiCharts .NET 64-bit (Henry MultiCharts, 2013b).

When no errors occurred, the ‘Build Succeeded’ message appears in the left-hand corner of the status bar. In the ‘Output’ window (to enable, ‘View’ menu and ‘Output’; Ctrl + Alt + O) we see the following:

Output window in Visual Studio

While not visible in the image above, the full message in this window is:

1>------ Build started: Project: HelperMethods, Configuration: Debug Any CPU ------
1>  HelperMethods -> c:\users\jos\documents\visual studio 2013\Projects\HelperMethods\HelperMethods\bin\Debug\HelperMethods.dll
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

The second line tells us the location of the DLL. We can use it in the PowerLanguage .NET Editor from there, but that has the disadvantage that each project build overwrites the DLL uses in MultiCharts .NET (including any untested code written in the class library).

So let’s move the DLL file to another location first. When we go to the ‘Output’ location in the Windows Explorer, we see two files:

Debug folder with DLL file

We now copy the ‘HelperMethods.dll’ file to another location, for instance to the ‘C:\Box Sync\’ folder that we made when backing up MultiCharts .NET scripts to the cloud. In that location we create the ‘MultiCharts .NET DLLs’ folder and place the DLL file in there:

Changing the location of the DLL file

With our DLL in place the next step is creating a MultiCharts .NET indicator and adding the DLL reference to the PowerLanguage .NET Editor.

Creating a new indicator script in the PowerLanguage .NET Editor

After starting the PowerLanguage .NET Editor we click on the small arrow besides the ‘New Study’ toolbar button ( ) and select ‘New Indicator…’:

Creating a new MultiCharts .NET indicator

In the subsequent opened ‘New Indicator Name’ window we name the script ‘Example_IndicatorWithDLL’, set its language to C#, and then press ‘OK’:

New Indicator Name window

That opens our new script in the PowerLanguage .NET Editor:

New script opened in the PowerLanguage .NET Editor

Adding a DLL reference to the PowerLanguage .NET Editor

Before coding the script’s logic, let’s add the DLL we’ve just made to the PowerLanguage .NET Editor. For that we need to add a reference to the DLL (Henry MultiCharts, 2013b; MultiCharts, 2014). We do that by right-clicking in the code editor and selecting ‘References…’:

References menu item in the PowerLanguage .NET Editor

That opens the ‘Assembly References’ window. Here we see a list of references added to the PowerLanguage .NET Editor (currently it’s empty) and here we can also remove the optimal assemblies. To add a reference, we click ‘Add Reference’:

Adding an assembly reference

A file browser window now opens wherein we navigate to the DLL location (in our case, ‘C:\Box Sync\MultiCharts .NET DLLs\‘). There we select the ‘HelperMethods.dll’ and click ‘Open’:

Browsing to the DLL file location

That adds the DLL to the ‘Assembly References’ window list. Then we click ‘Close’ to close this window:

Added the DLL reference to the PowerLanguage .NET Editor
Tip: Assembly references only need to be added to the PowerLanguage .NET Editor. Any references added through other programs, like Visual Studio, aren’t saved in the MultiCharts .NET project file (Henry MultiCharts, 2013a; MultiCharts, 2014). Also, a reference only needs to be added once to make it available for all scripts (Henry MultiCharts, 2012).

Now that the PowerLanguage .NET Editor is set up with the DLL, it’s time to code our example indicator.

Programming a MultiCharts .NET indicator that uses a DLL

The indicator that we’re going to make is simple: it plots the current bar number and uses the two methods from our DLL to add and subtract a value from the current bar number.

To easily reference the code in a DLL, we include the assembly’s namespace into the programming scope with a using directive (Sharp, 2013). That way we can simply type our DLL’s class name (Calc) instead of having to qualify it every time with its namespace (HelperMethods.Calc). For that we type using HelperMethods in the top of the indicator:

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using HelperMethods;           // Namespace of DLL

After that we add two attributes to our indicator:

[SameAsSymbol(false), UpdateOnEveryTick(false)]
public class Example_IndicatorWithDLL : IndicatorObject

With SameAsSymbol set to false the indicator isn’t displayed on the price chart but instead has its own subchart. Setting UpdateOnEveryTick to false makes the indicator calculate on bar close, which is often enough for our example.

We then make three plots to show the indicator’s values on the chart:

private IPlotObject barNumber, barNumAdd, barNumSub;

protected override void Create()
{
    // Create all three plots
    barNumber = AddPlot(new PlotAttributes("Bar number", 
        EPlotShapes.Line, Color.Black));

    barNumAdd = AddPlot(new PlotAttributes("Bar number + 10",
        EPlotShapes.Line, Color.Navy));

    barNumSub = AddPlot(new PlotAttributes("Bar number - 5",
        EPlotShapes.Line, Color.Red));
}

We first declare three IPlotObject objects. With barNumber we’ll display the current bar number, barNumAdd will plot the current bar number plus 10, and barNumSub shows the current bar number minus 5.

Then we make each of these plots with the AddPlot() method in Create(). That latter method is executed as soon as an instance of the script is made (MultiCharts, 2014). Each plot’s features are defined with PlotAttributes(): the first plot is named "Bar number" and set to a line (EPlotShapes.Line) in black (Color.Black). Similarly, we set the barNumAdd plot to blue line (Color.Navy) and the line that plots the bar number minus 5 is drawn in red.

Using a DLL’s methods in a MultiCharts .NET script

CalcBar() is the next part in our example indicator. That method is executed at least once on every price bar, starting from the first to the last bar (MultiCharts, 2014). In this method we implement the following:

protected override void CalcBar()
{
    // Calculate values
    double barNum   = Bars.CurrentBarAbsolute() + 1;
    double barPlus  = Calc.Add(barNum, 10);
    double barMinus = Calc.Subtract(barNum, 5);

    // Plot the lines
    barNumber.Set(barNum);
    barNumAdd.Set(barPlus);
    barNumSub.Set(barMinus);
}

We start by creating three double variables. The first, barNum, is assigned the value returned by the Bars.CurrentBarAbsolute() method plus 1. This method returns the zero-based bar number of the primary data series since it’s preceded by the Bars property (see PowerLanguage .NET Help, n.d.). To have its value lined up with the chart’s bar number, we increase it with 1.

The barPlus variable holds the value calculated by Calc.Add(). That method (from our own DLL) returns the sum of both arguments, which are the current bar number (barNum) and 10 here. Likewise, the barMinus variable is given the value returned by Calc.Subtract(), our DLL method that returns the difference between both arguments (barNum and 5 here).

Once those values are computed it’s time to plot them on the chart. We do that by calling each plot’s Set() method and pass in the value that the plot should be set to (see PowerLanguage .NET Help, n.d.). Since we do this in CalcBar(), which is executed on every bar (MultiCharts, 2014), these plots form a consecutive line after several bars.

Complete code of the example MultiCharts .NET indicator

After implementing all the steps listed above, the full indicator code is:

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using HelperMethods;           // Namespace of DLL

namespace PowerLanguage.Indicator
{
    [SameAsSymbol(false), UpdateOnEveryTick(false)]
    public class Example_IndicatorWithDLL : IndicatorObject
    {
        public Example_IndicatorWithDLL(object _ctx) : base(_ctx) { }

        private IPlotObject barNumber, barNumAdd, barNumSub;
        
        protected override void Create()
        {
            // Create all three plots
            barNumber = AddPlot(new PlotAttributes("Bar number", 
                EPlotShapes.Line, Color.Black));

            barNumAdd = AddPlot(new PlotAttributes("Bar number + 10",
                EPlotShapes.Line, Color.Navy));

            barNumSub = AddPlot(new PlotAttributes("Bar number - 5",
                EPlotShapes.Line, Color.Red));
        }
        
        protected override void CalcBar()
        {
            // Calculate values
            double barNum   = Bars.CurrentBarAbsolute() + 1;
            double barPlus  = Calc.Add(barNum, 10);
            double barMinus = Calc.Subtract(barNum, 5);

            // Plot the lines
            barNumber.Set(barNum);
            barNumAdd.Set(barPlus);
            barNumSub.Set(barMinus);
        }
    }
}

We now need to compile the script by clicking on the ‘Compile changed studies’ button ( ) in the toolbar or pressing the F7 keyboard shortcut.

By the way, if something went wrong when we added the ‘HelperMethods.dll’ to the PowerLanguage .NET Editor, we get the following error message in the ‘Error’ window panel:

Error message: missing DLL

Now that we’ve completed the indicator, let’s see how it looks on a chart.

Adding a MultiCharts .NET indicator that uses a DLL to a chart

To add the indicator to a chart, we right-click on the chart and select ‘Insert Study’ or press F7 (a study is, by the way, MultiCharts .NET’s term for indicators, strategies, and functions; MultiCharts Wiki, 2013):

Adding a MultiCharts .NET study to the chart

This opens the ‘Insert Study’ window. Here we go to the ‘Indicator’ tab, select the ‘Example_IndicatorWithDLL’ indicator, and then click ‘OK’:

Insert Study window

Our indicator is now added to the chart. In its subchart the current bar number (in black), the bar number plus 10 (blue), and the bar number minus 5 (red) are plotted:

Example indicator added to a chart

If we click on the last bar and hold down the mouse button, the ‘Chart Hint’ window appears. In it, we can see that the last bar is number 459 (see arrow). That’s in line with the three values that the indicator displays in its subchart, showing us that the indicator works correctly:

Example indicator added to a chart

For more about creating scripts in the PowerLanguage .NET Editor, see creating an indicator, programming a function, and coding a trading strategy.

Summary

C# compiles class libraries into a dynamic-link library (DLL) file that allows for easy code reuse. A MultiCharts .NET script can use a DLL once a reference is added to it in the PowerLanguage .NET Editor – DLL references added through other programs will not be saved. After a DLL is added to the PowerLanguage .NET Editor, all scripts can use it. We can access the classes in the DLL file more conveniently when we add the DLL’s namespace(s) with a using directive to the top of the script’s source code.


References

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

Henry MultiCharts (2012, December 28). Adding a reference/component to PLStudies2010 - forum discussion. Retrieved on August 21, 2015, from http://www.multicharts.com/discussion/viewtopic.php?f=19&t=11644#p57319

Henry MultiCharts (2013a, December 13). MultiCharts .NET FAQ - forum discussion. Retrieved on August 21, 2015, from http://www.multicharts.com/discussion/viewtopic.php?f=19&t=45848#p100789

Henry MultiCharts (2013b, December 13). MultiCharts .NET FAQ - forum discussion. Retrieved on August 21, 2015, from http://www.multicharts.com/discussion/viewtopic.php?f=19&t=45848#p100790

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

MultiCharts Wiki (2013, May 6). Using Studies (PowerLanguage Editor). Retrieved on August 22, 2015, from https://www.multicharts.com/trading-software/index.php/Using_Studies_%28PowerLanguage_Editor%29

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.

Troelsen, A. (2012). Pro C# 5.0 and the .NET 4.5 Framework (6th edition). Apress.