インディケータ: SpearmanRankCorrelation_Histogram

 

SpearmanRankCorrelation_Histogram:

カラーヒストグラムの形で実装された SpearmanRankCorrelation インジケータ

SpearmanRankCorrelation_Histogram

作者: Nikolay Kositsin

 

Mql4コード

//+------------------------------------------------------------------+
//| SpearmanRankCorrelation_Histogram.mq4
//| Copyright © 2007, MetaQuotes Software Corp.
//|http://www.metaquotes.net
//+------------------------------------------------------------------+
//http://www.infamed.com/stat/s05.html
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
//---- インジケータのバージョン番号
#property version   "1.00"
//---- インジケータを別ウィンドウで表示する。
#property indicator_separate_window
//---- インジケーター・バッファの数 1
#property indicator_buffers 1 
//---- グラフィカルな構成は1つだけ
#property indicator_plots   1
//+----------------------------------------------+
//|| インジケータ描画のパラメータ
//+----------------------------------------------+
//---- インジケータをヒストグラムとして描画する
#property indicator_type1 DRAW_HISTOGRAM
//---- ヒストグラムの色が使用されます。
#property indicator_color1 clrViolet
//---- 指示線は実線
#property indicator_style1 STYLE_SOLID
//---- インジケータの線の太さは2
#property indicator_width1 2
//+----------------------------------------------+
//---- インジケータの最小値と最大値のパラメータ
#property indicator_minimum -1.0
#property indicator_maximum +1.0
//+----------------------------------------------+
//|| インジケータ入力パラメータ
//+----------------------------------------------+
input int  rangeN=14;
input int  CalculatedBars=0;
input int  Maxrange=30;
input bool direction=true;
input double inHighLevel=+0.5;
input double inLowLevel=-0.5;
//+----------------------------------------------+
//---- データ開始点の整数変数の宣言
int min_rates_total;
//---- さらに指示バッファとして使用される動的配列の宣言
double IndBuffer[],ColorIndBuffer[];
double multiply;
double R2[],TrueRanks[];
int    PriceInt[],SortInt[],Maxrange_;
//+------------------------------------------------------------------+
//| RSP関数を計算する|
//+------------------------------------------------------------------+
double SpearmanRankCorrelation(double &Ranks[],int N)
  {
//----
   double res,z2=0.0;

   for(int iii=0; iii<N; iii++) z2+=MathPow(Ranks[iii]-iii-1,2);
   res=1-6*z2/(MathPow(N,3)-N);
//----
   return(res);
  }
//+------------------------------------------------------------------+
//| 価格関数のランキング配列|
//+------------------------------------------------------------------+
void RankPrices(double &TrueRanks_[],int &InitialArray[])
  {
//----
   int i,k,m,dublicat,counter,etalon;
   double dcounter,averageRank;

   ArrayCopy(SortInt,InitialArray,0,0,WHOLE_ARRAY);

   for(i=0; i<rangeN; i++) TrueRanks_[i]=i+1;
   
   ArraySort(SortInt);
   
   for(i=0; i<rangeN-1; i++)
     {
      if(SortInt[i]!=SortInt[i+1]) continue;

      dublicat=SortInt[i];
      k=i+1;
      counter=1;
      averageRank=i+1;

      while(k<rangeN)
        {
         if(SortInt[k]==dublicat)
           {
            counter++;
            averageRank+=k+1;
            k++;
           }
         else
            break;
        }
      dcounter=counter;
      averageRank=averageRank/dcounter;

      for(m=i; m<k; m++)
         TrueRanks_[m]=averageRank;
      i=k;
     }
   for(i=0; i<rangeN; i++)
     {
      etalon=InitialArray[i];
      k=0;
      while(k<rangeN)
        {
         if(etalon==SortInt[k])
           {
            R2[i]=TrueRanks_[k];
            break;
           }
         k++;
        }
     }
//----
   return;
  }
//+------------------------------------------------------------------+
//| カスタムインジケータ初期化関数
//+------------------------------------------------------------------+ 
void OnInit()
  {
//---- 変数配列のメモリ割り当て 
   ArrayResize(R2,rangeN);
   ArrayResize(PriceInt,rangeN);
   ArrayResize(SortInt,rangeN);
//---- 変数配列の要素のインデックスを変更する。
   if(direction) ArraySetAsSeries(SortInt,true);
   ArrayResize(TrueRanks,rangeN);
//---- 変数の初期化
   if(Maxrange<=0) Maxrange_=10;
   else Maxrange_=Maxrange;
   multiply=MathPow(10,_Digits);
//---- 動的配列 IndBuffer をインジケータ・バッファに変える
   SetIndexBuffer(0,IndBuffer,INDICATOR_DATA);
//---- カウントダウンを開始するインジケータの描画をシフトする。
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,rangeN);
//---- チャート上に表示されないインジケータの値を設定する。
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//---- インジケータ・ウィンドウの左上隅にインジケータ値を表示することを禁止する。
   PlotIndexSetInteger(0,PLOT_SHOW_DATA,false);
//---- 時系列と同じように、バッファ内のアイテムにインデックスを付ける。
   ArraySetAsSeries(IndBuffer,true);
//---- 短いインジケータ名の変数の初期化
   string shortname;
   if(rangeN>Maxrange_) shortname="Decrease rangeN input!";
   else StringConcatenate(shortname,"SpearmanRankCorrelation_Histogram(",rangeN,")");
//--- DataWindowに表示するラベルの作成
   PlotIndexSetString(0,PLOT_LABEL,shortname);
//--- 別のサブウィンドウとツールチップに表示する名前を作成する
   IndicatorSetString(INDICATOR_SHORTNAME,shortname);
//--- 指標値の表示精度の決定
   IndicatorSetInteger(INDICATOR_DIGITS,2);
//---- インジケータの水平レベル数 
   IndicatorSetInteger(INDICATOR_LEVELS,3);
//---- インジケータの水平レベルの値 
   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,inHighLevel);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,0);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,2,inLowLevel);
//---- ピンクとブルーは水平線の色として使用される。 
   IndicatorSetInteger(INDICATOR_LEVELCOLOR,0,clrMagenta);
   IndicatorSetInteger(INDICATOR_LEVELCOLOR,1,clrGray);
   IndicatorSetInteger(INDICATOR_LEVELCOLOR,2,clrBlue);
//---- 水平線には短い破線を使用。 
   IndicatorSetInteger(INDICATOR_LEVELSTYLE,0,STYLE_DASHDOTDOT);
   IndicatorSetInteger(INDICATOR_LEVELSTYLE,1,STYLE_DASH);
   IndicatorSetInteger(INDICATOR_LEVELSTYLE,2,STYLE_DASHDOTDOT);
//----
  }
//+------------------------------------------------------------------+
//| カスタム・インジケータ反復関数
//+------------------------------------------------------------------+
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[])
  {
//---- 計算に必要な小節数をチェックする。
   if(rates_total<rangeN) return(0);
   if(rangeN>Maxrange_) return(0);

//---- ローカル変数の宣言 
   int limit;

//---- バーの再計算サイクルの開始リミット番号を計算する。
   if(prev_calculated>rates_total || prev_calculated<=0) // インジケータの最初の計算開始をチェック
     {
      limit=rates_total-2-rangeN; // すべてのバーの計算の開始番号
     }
   else
     {
      if(!CalculatedBars) limit = rates_total - prev_calculated;
      else limit = CalculatedBars;
     }

//---- 配列の要素のインデックスを、時系列と同じように作成する。 
   ArraySetAsSeries(close,true);

//---- メイン・インジケータの計算サイクル
   for(int bar=limit; bar>=0 && !IsStopped(); bar--)
     {
      for(int k=0; k<rangeN; k++) PriceInt[k]=int(close[bar+k]*multiply);

      RankPrices(TrueRanks,PriceInt);
      IndBuffer[bar]=SpearmanRankCorrelation(R2,rangeN);
     }
//---- 
   return(rates_total);
  }
//+------------------------------------------------------------------+