Script parameters

Script parameters

When writing scripts on the TSLab API, you can define parameters that can be configured from the TSLab Optimization tab. Also, these parameters are used to optimize the script.

For example, a script builds an SMA indicator. To calculate it, you need the value of the period by which the indicator is calculated. In order not to write this value in the code, we can add a parameter to the script and use it as a calculation.

public OptimProperty PeriodFast = new OptimProperty(100, 10, 200, 1);

This line describes a property of type OptimProperty. The property contains a double type. By default, the value will be 100; during optimization, values from 10 to 200 will be moved over in increments of 1.

There are different types of parameters:

Types of Parameters

OptimProperty

Floating point number (double)

IntOptimProperty

Integer (int)

BoolOptimProperty

Boolean value (bool)

DateTimeOptimProperty

date and time (DateTime)

EnumOptimProperty

Listing (Enum)

StringOptimProperty

String (String)

TimeSpanOptimProperty

Time interval (TimeSpan)

We will write a script that calculates two SMA values, fast and slow. We will pass SMA periods through script parameters:

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

namespace MyLib
{
    public class ExampleParameters : IExternalScript
    {
        // Customizable options
        public OptimProperty PeriodFast = new OptimProperty(100, 10, 200, 1);
        public OptimProperty PeriodSlow = new OptimProperty(500, 200, 2000, 10);

        public void Execute(IContext ctx, ISecurity sec)
        {
            // Value calculation
            var smaFast = Series.SMA(sec.ClosePrices, PeriodFast);
            var smaSlow = Series.SMA(sec.ClosePrices, PeriodSlow);

            // Plotting
            ctx.First.AddList(string.Format("SMA fast ({0})", PeriodFast), smaFast, ListStyles.LINE, ScriptColors.Green,
                LineStyles.SOLID, PaneSides.RIGHT);
            ctx.First.AddList(string.Format("SMA slow ({0})", PeriodSlow), smaSlow, ListStyles.LINE, ScriptColors.Red,
                LineStyles.SOLID, PaneSides.RIGHT);
        }
    }
}

On the Optimization tab, we see our parameters, where they can be edited.

Last updated