0% found this document useful (1 vote)
31 views3 pages

VolEnvelopes Indicator

This document contains the source code for a custom indicator in MQL4 that calculates and plots a moving average envelope based on an ATR value. The indicator takes in parameters like period, shift, method and applied price and calculates the upper, lower and center bands to be plotted on the chart.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (1 vote)
31 views3 pages

VolEnvelopes Indicator

This document contains the source code for a custom indicator in MQL4 that calculates and plots a moving average envelope based on an ATR value. The indicator takes in parameters like period, shift, method and applied price and calculates the upper, lower and center bands to be plotted on the chart.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 3

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

//| VolEnvelopes.mq5 |
//| Copyright 2020, Phillipe S. Scofield |
//| phillipe.s.scofield@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, Phillipe S. Scofield"
#property link "phillipe.s.scofield@gmail.com"
#property version "1.00"
#property indicator_chart_window
//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots 3
#property indicator_type1 DRAW_LINE
#property indicator_type2 DRAW_LINE
#property indicator_type3 DRAW_LINE
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Black
#property indicator_label1 "Upper band"
#property indicator_label2 "Lower band"
#property indicator_label3 "Center"
//--- input parameters
input int InpMAPeriod=20; // MA Period
input int InpMAShift=0; // MA Shift
input ENUM_MA_METHOD InpMAMethod=MODE_SMA; // MA Method
input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // MA Applied price
input double InpDeviation=0.25; // ATR rate
input int InpATRPeriod=22; // ATR Period
//--- indicator buffers
double ExtUpBuffer[];
double ExtDownBuffer[];
double ExtMABuffer[];
double ExtATRBuffer[];
//--- MA handle
int ExtMAHandle;
int ExtATRHandle;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
void OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,ExtUpBuffer,INDICATOR_DATA);
SetIndexBuffer(1,ExtDownBuffer,INDICATOR_DATA);
SetIndexBuffer(2,ExtMABuffer,INDICATOR_DATA);
SetIndexBuffer(3,ExtATRBuffer,INDICATOR_CALCULATIONS);
//---
IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);
//--- sets first bar from what index will be drawn
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpMAPeriod-1);
PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpMAPeriod-1);
PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,InpMAPeriod-1);
//--- name for DataWindow
IndicatorSetString(INDICATOR_SHORTNAME,"Envelopes ATR");
//---- line shifts when drawing
PlotIndexSetInteger(0,PLOT_SHIFT,InpMAShift);
PlotIndexSetInteger(1,PLOT_SHIFT,InpMAShift);
PlotIndexSetInteger(2,PLOT_SHIFT,InpMAShift);
//---
ExtMAHandle=iMA(NULL,0,InpMAPeriod,0,InpMAMethod,InpAppliedPrice);
ExtATRHandle=iATR(NULL,0,InpATRPeriod);
//--- 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 i,limit;
//--- check for bars count
if(rates_total<InpMAPeriod)
return(0);
int calculated=BarsCalculated(ExtMAHandle);
if(calculated<rates_total)
{
Print("Not all data of ExtMAHandle is calculated (",calculated,"bars ).
Error",GetLastError());
return(0);
}
//--- we can copy not all data
int to_copy;
if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total;
else
{
to_copy=rates_total-prev_calculated;
if(prev_calculated>0) to_copy++;
}
//---- get ma buffer
if(IsStopped()) return(0); //Checking for stop flag
if(CopyBuffer(ExtMAHandle,0,0,to_copy,ExtMABuffer)<=0)
{
Print("Getting MA data is failed! Error",GetLastError());
return(0);
}
if(CopyBuffer(ExtATRHandle,0,0,to_copy,ExtATRBuffer)<=0)
{
Print("Getting ATR data is failed! Error",GetLastError());
return(0);
}
//--- preliminary calculations
limit=prev_calculated-1;
if(limit<InpMAPeriod)
limit=InpMAPeriod;
//--- the main loop of calculations
for(i=limit;i<rates_total && !IsStopped();i++)
{
ExtUpBuffer[i]=ExtMABuffer[i]+(InpDeviation*ExtATRBuffer[i-1]);
ExtDownBuffer[i]=ExtMABuffer[i]-(InpDeviation*ExtATRBuffer[i-1]);
}
//--- done
return(rates_total);
}
//+------------------------------------------------------------------+

You might also like