Stochastic Other TimeFrame

Download script
Author: Copyright © 2020, Vladimir Karputov

The Stochastic Other Timeframe indicator is a sophisticated technical analysis tool designed to bridge the gap between different time scales on a trading chart. Its primary purpose is to allow traders to view market momentum from a higher timeframe without having to switch back and forth between different charts, providing a multi-dimensional perspective on price action.

Core Functionality

The script works by retrieving data from a secondary "Other" timeframe (such as Daily or Weekly) and projecting that information onto your current active chart. It specifically pulls the Stochastic Oscillator values—a common momentum indicator—from that chosen higher timeframe and displays them in a dedicated sub-window.

Key Logic Components

To ensure accuracy and ease of use, the script follows several specific logical steps:

  • Multi-Timeframe Synchronization: The tool automatically identifies the correct historical point on the higher timeframe that corresponds with the current time on your active chart. This ensures that the data you see is perfectly aligned with the present moment.
  • Customizable Parameters: Users can define the specific "flavor" of the Stochastic indicator they want to view. This includes setting the K-period, D-period, and slowing factors, as well as choosing the specific price fields (like High/Low) used for the calculation.
  • Smart Validation: The script includes a safety check to ensure that the chosen "Other" timeframe is actually larger than your current timeframe. If you try to select a smaller timeframe, the tool will alert you, preventing visual confusion or data errors.
  • Dual-Line Visualization: It plots two distinct lines in the sub-window:
    1. Main Line: The primary Stochastic value.
    2. Signal Line: A smoothed moving average of the main line used to identify potential trend shifts or crossovers.
  • Dynamic Labeling: To keep the chart organized, the indicator automatically updates its labels and name based on your settings. It clearly shows which timeframe the data is being pulled from and what specific periods are being used for the calculation.

Summary for Traders

By using this script, you can maintain a "big picture" view of market trends while trading on shorter intervals. It allows you to see if the larger market trend is overbought or oversold, helping to filter out "noise" and potentially identify higher-probability entries based on multi-timeframe momentum.

Indicators Used
Stochastic oscillator
Miscellaneous
It issuies visual alerts to the screen
10 Views
0 Downloads
0 Favorites
Stochastic Other TimeFrame
ÿþ//+------------------------------------------------------------------+

//|                                   Stochastic Other TimeFrame.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43161 |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2020, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43161"

#property version   "1.000"

#property indicator_separate_window

#property indicator_buffers 2

#property indicator_plots   2

//--- plot STO

#property indicator_label1  "STO Main"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrLightSeaGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2

//--- plot STO_

#property indicator_label2  "STO Signal"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrRed

#property indicator_style2  STYLE_DASH

#property indicator_width2  1

//--- input parameters

input group             "Stochastic Other"

input ENUM_TIMEFRAMES   Inp_STO_Other_period          = PERIOD_D1;   // Stochastic Other: timeframe

input int               Inp_STO_Other_Kperiod         = 5;           // Stochastic Other: K-period (number of bars for calculations)

input int               Inp_STO_Other_Dperiod         = 3;           // Stochastic Other: D-period (period of first smoothing)

input int               Inp_STO_Other_slowing         = 3;           // Stochastic Other: final smoothing

input ENUM_MA_METHOD    Inp_STO_Other_ma_method       = MODE_SMA;    // Stochastic Other: type of smoothing

input ENUM_STO_PRICE    Inp_STO_Other_price_field     = STO_LOWHIGH; // Stochastic Other: stochastic calculation method

//--- indicator buffers

double   MainBuffer[];

double   SignalBuffer[];

//---

int      handle_iStochastic_other;              // variable for storing the handle of the iStochastic indicator

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,MainBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,SignalBuffer,INDICATOR_DATA);

//--- set accuracy

   IndicatorSetInteger(INDICATOR_DIGITS,2);

//--- set levels

   IndicatorSetInteger(INDICATOR_LEVELS,2);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,20);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,80);

//--- set maximum and minimum for subwindow

   IndicatorSetDouble(INDICATOR_MINIMUM,0);

   IndicatorSetDouble(INDICATOR_MAXIMUM,100);

//--- name for DataWindow and indicator subwindow label

   string other_timeframe=StringSubstr(EnumToString(Inp_STO_Other_period),7);

   string parameters="("+IntegerToString(Inp_STO_Other_Kperiod)+","+

                     IntegerToString(Inp_STO_Other_Dperiod)+","+IntegerToString(Inp_STO_Other_slowing)+")";

   IndicatorSetString(INDICATOR_SHORTNAME,"Stoch,"+other_timeframe+parameters);

   PlotIndexSetString(0,PLOT_LABEL,"STO"+parameters+"Main,"+other_timeframe);

   PlotIndexSetString(1,PLOT_LABEL,"STO"+parameters+"Signal,"+other_timeframe);

//---

   if(Inp_STO_Other_period<Period())

     {

      string current_timeframe=StringSubstr(EnumToString(Period()),7);

      string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?

                      "'Stochastic Other: timeframe' ("+other_timeframe+") =5 <>65B 1KBL <5=LH5 B5:CI53> B09<D@59<0 ("+current_timeframe+")!":

                      "'Stochastic Other: timeframe' ("+other_timeframe+")  cannot be less than the current timeframe ("+current_timeframe+")!";

      //--- when testing, we will only output to the log about incorrect input parameters

      if(MQLInfoInteger(MQL_TESTER))

        {

         Print(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);

         return(INIT_PARAMETERS_INCORRECT);

        }

      else // if the Expert Advisor is run on the chart, tell the user about the error

        {

         Alert(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);

         return(INIT_PARAMETERS_INCORRECT);

        }

     }

//--- create handle of the indicator iStochastic

   handle_iStochastic_other=iStochastic(Symbol(),Inp_STO_Other_period,

                                        Inp_STO_Other_Kperiod,Inp_STO_Other_Dperiod,Inp_STO_Other_slowing,

                                        Inp_STO_Other_ma_method,Inp_STO_Other_price_field);

//--- if the handle is not created

   if(handle_iStochastic_other==INVALID_HANDLE)

     {

      //--- tell about the failure and output the error code

      PrintFormat("Failed to create handle of the iStochastic indicator for the symbol %s/%s, error code %d",

                  Symbol(),

                  EnumToString(Inp_STO_Other_period),

                  GetLastError());

      //--- the indicator is stopped early

      return(INIT_FAILED);

     }

//---

   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[])

  {

//--- determine the number of values calculated in the indicator

   int calculated=BarsCalculated(handle_iStochastic_other);

   if(calculated<=0)

     {

      PrintFormat("BarsCalculated(Other) returned %d, error code %d",calculated,GetLastError());

      return(0);

     }

//--- main loop

   int limit=prev_calculated-1;

   if(prev_calculated==0)

      limit=0;

   for(int i=limit; i<rates_total; i++)

     {

      int i_bar_shift=iBarShift(Symbol(),Inp_STO_Other_period,time[i],false);

      if(i_bar_shift<0)

         return(0);

      datetime i_time=iTime(Symbol(),Inp_STO_Other_period,i_bar_shift);

      if(i_time==D'1970.01.01 00:00')

         return(0);

      //---

      double main_buffer[],signal_buffer[];

      int copy_main=CopyBuffer(handle_iStochastic_other,MAIN_LINE,i_time,1,main_buffer);

      int copy_signal=CopyBuffer(handle_iStochastic_other,SIGNAL_LINE,i_time,1,signal_buffer);

      if(copy_main<1 || copy_signal<1)

         return(0);

      MainBuffer[i]=main_buffer[0];

      SignalBuffer[i]=signal_buffer[0];

     }

//--- return the prev_calculated value for the next call

   return(rates_total);

  }

//+------------------------------------------------------------------+

//| Indicator deinitialization function                              |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

   if(handle_iStochastic_other!=INVALID_HANDLE)

      IndicatorRelease(handle_iStochastic_other);

  }

//+------------------------------------------------------------------+

Comments