//+------------------------------------------------------------------+
//|                                                     heik_mod.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include "indicator 2//ind_b.mqh"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2

#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrGreen
#property indicator_style1  STYLE_DASH
#property indicator_width1  2

#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrRed
#property indicator_style2  STYLE_DASH
#property indicator_width2  2
datetime expirations = datetime("2024.03.19 00:00:00");
double ExtMapBuffer1[];
double ExtMapBuffer2[];
input string ss_01="settings";//settings
input int bar_cal=200;//History bars
input color up_b=clrLightGreen;//color high
input color do_b=clrYellow;//color low
input string ss_02="settings Heiken";//settings Heiken
input int               mai_par=10;//1 parameter
input ENUM_MA_METHOD    ma_me=MODE_LWMA;//2 parameter
input color             uptre=clrDarkTurquoise;//3 parameter
input color             dotre=clrCrimson;//4 parameter
input bool              plot_back=false;//5 parameter
input bool              en_push=false;//6 parameter
input bool              en_aler=false;//7 parameter
ind_b indb;
int bars_calc;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(TimeCurrent()>expirations)
     {
      Alert("Demo over");
      return(INIT_FAILED);
     }
   bars_calc=bar_cal<0?0:bar_cal;
   ResetLastError();
   if(!indb.set_params(mai_par,ma_me,uptre,dotre,plot_back,en_push,en_aler))
     {
      Alert(__FUNCTION__+" some indicator error heinsi:",GetLastError());
      return -1;
     }
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
   SetIndexBuffer(0,ExtMapBuffer1,INDICATOR_DATA);
   SetIndexBuffer(1,ExtMapBuffer2,INDICATOR_DATA);
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   ArraySetAsSeries(ExtMapBuffer1,true);
   ArraySetAsSeries(ExtMapBuffer2,true);
   PlotIndexSetInteger(0,PLOT_ARROW,160);
   PlotIndexSetInteger(1,PLOT_ARROW,160);
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,up_b);
   PlotIndexSetInteger(1,PLOT_LINE_COLOR,do_b);
   PlotIndexSetString(0,PLOT_LABEL,"High");
   PlotIndexSetString(1,PLOT_LABEL,"Low");
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   Comment("");
  }
//+------------------------------------------------------------------+
//| 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 summ=BarsCalculated(indb.get_handle());

   int needds=summ<bars_calc?summ:bars_calc;

   ArrayInitialize(ExtMapBuffer1,0);
   ArrayInitialize(ExtMapBuffer2,0);
   double buf_up[];
   double buf_do[];
   double buf_color[];
   indb.fill_arrays(buf_up,buf_do,buf_color,needds);
   ArraySetAsSeries(buf_color,true);
   bool b_n=false;
   int num_nach=needds-1;
   int num_kon=needds-1;
   for(int i=needds-1; i>=0; i--)
     {
      if(buf_color[i]>0.91&&!b_n)
        {
         //Alert(__FUNCTION__+" i ",i," ",buf_color[i]);
         b_n=true;
         num_nach=i;
        }
      if(buf_color[i]>0.91)
        {
         num_kon=i;
        }
      if(buf_color[i]<0.01&&b_n)
        {
         b_n=false;
         num_kon=i+1;
         int find=ArrayMaximum(buf_up,num_kon,(num_nach-num_kon+1));
         double price=buf_up[find];
         for(int k=find; k>find-10; k--)
           {
            if(k<0)
               break;
            ExtMapBuffer1[k]=price;
           }
        }
      if(num_kon==0&&i==0)
        {
         ExtMapBuffer1[0]=buf_up[0];
        }
     }
   b_n=false;
   num_nach=needds-1;
   num_kon=needds-1;
   for(int i=needds-1; i>=0; i--)
     {
      if(buf_color[i]<0.01&&!b_n)
        {
         //Alert(__FUNCTION__+" i ",i," ",buf_color[i]);
         b_n=true;
         num_nach=i;
        }
      if(buf_color[i]<0.01)
        {
         num_kon=i;
        }
      if(buf_color[i]>0.91&&b_n)
        {
         b_n=false;
         num_kon=i+1;
         int find=ArrayMinimum(buf_do,num_kon,(num_nach-num_kon+1));
         double price=buf_do[find];
         for(int k=find; k>find-10; k--)
           {
            if(k<0)
               break;
            ExtMapBuffer2[k]=price;
           }
        }
      if(num_kon==0&&i==0)
        {
         ExtMapBuffer2[0]=buf_do[0];
        }
     }
   /*Comment(__FUNCTION__+"","\nbuf_up: ",DoubleToString(buf_up[1],_Digits),
           "\nbuf_do: ",DoubleToString(buf_do[1],_Digits),
           "\nbuf_col: ",DoubleToString(buf_color[1],_Digits));*/
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---

  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---

  }
//+------------------------------------------------------------------+
