_minKvad
Download scriptThis technical script is designed to analyze market price movements by identifying key structural points and calculating a linear trend line based on those significant peaks and valleys. Here is a professional breakdown of the logic used:
Core Logic Overview
The indicator works by scanning a specific lookback period (determined by the user) to identify "local extremes." Instead of looking at every single price point, it specifically isolates bars where the high or low represents a turning point—meaning the price reached a local peak or valley relative to the bars immediately preceding and following it.
Step-by-Step Process
- Data Identification: The script first filters the historical data to find these local highs and lows. If a bar contains both a significant high and low, it calculates the midpoint between them to ensure a balanced data point for analysis.
- Linear Regression Analysis: Once these significant points are identified, the script performs a mathematical "Linear Regression." This is a method used to find the "Line of Best Fit." It calculates:
- The Slope: The angle/direction of the trend.
- The Intercept: The starting point of the trend relative to the data.
- Variance Calculation: After establishing the main trend line, the script measures the distance between the actual market prices and this theoretical line. It specifically looks for the maximum "over-extension" (the highest point above the trend) and the maximum "under-extension" (the lowest point below the trend) within the lookback period.
- Dynamic Forecasting: The logic projects these values forward to the current moment in time. This allows it to determine where the trend is headed and how far the price has deviated from that primary path.
Visual Output & Information
The script provides three distinct layers of information directly on the chart:
- Trend Line Reference: It identifies the calculated average "path" of the market.
- Upper Boundary: A calculation showing the maximum positive deviation (the upper limit) and how far the current price sits from that boundary.
- Lower Boundary: A calculation showing the maximum negative deviation (the lower limit) and how far the current price sits from that boundary.
Summary for the User
In simple terms, this tool ignores "market noise" to find the underlying trend. It builds a mathematical bridge between past peaks and valleys to show you the most likely path of the market. By calculating the maximum distances from this path, it helps users visualize whether the current price is moving in line with the established trend or if it is becoming over-extended in either direction.
//+------------------------------------------------------------------+
//| 3.mq4 |
//| Copyright © 2005, HexSys Software Corp. |
//| http:// |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, HexSys Software Corp."
#property link "http://www.metaquotes.net/"
#include <stdlib.mqh>
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Yellow
#property indicator_color4 Red
#property indicator_color5 Blue
//---- indicator parameters
extern int CountBar=60;
extern int PosZnak=1000;
//extern int ExtDeviation=5;
//extern int ExtBackstep=3;
//---- indicator buffers
double ExtMapBuffer[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
IndicatorBuffers(5);
//---- drawing settings
SetIndexStyle(0,DRAW_ARROW);
SetIndexStyle(1,DRAW_ARROW);
SetIndexStyle(2,DRAW_SECTION);
SetIndexStyle(3,DRAW_SECTION);
SetIndexStyle(4,DRAW_SECTION);
//---- indicator buffers mapping
SetIndexBuffer(0,ExtMapBuffer);
SetIndexBuffer(1,ExtMapBuffer2);
SetIndexBuffer(2,ExtMapBuffer3);
SetIndexBuffer(3,ExtMapBuffer4);
SetIndexBuffer(4,ExtMapBuffer5);
SetIndexEmptyValue(0,0.0);
SetIndexEmptyValue(1,0.0);
SetIndexEmptyValue(2,0.0);
SetIndexEmptyValue(3,0.0);
SetIndexEmptyValue(4,0.0);
ArraySetAsSeries(ExtMapBuffer,true);
ArraySetAsSeries(ExtMapBuffer2,true);
ArraySetAsSeries(ExtMapBuffer3,true);
ArraySetAsSeries(ExtMapBuffer4,true);
ArraySetAsSeries(ExtMapBuffer5,true);
//---- indicator short name
IndicatorShortName("minKvadrat");//+ExtDepth+","+ExtDeviation+","+ExtBackstep+")");
//---- initialization done
return(0);
}
int deinit()
{
//---- TODO: add your code here
ObjectDelete(CountBar+"Kva1");
ObjectDelete(CountBar+"Kva2");
ObjectDelete(CountBar+"Kva3");
//----
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
int i;
double Sx=0,Sy=0,Sxv2=0,Sxy=0,n=0,tekY=0,x=0;
double m=0,b=0;
double max=0,min=0,pos=0,tempY=0;
string txt;
for(i=CountBar; i>1; i--)
{
ExtMapBuffer[i-1]=0; ExtMapBuffer2[i-1]=0;
if ((High[i-2]<=High[i-1]) && (High[i]<=High[i-1])) ExtMapBuffer[i-1]=High[i-1];
if ((Low[i-2]>=Low[i-1]) && (Low[i]>=Low[i-1])) ExtMapBuffer2[i-1]=Low[i-1];
}
ExtMapBuffer3[CountBar]=0; ExtMapBuffer4[CountBar]=0; ExtMapBuffer5[CountBar]=0;
ExtMapBuffer3[2]=0; ExtMapBuffer4[2]=0; ExtMapBuffer5[2]=0; //*
for(i=CountBar; i>1; i--)
{
if (ExtMapBuffer[i]!=0 || ExtMapBuffer2[i]!=0)
{
n=n+1;
x=CountBar-i;
Sx=Sx+x;
if (ExtMapBuffer[i]!=0) tekY=ExtMapBuffer[i];
if (ExtMapBuffer2[i]!=0) tekY=ExtMapBuffer2[i];
if (ExtMapBuffer[i]!=0 && ExtMapBuffer2[i]!=0) tekY=(ExtMapBuffer[i]+ExtMapBuffer2[i])/2;
Sy=Sy+tekY;
Sxv2=Sxv2+x*x;
Sxy=Sxy+x*tekY;
}
}
m=(n*Sxy-Sx*Sy)/(n*(Sxv2)-Sx*Sx);
b=(Sy*Sxv2-Sx*Sxy)/(n*(Sxv2)-Sx*Sx);
//Print("Sx:",DoubleToStr(Sx,4)," Sy:",DoubleToStr(Sy,4)," Sxy:",DoubleToStr(Sxy,4),
// " Sxv2:",DoubleToStr(Sxv2,4)," n:",DoubleToStr(n,4),," m:",DoubleToStr(m,12),
// " b:",DoubleToStr(b,4));
ExtMapBuffer3[CountBar]=b;
ExtMapBuffer3[2]=m*(CountBar-2)+b;
//---------------------------------------------
max=0;min=1000;pos=0;
for(i=CountBar; i>1; i--)
{
if (ExtMapBuffer[i]!=0 || ExtMapBuffer2[i]!=0)
{
x=CountBar-i;
if (ExtMapBuffer[i]!=0) tekY=ExtMapBuffer[i];
if (ExtMapBuffer2[i]!=0) tekY=ExtMapBuffer2[i];
if (ExtMapBuffer[i]!=0 && ExtMapBuffer2[i]!=0) tekY=(ExtMapBuffer[i]+ExtMapBuffer2[i])/2;
tempY=tekY-(m*x+b);
if (tempY>=max) max=tempY;
if (tempY<=min) min=tempY;
//if (MathAbs(tekY-(m*x+b))>=max) { max=MathAbs(tekY-(m*x+b)); pos=i; }
}
}
ExtMapBuffer4[CountBar]=b+max;
ExtMapBuffer4[2]=m*(CountBar-2)+b+max;
ExtMapBuffer5[CountBar]=b+min;
ExtMapBuffer5[2]=m*(CountBar-2)+(b+min);
//Print("K(",CountBar,") [m]",DoubleToStr(m,9)," [min]",DoubleToStr(ExtMapBuffer5[2],4),
// " [max]",DoubleToStr(ExtMapBuffer4[2],4)," [srd]",DoubleToStr(ExtMapBuffer3[2],4),
// " [max-srd]",DoubleToStr(ExtMapBuffer4[2]-ExtMapBuffer3[2],4),
// " [srd-min]",DoubleToStr(ExtMapBuffer3[2]-ExtMapBuffer5[2],4),
// " [Bid-max]",DoubleToStr(Bid-ExtMapBuffer4[2],4),
// " [Bid-srd]",DoubleToStr(Bid-ExtMapBuffer3[2],4),
// " [Bid-min]",DoubleToStr(Bid-ExtMapBuffer5[2],4),
// );
//-------------------------------------------
txt=" ";
txt=txt+" "+DoubleToStr(m*(CountBar-2)+b,4);
txt=txt+" /"+DoubleToStr(MathArctan(m*PosZnak)*(180/3.141592653589793),2);
if(ObjectFind(CountBar+"Kva1") != 0)
{
ObjectCreate(CountBar+"Kva1", OBJ_TEXT, 0, Time[0], m*(CountBar-2)+b);
ObjectSetText(CountBar+"Kva1", txt, 10, "Arial", White);
}
else
{
ObjectSetText(CountBar+"Kva1", txt, 10, "Arial", White);
ObjectMove(CountBar+"Kva1", 0, Time[0], m*(CountBar-2)+b);
}
//-------------------------------------------
txt=" ";
txt=txt+"+"+DoubleToStr((ExtMapBuffer4[2]-ExtMapBuffer3[2]),4)+" pt";
txt=txt+" ["+DoubleToStr(Bid-ExtMapBuffer4[2],4)+"]";
if(ObjectFind(CountBar+"Kva2") != 0)
{
ObjectCreate(CountBar+"Kva2", OBJ_TEXT, 0, Time[0], m*(CountBar-2)+b+max);
ObjectSetText(CountBar+"Kva2", txt, 8, "Arial", White);
}
else
{
ObjectSetText(CountBar+"Kva2", txt, 8, "Arial", White);
ObjectMove(CountBar+"Kva2", 0, Time[0], m*(CountBar-2)+b+max);
}
//-------------------------------------------
txt=" ";
txt=txt+"-"+DoubleToStr((ExtMapBuffer3[2]-ExtMapBuffer5[2]),4)+" pt";
txt=txt+" ["+DoubleToStr(Bid-ExtMapBuffer5[2],4)+"]";
if(ObjectFind(CountBar+"Kva3") != 0)
{
ObjectCreate(CountBar+"Kva3", OBJ_TEXT, 0, Time[0], m*(CountBar-2)+b+min);
ObjectSetText(CountBar+"Kva3", txt, 8, "Arial", White);
}
else
{
ObjectSetText(CountBar+"Kva3", txt, 8, "Arial", White);
ObjectMove(CountBar+"Kva3", 0, Time[0], m*(CountBar-2)+b+min);
}
}//end
Comments