Extremum Filter 2

Author: Copyright 2017, Tor
Miscellaneous
Implements a curve of type %1
2 Views
0 Downloads
0 Favorites
Extremum Filter 2
//+------------------------------------------------------------------+
//|                                              Extremum Filter.mq4 |
//|                                              Copyright 2017, Tor |
//|                                             http://einvestor.ru/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Tor"
#property link      "http://einvestor.ru/"
#property version   "2.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2

input int before=15; //Previous bars 1
input int before2=5; //Previous bars 2

double Extremum[],Extremum2[];
static int nowTrend=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorShortName("Extremum Filter 2");
   IndicatorDigits(Digits);
//--- indicator lines
   SetIndexBuffer(0,Extremum);
   SetIndexBuffer(1,Extremum2);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2,clrRed);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2,clrBlue);
   SetIndexLabel(0,"Extremum");
   SetIndexLabel(1,"Extremum2");
//---
   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 limit;
//---
   if(rates_total<=1)
      return(0);
//--- last counted bar will be recounted
   limit=rates_total-prev_calculated;
   if(prev_calculated>0)
      limit=limit+1;

   for(int x=limit-1; x>=0; x--)
     {
      Extremum[x]=0; Extremum2[x]=0;
      if(iHighest(_Symbol,_Period,MODE_HIGH,before,x)>(x+before2))
        {
         Extremum[x]=1;
        }
      if(iLowest(_Symbol,_Period,MODE_LOW,before,x)>(x+before2))
        {
         Extremum2[x]=1;
        }
      if(Extremum[x]>0 && Extremum2[x]>0){ Extremum[x]=0; Extremum2[x]=0; }
      if(Extremum[x]>0 && nowTrend>-1)
        { // Sell
         nowTrend=-1;
        }
      if(Extremum2[x]>0 && nowTrend<1)
        { // Buy
         nowTrend=1;
        }
      if(nowTrend>0){ Extremum2[x] = 1; }
      if(nowTrend<0){ Extremum[x] = 1; }
     }
//--- return value of prev_calculated for next call
   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 ---