using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TradeScripts;
using TSLab.Script;
using TSLab.Script.Handlers;
using TSLab.Script.Helpers;
using TSLab.Script.Optimization;
namespace TradeScripts
{
public class DoubleSma : IExternalScript
{
#region Параметры оптимизации
public OptimProperty SmaFast = new OptimProperty(16, 8, 512, 8);
public OptimProperty SmaSlow = new OptimProperty(32, 8, 512, 8);
#endregion
public void Execute(IContext ctx, ISecurity sec)
{
//Расчет Индикаторов
var smafast = Series.SMA(sec.ClosePrices, SmaFast);
var smaslow = Series.SMA(sec.ClosePrices, SmaSlow);
var start = Math.Max(SmaFast, SmaSlow);
for (int i = start + 1; i < ctx.BarsCount; i++)
{
var le = sec.Positions.GetLastActiveForSignal("LE", i);
var se = sec.Positions.GetLastActiveForSignal("SE", i);
//Позиции для лонга
if (le == null)
{
if (smafast[i - 1] < smaslow[i - 1] && smafast[i] > smaslow[i])
sec.Positions.BuyAtMarket(i + 1, 2, "LE");
}
else
{
if (smafast[i - 1] > smaslow[i - 1] && smafast[i] < smaslow[i])
le.CloseAtMarket(i + 1, "LX");
}
//Позиции для шорта
if (se == null)
{
if (smafast[i - 1] > smaslow[i - 1] && smafast[i] < smaslow[i])
sec.Positions.SellAtMarket(i + 1, 2, "SE");
}
else
{
if (smafast[i - 1] < smaslow[i - 1] && smafast[i] > smaslow[i])
se.CloseAtMarket(i + 1, "SX");
}
}
#region Визуализация
var pane = Helper.PaneChart(ctx, sec, 100, false);
Helper.Chart(pane, sec, Helper.blossom.Blak);
Helper.Led(pane, "smaslow", smaslow, Helper.blossom.Red, 2);
Helper.Led(pane, "smafast", smafast, Helper.blossom.Blue, 2);
#endregion
}
}
}