Stochastic Other TimeFrame

Author: Copyright © 2020, Vladimir Karputov
Indicators Used
Stochastic oscillator
Miscellaneous
It issuies visual alerts to the screen
0 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

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---