Author: © 2023, Alexey Viktorov
0 Views
0 Downloads
0 Favorites
AML_v4
ÿþ/********************************************************************\

|                                                            AML.mq5 |

|                                            © 2023, Alexey Viktorov |

|                       https://www.mql5.com/ru/users/alexeyvik/news |

\********************************************************************/

#property copyright "© 2023, Alexey Viktorov"

#property link      "https://www.mql5.com/ru/users/alexeyvik/news"

#property description "Adaptive Market Level"

#property version   "1.00"

#property indicator_chart_window

//#property indicator_separate_window

#define zz " ***** "

#property indicator_buffers 3

#property indicator_plots   1

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrDarkOrchid

#property indicator_label1  "AML"

#property indicator_width1  2

//--- input parameters

input int Fractal     = 6;

input int Lag         = 7;

//---

int draw_begin;

/****************indicator buffers****************/

double aml[];

double lvl[];

double fr[];

/**************Custom indicator initialization function**************/

int OnInit()

 {

  SetIndexBuffer(0, aml, INDICATOR_DATA);

  SetIndexBuffer(1, lvl, INDICATOR_CALCULATIONS);

  SetIndexBuffer(2, fr, INDICATOR_CALCULATIONS);

  draw_begin = fmax(Fractal, Lag)*2;

  PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, draw_begin);

  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[])

 {

  static int frSize = 0;

  if(prev_calculated == 0)

   {

    ArrayInitialize(aml, 1);

    ArrayInitialize(lvl, 1);

    ArrayInitialize(fr, 1);

   }

  //int maxbars = fmin(5000, TerminalInfoInteger(TERMINAL_MAXBARS)-draw_begin);

  int limit = prev_calculated == 0 ? draw_begin : rates_total-1;

  for(int i = limit; i < rates_total; i++)

   {

    double R1 = Range(Fractal, i, high, low)/Fractal,

           R2 = Range(Fractal, i-Fractal, high, low)/Fractal,

           R3 = Range(2*Fractal, i, high, low)/(2*Fractal);

    //---

    double dim=0;

    if(R1+R2 > 0 && R3 > 0)

      dim = (MathLog(R1+R2)-MathLog(R3))*1.44269504088896;



    double alpha = MathExp(-Lag*(dim-1.0));

    if(alpha > 1.0)

      alpha = 1.0;

    if(alpha < 0.01)

      alpha = 0.01;

    double price = (high[i]+low[i]+2*open[i]+2*close[i])/6;

    fr[i] = alpha*price+(1.0-alpha)*fr[i-1];



    if(MathAbs(fr[i]-fr[i-Lag]) >= Lag*Lag*_Point)

      lvl[i] = fr[i];

    else

      lvl[i] = lvl[i-1];

    aml[i] = lvl[i];

   }

  return(rates_total);

 }/******************************************************************/



/********************************************************************/

double Range(int count, int start, const double &high_[], const double &low_[])

 {

  int barHigh = ArrayMaximum(high_, start-count+1, count),

      barLow = ArrayMinimum(low_, start-count+1, count);

  double max = high_[barHigh],

         min = low_[barLow];

  return(max-min);

 }/******************************************************************/



/********************************************************************/

void OnDeinit(const int reason)

 {

//Print(__FUNCTION__, reason);

  Comment("");

 }/******************************************************************/

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---