Author: Nike
Corr

This tool is designed to help traders analyze the relationship between current price movements and historical price patterns. Instead of looking at raw price data alone, it focuses on measuring "correlation," which essentially tells you how closely two different periods of market activity mimic one another.

How the Logic Works

1. Identifying Price Movement (The Foundation) The first step the script takes is to look at each individual candle on your chart. It calculates the difference between the price when a candle opened and when it closed. By doing this, it ignores the absolute price level and focuses purely on the "momentum" or the direction of movement during that specific timeframe.

2. Establishing a Comparison Window The tool uses a specific setting (the "Correlation Length") to decide how far back in time it should look. This allows you to compare the most recent price movements against a specific historical window to see if the market is repeating its previous behavior.

3. Calculating the Correlation Coefficient This is the core "engine" of the script. It performs a mathematical calculation to determine the strength and direction of the relationship between the current price actions and the past ones:

  • Direction: It identifies if the two periods are moving in the same direction (positive correlation) or opposite directions (negative correlation).
  • Strength: It measures how consistent that movement is. A high score suggests that the current market behavior is very similar to the historical pattern, while a low score suggests that the current movements are erratic or unrelated to the past.

4. Visualizing the Result The final output is displayed as a line on a separate window in your trading platform. This allows you to observe changes in market harmony over time. When the line shows significant peaks or shifts, it indicates a change in how the market is interacting with its own history, providing a visual cue for traders who rely on pattern recognition and trend consistency.

Miscellaneous
Implements a curve of type %1
7 Views
0 Downloads
0 Favorites
Corr
//+------------------------------------------------------------------+
//|                                                         Corr.mq4 |
//|                                                             Nike |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright "Nike"
#property link      "http://www.mql4.com"
//----
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- input parameters
extern int CorrLen = 7;
//---- buffers
double ExtMapBuffer[];
double Buffer1[];
double Buffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(3);
   SetIndexStyle(0, DRAW_LINE, EMPTY, 2);
   SetIndexBuffer(0, ExtMapBuffer);
   SetIndexBuffer(1, Buffer1);
   SetIndexBuffer(2, Buffer2);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator start function                                  |
//+------------------------------------------------------------------+
int start()
  {
   for(int i = 0; i < Bars; i++)
       ExtMapBuffer[i] = Close[i] - Open[i];  
//----
   for(i = 0; i < Bars - CorrLen; i++)
       ExtMapBuffer[i] = corr(ExtMapBuffer, i, CorrLen);
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double corr(double& a[], int start, int len)
  {
   double m1 = 0; 
   double m2 = 0; 
   double m1_2 = 0; 
   double m2_2 = 0; 
   double m12 = 0;
//----
   for(int i = 0; i < len; i++)
     {
       m1 = m1 + a[i];
       m2 = m2 + a[start + i];
       m1_2 = m1_2 + a[i]*a[i];
       m2_2 = m2_2 + a[start + i]*a[start + i];
       m12 = m12 + a[i]*a[start + i];
     }
   m1 = m1/len;
   m2 = m2/len;
   m1_2 = m1_2/len;
   m2_2 = m2_2/len;
   m12 = m12/len;
   if((m1_2 - m1*m1)*(m2_2 - m2*m2) == 0) 
       return(0);
   return((m12 - m1*m2) / MathSqrt(MathAbs((m1_2 - m1*m1)*(m2_2 - m2*m2))));
}
//+------------------------------------------------------------------+

Comments