using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TSLab.DataSource; using TSLab.Script; using TSLab.Script.Handlers; using TSLab.Helper; using TSLab.Script.Optimization; using TSLab.Script.Helpers; namespace PositionTest { public class PositionTest : IExternalScript { public OptimProperty FastSMA_period = new OptimProperty(12, 1, 100, 1); public OptimProperty SlowSMA_period = new OptimProperty(24, 1, 100, 1); public OptimProperty Increment_stoploss = new OptimProperty(100, 1, 10000, 1); public void Execute(IContext ctx, ISecurity sec) { IPane pane1 = ctx.CreatePane("pane1", 100, true, true); string sLong = "Long"; string sShort = "Short"; Color colorBlue = new Color(System.Drawing.Color.Blue.ToArgb()); Color colorGreen = new Color(System.Drawing.Color.Green.ToArgb()); Color colorRed = new Color(System.Drawing.Color.Red.ToArgb()); var fastSMA = ctx.GetData("fastSMA", new[] { FastSMA_period.ToString() }, () => Series.SMA(sec.ClosePrices, FastSMA_period)); var slowSMA = ctx.GetData("slowSMA", new[] { SlowSMA_period.ToString() }, () => Series.SMA(sec.ClosePrices, SlowSMA_period)); for (int i = 1; i < ctx.BarsCount; i++) { bool isBuy = false; bool isSell = false; if (fastSMA[i] > slowSMA[i] && fastSMA[i - 1] < slowSMA[i - 1]) isBuy = true; if (fastSMA[i] < slowSMA[i] && fastSMA[i - 1] > slowSMA[i - 1]) isSell = true; CPosition Long = new CPosition(i+1, sec, ctx, "long"); CPosition Short = new CPosition(i+1, sec, ctx, "short"); //Long.Check(i + 1); if (isBuy) { if (Long.Position == null) { Long.OpenLong(sec.Bars[i].Close - Increment_stoploss, 1); } } if (Long.Position != null) { if (isSell) { Long.Close(); } else { Long.setStoploss(); } } if (isSell) { if (Short.Position == null) { Short.OpenShort(sec.Bars[i].Close + Increment_stoploss, 1); } } if (Short.Position != null) { if (isBuy) { Short.Close(); } else { Short.setStoploss(); } } } pane1.AddList("Bars", sec, CandleStyles.BAR_CANDLE, colorBlue, PaneSides.RIGHT); pane1.AddList("slowSMA", slowSMA, ListStyles.LINE_WO_ZERO, colorRed, LineStyles.SOLID, PaneSides.RIGHT); pane1.AddList("fastSMA", fastSMA, ListStyles.LINE_WO_ZERO, colorGreen, LineStyles.SOLID, PaneSides.RIGHT); /*throw new NotImplementedException();*/ } } }