Writing scripts on the API

Introduction

In order to write a script on the TSLab API, you must create a class inherited from the IExternalScript interface.

This interface has the following implementation:

public interface IExternalScript : IExternalScriptBase, IStreamHandler, IHandler, IOneSourceHandler
{
    void Execute(IContext ctx, ISecurity sec);
}

As you can see, the interface has only one Execute method, which takes two parameters.

IContext - contains the general script context; it is needed for drawing graphs and caching.

ISecurity - allows you to work with the tool, contains candles, ticks, you can work with positions.

The Execute method is called each time TSLab starts a script recount.

You can also use other interfaces, they differ only in the number of tools in the parameters:

IExternalScript2 - two tools at the input (ISecurity)

IExternalScript3 - three tools at the input (ISecurity)

IExternalScript4 - four tools at the input (ISecurity)

IExternalScriptMultiSec - input array of tools (ISecurity)

Scripts template

To write scripts, you can use the following template:

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

namespace MyLib
{
    public class MyScript : IExternalScript
    {
        public void Execute(IContext ctx, ISecurity sec)
        {
            // Find the last formed bar for calculations
            var barsCount = sec.Bars.Count;
            if (!ctx.IsLastBarUsed)
            {
                barsCount--;
            }

            // Trading cycle
            for (int i = ctx.TradeFromBar; i < barsCount; i++)
            {
                // Here work with positions (sec.Positions)
            }

            // If the optimization process is ongoing, then you do not need to draw graphics, this slows down the work
            if (ctx.IsOptimization)
            {
                return;
            }

            // Charting
            // ctx.First - main panel
        }
    }
}

Main properties and methods

Interface IContext

IsLastBarClosed

IsLastBarUsed

IsFixedBarsCount

To determine the number of bars in the calculation.

IsOptimization

Indicates that the optimization process is ongoing.

TradeFromBar

Indicates which bar to start trading with.

ScriptResult

Displays the result in an optimization table.

GetData(...)

Work with a cache. Used to calculate indicators.

LoadObject(...)

StoreObject(...)

Work with the script cache, which is not erased between calculations.

LoadGlobalObject(...)

StoreGlobalObject(...)

Working with a global cache that is shared between various agents and scripts.

Data in the cache may be erased if the program does not have enough memory.

Log(...)

Log output.

Recalc(...)

Run the script again.

Interface ISecurity

FinInfo

Current data on the security (bid, ask, price of the last transaction, open interest, etc.).

Bars

List of candles.

OpenPrices

HighPrices

LowPrices

ClosePrices

Volumes

List of opening prices.

List of highs.

List of lows.

Closing price list.

List of volumes.

Interval

IntervalBase

IntervalInstance

The interval of the security.

LotSize

LotTick

Margin

Tick

Decimals

Lot size for a security.

Size of the lot change for a security.

Margin ratio for a security.

Minimum possible price change.

The number of decimal places for the security price.

Positions

List of items.

Commission

The delegate to calculate the commission.

InitDeposit

Initial deposit.

IsRealtime

Indicates whether the agent is in real trading mode.

Symbol

Security symbol.

CompressTo(...)

CompressToPriceRange(...)

CompressToVolume(...)

Decompress(...)

Squeeze and unclench candles.

GetBuyQueue(...)

GetSellQueue(...)

UpdateQueueData(...)

Get the order queue.

GetTrades(...)

Get a list of offers.

RoundPrice(...)

RoundShares(...)

Rounding the entry price to the minimum tick.

Rounding the incoming quantity to the minimum step of the lot.

Last updated