emapredictive2_htf

Download script
Author: Copyright � 2015, Nikolay Kositsin

This script is designed to provide a visual representation of market trends by pulling data from a higher timeframe and projecting it onto your current chart. It essentially acts as a "bridge" that allows you to see the influence of larger market movements on your smaller, more detailed view.

Core Logic and Functionality

Higher Timeframe Integration The primary purpose of this script is to retrieve information from a specific, larger timeframe (for example, viewing 4-hour trends while trading on a 15-minute chart). It looks for a specific technical indicator named "EMAPredictive2" on that larger timeframe and brings its values back to your current screen.

Data Synchronization and Alignment Because the script is pulling data from a different time period, it performs several background tasks to ensure the information aligns correctly:

  • Time Matching: It constantly checks the timestamps of the current chart against the higher timeframe to ensure that the data being displayed corresponds exactly to the correct moment in history.
  • Data Filling (Smoothing): Since higher timeframe bars cover a much larger span of time than smaller candles, the script fills in the gaps between those large data points. If a new piece of data from the high timeframe isn't available for a specific tiny candle on your current chart, the script carries forward the previous known value. This creates a continuous, fluid line rather than a series of disconnected dots.

Customizable Visuals and Offsets The logic includes several user-adjustable settings to refine how this information is presented:

  • Period Settings: You can define the specific lengths for the "Long" and "Short" periods used by the underlying calculation.
  • Extrapolation: There is a specific setting to slightly "forward" or project the location of the data, which helps in visualizing where the trend might be heading relative to the current price.
  • Visual Positioning: You can manually shift the line horizontally (moving it forward or backward in time) or vertically (moving it up or down on the price scale) to better align it with your personal trading perspective.

Summary of Behavior

When you load this onto a chart, it looks at a "big picture" timeframe, grabs the predictive trend data from there, and "paints" that trend onto your current screen as a solid magenta line. It handles all the complex math of syncing two different time speeds so that you can see how the major trends are influencing the immediate price action.

11 Views
0 Downloads
0 Favorites
emapredictive2_htf
//+------------------------------------------------------------------+ 
//|                                           EMAPredictive2_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 indicator_chart_window 
//--- äëÿ ðàñ÷åòà è îòðèñîâêè èíäèêàòîðà èñïîëüçîâàí îäèí áóôåð
#property indicator_buffers 1
//--- èñïîëüçîâàíî îäíî ãðàôè÷åñêîå ïîñòðîåíèå
#property indicator_plots   1
//+----------------------------------------------+
//| Îáúÿâëåíèå êîíñòàíò                          |
//+----------------------------------------------+
#define RESET 0                         // êîíñòàíòà äëÿ âîçâðàòà òåðìèíàëó êîìàíäû íà ïåðåñ÷åò èíäèêàòîðà
#define INDICATOR_NAME "EMAPredictive2" // êîíñòàíòà äëÿ èìåíè èíäèêàòîðà
//+----------------------------------------------+
//| Ïàðàìåòðû îòðèñîâêè èíäèêàòîðà               |
//+----------------------------------------------+
//--- îòðèñîâêà èíäèêàòîðà 1 â âèäå ëèíèè
#property indicator_type1   DRAW_LINE
//---- â êà÷åñòâå öâåòà ëèíèè èíäèêàòîðà èñïîëüçîâàí ðîçîâûé öâåò
#property indicator_color1 clrMagenta
//--- ëèíèÿ èíäèêàòîðà 1 - íåïðåðûâíàÿ êðèâàÿ
#property indicator_style1  STYLE_SOLID
//--- òîëùèíà ëèíèè èíäèêàòîðà 1 ðàâíà 3
#property indicator_width1  3
//--- îòîáðàæåíèå ìåòêè èíäèêàòîðà
#property indicator_label1  INDICATOR_NAME
//+----------------------------------------------+
//| Âõîäíûå ïàðàìåòðû èíäèêàòîðà                 |
//+----------------------------------------------+
input ENUM_TIMEFRAMES TimeFrame=PERIOD_H4; // Ïåðèîä ãðàôèêà
input double LongPeriod=25.0;
input double ShortPeriod=8.0;
input double ExtraTimeForward=1.0;         // How much to further extrapolate location
input int    Shift=0;                      // Ñäâèã èíäèêàòîðà ïî ãîðèçîíòàëè â áàðàõ
input int    PriceShift=0;                 // Ñäâèã èíäèêàòîðà ïî âåðòèêàëè â ïóíêòàõ
//+----------------------------------------------+
//--- îáúÿâëåíèå äèíàìè÷åñêèõ ìàññèâîâ, êîòîðûå â äàëüíåéøåì
//--- áóäóò èñïîëüçîâàíû â êà÷åñòâå èíäèêàòîðíûõ áóôåðîâ
double IndBuffer[];
//--- îáúÿâëåíèå ñòðîêîâûõ ïåðåìåííûõ
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(TimeFrame<Period() && TimeFrame!=PERIOD_CURRENT)
     {
      Print("Ïåðèîä ãðàôèêà äëÿ èíäèêàòîðà EMAPredictive2 íå ìîæåò áûòü ìåíüøå ïåðèîäà òåêóùåãî ãðàôèêà");
      return(INIT_FAILED);
     }
//--- èíèöèàëèçàöèÿ ïåðåìåííûõ 
   min_rates_total=2;
   Symbol_=Symbol();
   Word=INDICATOR_NAME+" èíäèêàòîð: "+Symbol_+StringSubstr(EnumToString(_Period),7,-1);
//--- ïîëó÷åíèå õåíäëà èíäèêàòîðà EMAPredictive2
   Ind_Handle=iCustom(Symbol_,TimeFrame,"EMAPredictive2",LongPeriod,ShortPeriod,ExtraTimeForward,0,PriceShift);
   if(Ind_Handle==INVALID_HANDLE)
     {
      Print(" Íå óäàëîñü ïîëó÷èòü õåíäë èíäèêàòîðà EMAPredictive2");
      return(INIT_FAILED);
     }
//--- ïðåâðàùåíèå äèíàìè÷åñêîãî ìàññèâà â èíäèêàòîðíûé áóôåð
   SetIndexBuffer(0,IndBuffer,INDICATOR_DATA);
//--- îñóùåñòâëåíèå ñäâèãà íà÷àëà îòñ÷åòà îòðèñîâêè èíäèêàòîðà 1
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);
//--- óñòàíîâêà çíà÷åíèé èíäèêàòîðà, êîòîðûå íå áóäóò âèäèìû íà ãðàôèêå
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//---- îñóùåñòâëåíèå ñäâèãà èíäèêàòîðà ïî ãîðèçîíòàëè íà Shift
   PlotIndexSetInteger(0,PLOT_SHIFT,Shift);
//--- èíäåêñàöèÿ ýëåìåíòîâ â áóôåðå êàê â òàéìñåðèè
   ArraySetAsSeries(IndBuffer,true);
//--- ñîçäàíèå èìåíè äëÿ îòîáðàæåíèÿ â îòäåëüíîì ïîäîêíå è âî âñïëûâàþùåé ïîäñêàçêå
   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);
  }
//---
#define SIZE 1 // êîíñòàíòà äëÿ êîëè÷åñòâà âûçîâîâ ôóíêöèè CountLine
//+------------------------------------------------------------------+
//| 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_,TimeFrame,iTime[bar],1,IndTime)<=0) return(RESET);
      //---
      if(iTime[bar]>=IndTime[0] && iTime[bar+1]<IndTime[0])
        {
         LastCountBar[Numb]=bar;
         double Arr[1];
         //--- êîïèðóåì âíîâü ïîÿâèâøèåñÿ äàííûå â ìàññèâ Arr
         if(CopyBuffer(IndHandle,BuffNumb,iTime[bar],1,Arr)<=0) return(RESET);
         IndBuf[bar]=Arr[0];
        }
      else IndBuf[bar]=IndBuf[bar+1];
     }
//---     
   return(true);
  }
//+------------------------------------------------------------------+

Comments