jmomentum_htf
Download scriptThis script is designed to provide traders with a specialized "Higher Time Frame" (HTF) momentum indicator. Its primary purpose is to take data from a larger time period (like a 4-hour chart) and display it accurately on a smaller, faster chart (like a 15-minute chart).
Core Functionality
The script acts as a bridge between different timeframes. Instead of just showing the raw price movement of your current chart, it pulls data from a "Parent" timeframe that you specify in the settings. This allows you to see the "big picture" trends without switching back and forth between different windows.
How the Logic Works
To achieve this, the script follows a specific logical flow:
- Timeframe Synchronization: The tool first checks to ensure the selected higher timeframe is actually larger than your current view. If you try to set it to a smaller period, the script will alert you that this logic cannot function correctly.
- Data Retrieval: It connects to a secondary calculation called "JMomentum." It fetches the specific values from this indicator based on your chosen settings (such as smoothing depth and price type).
- Smart Alignment: This is the most important part of the logic. Because a 4-hour candle covers many smaller 15-minute candles, the script has to decide which value to show at any given moment. It looks at the current time on your chart and "searches" for the corresponding point in the higher timeframe's data.
- Persistence (Value Holding): When the higher timeframe data updates, the script captures that new value. For all the smaller candles that occur between those updates, the script "holds" the last known value. This creates a smooth, stepped line on your chart that reflects the current state of the larger trend.
Visual Output
- Dedicated Window: The indicator appears in its own separate panel at the bottom of your trading screen, rather than overlapping your price candles.
- Visual Cues: It features a thick maroon line and uses specific horizontal levels (set at 0.5, 0.0, and -0.5) with dashed styling to help you identify when momentum is shifting from positive to negative or staying neutral.
Summary for the User
In simple terms: This script allows you to watch the "momentum" of a large time period while staying on your fast trading chart. It automatically aligns the data so that the line stays updated with the higher timeframe's progress, helping you avoid making trades that go against the broader market trend.
//+------------------------------------------------------------------+
//| JMomentum_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.00"
#property description "JMomentum ñ âîçìîæíîñòüþ èçìåíåíèÿ òàéìôðåéìà âî âõîäíûõ ïàðàìåòðàõ"
//---- îòðèñîâêà èíäèêàòîðà â îòäåëüíîì îêíå
#property indicator_separate_window
//---- êîëè÷åñòâî èíäèêàòîðíûõ áóôåðîâ
#property indicator_buffers 2
//---- èñïîëüçîâàíî âñåãî îäíî ãðàôè÷åñêîå ïîñòðîåíèå
#property indicator_plots 1
//+-------------------------------------+
//| Îáúÿâëåíèå êîíñòàíò |
//+-------------------------------------+
#define RESET 0 // êîíñòàíòà äëÿ âîçâðàòà òåðìèíàëó êîìàíäû íà ïåðåñ÷åò èíäèêàòîðà
#define INDICATOR_NAME "JMomentum" // êîíñòàíòà äëÿ èìåíè èíäèêàòîðà
#define SIZE 1 // êîíñòàíòà äëÿ êîëè÷åñòâà âûçîâîâ ôóíêöèè CountIndicator
//+-------------------------------------+
//| Ïàðàìåòðû îòðèñîâêè èíäèêàòîðà 1 |
//+-------------------------------------+
//---- â êà÷åñòâå èíäèêàòîðà èñïîëüçîâàíà ëèíèÿ
#property indicator_type1 DRAW_LINE
//---- â êà÷åñòâå öâåòà ëèíèè èíäèêàòîðà èñïîëüçîâàí êîðè÷íåâûé öâåò
#property indicator_color1 clrMaroon
//---- ëèíèÿ èíäèêàòîðà - ñïëîøíàÿ
#property indicator_style1 STYLE_SOLID
//---- òîëùèíà ëèíèè èíäèêàòîðà ðàâíà 4
#property indicator_width1 4
//---- îòîáðàæåíèå ìåòêè èíäèêàòîðà
#property indicator_label1 INDICATOR_NAME
//---- ïàðàìåòðû ãîðèçîíòàëüíûõ óðîâíåé èíäèêàòîðà
#property indicator_level1 0.5
#property indicator_level2 -0.5
#property indicator_level3 0.0
#property indicator_levelcolor clrMagenta
#property indicator_levelstyle STYLE_DASHDOTDOT
//+-------------------------------------+
//| Îáúÿâëåíèå ïåðå÷èñëåíèé |
//+-------------------------------------+
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
};
//+-------------------------------------+
//| Âõîäíûå ïàðàìåòðû èíäèêàòîðà |
//+-------------------------------------+
input ENUM_TIMEFRAMES TimeFrame=PERIOD_H4; // Ïåðèîä ãðàôèêà èíäèêàòîðà
input int Length=8; // Ãëóáèíà ñãëàæèâàíèÿ
input int JLength=8; // Ãëóáèíà JMA ñãëàæèâàíèÿ èíäèêàòîðà Momentum
input int JPhase=100; // Ïàðàìåòð JMA ñãëàæèâàíèÿ
//---- èçìåíÿþùèéñÿ â ïðåäåëàõ -100 ... +100,
//---- âëèÿåò íà êà÷åñòâî ïåðåõîäíîãî ïðîöåññà;
input Applied_price_ IPC=PRICE_CLOSE; // Öåíîâàÿ êîíñòàíòà
input int Shift=0; // Ñäâèã èíäèêàòîðà ïî ãîðèçîíòàëè â áàðàõ
//+-------------------------------------+
//---- îáúÿâëåíèå äèíàìè÷åñêèõ ìàññèâîâ, êîòîðûå áóäóò â
//---- äàëüíåéøåì èñïîëüçîâàíû â êà÷åñòâå èíäèêàòîðíûõ áóôåðîâ
double IndBuffer[];
double ColorIndBuffer[];
//---- îáúÿâëåíèå ñòðîêîâûõ ïåðåìåííûõ
string Symbol_,Word;
//---- îáúÿâëåíèå öåëî÷èñëåííûõ ïåðåìåííûõ íà÷àëà îòñ÷åòà äàííûõ
int min_rates_total;
//---- îáúÿâëåíèå öåëî÷èñëåííûõ ïåðåìåííûõ äëÿ õåíäëîâ èíäèêàòîðîâ
int Ind_Handle;
//+------------------------------------------------------------------+
//| Ïîëó÷åíèå òàéìôðåéìà â âèäå ñòðîêè |
//+------------------------------------------------------------------+
string GetStringTimeframe(ENUM_TIMEFRAMES timeframe)
{return(StringSubstr(EnumToString(timeframe),7,-1));}
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---- ïðîâåðêà ïåðèîäîâ ãðàôèêîâ íà êîððåêòíîñòü
if(!TimeFramesCheck(INDICATOR_NAME,TimeFrame)) return(INIT_FAILED);
//---- èíèöèàëèçàöèÿ ïåðåìåííûõ
min_rates_total=2;
Symbol_=Symbol();
Word=INDICATOR_NAME+" èíäèêàòîð: "+Symbol_+StringSubstr(EnumToString(_Period),7,-1);
//---- ïîëó÷åíèå õåíäëà èíäèêàòîðà JMomentum
Ind_Handle=iCustom(Symbol(),TimeFrame,INDICATOR_NAME,Length,JLength,JPhase,IPC,0);
if(Ind_Handle==INVALID_HANDLE)
{
Print(" Íå óäàëîñü ïîëó÷èòü õåíäë èíäèêàòîðà JMomentum");
return(INIT_FAILED);
}
//---- èíèöèàëèçàöèÿ èíäèêàòîðíîãî áóôåðà
IndInit(0,IndBuffer,0.0,min_rates_total,Shift);
//---- ñîçäàíèå èìåíè äëÿ îòîáðàæåíèÿ â îòäåëüíîì ïîäîêíå è âî âñïëûâàþùåé ïîäñêàçêå
string shortname;
StringConcatenate(shortname,INDICATOR_NAME,"(",GetStringTimeframe(TimeFrame),")");
IndicatorSetString(INDICATOR_SHORTNAME,shortname);
//---- îïðåäåëåíèå òî÷íîñòè îòîáðàæåíèÿ çíà÷åíèé èíäèêàòîðà
IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//---- çàâåðøåíèå èíèöèàëèçàöèè
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom 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);
//---- èíäåêñàöèÿ ýëåìåíòîâ â ìàññèâàõ êàê â òàéìñåðèÿõ
ArraySetAsSeries(time,true);
//----
if(!CountIndicator(0,NULL,TimeFrame,Ind_Handle,0,IndBuffer,time,rates_total,prev_calculated,min_rates_total)) return(RESET);
//----
return(rates_total);
}
//+------------------------------------------------------------------+
//| Èíèöèàëèçàöèÿ èíäèêàòîðíîãî áóôåðà |
//+------------------------------------------------------------------+
void IndInit(int Number,double &Buffer[],double Empty_Value,int Draw_Begin,int nShift)
{
//---- ïðåâðàùåíèå äèíàìè÷åñêèõ ìàññèâîâ â èíäèêàòîðíûå áóôåðû
SetIndexBuffer(Number,Buffer,INDICATOR_DATA);
//---- îñóùåñòâëåíèå ñäâèãà íà÷àëà îòñ÷åòà îòðèñîâêè èíäèêàòîðà
PlotIndexSetInteger(Number,PLOT_DRAW_BEGIN,Draw_Begin);
//---- óñòàíîâêà çíà÷åíèé èíäèêàòîðà, êîòîðûå íå áóäóò âèäèìû íà ãðàôèêå
PlotIndexSetDouble(Number,PLOT_EMPTY_VALUE,Empty_Value);
//---- îñóùåñòâëåíèå ñäâèãà èíäèêàòîðà ïî ãîðèçîíòàëè íà Shift
PlotIndexSetInteger(Number,PLOT_SHIFT,nShift);
//---- èíäåêñàöèÿ ýëåìåíòîâ â áóôåðàõ êàê â òàéìñåðèÿõ
ArraySetAsSeries(Buffer,true);
}
//+------------------------------------------------------------------+
//| CountLine |
//+------------------------------------------------------------------+
bool CountIndicator(uint Numb, // íîìåð ôóíêöèè CountLine ïî ñïèñêó â êîäå èíäèêàòîðà (ñòàðòîâûé íîìåð - 0)
string Symb, // ñèìâîë ãðàôèêà
ENUM_TIMEFRAMES TFrame, // ïåðèîä ãðàôèêà
int IndHandle, // õåíäë îáðàáàòûâàåìîãî èíäèêàòîðà
uint BuffNumb, // íîìåð áóôåðà îáðàáàòûâàåìîãî èíäèêàòîðà
double& IndBuf[], // ïðèåìíûé áóôåð èíäèêàòîðà
const datetime& iTime[], // òàéìñåðèÿ âðåìåíè
const int Rates_Total, // êîëè÷åñòâî èñòîðèè â áàðàõ íà òåêóùåì òèêå
const int Prev_Calculated,// êîëè÷åñòâî èñòîðèè â áàðàõ íà ïðåäûäóùåì òèêå
const int Min_Rates_Total)// ìèíèìàëüíîå êîëè÷åñòâî èñòîðèè â áàðàõ äëÿ ðàñ÷åòà
{
//----
static int LastCountBar[SIZE];
datetime IndTime[1];
int limit;
//---- ðàñ÷åòû íåîáõîäèìîãî êîëè÷åñòâà êîïèðóåìûõ äàííûõ è
//---- ñòàðòîâîãî íîìåðà limit äëÿ öèêëà ïåðåñ÷åòà áàðîâ
if(Prev_Calculated>Rates_Total || Prev_Calculated<=0)// ïðîâåðêà íà ïåðâûé ñòàðò ðàñ÷åòà èíäèêàòîðà
{
limit=Rates_Total-Min_Rates_Total-1; // ñòàðòîâûé íîìåð äëÿ ðàñ÷åòà âñåõ áàðîâ
LastCountBar[Numb]=limit;
}
else limit=LastCountBar[Numb]+Rates_Total-Prev_Calculated; // ñòàðòîâûé íîìåð äëÿ ðàñ÷åòà íîâûõ áàðîâ
//---- îñíîâíîé öèêë ðàñ÷åòà èíäèêàòîðà
for(int bar=limit; bar>=0 && !IsStopped(); bar--)
{
//---- îáíóëèì ñîäåðæèìîå èíäèêàòîðíûõ áóôåðîâ äî ðàñ÷åòà
IndBuf[bar]=0.0;
//---- êîïèðóåì âíîâü ïîÿâèâøèåñÿ äàííûå â ìàññèâ IndTime
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];
//---- êîïèðóåì âíîâü ïîÿâèâøèåñÿ äàííûå â ìàññèâû
if(CopyBuffer(IndHandle,BuffNumb,iTime[bar],1,Arr)<=0) return(RESET);
//----
IndBuf[bar]=Arr[0];
}
else IndBuf[bar]=IndBuf[bar+1];
}
//----
return(true);
}
//+------------------------------------------------------------------+
//| TimeFramesCheck() |
//+------------------------------------------------------------------+
bool TimeFramesCheck(string IndName,
ENUM_TIMEFRAMES TFrame) //ïåðèîä ãðàôèêà èíäèêàòîðà
{
//---- ïðîâåðêà ïåðèîäîâ ãðàôèêîâ íà êîððåêòíîñòü
if(TFrame<Period() && TFrame!=PERIOD_CURRENT)
{
Print("Ïåðèîä ãðàôèêà äëÿ èíäèêàòîðà "+IndName+" íå ìîæåò áûòü ìåíüøå ïåðèîäà òåêóùåãî ãðàôèêà!");
Print("Ñëåäóåò èçìåíèòü âõîäíûå ïàðàìåòðû èíäèêàòîðà!");
return(RESET);
}
//----
return(true);
}
//+------------------------------------------------------------------+
Comments