kalmanfilter_htf
Download scriptThis script is designed to bring high-level market data from a larger timeframe into your current trading view. It acts as a bridge, allowing you to see the "big picture" trend without having to constantly switch back and forth between different time charts.
Core Logic Overview
The primary function of this tool is to fetch information from a specific, higher timeframe (for example, looking at 4-hour trends while you are trading on a 15-minute chart) and display it as a smooth signal line on your active screen. It utilizes a mathematical approach known as a "Kalman Filter" to process this data, which helps in creating a cleaner, more stable trend line by filtering out minor market "noise."
Key Features and Functionality
- Multi-Timeframe Integration: You can choose a specific timeframe for the indicator to monitor. It will automatically pull the data from that higher period and project it onto your current screen.
- Smart Data Synchronization: The script is designed to be precise with time. It ensures that the data shown on your chart aligns perfectly with the correct timestamps of the higher timeframe, preventing "misaligned" signals.
- Dynamic Visual Feedback: The indicator features a multi-colored line. Depending on how you configure it, the line can change colors to visually signal different market conditions (such as trend directions), making it easier to identify shifts in momentum at a glance.
- Customizable Price Inputs: The logic allows you to choose which specific price point from a candle should be used for calculations—such as the Close price, Open price, High/Low averages, or even more complex weighted averages.
- Smoothing & Shift Controls: Users can adjust how much the data is smoothed (via a smoothing coefficient) and manually shift the indicator's position both horizontally (across bars) and vertically (in price points) to better suit their personal trading layout.
- Seamless Re-drawing: The script includes logic to ensure that if data is updated or missing during live market movement, it can "fill in" the gaps or re-draw information to maintain a continuous, unbroken line on your chart.
How it Works for the User
When you add this to your platform, the script looks at the higher timeframe you've selected, runs the Kalman Filter calculation on that data, and draws a thick, colored "Signal Line" on your current chart. This allows you to trade with the awareness of the broader trend while still performing your analysis on a more detailed timeframe.
//+------------------------------------------------------------------+
//| KalmanFilter_HTF.mq5 |
//| Copyright © 2015, Nikolay Kositsin |
//| Khabarovsk, farria@mail.redcom.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2015, Nikolay Kositsin"
#property link "farria@mail.redcom.ru"
//---- íîìåð âåðñèè èíäèêàòîðà
#property version "1.60"
//---- îòðèñîâêà èíäèêàòîðà â ãëàâíîì îêíå
#property indicator_chart_window
//---- êîëè÷åñòâî èíäèêàòîðíûõ áóôåðîâ 2
#property indicator_buffers 2
//---- èñïîëüçîâàíî âñåãî îäíî ãðàôè÷åñêèå ïîñòðîåíèå
#property indicator_plots 1
//+----------------------------------------------+
//| Ïàðàìåòðû îòðèñîâêè èíäèêàòîðà |
//+----------------------------------------------+
//---- îòðèñîâêà èíäèêàòîðà â âèäå òðåõöâåòíîé ëèíèè
#property indicator_type1 DRAW_COLOR_LINE
//---- â êà÷åñòâå öâåòîâ òðåõöâåòíîé ëèíèè èñïîëüçîâàíû
#property indicator_color1 clrOrange,clrTurquoise
//---- ëèíèÿ èíäèêàòîðà - ñïëîøíàÿ
#property indicator_style1 STYLE_SOLID
//---- òîëùèíà ëèíèè èíäèêàòîðà ðàâíà 3
#property indicator_width1 3
//---- îòîáðàæåíèå ìåòêè ñèãíàëüíîé ëèíèè
#property indicator_label1 "Signal Line"
//+----------------------------------------------+
//| Îáúÿâëåíèå ïåðå÷èñëåíèé |
//+----------------------------------------------+
enum Applied_price_ //òèï êîíñòàíòû
{
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_, //Simpl Price (OC/2)
PRICE_QUARTER_, //Quarted Price (HLOC/4)
PRICE_TRENDFOLLOW0_, //TrendFollow_1 Price
PRICE_TRENDFOLLOW1_, //TrendFollow_2 Price
PRICE_DEMARK_ //Demark Price
};
//+----------------------------------------------+
enum Signal_mode
{
Trend, //ïî òðåíäó
Kalman //ïî Êàëüìàíó
};
//+----------------------------------------------+
//| Îáúÿâëåíèå êîíñòàíò |
//+----------------------------------------------+
#define RESET 0 // êîíñòàíòà äëÿ âîçâðàòà òåðìèíàëó êîìàíäû íà ïåðåñ÷åò èíäèêàòîðà
//+----------------------------------------------+
//| Âõîäíûå ïàðàìåòðû èíäèêàòîðà |
//+----------------------------------------------+
input ENUM_TIMEFRAMES TimeFrame=PERIOD_H4; // Ïåðèîä ãðàôèêà èíäèêàòîðà
input double K=1.0; // Êîýôôèöèåíò ñãëàæèâàíèÿ
input Applied_price_ IPC=PRICE_WEIGHTED; // Öåíîâàÿ êîíñòàíòà
input Signal_mode Signal=Kalman; // Ìåòîä èçìåíåíèÿ îêðàñêè ëèíèè
input int Shift=0; // Ñäâèã èíäèêàòîðà ïî ãîðèçîíòàëè â áàðàõ
input int PriceShift=0; // Ñäâèã èíäèêàòîðà ïî âåðòèêàëè â ïóíêòàõ
input bool ReDraw=true; // Ïîâòîð îòîáðàæåíèÿ èíôîðìàöèè íà ïóñòûõ áàðàõ
//+----------------------------------------------+
//---- îáúÿâëåíèå öåëî÷èñëåííûõ ïåðåìåííûõ íà÷àëà îòñ÷åòà äàííûõ
int min_rates_total;
//---- îáúÿâëåíèå öåëî÷èñëåííûõ ïåðåìåííûõ äëÿ õåíäëîâ èíäèêàòîðîâ
int Ind_Handle;
//---- îáúÿâëåíèå äèíàìè÷åñêèõ ìàññèâîâ, êîòîðûå áóäóò â
//---- äàëüíåéøåì èñïîëüçîâàíû â êà÷åñòâå èíäèêàòîðíûõ áóôåðîâ
double IndBuffer[],ColorIndBuffer[];
//+------------------------------------------------------------------+
//| Ïîëó÷åíèå òàéìôðåéìà â âèäå ñòðîêè |
//+------------------------------------------------------------------+
string GetStringTimeframe(ENUM_TIMEFRAMES timeframe)
{
//----
return(StringSubstr(EnumToString(timeframe),7,-1));
}
//+------------------------------------------------------------------+
//| KalmanFilter indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---- èíèöèàëèçàöèÿ ïåðåìåííûõ íà÷àëà îòñ÷åòà äàííûõ
min_rates_total=3;
//---- ïðîâåðêà ïåðèîäîâ ãðàôèêîâ íà êîððåêòíîñòü
if(TimeFrame<Period() && TimeFrame!=PERIOD_CURRENT)
{
Print("Ïåðèîä ãðàôèêà äëÿ èíäèêàòîðà KalmanFilter íå ìîæåò áûòü ìåíüøå ïåðèîäà òåêóùåãî ãðàôèêà");
return(INIT_FAILED);
}
//---- ïîëó÷åíèå õåíäëà èíäèêàòîðà KalmanFilter
Ind_Handle=iCustom(Symbol(),TimeFrame,"KalmanFilter",K,IPC,Signal,0,PriceShift);
if(Ind_Handle==INVALID_HANDLE)
{
Print(" Íå óäàëîñü ïîëó÷èòü õåíäë èíäèêàòîðà KalmanFilter");
return(INIT_FAILED);
}
//---- ïðåâðàùåíèå äèíàìè÷åñêîãî ìàññèâà IndBuffer â èíäèêàòîðíûé áóôåð
SetIndexBuffer(0,IndBuffer,INDICATOR_DATA);
//---- îñóùåñòâëåíèå ñäâèãà íà÷àëà îòñ÷åòà îòðèñîâêè èíäèêàòîðà
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);
//---- óñòàíîâêà çíà÷åíèé èíäèêàòîðà, êîòîðûå íå áóäóò âèäèìû íà ãðàôèêå
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//---- èíäåêñàöèÿ ýëåìåíòîâ â áóôåðå êàê â òàéìñåðèè
ArraySetAsSeries(IndBuffer,true);
//---- ïðåâðàùåíèå äèíàìè÷åñêîãî ìàññèâà â öâåòîâîé, èíäåêñíûé áóôåð
SetIndexBuffer(1,ColorIndBuffer,INDICATOR_COLOR_INDEX);
//---- èíäåêñàöèÿ ýëåìåíòîâ â áóôåðå êàê â òàéìñåðèè
ArraySetAsSeries(ColorIndBuffer,true);
//---- èíèöèàëèçàöèÿ ïåðåìåííîé äëÿ êîðîòêîãî èìåíè èíäèêàòîðà
string shortname;
StringConcatenate(shortname,"KalmanFilter HTF( ",GetStringTimeframe(TimeFrame)," )");
//--- ñîçäàíèå èìåíè äëÿ îòîáðàæåíèÿ â îòäåëüíîì ïîäîêíå è âî âñïëûâàþùåé ïîäñêàçêå
IndicatorSetString(INDICATOR_SHORTNAME,shortname);
//--- îïðåäåëåíèå òî÷íîñòè îòîáðàæåíèÿ çíà÷åíèé èíäèêàòîðà
IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//---- çàâåðøåíèå èíèöèàëèçàöèè
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| KalmanFilter 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(rates_total<min_rates_total) return(RESET);
if(BarsCalculated(Ind_Handle)<Bars(Symbol(),TimeFrame)) return(prev_calculated);
//---- îáúÿâëåíèå öåëî÷èñëåííûõ ïåðåìåííûõ
int limit,bar;
//---- îáúÿâëåíèå ïåðåìåííûõ ñ ïëàâàþùåé òî÷êîé
double Ind[1],Col[1];
datetime IndTime[1];
static uint LastCountBar;
//---- ðàñ÷åòû íåîáõîäèìîãî êîëè÷åñòâà êîïèðóåìûõ äàííûõ è
//---- ñòàðòîâîãî íîìåðà limit äëÿ öèêëà ïåðåñ÷åòà áàðîâ
if(prev_calculated>rates_total || prev_calculated<=0)// ïðîâåðêà íà ïåðâûé ñòàðò ðàñ÷åòà èíäèêàòîðà
{
limit=rates_total-min_rates_total-1; // ñòàðòîâûé íîìåð äëÿ ðàñ÷åòà âñåõ áàðîâ
LastCountBar=rates_total;
}
else limit=int(LastCountBar)+rates_total-prev_calculated; // ñòàðòîâûé íîìåð äëÿ ðàñ÷åòà íîâûõ áàðîâ
//---- èíäåêñàöèÿ ýëåìåíòîâ â ìàññèâàõ êàê â òàéìñåðèÿõ
ArraySetAsSeries(time,true);
//---- îñíîâíîé öèêë ðàñ÷åòà èíäèêàòîðà
for(bar=limit; bar>=0 && !IsStopped(); bar--)
{
//---- îáíóëèì ñîäåðæèìîå èíäèêàòîðíûõ áóôåðîâ äî ðàñ÷åòà
IndBuffer[bar]=EMPTY_VALUE;
ColorIndBuffer[bar]=0;
//---- êîïèðóåì âíîâü ïîÿâèâøèåñÿ äàííûå â ìàññèâ
if(CopyTime(Symbol(),TimeFrame,time[bar],1,IndTime)<=0) return(RESET);
//----
if(time[bar]>=IndTime[0] && time[bar+1]<IndTime[0])
{
LastCountBar=bar;
//---- êîïèðóåì âíîâü ïîÿâèâøèåñÿ äàííûå â ìàññèâû
if(CopyBuffer(Ind_Handle,0,time[bar],1,Ind)<=0) return(RESET);
if(CopyBuffer(Ind_Handle,1,time[bar],1,Col)<=0) return(RESET);
//---- çàãðóçêà ïîëó÷åííûõ çíà÷åíèé â èíäèêàòîðíûå áóôåðû
IndBuffer[bar]=Ind[0];
ColorIndBuffer[bar]=Col[0];
}
if(ReDraw)
{
if(IndBuffer[bar+1]!=EMPTY_VALUE && IndBuffer[bar]==EMPTY_VALUE)
{
IndBuffer[bar]=IndBuffer[bar+1];
ColorIndBuffer[bar]=ColorIndBuffer[bar+1];
}
}
}
//----
return(rates_total);
}
//+------------------------------------------------------------------+
Comments