C ++ / CLI script

C++/CLI - one of the languages of the .NET Framework - is rarely used to develop large independent projects. Its main purpose is to create assemblies for .NET interaction with native (unmanaged) code.

In TSLab, you can connect scripts written in C #, VB.NET. Or connect dll libraries written in .NET (C #, VB.NET, C ++ / CLI).

For an example, we will write a library on C ++ / CLI, and we will connect in TSLab.

And so, create a new project in Visual Studio 2019. Let's choose a type: CLR Class Library in C ++.

Immediately switch the project to x64, add the libraries TSLab.Script.dll, TSLab.Script.Handlers.dll, TSLab.DataSource.dll.

Let's take an example of a script from the article First script (API) written in C #, but we will redo it in C ++. Create a BuyScript class and write the following code:

#pragma once
using namespace System;
using namespace TSLab::Script;
using namespace TSLab::Script::Handlers;
using namespace TSLab::DataSource;

namespace MyLibC {
    public ref class BuyScript sealed : IExternalScript
    {
    public:
        virtual void Execute(TSLab::Script::Handlers::IContext^ ctx, TSLab::Script::ISecurity^ sec)
        {
            for (int i = 0; i < ctx->BarsCount; i++)
            {
                IPosition^ longPos = sec->Positions->GetLastActiveForSignal("LE", i);

                if (!longPos)
                {
                    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", "");
                }
            }

        }
    };
}

Now you can build the project and if there are no errors, then the studio will create a library \x64\Debug\TestLibC.dll and connect it to TSLab.

As you can see, the script is connected, transactions are displayed on the chart.

Last updated