mql5/Indicators/Free Indicators/Donchian Channel.mq5

176 lines
14 KiB
MQL5

2025-08-05 01:57:33 -04:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| Donchian Channel.mq5 |
//| Copyright 2009-2025, MetaQuotes Ltd |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009-2025, MetaQuotes Ltd"
#property link "http://www.mql5.com"
#property description "Donchian Channel"
//---
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots 3
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrBlue
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrGray
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrRed
//--- labels
#property indicator_label1 "Upper Donchian"
#property indicator_label2 "Middle Donchian"
#property indicator_label3 "Lower Donchian"
//--- input parameter
input int InpDonchianPeriod=20; // period of the channel
input bool InpShowLabel =true; // show price of the level
//--- indicator buffers
double ExtUpBuffer[];
double ExtMdBuffer[];
double ExtDnBuffer[];
//--- unique prefix for indicator objects
string ExtPrefixUniq;
//--- current chart scale
long ExtChartScale=2;
//--- flag indicating that the indicator is running on the built-in VPS
bool ExtBuiltOnVPS=false;
bool ExtShowLabel=false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- if the indicator is running on the built-in VPS, price labels should not be shown
if(!TerminalInfoInteger(TERMINAL_VPS))
ExtShowLabel=InpShowLabel;
//--- define buffers
SetIndexBuffer(0, ExtUpBuffer);
SetIndexBuffer(1, ExtMdBuffer);
SetIndexBuffer(2, ExtDnBuffer);
//--- set a 1-bar offset for each line
PlotIndexSetInteger(0, PLOT_SHIFT, 1);
PlotIndexSetInteger(1, PLOT_SHIFT, 1);
PlotIndexSetInteger(2, PLOT_SHIFT, 1);
//--- indicator name
IndicatorSetString(INDICATOR_SHORTNAME, "Donchian Channel");
//--- number of digits of indicator value
IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
//--- prepare prefix for objects
string number=StringFormat("%I64d", GetTickCount64());
ExtPrefixUniq=StringSubstr(number, StringLen(number)-4);
ExtPrefixUniq=ExtPrefixUniq+"_DN";
Print("Indicator \"Donchian Channels\" started, prefix=", ExtPrefixUniq);
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[])
{
//--- if the indicator has previously been calculated, start from the bar preceding the last one
int start=prev_calculated-1;