EnvelopesATR

Author: 2016, Tor
Indicators Used
Moving average indicatorIndicator of the average true range
Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
EnvelopesATR
//+------------------------------------------------------------------+
//|                                                 EnvelopesATR.mq5 |
//|                                              Copyright 2016, Tor |
//|                                             http://einvestor.ru/ |
//+------------------------------------------------------------------+
#property copyright "2016, Tor"
#property link      "http://einvestor.ru/"
//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_plots   4
//--- input parameters
input int                InpATRperiod=14;              // ATR Period

input int                InpMAPeriod=14;              // MA Period
input ENUM_MA_METHOD     InpMAMethod=MODE_SMA;        // MA Method
input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // MA Applied price
input double             InpDeviation=0.1;            // Deviation %
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
enum TypeEnv
  {
   DEV=0,     // Deviation %
   DAT=1,     // Deviation by ATR
   DDA=2,     // Deviation % + Deviation by ATR
  };
//--- input parameters
input TypeEnv EnvelopesType=DDA; // Envelopes Type

input color clr1 = clrRed;// Deviation % color down
input color clr2 = clrBlue;// Deviation % color up
input color clr3 = clrDarkOrange;// Deviation ATR color down
input color clr4 = clrDeepSkyBlue;// Deviation ATR color up
//--- indicator buffers
double                   ExtUpBuffer[];
double                   ExtDownBuffer[];
double                   ExtUp2Buffer[];
double                   ExtDown2Buffer[];
double                   ExtMABuffer[];
double                   ExtATRBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtUpBuffer);
   SetIndexBuffer(1,ExtDownBuffer);
   SetIndexBuffer(2,ExtUp2Buffer);
   SetIndexBuffer(3,ExtDown2Buffer);
   if(EnvelopesType==0)
     {
      SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1,clr2);
      SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1,clr1);
      SetIndexStyle(2,DRAW_NONE);
      SetIndexStyle(3,DRAW_NONE);
     }
   if(EnvelopesType==1)
     {
      SetIndexStyle(0,DRAW_NONE);
      SetIndexStyle(1,DRAW_NONE);
      SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,1,clr4);
      SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,1,clr3);
     }
   if(EnvelopesType==2)
     {
      SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1,clr2);
      SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1,clr1);
      SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,1,clr4);
      SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,1,clr3);
     }
   SetIndexBuffer(4,ExtMABuffer);
   SetIndexBuffer(5,ExtATRBuffer);
   SetIndexStyle(4,DRAW_NONE);
   SetIndexStyle(5,DRAW_NONE);
//---
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);
//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpMAPeriod-1);
//--- name for DataWindow
   IndicatorSetString(INDICATOR_SHORTNAME,"Env("+string(InpMAPeriod)+")+ATR("+string(InpATRperiod)+")");
   SetIndexLabel(0,"Env("+string(InpMAPeriod)+")Upper");
   SetIndexLabel(1,"Env("+string(InpMAPeriod)+")Lower");
   SetIndexLabel(2,"EnvATR("+string(InpATRperiod)+")Upper");
   SetIndexLabel(3,"EnvATR("+string(InpATRperiod)+")Lower");
//--- initialization done
  }
//+------------------------------------------------------------------+
//| Envelopes                                                        |
//+------------------------------------------------------------------+
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;
//--- check for bars count
   if(rates_total<=1)
   return(0);
   //--- last counted bar will be recounted
   limit=rates_total-prev_calculated;
   if(limit<InpMAPeriod){ limit=InpMAPeriod; }
   if(prev_calculated>0){ limit = limit+1; }
//--- the main loop of calculations
   for(int i=limit-InpMAPeriod; i>=0; i--)
     {
      ExtUpBuffer[i] = EMPTY_VALUE;
      ExtDownBuffer[i] = EMPTY_VALUE;
      ExtUp2Buffer[i] = EMPTY_VALUE;
      ExtDown2Buffer[i] = EMPTY_VALUE;
      ExtMABuffer[i] = iMA(_Symbol, _Period, InpMAPeriod, 0, InpMAMethod, InpAppliedPrice, i);
      ExtATRBuffer[i] = iATR(_Symbol, _Period, InpATRperiod, i);
      if(EnvelopesType==0 || EnvelopesType==2)
        {
         ExtUpBuffer[i]=(1+InpDeviation/100.0)*ExtMABuffer[i];
         ExtDownBuffer[i]=(1-InpDeviation/100.0)*ExtMABuffer[i];
        }
      if(EnvelopesType==1 || EnvelopesType==2)
        {
         ExtUp2Buffer[i]=ExtMABuffer[i]+ExtATRBuffer[i];
         ExtDown2Buffer[i]=ExtMABuffer[i]-ExtATRBuffer[i];
        }
     }
//--- done
   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 ---