Ниже ещё одна реализация.
Это вариант через последние найденные фракталы, вне зависимости от того когда они образовались.
Здесь параметр Period уже используется для поиска фрактала и указывает на количество в барах для поиска фрактала +\- в обе стороны от предполагаемого экстремума.
Последние фракталы Low (глобальная переменная L) и High (глобальная переменная H) и используются для определения отклонения от них.
Код: Выделить всё
function Initialize()
{
IndicatorName = "FiboBarsFractal";
AddInput("Input", Inputs.Candle);
AddSeries("Bear", DrawAs.Custom, Color.Red, AxisType.Default, true, Axes.Parent);
AddSeries("Bull", DrawAs.Custom, Color.Green, AxisType.Default, true, Axes.Parent);
AddSeries("trend", DrawAs.Custom, Color.Red, false);
AddGlobalVariable("Last", Types.Boolean, false);
AddParameter("Period", 7, 1);
AddParameter("Level", 0.236, 1);
AddGlobalVariable("H", Types.Double, 0);
AddGlobalVariable("L", Types.Double, 9999999);
}
function Evaluate()
{
//evge 16.10.2020 https://alfadirect4.ru
var I = Input;
bool isL = true, isH = true;
for (int x = 1; x <= Period; x++)
{
if (I.Low[Period + x] < I.Low[Period] || I.Low[Period - x] < I.Low[Period]) isL = false;
if (I.High[Period + x] > I.High[Period] || I.High[Period - x] > I.High[Period]) isH = false;
if (!isL && !isH) break;
}
if (isL) L = I.Low[Period];
if (isH) H = I.High[Period];
int trend1 = (trend[1] >= 0) && ((H - L) * Level < (I.Close[0] - L)) ? 1 : -1;
int trend2 = (trend[1] <= 0) && ((H - L) * Level < (H - I.Close[0])) ? -1 : 1;
trend[0] = I.Open[0] > I.Close[0] ? trend1 : trend2;
Bear = Input.Low[0];
Bull = Input.High[0];
if (trend[0] > 0)
{
Bull.DrawHistogram(Bear);
Last = true;
}
else
{
Bear.DrawHistogram(Bull);
Last = false;
}
}
Скачать индикатор