AKF
Download scriptThis script is a technical analysis tool designed to provide traders with a visualized representation of market trends by applying advanced statistical modeling to historical price data. Instead of simply following the immediate "zig-zag" of the market, this tool looks back at a specific window of time (defined by the user) to identify underlying patterns.
Core Logic and Methodology
The indicator operates through a multi-step mathematical process to filter out "noise" and highlight significant price movements:
1. Historical Data Collection The script starts by gathering a set amount of historical data based on the "History" parameter. It focuses on the closing prices of the assets and maps them against a chronological timeline. This creates a foundation for calculating how price behaves over a specific period.
2. Linear Regression Analysis The tool uses a method called Linear Regression to determine the general direction of the market. It calculates a "line of best fit"—essentially a straight line that represents the average path of the price movement across the selected history. This identifies the primary trend (the "skeleton" of the price action) by calculating specific coefficients that define the slope and position of this trend line.
3. Residual Calculation Once the trend line is established, the script calculates the difference between the actual market prices and the calculated trend line. These differences are known as "residuals." By isolating these residuals, the tool can separate the temporary volatility (market noise) from the actual price direction.
4. Autocorrelation Modeling The script then performs a sophisticated calculation to determine how current price fluctuations relate to past fluctuations. It measures the correlation between the residuals at different points in time. This helps the indicator understand if recent movements are repeating previous patterns or if they represent new, unique behavior.
5. Normalization and Visualization Finally, the tool normalizes these complex calculations into a single value. It scales the data so that it can be displayed clearly on a separate window in the trading platform. The result is a red line that reflects the "autocorrelative" trend of the market, allowing traders to see smoothed, statistically significant trends rather than raw, erratic price changes.
Summary for the User
In practical terms, this indicator acts as a sophisticated filter. It takes a large chunk of historical data, calculates the average mathematical trend, removes the random noise from that trend, and then projects a smooth line that shows the relationship between past and present market movements. It is intended to help traders identify the structural flow of the market rather than reacting to every minor price fluctuation.
//+------------------------------------------------------------------+
//| AKF.mq4 |
//+------------------------------------------------------------------+
#property copyright "Ïðèâàëîâ"
#property link "Skype privalov-sv"
#property indicator_separate_window // Èíäèê. ðèñóåòñÿ â îñíîâíîì îêíå
#property indicator_minimum -1.0 // Ãðàíèöû îêíà
#property indicator_maximum 1.0
#property indicator_level1 0.0 // ëèíèÿ íóëÿ
#property indicator_buffers 1 // Êîëè÷åñòâî áóôåðîâ
#property indicator_color1 Red // Öâåò ëèíèè
extern int History = 1440;
extern bool WriteToFile = true;
double AKF[]; // Îòêðûòèå èíäèêàòîðíûõ ìàññèâîâ
double X[],Y[],Mu[];
int CountedBar = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
ArrayResize(AKF,History);
ArrayResize( X,History);
ArrayResize( Y,History);
ArrayResize( Mu,History);
//--------------------------------------------------------------------
IndicatorDigits(8);
SetIndexBuffer(0,AKF); // Íàçíà÷åíèå ìàññèâà áóôåðó
SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,1);// Ñòèëü ëèíèè
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
//----
if ( Time[0] <= CountedBar ) { return(0);}
CountedBar = Time[0];
if (Bars < History)
{
Alert(": Íåäîñòàòî÷íî áàðîâ íà ãðàôèêå");
return(0);
}
// íåîáõîäèìî ðàñ÷èòàòü ìàññèâ ìþ - ñîäåðæèò çíà÷åíèÿ ëèí. ðåãðåñèè
// ôîðìèðóåì ìàññèâû X è Y
for ( int i = 0; i <= History; i ++ )
{
X[i]=Time[i]-Time[History];
Y[i]=Close[i];
}
double A = 0, B = 0; // îáíóëÿþ êîýôôèöèåíòû ëèí. ðåãðåñcèè
double SKO = 0.0, AKF_norm = 0.0;
LinearRegr(X, Y, History, A, B);
for ( i = 0; i <= History; i ++ )
{
Mu[i]=A*X[i]+B;
SKO += MathPow( Y[i] - Mu[i], 2 );
}
SKO /= History;
Print(" A =", DoubleToStr( A, 8 ), " B =", DoubleToStr( B, 8 ));
//-------------------------------------------------------------------------------------------
// Ðàñ÷¸ò ÀÊÔ
for ( int m = 0; m <= History; m ++ )
{
double summ = 0.0;
for ( i = 0; i <= History; i ++ )
{
if ( i+m > History ) continue;
summ += ( Y[i] - Mu[i] ) * ( Y[i+m] - Mu[i+m] );
}
AKF[m] = 1/SKO*summ;
}
// 5. Íîðìèðîâêà
// Ïîëó÷åííûé ìàññèâ AKFm ïðîñòî äåëèì íà çíà÷åíèå, êîòîðîå íàõîäèòüñÿ â 0 ÿ÷åéêå
for ( m = History; m >= 0; m -- ) AKF[m] /= AKF[0];
return(0);
}
//+------------------------------------------------------------------+
//| Ðàññ÷åò êîýôôèöèåíòîâ A è B â óðàâíåíèè |
//| y(x)=A*x+B |
//| èñïîëüçóþòüñÿ ôîðìóëû http://forum.mql4.com/ru/10780/page5 |
//+------------------------------------------------------------------+
void LinearRegr(double X[], double Y[], int N, double& A, double& B)
{
double mo_X = 0.0, mo_Y = 0.0, var_0 = 0.0, var_1 = 0.0;
for ( int i = 0; i < N; i ++ )
{
mo_X +=X[i];
mo_Y +=Y[i];
}
mo_X /=N;
mo_Y /=N;
for ( i = 0; i < N; i ++ )
{
var_0 +=(X[i]-mo_X)*(Y[i]-mo_Y);
var_1 +=(X[i]-mo_X)*(X[i]-mo_X);
}
A = var_0 / var_1;
B = mo_Y - A * mo_X;
}
Comments