Author: (C) 2020, Eugene Shell
Indicators Used
Indicator of the average true range
Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
ABTrend3.0
//+------------------------------------------------------------------+
//|                                                 Alpha-beta trend |
//|                                          (C) 2020, Eugene Shell  |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+

// v.3.0 Normalized by ATR

#property copyright "(C) 2020, Eugene Shell"
#property link      "https://www.mql5.com"
#property version   "3.0"
#property strict
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Trend
#property indicator_label1  "Trend"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

#property indicator_level1 0
#property indicator_level2 0.25
#property indicator_level3 -0.25

//#property indicator_color2  clrGreen

//--- input parameters
input uint     FilterPeriod=5;
input double   q=0.3;
input uint     ATRPeriod=21;

//--- indicator buffers
double   Trend[];
double   X0[];
double   X1[];

//--- global vars
double   a,b; // fast filter gain
double   dT; // time discrete
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   IndicatorBuffers(4);
//--- indicator buffers mapping
   SetIndexBuffer(0,Trend);
   SetIndexBuffer(1,X0);
   SetIndexBuffer(2,X1);

   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_NONE);
   SetIndexStyle(2,DRAW_NONE);

   SetIndexDrawBegin(0,ATRPeriod);

   dT=1;//PeriodSeconds()/60;
   a = 2.0/(FilterPeriod +1);
   b = a*q;
//---
   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[])
  {
//---
   double X0e; // extrapolatios
   double e;  // residual
   double ATR;
   int    i,pos;

   ArraySetAsSeries(Trend,false);
   ArraySetAsSeries(X0,false);
   ArraySetAsSeries(X1,false);
   ArraySetAsSeries(close,false);

// --- initialize filter ---
   if(rates_total<2)
      return(0);

   if(prev_calculated>1)
      pos=prev_calculated-1;
   else
     {
      X0[0]=0;
      X1[0]=0;
      pos=1;
     }

//--- main cycle
   for(i=pos; i<rates_total && !IsStopped(); i++)
     {
      // 2-order filter
      X0e=X0[i-1]+X1[i-1]*dT; // extrapolated value
      e=close[i]-X0e; // residual
      X0[i]=X0e+a*e;
      X1[i]=X1[i-1]+b*e/dT; // velocity estimation

      if(rates_total>int(ATRPeriod))
        {
         ATR=iATR(NULL,0,ATRPeriod,rates_total-i-1);
         if(ATR>0)
            Trend[i]=X1[i]/ATR;
         else
            Trend[i]=0;
        }
      else 
         Trend[i]=0;
     } //for

   //---- OnCalculate done. Return new prev_calculated.
   return(rates_total);
  }

//+------------------------------------------------------------------+

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 ---