An example of a strategy Breakout of the Donchian channel

using System;
using System.Collections.Generic;
using System.Linq;
using TSLab.Script;
using TSLab.Script.Handlers;
using TSLab.Script.Helpers;
using TSLab.Script.Optimization;

namespace MyLib
{
    public class Breakout : IExternalScript
    {
        // Optimization Options
        public OptimProperty HighPeriod = new OptimProperty(20, 10, 100, 5);
        public OptimProperty LowPeriod = new OptimProperty(20, 10, 100, 5);

        // Processing method, launched when recounting the script
        public virtual void Execute(IContext ctx, ISecurity sec)
        {
            // Calculate the highs and lows
            // We use GetData for data caching and acceleration of optimization
            var high = ctx.GetData("Highest", new[] { HighPeriod.ToString() },
                () => Series.Highest(sec.GetHighPrices(ctx), HighPeriod));
            var low = ctx.GetData("Lowest", new[] { LowPeriod.ToString() },
                () => Series.Lowest(sec.GetLowPrices(ctx), LowPeriod));

            // If the last candlestick is not fully formed, it does not need to be used in the trading cycle.
            var barsCount = sec.Bars.Count;
            if (!ctx.IsLastBarUsed)
            {
                barsCount--;
            }

            // Trade
            for (int i = ctx.TradeFromBar; i < barsCount; i++)
            {
                // Get Active Positions
                var posLong = sec.Positions.GetLastActiveForSignal("LE", i);
                var posShort = sec.Positions.GetLastActiveForSignal("SE", i);

                if (posLong == null)
                {
                    // If there is no active long position, issue a conditional order to create a new position
                    sec.Positions.BuyIfGreater(i + 1, 1, high[i], "LE");
                }
                else
                {
                    // If there is a long position, then put a stop
                    posLong.CloseAtStop(i + 1, low[i], "LX");
                }


                if (posShort == null)
                {
                    // If there is no active short position, issue a conditional order to create a new position
                    sec.Positions.SellIfLess(i + 1, 1, low[i], "SE");
                }
                else
                {
                    // If there is a short position, then put a stop
                    posShort.CloseAtStop(i + 1, high[i], "SX");
                }
            }

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

            // Drawing graphs
            ctx.First.AddList(string.Format("High({0})", HighPeriod), high, ListStyles.LINE, ScriptColors.Green,
                LineStyles.SOLID, PaneSides.RIGHT);
            ctx.First.AddList(string.Format("Low({0})", LowPeriod), low, ListStyles.LINE, ScriptColors.Red,
                LineStyles.SOLID, PaneSides.RIGHT);
        }
    }
}

Last updated