using System; using System.Collections.Generic; using TSLab.Script; using TSLab.Script.Handlers; namespace FlatOsc { /// /// Description of flat_osc. /// public class FlatOsc : IBar2DoubleHandler { private int m_period; [HandlerParameter(true, "20", Max = "100", Min = "10", Step = "5")] public int Period { get { return Math.Max(1, this.m_period); } set { this.m_period = value; } } public IList Execute(ISecurity source) { var Bars = source.Bars; var res = new double[Bars.Count]; int FirstValidValue = Period; double rangePeriod; // Значение периода double highValue, lowValue; // Значения границ флета for (int bar = FirstValidValue; bar < Bars.Count; bar++) // Пробегаемся по всем свечкам { rangePeriod = 100d; // Для каждой свечки изначально считаем, что она - трендовая highValue = double.MinValue; lowValue = double.MaxValue; for (int i = bar - 1; i > bar - 1 - Period; i--) // Для каждой свечки будем пробегаться по истории на величину Period { if (highValue < Bars[i].High) highValue = Bars[i].High; // Поднимаем верхнюю границу if (lowValue > Bars[i].Low) lowValue = Bars[i].Low; // Опускаем нижнюю границу if (Bars[bar].High <= highValue && Bars[bar].Low >= lowValue) // Нашли флет, в который вписываем текущую свечку { rangePeriod = 100d / Period * (bar - i - 1); break; } } res[bar] = 100d - rangePeriod;// Значение Осцилятора флета } return res; } } }