First Indicator (API)

Creating an indicator in Visual Studio

TSLab can use indicators written in .NET and compiled into a .dll library.

For example, we write an indicator that considers the price of OHLC4 according to the formula:

(open+high+low+close)/4(open + high + low + close) / 4

Let's create a new project in Visual Studio, using the Template Class Library template and name it MyHandlers.

Important: for TSLab version 2.2 and higher, you need to use the .Net Standard2.0, .Net Standard2.1, Net6 or higher platform.

Add the libraries TSLab.Script.dll, TSLab.Script.Handlers.dll, TSLab.DataSource.dll to the project. Create a new OHLC4 class and write the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TSLab.Script;
using TSLab.Script.Handlers;
using TSLab.Script.Handlers.Options;

namespace MyHandlers
{
    [HandlerCategory("MyHandlers")]
    [HelperName("OHLC4")]
    public class OHLC4 : IBar2DoubleHandler
    {
        public IList<double> Execute(ISecurity source)
        {
            return source.Bars.Select(x => (x.Open + x.High + x.Low + x.Close) / 4).ToList();
        }
    }
}

We created the OHLC4 class from the IBar2DoubleHandler interface. This interface implements one Execute method, which takes a tool as an input and produces a list of numbers.

For the class, we specified the HandlerCategory attribute, called MyHandlers. This is the name of the category where our indicator will be placed in TSLab. HelperName - indicates the name of the indicator that will be displayed with TSLab, you can write any other name.

In the Execute method, we made the calculation of the OHLC4 value a simple expression:

source.Bars.Select (x => (x.Open + x.High + x.Low + x.Close) / 4)

Now you can build the project by pressing F6. If everything was done correctly, the studio will save, assemble the project and create the file MyHandlers.dll in the project folder \bin\Debug.

This file must be copied to the TSLab folder: % APPDATA% \ .. \ Local \ TSLab \ TSLab 2.0 \ Handlers

Running the indicator in TSLab

Run TSLab and in Editor mode should display our new indicator.

Connect our new indicator, select the tool and run.

We see that the blue line is displayed on the chart, this is the price of OHLC4.

Additionally

In order not to manually copy the .dll file each time, you can register a command for automatic copying in the studio. The file will be copied after each build of the project.

But it should be remembered that before building, you need to close TSLab, because the file cannot be overwritten.

Command: xcopy / Y MyHandlers.dll "%APPDATA%\..\Local\TSLab\TSLab 2.0\Handlers"

Last updated