Get script results

To calculate the script data, you can use a standard algorithm using the Perfomance class.

Below is an example of a script that calculates the main metrics and displays them in the log. To shorten the code, the trading algorithm itself is not given; instead, put any of your own or from the examples.

Add TSLab.ScriptEngine.dll to the External script block.

using System.Text;
using TSLab.Script;
using TSLab.Script.Handlers;
using TSLab.Script.Helpers;
using TSLab.Script.Optimization;
using TSLab.ScriptEngine;

namespace MyLib
{
    public class TestPerfomance : IExternalScript
    {
        public virtual void Execute(IContext ctx, ISecurity sec)
        {
            // Trading algorithm
            // ....

            // Statistics are calculated at the end of the script, after the trading algorithm.
             // Do not forget to add TSLab.ScriptEngine.dll to the External script block
            var list = new [] { (ISecurity2)sec };
            var perfomance = new Perfomance(list, ((IRuntime2)ctx.Runtime).InitDeposit);
            perfomance.UpdateDataFromRuntime();

            // Logging
            var sb = new StringBuilder();
            sb.AppendLine($"AllTrades: {perfomance.AllTrades}");
            sb.AppendLine($"WinTrades: {perfomance.WinTrades}");
            sb.AppendLine($"LossTrades: {perfomance.LossTrades}");
            sb.AppendLine($"MaxDrawdown: {perfomance.MaxDrawdown}");
            sb.AppendLine($"MaxDrawdownPct: {perfomance.MaxDrawdownPct}");
            sb.AppendLine($"ProfitFactor: {perfomance.ProfitFactor}");
            sb.AppendLine($"RecoveryFactor: {perfomance.RecoveryFactor}");
            ctx.Log(sb.ToString(), MessageType.Info, true);
    }
}

Last updated