ianchmom_htf
Download scriptThe provided script is designed to function as a specialized technical analysis tool that integrates multi-timeframe data into your current trading chart. It specifically acts as a bridge, pulling information from a primary indicator and displaying it in a way that respects the different time scales between your main chart and higher timeframes.
Core Logic Overview
The primary purpose of this script is to allow you to view "High Timeframe" (HTF) data on a lower timeframe chart without cluttering your screen or losing the context of the larger market trend. It achieves this by performing the following steps:
- Multi-Timeframe Integration: The script allows you to select a specific higher timeframe (such as H4) to monitor, even if you are currently viewing a much smaller timeframe (like M15).
- External Indicator Reference: Instead of calculating its own unique math from scratch, this script "calls" and reads data from another specialized tool named
iAnchMom. It looks specifically for the values and colors produced by that underlying indicator. - Time Synchronization: One of the most complex tasks in multi-timeframe analysis is matching a large time block (like an H4 candle) with many smaller candles. This script handles this logic automatically. It identifies where the higher timeframe data aligns with your current chart's timeline and "maps" those values onto your current view.
- Visual Representation: The data is presented as a multi-colored histogram in a separate window below your main price chart. The colors of the histogram are dynamically assigned based on the data received from the source indicator, providing an immediate visual cue regarding market sentiment or trend direction.
- Smart Smoothing and Filling: To ensure the visual display remains clean, the script includes logic to handle "empty" bars. If there is a gap in data due to the time difference between your chart and the higher timeframe, the script can be configured to "fill in" the information so that the visual line or histogram remains continuous and easy to read.
Summary of User Experience
For a trader using this tool, the logic boils down to contextual awareness. You stay on your preferred trading timeframe for precise entry and exit points, while the script provides a constant, color-coded visual "pulse" of what is happening on the larger, more significant timeframe. It essentially paints the "big picture" onto your "detailed picture."
//+------------------------------------------------------------------+
//| iAnchMom_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_separate_window
//---- êîëè÷åñòâî èíäèêàòîðíûõ áóôåðîâ 2
#property indicator_buffers 2
//---- èñïîëüçîâàíî âñåãî îäíî ãðàôè÷åñêèå ïîñòðîåíèå
#property indicator_plots 1
//+----------------------------------------------+
//| Ïàðàìåòðû îòðèñîâêè èíäèêàòîðà |
//+----------------------------------------------+
//---- îòðèñîâêà èíäèêàòîðà â âèäå ìíîãîöâåòíîé ãèñòîãðàììû
#property indicator_type1 DRAW_COLOR_HISTOGRAM
//---- â êà÷åñòâå öâåòîâ ãèñòîãðàììû èñïîëüçîâàíû
#property indicator_color1 clrRed,clrMagenta,clrGray,clrBlue,clrGreen
//---- ëèíèÿ èíäèêàòîðà - íåïðåðûâíàÿ êðèâàÿ
#property indicator_style1 STYLE_SOLID
//---- òîëùèíà ëèíèè èíäèêàòîðà ðàâíà 2
#property indicator_width1 2
//---- îòîáðàæåíèå ìåòêè èíäèêàòîðà
#property indicator_label1 "iAnchMom"
//+----------------------------------------------+
//| Îáúÿâëåíèå êîíñòàíò |
//+----------------------------------------------+
#define RESET 0 // êîíñòàíòà äëÿ âîçâðàòà òåðìèíàëó êîìàíäû íà ïåðåñ÷åò èíäèêàòîðà
//+----------------------------------------------+
//| Âõîäíûå ïàðàìåòðû èíäèêàòîðà |
//+----------------------------------------------+
input ENUM_TIMEFRAMES TimeFrame=PERIOD_H4; // Ïåðèîä ãðàôèêà
input uint SMAPeriod=34; // Ïåðèîä SMA
input uint EMAPeriod=20; // Ïåðèîä EMA
input ENUM_APPLIED_PRICE IPC=PRICE_CLOSE; // Öåíîâàÿ êîíñòàíòà, ïî êîòîðîé ïðîèçâîäèòñÿ ðàñ÷åò èíäèêàòîðà
input int Shift=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));
}
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---- èíèöèàëèçàöèÿ ïåðåìåííûõ íà÷àëà îòñ÷åòà äàííûõ
min_rates_total=3;
//---- ïðîâåðêà ïåðèîäîâ ãðàôèêîâ íà êîððåêòíîñòü
if(TimeFrame<Period() && TimeFrame!=PERIOD_CURRENT)
{
Print("Ïåðèîä ãðàôèêà äëÿ èíäèêàòîðà iAnchMom íå ìîæåò áûòü ìåíüøå ïåðèîäà òåêóùåãî ãðàôèêà");
return(INIT_FAILED);
}
//---- ïîëó÷åíèå õåíäëà èíäèêàòîðà iAnchMom
Ind_Handle=iCustom(Symbol(),TimeFrame,"iAnchMom",SMAPeriod,EMAPeriod,IPC,0);
if(Ind_Handle==INVALID_HANDLE)
{
Print(" Íå óäàëîñü ïîëó÷èòü õåíäë èíäèêàòîðà iAnchMom");
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,"iAnchMom HTF( ",GetStringTimeframe(TimeFrame)," )");
//--- ñîçäàíèå èìåíè äëÿ îòîáðàæåíèÿ â îòäåëüíîì ïîäîêíå è âî âñïëûâàþùåé ïîäñêàçêå
IndicatorSetString(INDICATOR_SHORTNAME,shortname);
//--- îïðåäåëåíèå òî÷íîñòè îòîáðàæåíèÿ çíà÷åíèé èíäèêàòîðà
IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);
//---- çàâåðøåíèå èíèöèàëèçàöèè
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(rates_total<min_rates_total) return(RESET);
if(BarsCalculated(Ind_Handle)<Bars(Symbol(),TimeFrame)) return(prev_calculated);
//---- îáúÿâëåíèå öåëî÷èñëåííûõ ïåðåìåííûõ
int limit,bar;
//---- îáúÿâëåíèå ïåðåìåííûõ ñ ïëàâàþùåé òî÷êîé
double Ind[2],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