#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
//--- plot Up
#property indicator_label1 "Up"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- plot Down
#property indicator_label2 "Down"
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrRed
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//--- input parameters
input int periods=50;
//--- indicator buffers
double Up[];
double Down[];
//---
//bool FirstPass = true;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
SetIndexBuffer(0,Up);
SetIndexBuffer(1,Down);
SetIndexArrow(0,159);
SetIndexArrow(1,159);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[])
{
int toCount, i, j, significance, bestj, bestsignificance;
double importance, bestimportance;
toCount = (int)MathMin(rates_total, rates_total - prev_calculated + 1);
for (i = toCount - 2*periods; i >= 0; --i) {
bestj = i+1;
bestimportance = 0.0;
for (j = i+1; j <= i+periods; ++j) {
if ((j <= i+1)/*bar just before i*/ || (High[j] > High[iHighest(NULL, NULL, MODE_HIGH, (j-1)-(i+1)+1, i+1)])/*there is no higher high after j till i+1*/) {
for (significance = 0; significance <= periods; ++significance) {
if (High[j+significance] > High[j]) { break; }
}
importance = MathLog(1.0+(double)significance) / (j-i); // j-i is "LAG"
if ((j-i>=2) && (importance > bestimportance)) {
bestimportance = importance;
bestj = j;
bestsignificance = significance;
}
}
}
Up[i] = High[bestj];
//if (FirstPass) { printf(" Up: i=%d (%s); j=%d; significance=%d; lag=%d; importance=%f", i, TimeToStr(Time[i], TIME_DATE||TIME_MINUTES), bestj, bestsignificance, (bestj-i), bestimportance); }
bestj = i+1;
bestimportance = 0.0;
for (j = i+1; j <= i+periods; ++j) {
if ((j <= i+1)/*bar just before i*/ || (Low[j] < Low[iLowest(NULL, NULL, MODE_LOW, (j-1)-(i+1)+1, i+1)])/*there is no lower low after j till i+1*/) {
for (significance = 0; significance <= periods; ++significance) {
if (Low[j+significance] < Low[j]) { break; }
}
importance = MathLog(1.0+(double)significance) / (j-i); // j-i is "LAG"
if ((j-i>=2) && (importance > bestimportance)) {
bestimportance = importance;
bestj = j;
bestsignificance = significance;
}
}
}
Down[i] = Low[bestj];
//if (FirstPass) { printf("Down: i=%d (%s); j=%d; significance=%d; lag=%d; importance=%f", i, TimeToStr(Time[i], TIME_DATE||TIME_MINUTES), bestj, bestsignificance, (bestj-i), bestimportance); }
}
//FirstPass = false;
return(rates_total);
}
//+------------------------------------------------------------------+