using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TSLab.Script; using TSLab.Script.Handlers; using TSLab.Script.Helpers; using TSLab.Script.Optimization; namespace test { public class test:IExternalScript { public OptimProperty FastSmaPeriod = new OptimProperty(10, 5, 40, 5); public OptimProperty SlowSmaPeriod = new OptimProperty(40, 10, 100, 10); public void Execute(IContext ctx, ISecurity sec) { var smaFast = Series.SMA(sec.GetClosePrices(ctx), 10); var smaSlow = Series.SMA(sec.GetClosePrices(ctx),40); var tradeFromBar = Math.Max(FastSmaPeriod, SlowSmaPeriod); for (int i = tradeFromBar; i < ctx.BarsCount ; i++) { var pos = sec.Positions.GetActiveForBar(i); if (smaFast[i - 1] > smaSlow[i - 1] && smaFast[i] < smaSlow[i]) sec.Positions.SellAtMarket(i+1,1,"S"); if (smaFast[i - 1] < smaSlow[i - 1] && smaFast[i] > smaSlow[i]) sec.Positions.BuyAtMarket(i + 1, 1, "L"); foreach (var position in pos) if ((smaFast[i - 1] > smaSlow[i - 1] && smaFast[i] < smaSlow[i]) || (smaFast[i - 1] < smaSlow[i - 1] && smaFast[i] > smaSlow[i])) position.CloseAtMarket(i+1,"X"); } var pane = ctx.CreateGraphPane(sec.ToString(), sec.ToString(), false); var color = new Color(System.Drawing.Color.Green.ToArgb()); pane.AddList(sec.ToString(), sec.ToString(), sec, CandleStyles.BAR_CANDLE, color, PaneSides.RIGHT); color = new Color(System.Drawing.Color.Red.ToArgb()); pane.AddList("fastSma", "fastSma", smaFast, ListStyles.LINE,color,LineStyles.SOLID,PaneSides.RIGHT); color = new Color(System.Drawing.Color.Blue.ToArgb()); pane.AddList("slowSma", "slowSma", smaSlow, ListStyles.LINE, color, LineStyles.SOLID, PaneSides.RIGHT); var positionsList = sec.Positions; foreach (var pos in positionsList) ctx.Log("бар входа: " + pos.EntryBarNum); } } }