First script (API)

Creating a script in Visual Studio

Let's create the first script, it will buy on the market and put a stop and take at a distance of 0.5%.

Launch Visual Studio, create a new MyLib project if not already created.

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

Add TSLab library to the project. They are in the TSLab installation folder. For this:

  1. Right click on References -> Add Reference...

  2. Go to the Browse tab, click on the Browse... button

  3. In the dialog that opens, go to the TSLab program folder: - TSLab 2.1: C:\Program Files (x86)\TSLab 2.0. - TSLab 2.2:C:\Program Files\TSLab\TSLab 2.2

  4. Select the following files: TSLab.Script.dll, TSLab.Script.Handlers.dll, TSLab.DataSource.dll, TSLab.Utility.dll.

You can also load these files into your project using the NuGet tool available in Visual Studio.

  • In Solution Explorer, right-click References and choose Manage NuGet Packages.

  • Choose "nuget.org" as the Package source, select the Browse tab, search for TSLab, select that package in the list, and select Install:

Add a new BuyScript class. To do this, right-click on the project name, select Add -> Class... and write the class name BuyScript.cs.

In the code editor, 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.DataSource;

namespace MyLib
{
    public class BuyScript : IExternalScript
    {
        public void Execute(IContext ctx, ISecurity sec)
        {
            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
                {
                    longPos.CloseAtStop(i + 1, longPos.EntryPrice * 0.995, "LXS");
                    longPos.CloseAtProfit(i + 1, longPos.EntryPrice * 1.005, "LXP");
                }
            }
        }
    }
}

We created the BuyScript class from the IExternalScript interface. This interface describes the Execute method, which is called every time the script is run in TSLab.

The method has two parameters, IContext ctx and ISecurity sec.

IContext - contains the general script context; it is needed for drawing graphs, caching, obtaining trading statistics.

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

In the body of the method, we wrote enumeration over all bars through a for loop. Using the sec.Positions.GetLastActiveForSignal method, we get the last active position and write it to the longPos variable. Next, check this variable, if there is no position (longPos == null), then buy by market (sec.Positions.BuyAtMarket), if the position exists, then put a stop for the position (longPos.CloseAtStop) and take profit (longPos.CloseAtProfit). It is important to note that we open a position with the signal name "LE" and get a position by the same name (sec.Positions.GetLastActiveForSignal ("LE", i)). Only one position with the same name can be opened at a time.

sec.Positions - access to positions

sec.Positions.GetLastActiveForSignal ("LE", i) - get the last active position with the signal "LE".

sec.Positions.BuyAtMarket (i + 1, 1, "LE") - open a market position with the signal "LE"

longPos.CloseAtStop - set a stop loss.

longPos.CloseAtProfit - set take profit.

Press F6, the studio will check for errors and if they are not there, it will assemble the project.

Now let's move on to TSLab.

Running the script in TSLab

Create a new script, add the source, the External script block and include our BuyScript.cs file.

Click Save and Run. If everything was done correctly, a chart with deals will be displayed.

Additionally

  1. If additional libraries or classes are used in the script, then they must also be added to TSLab in the External script block.

  2. In TSLab, you can connect the dll library instead of the script file.

Last updated