ang_PR (Din)-v1+SQStdN_v1
Download scriptThis script functions as a sophisticated technical analysis tool designed to project price trends and identify potential volatility boundaries on your trading chart. Instead of relying on simple moving averages, it utilizes advanced mathematical modeling to "smooth out" market noise and provide a clearer picture of the underlying price momentum.
Core Logic and Mechanism
The tool operates by analyzing historical price data over a specific timeframe (defined by the "hours" setting) to perform three primary functions:
1. Trend Projection (Curve Fitting) The script employs a mathematical process known as Gaussian elimination. It takes the closing prices of recent bars and treats them as a data set to calculate a polynomial curve. Essentially, it looks at how the price has behaved over the past few hours and calculates the most mathematically accurate "path" that those points follow. This results in a smooth trend line that represents the core direction of the market.
2. Dynamic Volatility Analysis Beyond just drawing a trend line, the script calculates the standard deviation of the price relative to that trend. It measures how much the price typically "wiggles" or deviates from the calculated path. By calculating this variance, the tool can determine the normal range of market behavior at any given moment.
3. Support and Resistance Boundaries Based on the trend projection and the volatility analysis, the script generates four distinct visual outputs:
- The Primary Trend Line: The central "spine" of the price movement.
- Upper and Lower Channels: These act as dynamic boundaries. They represent the expected "envelope" where the price is likely to trade based on current volatility.
- Standard Deviation Bands: These provide a secondary layer of depth, helping traders see how far the price has stretched away from its mathematical norm.
How it Appears on your Chart
When active, the script visualizes these calculations as colored lines. It provides a clear "corridor" for the price to move within. When the price stays within these boundaries, it suggests the market is following the established trend. If the price breaks significantly outside of these calculated boundaries, it may indicate a high-volatility event or a potential shift in market direction.
Key Settings Overview
- Hours: Determines how far back the script looks to calculate the trend (the "memory" of the tool).
- M: Adjusts the complexity of the polynomial curve (higher numbers allow for more complex curves; lower numbers keep it simpler).
- Kstd: A multiplier that expands or contracts the width of the volatility channels, allowing you to customize how much "room" you give the price to move.
#property copyright "ANG3110@latchess.com"
//---------ang_PR (Din)--------------------
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 DeepSkyBlue
#property indicator_color2 Lime
#property indicator_color3 Lime
#property indicator_color4 Yellow
#property indicator_color5 Yellow
//-----------------------------------
extern double hours = 120;
extern int m = 2;
extern int i = 0;
extern int kstd = 2;
extern int sName = 1102;
//-----------------------
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("at_PR (Din)");
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,fx);
SetIndexBuffer(1,sqh);
SetIndexBuffer(2,sql);
SetIndexBuffer(3,stdh);
SetIndexBuffer(4,stdl);
p=hours*60/Period();
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=ip;
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;}
//-------------------------------------------------------------------------------
ObjectMove("pr"+sName,0,Time[p],fx[p]);
//----------------------------------------------------------------------------------------------------------------------------
return(0);}
//==========================================================================================================================
Comments