Indikatoren: SpearmanRankCorrelation_Histogram

 

SpearmanRankCorrelation_Histogram:

Der Indikator SpearmanRankCorrelation implementiert als farbiges Histogramm

SpearmanRankCorrelation_Histogram

Autor: Nikolay Kositsin

 

Mql4-Code

//+------------------------------------------------------------------+
//| 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"
//---- Versionsnummer des Indikators
#property version   "1.00"
//---- Darstellung des Indikators in einem separaten Fenster
#property indicator_separate_window
//---- Anzahl der Indikatorpuffer 1
#property indicator_buffers 1 
//---- wurde nur eine grafische Konstruktion verwendet
#property indicator_plots   1
//+----------------------------------------------+
//|| Parameter der Indikatorzeichnung |
//+----------------------------------------------+
//---- Zeichnen des Indikators als Histogramm
#property indicator_type1 DRAW_HISTOGRAM
//---- als Farbe des Histogramms wird verwendet
#property indicator_color1 clrViolet
//---- Indikatorlinie ist durchgezogen
#property indicator_style1 STYLE_SOLID
//---- Indikator Linienstärke ist 2
#property indicator_width1 2
//+----------------------------------------------+
//---- Parameter der minimalen und maximalen Indikatorwerte
#property indicator_minimum -1.0
#property indicator_maximum +1.0
//+----------------------------------------------+
//|| Indikator-Eingabeparameter |
//+----------------------------------------------+
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;
//+----------------------------------------------+
//---- Deklaration von Integer-Variablen des Datenstartpunktes
int min_rates_total;
//---- Deklaration von dynamischen Arrays, die als Indikatorpuffer weiterverwendet werden sollen
double IndBuffer[],ColorIndBuffer[];
double multiply;
double R2[],TrueRanks[];
int    PriceInt[],SortInt[],Maxrange_;
//+------------------------------------------------------------------+
//| RSP-Funktion berechnen|
//+------------------------------------------------------------------+
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);
  }
//+------------------------------------------------------------------+
//| Rangfolge der Preise Funktion|
//+------------------------------------------------------------------+
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;
  }
//+------------------------------------------------------------------+
//| Benutzerdefinierte Initialisierungsfunktion für Indikatoren |
//+------------------------------------------------------------------+ 
void OnInit()
  {
//---- Speicherzuweisung für variable Arrays 
   ArrayResize(R2,rangeN);
   ArrayResize(PriceInt,rangeN);
   ArrayResize(SortInt,rangeN);
//---- Änderung der Indizierung von Elementen in der Variablenmatrix
   if(direction) ArraySetAsSeries(SortInt,true);
   ArrayResize(TrueRanks,rangeN);
//---- Initialisierung von Variablen
   if(Maxrange<=0) Maxrange_=10;
   else Maxrange_=Maxrange;
   multiply=MathPow(10,_Digits);
//---- macht aus dem dynamischen Array IndBuffer einen Indikatorpuffer
   SetIndexBuffer(0,IndBuffer,INDICATOR_DATA);
//---- Verschiebung des Indikators Zeichnung Start Countdown
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,rangeN);
//---- Einstellung von Indikatorwerten, die auf dem Diagramm nicht sichtbar sind
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//---- Verbot der Anzeige von Indikatorwerten in der linken oberen Ecke des Indikatorfensters
   PlotIndexSetInteger(0,PLOT_SHOW_DATA,false);
//---- Indizierung der Elemente im Puffer wie bei Zeitreihen
   ArraySetAsSeries(IndBuffer,true);
//---- Initialisierung der Variablen für den kurzen Indikatornamen
   string shortname;
   if(rangeN>Maxrange_) shortname="Decrease rangeN input!";
   else StringConcatenate(shortname,"SpearmanRankCorrelation_Histogram(",rangeN,")");
//--- Erstellen eines Etiketts, das im Datenfenster angezeigt werden soll
   PlotIndexSetString(0,PLOT_LABEL,shortname);
//--- Erstellen eines Namens, der in einem separaten Unterfenster und einer QuickInfo angezeigt wird
   IndicatorSetString(INDICATOR_SHORTNAME,shortname);
//--- Bestimmung der Genauigkeit der Anzeige der Indikatorwerte
   IndicatorSetInteger(INDICATOR_DIGITS,2);
//---- Anzahl der horizontalen Ebenen des Indikators 
   IndicatorSetInteger(INDICATOR_LEVELS,3);
//---- Werte der horizontalen Ebenen des Indikators 
   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,inHighLevel);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,0);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,2,inLowLevel);
//---- rosa und blaue Farben werden als Farben der horizontalen Niveaulinien verwendet 
   IndicatorSetInteger(INDICATOR_LEVELCOLOR,0,clrMagenta);
   IndicatorSetInteger(INDICATOR_LEVELCOLOR,1,clrGray);
   IndicatorSetInteger(INDICATOR_LEVELCOLOR,2,clrBlue);
//---- wird eine kurze gestrichelte Linie in der horizontalen Niveaulinie verwendet 
   IndicatorSetInteger(INDICATOR_LEVELSTYLE,0,STYLE_DASHDOTDOT);
   IndicatorSetInteger(INDICATOR_LEVELSTYLE,1,STYLE_DASH);
   IndicatorSetInteger(INDICATOR_LEVELSTYLE,2,STYLE_DASHDOTDOT);
//----
  }
//+------------------------------------------------------------------+
//| Benutzerdefinierte Indikator-Iterationsfunktion |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,    // Menge der Historie in Balken zum aktuellen Tick
                const int prev_calculated,// Betrag der Historie in Balken zum vorherigen Tick
                const datetime &time[],
                const double &open[],
                const double& high[],     // Preisfeld mit Preismaxima für die Indikatorberechnung
                const double& low[],      // Preisfeld mit Preisminima für die Indikatorberechnung
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---- prüfen, ob die Anzahl der Balken für die Berechnung ausreicht
   if(rates_total<rangeN) return(0);
   if(rangeN>Maxrange_) return(0);

//---- Deklarationen lokaler Variablen 
   int limit;

//---- Berechnung der Startgrenzennummer für den Taktneuberechnungszyklus
   if(prev_calculated>rates_total || prev_calculated<=0) // Prüfung auf den ersten Start der Indikatorberechnung
     {
      limit=rates_total-2-rangeN; // Startnummer für die Berechnung aller Balken
     }
   else
     {
      if(!CalculatedBars) limit = rates_total - prev_calculated;
      else limit = CalculatedBars;
     }

//---- Indizierung von Elementen in Arrays wie in Zeitreihen 
   ArraySetAsSeries(close,true);

//---- Berechnungszyklus des Hauptindikators
   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);
  }
//+------------------------------------------------------------------+