#-Pirson_v1
Download scriptThis technical indicator is designed to provide traders with a visual representation of the statistical relationship between current market prices and their recent moving averages. Rather than simply showing where the price is going, this tool measures how closely the price is "tracking" or "aligning" with its established trend over a specific period.
Core Logic and Methodology
The script operates by calculating a specialized correlation coefficient. To understand how this works in practical terms, the logic follows three primary steps:
1. Establishing a Baseline (Moving Averages) The tool first calculates a Simple Moving Average (SMA) based on a user-defined number of bars (the "N" value). This average serves as the smoothed baseline of the market's recent price action. It looks at both the current average and the average from the previous period to understand how the trend is shifting.
2. Measuring Price Deviation The script then examines the individual closing prices within that same timeframe. It measures the distance between these actual prices and the moving averages calculated in the previous step. Essentially, it is looking for the "gap" or the variance between where the price actually is and where the mathematical average suggests it should be.
3. Statistical Correlation Calculation Finally, the tool performs a sophisticated calculation to determine the correlation. It takes the sum of the differences (the gaps) and divides them by the standard deviation (a measure of market volatility).
What This Means for the User
The resulting output is displayed in a separate window as a line graph. This graph represents the strength of the relationship between price and trend:
- High Correlation: When the indicator shows a strong positive value, it suggests that the price is moving in high harmony with its moving average. The current trend is well-defined and consistent.
- Low/Weak Correlation: A drop in the indicator's value suggests that the price is beginning to move independently of its recent trend. This can be a sign of increased market noise, erratic behavior, or the beginning of a significant trend reversal.
By monitoring this relationship, traders can better identify periods of stable market momentum versus periods of high uncertainty or "decoupling," where the price action may no longer be following its historical pattern.
//+------------------------------------------------------------------+
//| #DT-Pirsonq4 |
//| Copyright © 2007, klot |
//| klot@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, klot"
#property link "klot@mail.ru"
//----
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Aqua
#property indicator_level1 0.0
extern int N=20;
//---- indicator buffers
double Pirson[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_SECTION);
//---- indicator buffers mapping
SetIndexBuffer(0,Pirson);
SetIndexEmptyValue(0,0.0);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
//----
double X,Y,Sx,Sy;
double sum=0.0;
//----
for( int shift=Bars-N-1; shift>=0; shift--)
{
X=iMA(NULL,0,N,0,MODE_SMA,PRICE_CLOSE,shift);
Y=iMA(NULL,0,N,0,MODE_SMA,PRICE_CLOSE,shift+1);
//----
sum=0;
for( int i=N-1; i>=0; i--)
{
sum+=(Close[shift+i]-X)*(Close[shift+i+1]-Y);
}
//---
Sx=iStdDev(NULL,0,N,0,MODE_SMA,PRICE_CLOSE,shift);
Sy=iStdDev(NULL,0,N,0,MODE_SMA,PRICE_CLOSE,shift+1);
//---
Pirson[shift]=sum/((N-1)*Sx*Sy);
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments