Correlation_v3.1_kroufr

Author: Copyright � 2007, zIG
Correlation_v3.1_kroufr

This tool is designed to help traders understand the statistical relationship between two different financial instruments—specifically how closely they move in tandem or in opposite directions. By calculating a "correlation coefficient," it provides a visual and numerical representation of whether one currency pair acts as a mirror, a partner, or an independent mover compared to another.

Core Functionality

The script analyzes the price history of your current trading symbol against a secondary base currency (defaulted to EURUSD). It calculates how these two assets interact over specific timeframes to help identify patterns that might not be obvious by looking at price charts alone.

Key Features and Logic

  • Multi-Timeframe Analysis: The indicator calculates data across different lengths of history simultaneously. It distinguishes between "Short-Term" movements (capturing immediate, rapid fluctuations) and "Long-Term" movements (identifying broader historical trends). This allows you to see if a correlation that holds true over months is currently breaking down in the short term.

  • Dynamic Correlation Calculation: The logic works by comparing the price of your active chart against the base currency. It calculates the average price for both assets and then measures the variance. By mathematically determining how much these variances deviate from one another, it produces a correlation value:

    • A high positive value suggests the two assets move in the same direction.
    • A negative value suggests they move in opposite directions.
    • A value near zero suggests they have little to no relationship with each other.
  • Visual Representation: The tool displays these results in a separate window below your main chart using three distinct visual elements:

    1. Short-Term Line: A continuous line showing the immediate correlation.
    2. Long-Term Histogram: A vertical bar system representing the broader correlation.
    3. Dynamic Highlighting: The logic includes a switching mechanism where it intelligently chooses which bars to highlight based on the strength of the relationship, ensuring that the most significant data points are easier to identify at a glance.
  • Reference Levels: To make the data actionable, the indicator includes preset horizontal levels (specifically at -1.3 and 1.3). These act as "thresholds," helping traders quickly identify when the correlation becomes extreme or reaches a point of significance.

Practical Application

For a user, this means you can observe how your current trade is behaving in relation to a major global benchmark. If the correlation is consistently high, it tells you that the two assets are "linked." If the correlation suddenly drops or flips, it provides a signal that the market dynamics may be changing, potentially offering a warning or a new opportunity for different types of trading strategies.

Price Data Components
Series array that contains close prices for each bar
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
12 Views
0 Downloads
0 Favorites
Correlation_v3.1_kroufr
#property copyright "Copyright © 2007, zIG"
#property link      ""

#property indicator_separate_window


#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_width1 1
#property indicator_color2 SkyBlue
#property indicator_width2 2
#property indicator_color3 Green
#property indicator_width3 1

#property indicator_level1 -1.3
#property indicator_level2 0
#property indicator_level3 1.3
#property indicator_levelwidth 1
#property indicator_levelstyle STYLE_DOT

double buff1[];
double buff2[];
double buff3[];

extern int BarsForComment=30;  //êîëè÷åñòâî áàðîâ äëÿ ðàñ÷åòà êîððåëÿöèè, ðåçóëüòàòû êîòîðîé áóäóò âûâåäåíû â êîììåíòàðèè
extern int BarsForLongTF=30;  //êîëè÷åñòâî áàðîâ äëÿ ðàñ÷åòà êîððåëÿöèè íà äëèòåëüíîì ïåðèîäå
extern int BarsForShortTF=7; //êîëè÷åñòâî áàðîâ äëÿ ðàñ÷åòà êîððåëÿöèè íà êîðîòêîì ïåðèîäå
int SortMode=2;       // ðåæèì ñîðòèðîâêè 1- ïî âîçðàñòàíèþ, 2 - ïî óáûâàíèþ

extern int HistoryBars=1000;
extern string Base="EURUSD";

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
IndicatorBuffers(8); 
	
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0,buff1);
   SetIndexEmptyValue(0,0); 
   SetIndexLabel(0,"Êîððåëÿöèÿ çà "+BarsForLongTF+" áàðîâ");
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,buff2);
   SetIndexEmptyValue(1,EMPTY_VALUE); 
   SetIndexLabel(1,"Êîððåëÿöèÿ çà "+BarsForShortTF+" áàðîâ");

   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(2,buff3);
   SetIndexEmptyValue(2,0); 
   SetIndexLabel(2,"Êîððåëÿöèÿ çà "+BarsForLongTF+" áàðîâ");
   IndicatorShortName("Correlation calculator v3.0 ("+Period()+","+Base+" vs "+Symbol()+")");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   Comment("");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int i;

   


// âûâîä ãðàôèêîâ êîððåëÿöèè íà äëèòåëüíîì è êîðîòêîì ïåðèîäàõ äëÿ áàçîâîé è òåêóùåé âàëþò
   for(i=HistoryBars;i>=0;i--)
   {
    double RL=Rcalc(Symbol(),BarsForLongTF,i);
    double RS=Rcalc(Symbol(),BarsForShortTF,i);
    if(buff1[i+1]+buff3[i+1]>RL)
      {
      buff1[i]=RL;
      buff3[i]=0;
      }
      else
      {
      buff3[i]=RL;
      buff1[i]=0;
      }
//    buff1[i]=RL;
    
    buff2[i]=RS;
   }

string txt=""
+"\n ----------------------------------------------------------------------------------"
+"\n Ðàñ÷åò êîýôôèöèåíòà êîððåëÿöèè ìåæäó âàëþòàìè"
+"\n  Copyright © 2007, zIG & akadex"
+"\n ----------------------------------------------------------------------------------";
Comment(txt);
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

double Rcalc(string symb,int Bars_,int shift)
{
         int currday=iBarShift(NULL,PERIOD_D1,iTime(NULL,Period(),shift));
      double avgBase=iMA(Base,PERIOD_D1,Bars_,0,MODE_SMA,PRICE_CLOSE,currday);
      double avgSmb=iMA(symb,PERIOD_D1,Bars_,0,MODE_SMA,PRICE_CLOSE,currday);
      
      double E=0;
      double D=0;
      double F=0;
      
      for(int i=currday;i<=currday+Bars_;i++)
      {
       E+=((iClose(Base,PERIOD_D1,i))-avgBase)*((iClose(symb,PERIOD_D1,i))-avgSmb);
       D+=MathPow(((iClose(Base,PERIOD_D1,i))-avgBase),2);
       F+=MathPow(((iClose(symb,PERIOD_D1,i))-avgSmb),2);
      }
      D=MathSqrt(D);
      F=MathSqrt(F);
      double G=D*F;
      double R=E/G;
return(R);
}
//--------------------------------------------------------------------+
//+------------------------------------------------------------------+




Comments