Stochastic Time Control

Author: Copyright © 2019, Vladimir Karputov
Indicators Used
Stochastic oscillator
0 Views
0 Downloads
0 Favorites
Stochastic Time Control
ÿþ//+------------------------------------------------------------------+

//|                                      Stochastic Time Control.mq5 |

//|                              Copyright © 2019, Vladimir Karputov |

//|                                           http://wmua.ru/slesar/ |

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

#property copyright "Copyright © 2019, Vladimir Karputov"

#property link      "http://wmua.ru/slesar/"

#property version   "1.001"

#property description "Stochastic Time Control"

//--- indicator settings

#property indicator_separate_window

#property indicator_buffers 4

#property indicator_plots   2

#property indicator_type1   DRAW_LINE

#property indicator_type2   DRAW_LINE

#property indicator_color1  clrLightSeaGreen

#property indicator_color2  clrRed

#property indicator_style2  STYLE_DOT

//--- input parameters

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

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

input int               Inp_STO_slowing      = 3;              // Stochastic: final smoothing

input ENUM_MA_METHOD    Inp_STO_ma_method    = MODE_EMA;       // Stochastic: type of smoothing

input ENUM_STO_PRICE    Inp_STO_price_field  = STO_CLOSECLOSE; // Stochastic: stochastic calculation method

input int               Inp_STO_Level_Down   = 25.0;           // Value Level Down

input double            Inp_STO_Level_Up     = 75.0;           // Value Level Up

input bool     InpTimeControl    = true;     // Use time control

input uchar    InpStartHour      = 09;       // Start Hour

input uchar    InpStartMinute    = 00;       // Start Minute

input uchar    InpEndHour        = 23;       // End Hour

input uchar    InpEndMinute      = 01;       // End Minute

//--- indicator buffers

double   ExtTimeMainBuffer[];

double   ExtTimeSignalBuffer[];

double   ExtMainBuffer[];

double   ExtSignalBuffer[];

long     m_start=0,m_end=0;

int      bars_calculated=0;   // we will keep the number of values in the Stochastic Oscillator indicator

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

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,ExtTimeMainBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ExtTimeSignalBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,ExtMainBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,ExtSignalBuffer,INDICATOR_CALCULATIONS);

//---

   if(InpTimeControl)

     {

      m_start=InpStartHour*60*60+InpStartMinute*60;

      m_end=InpEndHour*60*60+InpEndMinute*60;

     }

//--- the 0.0 (empty) value will mot participate in drawing

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,-100.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,-100.0);

//--- set accuracy

   IndicatorSetInteger(INDICATOR_DIGITS,2);

//--- set levels

   IndicatorSetInteger(INDICATOR_LEVELS,2);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,Inp_STO_Level_Down);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,Inp_STO_Level_Up);

//--- set maximum and minimum for subwindow

   IndicatorSetDouble(INDICATOR_MINIMUM,0);

   IndicatorSetDouble(INDICATOR_MAXIMUM,100);

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

   string time="";

   if(InpTimeControl)

      time=StringFormat(" (%02d:%02d-%02d:%02d)",InpStartHour,InpStartMinute,InpEndHour,InpEndMinute);



   IndicatorSetString(INDICATOR_SHORTNAME,"Stoch("+

                      IntegerToString(Inp_STO_Kperiod)+","+

                      IntegerToString(Inp_STO_Dperiod)+","+

                      IntegerToString(Inp_STO_slowing)+")"+

                      time);

   PlotIndexSetString(0,PLOT_LABEL,"Main");

   PlotIndexSetString(1,PLOT_LABEL,"Signal");

//--- sets first bar from what index will be drawn

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,Inp_STO_Kperiod+Inp_STO_slowing-2);

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,Inp_STO_Kperiod+Inp_STO_Dperiod);

//--- create handle of the indicator iStochastic

   handle_iStochastic=iStochastic(Symbol(),Period(),

                                  Inp_STO_Kperiod,Inp_STO_Dperiod,Inp_STO_slowing,

                                  Inp_STO_ma_method,Inp_STO_price_field);

//--- if the handle is not created

   if(handle_iStochastic==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(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[])

  {

//--- number of values copied from the iStochastic indicator

   int values_to_copy;

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

   int calculated=BarsCalculated(handle_iStochastic);

   if(calculated<=0)

     {

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

      return(0);

     }

//--- if it is the first start of calculation of the indicator or if the number of values in the iStochastic indicator changed

//---or if it is necessary to calculated the indicator for two or more bars (it means something has changed in the price history)

   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)

     {

      //--- if the StochasticBuffer array is greater than the number of values in the iStochastic indicator for symbol/period, then we don't copy everything

      //--- otherwise, we copy less than the size of indicator buffers

      if(calculated>rates_total)

         values_to_copy=rates_total;

      else

         values_to_copy=calculated;

     }

   else

     {

      //--- it means that it's not the first time of the indicator calculation, and since the last call of OnCalculate()

      //--- for calculation not more than one bar is added

      values_to_copy=(rates_total-prev_calculated)+1;

     }

//--- fill the arrays with values of the iStochastic indicator

//--- if FillArraysFromBuffer returns false, it means the information is nor ready yet, quit operation

   if(!FillArraysFromBuffers(ExtMainBuffer,ExtSignalBuffer,handle_iStochastic,values_to_copy))

      return(0);

//--- memorize the number of values in the Stochastic Oscillator indicator

   bars_calculated=calculated;

//---

   int limit=prev_calculated-1;

   if(prev_calculated==0)

      limit=0;



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

     {

      if(InpTimeControl)

        {

         MqlDateTime STime;

         TimeToStruct(time[i],STime);

         if((STime.hour*60*60+STime.min*60)<m_start || (STime.hour*60*60+STime.min*60)>=m_end)

           {

            ExtTimeMainBuffer[i]=-100.0;

            ExtTimeSignalBuffer[i]=-100.0;

            continue;

           }

        }

      ExtTimeMainBuffer[i]=ExtMainBuffer[i];

      ExtTimeSignalBuffer[i]=ExtSignalBuffer[i];

     }

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

   return(rates_total);

  }

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

//| Filling indicator buffers from the iStochastic indicator         |

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

bool FillArraysFromBuffers(double &main_buffer[],    // indicator buffer of Stochastic Oscillator values

                           double &signal_buffer[],  // indicator buffer of the signal line

                           int ind_handle,           // handle of the iStochastic indicator

                           int amount                // number of copied values

                          )

  {

//--- reset error code

   ResetLastError();

//--- fill a part of the StochasticBuffer array with values from the indicator buffer that has 0 index

   if(CopyBuffer(ind_handle,MAIN_LINE,0,amount,main_buffer)<0)

     {

      //--- if the copying fails, tell the error code

      PrintFormat("Failed to copy data from the iStochastic indicator, error code %d",GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated

      return(false);

     }

//--- fill a part of the SignalBuffer array with values from the indicator buffer that has index 1

   if(CopyBuffer(ind_handle,SIGNAL_LINE,0,amount,signal_buffer)<0)

     {

      //--- if the copying fails, tell the error code

      PrintFormat("Failed to copy data from the iStochastic indicator, error code %d",GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated

      return(false);

     }

//--- everything is fine

   return(true);

  }

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



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

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 ---