Stochastic-4H_v02

Download script
Stochastic-4H_v02

This technical script is designed to provide traders with a multi-timeframe perspective on market momentum. Specifically, it allows you to view "Stochastic Oscillator" data from a higher timeframe while viewing your current chart.

Core Functionality

The primary purpose of this tool is to bridge the gap between different timeframes. Instead of forcing you to switch back and forth between your active trading chart (e.g., 15-minute or 1-hour charts) and a larger timeframe chart (such as the 4-hour chart), the script pulls the data from the larger timeframe and displays it directly on your screen.

How the Logic Works

  • Timeframe Synchronization: The script takes a specific "target" timeframe (defaulted to 240 minutes, which corresponds to 4 hours) and identifies the exact historical point in time that matches the current bar you are looking at.
  • Data Mapping: It calculates the "repetition factor." This means it figures out how many smaller candles on your current chart fit into one larger candle of the target timeframe. By doing this, it ensures that the data you see is perfectly aligned with the current moment in time.
  • Stochastic Calculation: Once the script identifies the correct point in time on the higher timeframe, it calculates two specific lines:
    1. The Main Line (K): Represents the primary momentum of the price.
    2. The Signal Line (D): A smoothed version of the main line used to identify trend changes and potential overbought/oversold conditions.

Key Features for the User

  • Customizable Smoothing: You can choose how "smooth" the lines appear by adjusting the Moving Average methods (Simple, Exponential, Smoothed, or Linear Weighted).
  • Price Source Selection: The logic allows you to choose whether the calculation should be based on the high/low prices of the candles or the closing prices.
  • Visual Clarity: The script provides a dedicated window where these two lines are plotted clearly, helping you see "the big picture" without losing sight of your immediate price action.

In short, this tool acts as a "window" into the 4-hour market trend, allowing you to make trading decisions based on higher-level momentum while staying focused on your preferred smaller timeframe chart.

Indicators Used
Stochastic oscillator
Miscellaneous
Implements a curve of type %1
13 Views
0 Downloads
0 Favorites
Stochastic-4H_v02
//+------------------------------------------------------------------+
//|                                                Stochastic 4H.mq4 |
//|   Copyright © 2007, lukas1                                       | 
//|   Àâòîðñêîå ïðàâî: Ëóêàøóê Âèêòîð Ãåííàäüåâè÷                           
//|   Stochastic from   all   timeframe  
//|   16 ôåâðàëÿ 2007ã. âåðñèÿ v.02 èñïðàâëåí ðàñ÷åò ñðåäíåé
//+------------------------------------------------------------------+
#property  copyright "Copyright © 2007, lukas1"
//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 2
#property  indicator_color1  MediumPurple
#property  indicator_color2  Red
#property  indicator_width1  2
//---- indicator parameters
extern int KPeriod=5;
extern int DPeriod=3;
extern int Slowing=3;
extern int Mode_ma = 0;       // Ìåòîä óñðåäíåíèÿ 
               /*MODE_SMA 0 Ïðîñòîå ñêîëüçÿùåå ñðåäíåå 
                 MODE_EMA 1 Ýêñïîíåíöèàëüíîå ñêîëüçÿùåå ñðåäíåå 
                MODE_SMMA 2 Ñãëàæåííîå ñêîëüçÿùåå ñðåäíåå 
                MODE_LWMA 3 Ëèíåéíî-âçâåøåííîå ñêîëüçÿùåå ñðåäíåå 
               */
extern int VarPeriod=240;      //old timeframe
         //PERIOD_M5   5         5 minutes
         //PERIOD_M15  15        15 minutes 
         //PERIOD_M30  30        30 minutes 
         //PERIOD_H1   60        1 hour 
         //PERIOD_H4   240       4 hours 
         //PERIOD_D1   1440      1 day 
         //PERIOD_W1   10080     1 week 
         //PERIOD_MN1  43200     1 month 
extern int AppliedPrice=0;//Îäíà èç ñëåäóþùèõ âåëè÷èí: 0-Low/High èëè 1-Close/Close.
//---- indicator buffers
double     Buffer0[],Buffer1[];
int p;  // current period
int k;  // repetition factor old timeframe
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   SetIndexStyle(0, DRAW_LINE);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexStyle(2, 12);
   SetIndexStyle(3, 12);   
   SetIndexDrawBegin(0, DPeriod);
   SetIndexDrawBegin(1, DPeriod);   
   IndicatorDigits(Digits + 1);
//---- indicator buffers mapping
   SetIndexBuffer(0, Buffer0);
   SetIndexBuffer(1, Buffer1);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("Stoch 4H (" + KPeriod + "," + DPeriod + "," + Slowing + 
                      ", " + VarPeriod + "min)");
   SetIndexLabel(0, "Stoch 4H(" + VarPeriod + ")");
   SetIndexLabel(1, "Signal 4H(" + VarPeriod + ")");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//|                                    Stochastic for old timeframe  |
//+------------------------------------------------------------------+
int start()
  {
   int i, limit;
   int counted_bars = IndicatorCounted();
   //---- check for possible errors
   if(counted_bars < 0)       return(-1);
   if(counted_bars <= DPeriod)
       limit = Bars - DPeriod ;
   else
       limit = Bars - counted_bars;   
   p = Period();
   k = VarPeriod/p;                              // repetition factor old timeframe
   i=limit-1;
   while (i>=0)
      {
      int bb=HighBar(Symbol(), 0, i, VarPeriod); // definition of HighBar, having symbol, lowTF,
                                                 // and bar's number in lowTF, calculated highTF;
                                                 // (symbol, lowTF, bar's number, highTF)     
      if (bb==-1) continue;       
      Buffer0[i] =iStochastic(Symbol(),VarPeriod,KPeriod,DPeriod,Slowing,Mode_ma,AppliedPrice,0,bb);
      Buffer1[i]=iStochastic(Symbol(),VarPeriod, KPeriod,DPeriod,Slowing,Mode_ma,AppliedPrice,1,bb);
      i--;
      }
//---- done
   return(0);
  }
//+------------------------------------------------------------------+
int HighBar(string symbol,int TFLow,int BarLow,int TFHigh) 
                                                   // definition of HighBar, having symbol, lowTF,
                                                   // and bar's number in lowTF, calculated highTF;
                                                   // (symbol, lowTF, bar's number, highTF)     
 {
   datetime BarTimeLow=iTime(symbol, TFLow, BarLow);    // time's value of bar means in BarLow
   int error=GetLastError();
   if(error==4066) return (-1);
   
   int res=iBarShift(symbol, TFHigh, BarTimeLow, false);// displacement of bar means in BarTimeLow
   error=GetLastError();
   if(error==4066) return (-1);
   return (res);
 }
//+------------------------------------------------------------------+

Comments