Standard Indicators and Handlers

Standard Indicators and Handlers

The TSLab API has standard, commonly used indicators. They can be easily used in their scripts. These indicators are in two places:

TSLab.Script.Helpers.Series - the class contains static methods for calculating indicators, for example, SMA, EMA, Highest, Lowest, ATR, BollingerBands and others.

TSLab.Script.Handlers - namespace that contains classes of handlers, for example MACD, ParabolicSAR, TrailStop. All these handlers are displayed as cubes in the TSLab visual editor.

Series indicators

The Series class contains some indicators, for example SMA, EMA, Highest, Lowest, ATR, BollingerBands and others.

For example, if you need to calculate the SMA indicator, then simply call the SMA method and transfer the list of prices for which you want to read (sec.ClosePrices) as the first parameter, and the calculation period will be the second parameter:

var sma = Series.SMA(sec.ClosePrices, 100);

We will write a script that displays the SMA and EMA indicators on closing prices:

using TSLab.Script;
using TSLab.Script.Helpers;
using TSLab.Script.Handlers;

namespace MyLib
{
    public class IndicatorsMA : IExternalScript
    {
        public void Execute(IContext ctx, ISecurity sec)
        {
            var period = 100;
            var sma = Series.SMA(sec.ClosePrices, period); // SMA indicator calculation
            var ema = Series.EMA(sec.ClosePrices, period); // EMA indicator calculation

            // Plotting
            ctx.First.AddList("SMA", sma, ListStyles.LINE, ScriptColors.Red, LineStyles.SOLID, PaneSides.RIGHT);
            ctx.First.AddList("EMA", ema, ListStyles.LINE, ScriptColors.Blue, LineStyles.SOLID, PaneSides.RIGHT);
        }
    }
}

The red line is the SMA indicator, the blue line is the EMA.

Indicators and Handlers from Handlers

TSLab.Script.Handlers - namespace that contains classes of handlers, for example MACD, ParabolicSAR, TrailStop. All these handlers are displayed as cubes in the TSLab visual editor.

Unlike indicators from Series, you must first create an indicator object and then call the Execute method to calculate the indicator values.

Source codes of indicators: https://github.com/tslab-hub/handlers

For example, we will write a script that considers and displays the ParabolicSAR indicator on a chart:

using TSLab.Script;
using TSLab.Script.Helpers;
using TSLab.Script.Handlers;

namespace MyLib
{
    public class IndicatorParabolicSAR : IExternalScript
    {
        public void Execute(IContext ctx, ISecurity sec)
        {
            var parabolic = new ParabolicSAR() // Create the ParabolicSAR indicator class
            {
                Context = ctx,
                AccelerationMax = 0.2,
                AccelerationStart = 0.02,
                AccelerationStep = 0.02
            };
            var list = parabolic.Execute(sec); // Making indicator calculation


            // We draw on the chart
            var line = ctx.First.AddList("Parabolic SAR", list, ListStyles.LINE, ScriptColors.Red, LineStyles.DOT, PaneSides.RIGHT);
            line.Thickness = 2; // Line Thickness 2
        }
    }
}

For another example, we will write a script that buys by market and sets a trail stop. Stop will be considered a finished TrailStop class from TSLab.Script.Handlers:

using TSLab.Script;
using TSLab.Script.Handlers;

namespace MyLib
{
    public class HandlerTrailStop : IExternalScript
    {
        public void Execute(IContext ctx, ISecurity sec)
        {
            var trail = new TrailStop // Create a TrailStop Handler Class
            {
                StopLoss = 0.5,
                TrailEnable = 0.5,
                TrailLoss = 0.5,
            };

            for (int i = 0; i < ctx.BarsCount; i++)
            {
                var longPos = sec.Positions.GetLastActiveForSignal("LE", i);

                if (longPos == null)
                {
                    sec.Positions.BuyAtMarket(i + 1, 1, "LE");
                }
                else
                {
                    var stop = trail.Execute(longPos, i); // Trail Stop Calculation
                    longPos.CloseAtStop(i + 1, stop, "LX");
                }
            }
        }
    }
}

As you can see, there is a deal and immediately set a trail stop (red line), which is pulled up after the price:

Last updated