MQL5

マルチタイムフレームストキャスティクス for MT5

RSI、ボリンジャーバンドに引き続き、マルチタイムフレーム対応のストキャスティクスを公開します!
コードの説明はRSIのほうを参照してもらえたらと思います。

ソースコード

#property copyright   "© shanch, 2020"
#property link        "shanch2@gmail.com"
#property description "MTFStocastics"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots   3

#property indicator_type1   DRAW_LINE
#property indicator_color1  clrLightGreen
#property indicator_width1  1
#property indicator_style1  STYLE_SOLID
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRed
#property indicator_width2  1
#property indicator_style2  STYLE_DASH

input ENUM_TIMEFRAMES  TimeFrame = PERIOD_H1;
input int K_Period = 14;
input int D_Period = 3;
input int Slow_Period = 3;

int hSto;
double BufMain[];
double BufSignal[];
double BufTemp[];

int OnInit()
{
   SetIndexBuffer(0, BufMain, INDICATOR_DATA);
   ArraySetAsSeries(BufMain, true);
   
   SetIndexBuffer(1, BufSignal, INDICATOR_DATA);
   ArraySetAsSeries(BufSignal, true);
   
   ArraySetAsSeries(BufTemp, true);
   
   IndicatorSetInteger(INDICATOR_LEVELS,2);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,20);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,80);
   IndicatorSetDouble(INDICATOR_MINIMUM,0);
   IndicatorSetDouble(INDICATOR_MAXIMUM,100);
     
   hSto = iStochastic(NULL, TimeFrame, K_Period, D_Period, Slow_Period, MODE_SMA, STO_LOWHIGH);

   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
    IndicatorRelease(hSto);
}

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[])
  {
   ArraySetAsSeries(time,true);
   
   int limit = rates_total - prev_calculated;
   if(limit == 0) limit= 1;
   
   int shift = 0;
   
   for(int i=0; i<limit; i++)
   {
      shift=iBarShift(_Symbol,TimeFrame,time[i]);
      
      if(CopyBuffer(hSto, 0, shift, 1, BufTemp) <= 0)
      {
         Print("Getting Stocastics failed! Error",GetLastError());
         return(0);
      }
      else
      {
          BufMain[i]=BufTemp[0];
      }
      if(CopyBuffer(hSto, 1, shift, 1, BufTemp) <= 0)
      {
         Print("Getting Stocastics Signal failed! Error",GetLastError());
         return(0);
      }
      else
      {
          BufSignal[i]=BufTemp[0];
      }
   }

   for(int i=1; i<rates_total; i++)
   {
      if(iBarShift(NULL, TimeFrame, time[i]) > 0)
      {
         break;
      }
      BufMain[i]=BufMain[0];
      BufSignal[i]=BufSignal[0];
   }

   return(rates_total);
}