using System; using System.Collections.Generic; using TSLab.Script; using TSLab.Script.Handlers; using TSLab.Script.Helpers; using TSLab.Script.Optimization; namespace RTS_Sber { public class Class1 : IExternalScript2 { // Создаем 2 объекта кубиков, Объемы, АТР public VolumeCube.Volume VolumeCube_h = new VolumeCube.Volume(); public ATRCube.ATRCube ATRCube_h = new ATRCube.ATRCube(); // Оптимизируемые параметры public OptimProperty VolumeCube_dayPercent = new OptimProperty(0.2, 0.1, 0.5, 0.1); public OptimProperty VolumeCube_monthPercent = new OptimProperty(0.2, 0.1, 0.5, 0.1); public OptimProperty ATRCube_periodATR = new OptimProperty(5, 3, 10, 1); public void Execute(IContext ctx, ISecurity sec, ISecurity sec2) { // Инициализируем объекты и кэшируем по названию и параметрам VolumeCube_h.dayPercent = VolumeCube_dayPercent; VolumeCube_h.monthPercent = VolumeCube_monthPercent; IList VolumeCube = ctx.GetData("VolumeCube", new string[] { VolumeCube_h.dayPercent.ToString(), VolumeCube_h.monthPercent.ToString(), "Источник1" }, delegate { return VolumeCube_h.Execute(sec); }); ATRCube_h.periodATR = ATRCube_periodATR; IList ATRCube = ctx.GetData("ATRCube", new string[] { ATRCube_h.periodATR.ToString(), "Источник1" }, delegate { return ATRCube_h.Execute(sec); }); DateTime count = sec.Bars[0].Date.AddDays(1); double high = sec.Bars[0].High; double low = sec.Bars[0].Low; double highM = sec2.Bars[0].High; double lowM = sec2.Bars[0].Low; double sstopL = 0; double sstopS = 0; for (int i = 1; i < sec.Bars.Count; i++) { if (sec.Bars[i].Date > count) { count = sec.Bars[i].Date.AddDays(1); high = sec.HighPrices[i]; low = sec.LowPrices[i]; highM = sec2.HighPrices[i]; lowM = sec2.LowPrices[i]; } else { var le = sec.Positions.GetLastActiveForSignal("LE", i); var se = sec.Positions.GetLastActiveForSignal("SE", i); // Если РТС пробивает хай, а Сбер нет, то заходим в лонг if (sec.HighPrices[i] > high) { if(sec2.HighPrices[i] < highM) { sec.Positions.BuyAtMarket(i + 1, 1, "LE"); high = sec.HighPrices[i]; sstopL = sec.Bars[i].Close - ATRCube[i]*0.3; } else { high = sec.HighPrices[i]; } } if (le != null) { sstopL -= 15; le.CloseAtStop(i + 1, sstopL, "LXS"); } // Если РТС пробивает лоу, заходим в шорт if (sec.LowPrices[i] < low) { if(sec2.LowPrices[i] > lowM) { sec.Positions.SellAtMarket(i + 1, 1, "SE"); low = sec.LowPrices[i]; sstopS = sec.Bars[i].Close + ATRCube[i]*0.3; } else { low = sec.LowPrices[i]; } } if (se != null) { sstopS -= 15; se.CloseAtStop(i + 1, sstopS, "SXS"); } if (sec2.HighPrices[i] > highM) highM = sec2.HighPrices[i]; if (sec2.LowPrices[i] < lowM) lowM = sec.LowPrices[i]; } } if (ctx.IsOptimization) return; var pane = ctx.CreatePane("RTS", 50, false); var color = new Color(System.Drawing.Color.Black.ToArgb()); var lst = pane.AddList(sec.ToString(), sec, CandleStyles.BAR_CANDLE, color, PaneSides.RIGHT); pane = ctx.CreatePane("Sber", 50, false); color = new Color(System.Drawing.Color.Black.ToArgb()); lst = pane.AddList(sec2.ToString(), sec2, CandleStyles.BAR_CANDLE, color, PaneSides.RIGHT); } } }