chande_momentum_oscillator_htf

Download script
Author: Copyright � 2015, Nikolay Kositsin

This script is designed for the MetaTrader 5 platform to display a specific technical analysis tool called the Chande Momentum Oscillator (CMO). What makes this particular version unique is its ability to pull and sync data from a higher timeframe compared to the one you are currently viewing on your chart.

Here is a breakdown of how the logic works in plain language:

Multi-Timeframe Integration

The core purpose of this script is "Higher Timeframe" (HTF) analysis. Normally, most indicators only show data for the candles on your current screen (e.g., if you are looking at 15-minute charts, the indicator only sees 15-minute data). This script allows you to choose a larger timeframe—such as the 4-hour chart—and project that information onto your smaller 15-minute chart. This helps traders see the "big picture" trend without switching back and forth between different windows.

How the Logic Processes Data

The script follows a specific sequence of steps to ensure the data is accurate:

  • Safety Validation: Before starting, the script checks if the timeframe you chose for the indicator is valid. It ensures that you aren't trying to display a "smaller" timeframe (like 1-minute) on a "larger" chart (like Daily), which would cause the data to look cluttered or incorrect.
  • Data Fetching: The script connects to the standard Chande Momentum Oscillator and requests its values based on your chosen period settings.
  • Time Synchronization: Because it is pulling data from a different timeframe, the script has to do "math" behind the scenes to make sure the dots and lines line up correctly with your current candles. It looks at the timestamps of your current chart and matches them with the corresponding timestamps of the higher timeframe.
  • Data Smoothing (Mapping): If the higher timeframe hasn't updated yet (for example, a 4-hour candle is still forming), the script intelligently carries over the last known value from the previous calculation. This ensures that the gold line on your chart remains continuous and smooth rather than jumping or disappearing.

Visual Output

  • The Gold Line: The primary result of this logic is drawn as a thick, solid gold line in a separate window at the bottom of your trading screen.
  • Reference Levels: The script automatically creates three horizontal "guide" levels (set at +50, 0, and -50). These are marked with a magenta dashed style to help you identify when the momentum is reaching extremes or crossing the zero line.
  • Customization: Users can adjust how far back the indicator looks (the Period) and slightly shift the appearance of the line left or right on the screen without changing the underlying math.
5 Views
0 Downloads
0 Favorites
chande_momentum_oscillator_htf
//+------------------------------------------------------------------+ 
//|                               Chande Momentum Oscillator_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 "Èíäèêàòîð Chande Momentum Oscillator ñ âîçìîæíîñòüþ èçìåíåíèÿ òàéìôðåéìà âî âõîäíûõ ïàðàìåòðàõ"
//--- îòðèñîâêà èíäèêàòîðà â îòäåëüíîì îêíå
#property indicator_separate_window
//---- êîëè÷åñòâî èíäèêàòîðíûõ áóôåðîâ
#property indicator_buffers 1 
//---- èñïîëüçîâàíî âñåãî îäíî ãðàôè÷åñêîå ïîñòðîåíèå
#property indicator_plots   1
//+-------------------------------------+
//| Îáúÿâëåíèå êîíñòàíò                 |
//+-------------------------------------+
#define RESET 0                                      // êîíñòàíòà äëÿ âîçâðàòà òåðìèíàëó êîìàíäû íà ïåðåñ÷åò èíäèêàòîðà
#define INDICATOR_NAME "Chande_Momentum_Oscillator"  // êîíñòàíòà äëÿ èìåíè èíäèêàòîðà
#define SIZE 1                                       // êîíñòàíòà äëÿ êîëè÷åñòâà âûçîâîâ ôóíêöèè CountIndicator
//+-------------------------------------+
//| Ïàðàìåòðû îòðèñîâêè èíäèêàòîðà 1    |
//+-------------------------------------+
//---- â êà÷åñòâå èíäèêàòîðà èñïîëüçîâàíà ëèíèÿ
#property indicator_type1   DRAW_LINE
//---- â êà÷åñòâå öâåòà ëèíèè èíäèêàòîðà èñïîëüçîâàí çîëîòîé öâåò
#property indicator_color1  clrGold
//---- ëèíèÿ èíäèêàòîðà - ñïëîøíàÿ
#property indicator_style1  STYLE_SOLID
//---- òîëùèíà ëèíèè èíäèêàòîðà ðàâíà 4
#property indicator_width1  4
//---- îòîáðàæåíèå ìåòêè èíäèêàòîðà
#property indicator_label1  INDICATOR_NAME
//---- ïàðàìåòðû ãîðèçîíòàëüíûõ óðîâíåé èíäèêàòîðà
#property indicator_level1  +50
#property indicator_level2    0
#property indicator_level3  -50
#property indicator_levelcolor clrMagenta
#property indicator_levelstyle STYLE_DASHDOTDOT
//+-------------------------------------+
//| Âõîäíûå ïàðàìåòðû èíäèêàòîðà        |
//+-------------------------------------+ 
input ENUM_TIMEFRAMES TimeFrame=PERIOD_H4;  // Ïåðèîä ãðàôèêà èíäèêàòîðà
input uint CMO_period=9;                    // Ïåðèîä CMO
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);
//---- ïîëó÷åíèå õåíäëà èíäèêàòîðà Chande Momentum Oscillator
   Ind_Handle=iCustom(Symbol(),TimeFrame,INDICATOR_NAME,CMO_period,0);
   if(Ind_Handle==INVALID_HANDLE)
     {
      Print(" Íå óäàëîñü ïîëó÷èòü õåíäë èíäèêàòîðà Chande Momentum Oscillator");
      return(INIT_FAILED);
     }
//---- èíèöèàëèçàöèÿ èíäèêàòîðíîãî áóôåðîâ
   IndInit(0,IndBuffer,EMPTY_VALUE,min_rates_total,Shift);
//---- ñîçäàíèå èìåíè äëÿ îòîáðàæåíèÿ â îòäåëüíîì ïîäîêíå è âî âñïëûâàþùåé ïîäñêàçêå
   string shortname;
   StringConcatenate(shortname,INDICATOR_NAME,"(",GetStringTimeframe(TimeFrame),")");
   IndicatorSetString(INDICATOR_SHORTNAME,shortname);
//---- îïðåäåëåíèå òî÷íîñòè îòîáðàæåíèÿ çíà÷åíèé èíäèêàòîðà
   IndicatorSetInteger(INDICATOR_DIGITS,0);
//---- çàâåðøåíèå èíèöèàëèçàöèè
   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