Author: Copyright � 2007, MetaQuotes Software Corp.
SRCma
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
SRCma
//+------------------------------------------------------------------+
//|                                      SpearmanRankCorrelation.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
// http://www.improvedoutcomes.com/docs/WebSiteDocs/Clustering/
// Clustering_Parameters/Spearman_Rank_Correlation_Distance_Metric.htm
// http://www.infamed.com/stat/s05.html
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
//----
// Bookkeeper: Äîáàâèë áóôåð ExtMapBuffer2 - ñãëàæèâàíèå ìóâèíãîì
//----
#property indicator_separate_window
#property indicator_maximum  1.4
#property indicator_minimum -1.4
#property indicator_level1   1
#property indicator_level2  -1
#property indicator_level3   0.6
#property indicator_level4  -0.6
#property indicator_levelcolor DimGray
#property indicator_buffers  2
#property indicator_color1   Red
#property indicator_color2   Blue
//---- input parameters
extern int    CalculatedBars = 500;
extern int    rangeN         = 14;
extern int    MAPeriod       = 5;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double R2[];
double multiply;
int    PriceInt[];
int    SortInt[];
int  Maxrange = 30;
bool direction = true;
//----
//+------------------------------------------------------------------+
//| calculate  RSP  function                                         |
//+------------------------------------------------------------------+
double SpearmanRankCorrelation(double Ranks[], int N)
  {
//----
   double res,z2;
   int i;
   for(i = 0; i < N; i++)
     {
       z2 += MathPow(Ranks[i] - i - 1, 2);
     }
   res = 1 - 6*z2 / (MathPow(N,3) - N);
//----
   return(res);
  }
//+------------------------------------------------------------------+
//| Ranking array of prices function                                 |
//+------------------------------------------------------------------+
void RankPrices(int InitialArray[])
  {
//----
   int i, k, m, dublicat, counter, etalon;
   double dcounter, averageRank;
   double TrueRanks[];
   ArrayResize(TrueRanks, rangeN);
   ArrayCopy(SortInt, InitialArray);
   for(i = 0; i < rangeN; i++) 
       TrueRanks[i] = i + 1;
   if(direction)
       ArraySort(SortInt, 0, 0, MODE_DESCEND);
   else
       ArraySort(SortInt, 0, 0, MODE_ASCEND);
   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;
  }
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexBuffer(1, ExtMapBuffer2);
   ArrayResize(R2, rangeN);
   ArrayResize(PriceInt, rangeN);
   ArrayResize(SortInt, rangeN);
   if(Maxrange <= 0) 
       Maxrange = 10;
   if(rangeN > Maxrange) 
       IndicatorShortName("Decrease rangeN input!");
   else 
       IndicatorShortName("SRCma(" + rangeN + ")");
   if(CalculatedBars < 0) 
       CalculatedBars = 0;
   multiply = MathPow(10, Digits);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int counted_bars = IndicatorCounted();
//----
   if(rangeN > Maxrange) 
       return(-1);
   int i, k, limit;
   if(counted_bars == 0)
     {
       if(CalculatedBars == 0)
           limit = Bars - rangeN;
       else
           limit = CalculatedBars;
     }
   if(counted_bars > 0)
       limit = Bars - counted_bars;
   for(i = limit; i >= 0; i--)
     {
       for(k = 0; k < rangeN; k++) 
           PriceInt[k] = Close[i+k]*multiply;
       RankPrices(PriceInt);
       ExtMapBuffer1[i] = SpearmanRankCorrelation(R2,rangeN);
     } 
   for(i = limit; i >= 0; i--)
     {
       ExtMapBuffer2[i] = iMAOnArray(ExtMapBuffer1,0,MAPeriod,0,MODE_SMA,i);
     } 
//----
   return(0);
  }
//+------------------------------------------------------------------+

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