//+------------------------------------------------------------------+
//| Keltner_Channel_HTF.mq5 |
//| Copyright © 2013, Nikolay Kositsin |
//| Khabarovsk, farria@mail.redcom.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013, Nikolay Kositsin"
#property link "farria@mail.redcom.ru"
//--- Indicator version
#property version "1.00"
#property description "Keltner Channel with a preset timeframe specified in input parameters"
//--- drawing the indicator in the main window
#property indicator_chart_window
//---- number of indicator buffers is 3
#property indicator_buffers 3
//---- three plots are used
#property indicator_plots 3
//+----------------------------------------------+
//| declaring constants |
//+----------------------------------------------+
#define RESET 0 // A constant for returning the indicator recalculation command to the terminal
#define INDICATOR_NAME "Keltner Channel" // A constant for the indicator name
#define SIZE 1 // A constant for the number of calls of the CountIndicator function in the code
//+----------------------------------------------+
//| Indicator 1 drawing parameters |
//+----------------------------------------------+
//--- drawing indicator 1 as a line
#property indicator_type1 DRAW_LINE
//--- yellow color is used as the color of the indicator line
#property indicator_color1 clrYellow
//---- the indicator line is a continuous curve
#property indicator_style1 STYLE_SOLID
//--- indicator 1 line width is equal to 2
#property indicator_width1 2
//--- displaying the indicator label
#property indicator_label1 INDICATOR_NAME+"Up"
//+----------------------------------------------+
//| Indicator 2 drawing parameters |
//+----------------------------------------------+
//--- drawing indicator 2 as a line
#property indicator_type2 DRAW_LINE
//--- use gray color for the indicator line
#property indicator_color2 clrGray
//---- the indicator line is a continuous curve
#property indicator_style2 STYLE_SOLID
//--- indicator 2 line width is equal to 2
#property indicator_width2 2
//--- displaying the indicator label
#property indicator_label2 INDICATOR_NAME+"Middle"
//+----------------------------------------------+
//| Indicator 3 drawing parameters |
//+----------------------------------------------+
//--- drawing indicator 3 as a line
#property indicator_type3 DRAW_LINE
//--- Pink color is used for indicator line
#property indicator_color3 clrMagenta
//---- the indicator line is a continuous curve
#property indicator_style3 STYLE_SOLID
//---- indicator 3 line width is equal to 4
#property indicator_width3 4
//--- displaying the indicator label
#property indicator_label3 INDICATOR_NAME+"Down"
//+----------------------------------------------+
//| declaration of enumerations |
//+----------------------------------------------+
enum Applied_price_ // type of constant
{
PRICE_CLOSE_ = 1, // Close
PRICE_OPEN_, // Open
PRICE_HIGH_, // High
PRICE_LOW_, // Low
PRICE_MEDIAN_, // Median Price (HL/2)
PRICE_TYPICAL_, // Typical Price (HLC/3)
PRICE_WEIGHTED_, // Weighted Close (HLCC/4)
PRICE_SIMPL_, // Simple Price (OC/2)
PRICE_QUARTER_, // Quarted Price (HLOC/4)
PRICE_TRENDFOLLOW0_, // TrendFollow_1 Price
PRICE_TRENDFOLLOW1_, // TrendFollow_2 Price
PRICE_DEMARK_ // Demark Price
};
//+----------------------------------------------+
//| Indicator input parameters |
//+----------------------------------------------+
input ENUM_TIMEFRAMES TimeFrame=PERIOD_H4; // Indicator chart period
//---
input int KeltnerPeriod=20; // Period of averaging
input ENUM_MA_METHOD MA_Method_=MODE_SMA; // Method of averaging
input double Ratio = 1.0; // Expansion ratio
input Applied_price_ IPC=PRICE_CLOSE_; // Price constant
input int Shift=0; // Horizontal shift of the indicator in bars
//--- declaration of dynamic arrays that
//--- will be used as indicator buffers
double Ind1Buffer[];
double Ind2Buffer[];
double Ind3Buffer[];
//--- declaration of integer variables for the start of data calculation
int min_rates_total;
//--- declaration of integer variables for the indicators handles
int Ind_Handle;
//+------------------------------------------------------------------+
//| Getting a timeframe as a line |
//+------------------------------------------------------------------+
string GetStringTimeframe(ENUM_TIMEFRAMES timeframe)
{return(StringSubstr(EnumToString(timeframe),7,-1));}
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- Checking correctness of the chart periods
if(!TimeFramesCheck(INDICATOR_NAME,TimeFrame)) return(INIT_FAILED);
//--- initialization of variables
min_rates_total=2;
//--- getting the handle of the Keltner_Channel indicator
Ind_Handle=iCustom(Symbol(),TimeFrame,"Keltner_Channel",KeltnerPeriod,MA_Method_,Ratio,IPC);
if(Ind_Handle==INVALID_HANDLE)
{
Print(" Failed to get the handle of Keltner_Channel");
return(INIT_FAILED);
}
//--- Initialize indicator buffers
IndInit(0,Ind1Buffer,0.0,min_rates_total,Shift);
IndInit(1,Ind2Buffer,0.0,min_rates_total,Shift);
IndInit(2,Ind3Buffer,0.0,min_rates_total,Shift);
//--- creation of the name to be displayed in a separate sub-window and in a pop up help
string shortname;
StringConcatenate(shortname,INDICATOR_NAME,"(",KeltnerPeriod,", ",
EnumToString(MA_Method_),", ",DoubleToString(Ratio,2),", ",EnumToString(IPC),")");
//---
IndicatorSetString(INDICATOR_SHORTNAME,shortname);
//--- determining the accuracy of the indicator values
IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//--- initialization end
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total, // number of bars in history at the current tick
const int prev_calculated,// amount of history in bars at the previous tick
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[])
{
//--- checking if the number of bars is enough for the calculation
if(rates_total<min_rates_total) return(RESET);
if(BarsCalculated(Ind_Handle)<Bars(Symbol(),TimeFrame)) return(prev_calculated);
//--- indexing elements in arrays as in timeseries
ArraySetAsSeries(time,true);
//---
if(!CountIndicator(0,NULL,TimeFrame,Ind_Handle,0,Ind1Buffer,1,Ind2Buffer,2,Ind3Buffer,
time,rates_total,prev_calculated,min_rates_total)) return(RESET);
//---
return(rates_total);
}
//---
//+------------------------------------------------------------------+
//| Indicator buffer initialization |
//+------------------------------------------------------------------+
void IndInit(int Number,double &Buffer[],double Empty_Value,int Draw_Begin,int nShift)
{
//--- set dynamic array as an indicator buffer
SetIndexBuffer(Number,Buffer,INDICATOR_DATA);
//--- shifting the start of drawing of the indicator
PlotIndexSetInteger(Number,PLOT_DRAW_BEGIN,Draw_Begin);
//--- setting the indicator values that won't be visible on a chart
PlotIndexSetDouble(Number,PLOT_EMPTY_VALUE,Empty_Value);
//---- shifting the indicator 2 horizontally by Shift
PlotIndexSetInteger(Number,PLOT_SHIFT,nShift);
//--- indexing elements in the buffer as in timeseries
ArraySetAsSeries(Buffer,true);
//---
}
//+------------------------------------------------------------------+
//| CountIndicator |
//+------------------------------------------------------------------+
bool CountIndicator(uint Numb, // The number of the CountLine function in the list in the indicator code (starting number - 0)
string Symb, // Chart symbol
ENUM_TIMEFRAMES TFrame, // Chart period
int IndHandle, // The handle of the processed indicator
uint BuffNumb1, // The number of the buffer of the processed indicator 1
double& IndBuf1[], // Receiving buffer of the indicator 1
uint BuffNumb2, // The number of the buffer of the processed indicator 2
double& IndBuf2[], // Receiving buffer of the indicator 2
uint BuffNumb3, // The number of the buffer of the processed indicator 3
double& IndBuf3[], // Receiving buffer of the indicator 3
const datetime& iTime[], // Timeseries of time
const int Rates_Total, // Amount of history in bars on the current tick
const int Prev_Calculated,// amount of history in bars at the previous tick
const int Min_Rates_Total)// minimum amount of history in bars for calculation
{
//---
static int LastCountBar[SIZE];
datetime IndTime[1];
int limit;
//--- calculations of the necessary amount of data to be copied
//--- and the 'limit' starting index for the bars recalculation loop
if(Prev_Calculated>Rates_Total || Prev_Calculated<=0)// Checking for the first start of the indicator calculation
{
limit=Rates_Total-Min_Rates_Total-1; // Starting index for calculation of all bars
LastCountBar[Numb]=limit;
}
else limit=LastCountBar[Numb]+Rates_Total-Prev_Calculated; // Starting index for calculation of new bars
//--- main indicator calculation loop
for(int bar=limit; bar>=0 && !IsStopped(); bar--)
{
//---- Copy new data to the IndTime array
if(CopyTime(Symbol(),TFrame,iTime[bar],1,IndTime)<=0) return(RESET);
if(iTime[bar]>=IndTime[0] && iTime[bar+1]<IndTime[0])
{
LastCountBar[Numb]=bar;
double Arr[1];
//--- copy newly appeared data in the arrays
if(CopyBuffer(IndHandle,BuffNumb1,iTime[bar],1,Arr)<=0) return(RESET); IndBuf1[bar]=Arr[0];
if(CopyBuffer(IndHandle,BuffNumb2,iTime[bar],1,Arr)<=0) return(RESET); IndBuf2[bar]=Arr[0];
if(CopyBuffer(IndHandle,BuffNumb3,iTime[bar],1,Arr)<=0) return(RESET); IndBuf3[bar]=Arr[0];
}
else
{
IndBuf1[bar]=IndBuf1[bar+1];
IndBuf2[bar]=IndBuf2[bar+1];
IndBuf3[bar]=IndBuf3[bar+1];
}
}
//---
return(true);
}
//+------------------------------------------------------------------+
//| TimeFramesCheck() |
//+------------------------------------------------------------------+
bool TimeFramesCheck(string IndName,
ENUM_TIMEFRAMES TFrame) // Indicator chart period
{
//--- checking correctness of the chart periods
if(TFrame<Period() && TFrame!=PERIOD_CURRENT)
{
Print("Chart period for the "+IndName+" indicator cannot be less than the period of the current chart!");
Print ("You must change the indicator input parameters!");
return(RESET);
}
//---
return(true);
}
//+------------------------------------------------------------------+
Comments