CenterofGravityalerttoughprice2
Download scriptThis script functions as a sophisticated technical analysis tool designed to identify the underlying "equilibrium" of price action on a trading chart. Rather than simply following the immediate price, it attempts to calculate a mathematically derived trajectory to determine where the price "should" be based on historical trends.
Core Logic and Mechanics
1. Mathematical Trend Modeling (Least Squares Regression) The heart of this script is a regression analysis. It looks back at a specific number of past candles (defined by the user) and analyzes the relationship between time and price. Instead of using a simple moving average—which often lags behind current events—it uses a complex matrix calculation to solve for a polynomial curve. This allows the tool to project a smoother, more accurate "line of best fit" that accounts for the momentum and curvature of the market over the selected period.
2. Dynamic Boundary Calculation To help traders identify overbought or oversold conditions, the script doesn't just provide a single center line; it establishes dynamic boundaries around that line. It calculates two types of "envelopes":
- Standard Deviation Bands: These use standard volatility measurements to show how far the price is straying from the average path.
- Square Root Variance: These provide an additional layer of geometric smoothing to help define the range of "normal" price behavior.
3. Real-Time Price Tracking and Projection The script constantly updates these calculations as new candles form. It identifies a specific "Pivot Point" on the chart (a reference anchor) and moves the calculated trend line dynamically to ensure it remains synchronized with the latest market data. This allows for a visual representation of the "Center of Gravity"—the point where the market's buying and selling pressures are currently balanced.
4. Automated Alert System The logic includes an intelligent notification system. By comparing the current live market price against the upper and lower boundaries calculated by the regression model, the script can trigger alerts. It specifically looks for instances where the price breaks out beyond the "normal" volatility range, signaling potential exhaustion in a trend or a significant breakout opportunity.
Summary for the User
Think of this tool as a "gravity map" for price. While the market may fluctuate wildly in the short term (noise), this script attempts to calculate the invisible "magnetic" path the price is following. It draws that path as a central line and creates a "tunnel" around it; when the price moves outside that tunnel, the script identifies it as a significant deviation from the norm, providing the trader with a visual and audible signal of potential market extremes.
//+------------------------------------------------------------------+
//| Center of Gravity.mq4 |
//| Original Code from NG3110@latchess.com |
//| Linuxser 2007 for TSD http://www.forex-tsd.com/ |
//+------------------------------------------------------------------+
#property copyright "ANG3110@latchess.com"
//---------ang_PR (Din)--------------------
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 RoyalBlue
#property indicator_color2 LimeGreen
#property indicator_color3 LimeGreen
#property indicator_color4 Goldenrod
#property indicator_color5 Goldenrod
//-----------------------------------
int sum1=0;
extern int bars_back = 125;
extern int m = 2;
extern int i = 0;
extern double kstd = 2.0;
extern int sName = 1102;
extern int Alertnumber=10;
//-----------------------
double fx[], sqh[], sql[], stdh[], stdl[];
double ai[10,10], b[10], x[10], sx[20];
double sum;
int ip, p, n, f;
double qq, mm, tt;
int ii, jj, kk, ll, nn;
double sq, std;
//*******************************************
int init()
{
IndicatorShortName("Center of Gravity");
SetIndexStyle(0, DRAW_LINE);
SetIndexBuffer(0, fx);
SetIndexBuffer(1, sqh);
SetIndexBuffer(2, sql);
SetIndexBuffer(3, stdh);
SetIndexBuffer(4, stdl);
p = MathRound(bars_back);
nn = m + 1;
ObjectCreate("pr" + sName, 22, 0, Time[p], fx[p]);
ObjectSet("pr" + sName, 14, 159);
return(0);
}
//----------------------------------------------------------
int deinit()
{
ObjectDelete("pr" + sName);
}
//**********************************************************************************************
int start()
{
int mi;
//-------------------------------------------------------------------------------------------
ip = iBarShift(Symbol(), Period(), ObjectGet("pr" + sName, OBJPROP_TIME1));
p = bars_back;
sx[1] = p + 1;
SetIndexDrawBegin(0, Bars - p - 1);
SetIndexDrawBegin(1, Bars - p - 1);
SetIndexDrawBegin(2, Bars - p - 1);
SetIndexDrawBegin(3, Bars - p - 1);
SetIndexDrawBegin(4, Bars - p - 1);
//----------------------sx-------------------------------------------------------------------
for(mi = 1; mi <= nn * 2 - 2; mi++)
{
sum = 0;
for(n = i; n <= i + p; n++)
{
sum += MathPow(n, mi);
}
sx[mi + 1] = sum;
}
//----------------------syx-----------
for(mi = 1; mi <= nn; mi++)
{
sum = 0.00000;
for(n = i; n <= i + p; n++)
{
if(mi == 1)
sum += Close[n];
else
sum += Close[n] * MathPow(n, mi - 1);
}
b[mi] = sum;
}
//===============Matrix=======================================================================================================
for(jj = 1; jj <= nn; jj++)
{
for(ii = 1; ii <= nn; ii++)
{
kk = ii + jj - 1;
ai[ii, jj] = sx[kk];
}
}
//===============Gauss========================================================================================================
for(kk = 1; kk <= nn - 1; kk++)
{
ll = 0; mm = 0;
for(ii = kk; ii <= nn; ii++)
{
if(MathAbs(ai[ii, kk]) > mm)
{
mm = MathAbs(ai[ii, kk]);
ll = ii;
}
}
if(ll == 0)
return(0);
if(ll != kk)
{
for(jj = 1; jj <= nn; jj++)
{
tt = ai[kk, jj];
ai[kk, jj] = ai[ll, jj];
ai[ll, jj] = tt;
}
tt = b[kk]; b[kk] = b[ll]; b[ll] = tt;
}
for(ii = kk + 1; ii <= nn; ii++)
{
qq = ai[ii, kk] / ai[kk, kk];
for(jj = 1; jj <= nn; jj++)
{
if(jj == kk)
ai[ii, jj] = 0;
else
ai[ii, jj] = ai[ii, jj] - qq * ai[kk, jj];
}
b[ii] = b[ii] - qq * b[kk];
}
}
x[nn] = b[nn] / ai[nn, nn];
for(ii = nn - 1; ii >= 1; ii--)
{
tt = 0;
for(jj = 1; jj <= nn - ii; jj++)
{
tt = tt + ai[ii, ii + jj] * x[ii + jj];
x[ii] = (1 / ai[ii, ii]) * (b[ii] - tt);
}
}
//===========================================================================================================================
for(n = i; n <= i + p; n++)
{
sum = 0;
for(kk = 1; kk <= m; kk++)
{
sum += x[kk + 1] * MathPow(n, kk);
}
fx[n] = x[1] + sum;
}
//-----------------------------------Std-----------------------------------------------------------------------------------
sq = 0.0;
for(n = i; n <= i + p; n++)
{
sq += MathPow(Close[n] - fx[n], 2);
}
sq = MathSqrt(sq / (p + 1)) * kstd;
std = iStdDev(NULL, 0, p, MODE_SMA, 0, PRICE_CLOSE, i) * kstd;
for(n = i; n <= i + p; n++)
{
sqh[n] = fx[n] + sq;
sql[n] = fx[n] - sq;
stdh[n] = fx[n] + std;
stdl[n] = fx[n] - std;
double a=sqh[0];
double b=sql[0];
double bid=Bid;
if (sum1 >Alertnumber && bid > b && bid < a )
{
sum1=0;
}
if(bid >= a && sum1 < Alertnumber )
{
Alert("ÃäÊÈå ÇáÎØ ÇáÚáæí",Symbol() );
sum1=sum1+1;
}
if(bid <=b && sum1 < Alertnumber )
{
Alert("ÃäÊÈå ÇáÎØ ÇáÓÝáí", Symbol() );
sum1=sum1+1;
}
}
//-------------------------------------------------------------------------------
ObjectMove("pr" + sName, 0, Time[p], fx[p]);
//----------------------------------------------------------------------------------------------------------------------------
return(0);
}
//==========================================================================================================================
Comments