Jump to content

REQ: Spearman Rank Correlation + indicator


Recommended Posts

  • 4 months later...
  • 2 weeks later...

it is on the link provided by dk1aussie..

//+------------------------------------------------------------------+
//|                                      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"
//----
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DarkBlue
//---- input parameters
extern int  rangeN = 14;
extern int  CalculatedBars = 0;
extern int  Maxrange = 30;
extern bool direction = true;
//---- buffers
double ExtMapBuffer1[];
double R2[];
double multiply;
int    PriceInt[];
int    SortInt[];
//+------------------------------------------------------------------+
//| 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);
  ArrayResize(R2, rangeN);
  ArrayResize(PriceInt, rangeN);
  ArrayResize(SortInt, rangeN);
  if(Maxrange <= 0) 
      Maxrange = 10;
  if(rangeN > Maxrange) 
      IndicatorShortName("Decrease rangeN input!");
  else 
      IndicatorShortName("Spearman(" + 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);
    }
//----
  return(0);
 }
//+------------------------------------------------------------------+

just copy those code to notepad > replace txt extension to mq4 > open with MetaEditor > save to indicator folder > compile

 

there a lot of spearman rank correlation based indi in this thread

http://indo-investasi.com/showthread.php/16735-Forex.TSD-advanced-elite-indicator-pack

Edited by temon
just realize i can't upload images
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...