i-Regr_alert[1]

Download script
i-Regr_alert[1]

This script is a technical analysis tool designed to model market trends by calculating a mathematical "best-fit" curve over historical price data. Instead of simply following the price point-by-point, it attempts to identify the underlying trend line based on a specific window of time.

Core Logic and Methodology

1. Data Collection and Preparation The script begins by looking at a user-defined number of past price bars (the "history"). It focuses on the closing prices within this period to establish a baseline for market movement.

2. Polynomial Regression Modeling The primary engine of this script is a mathematical process called Polynomial Regression. The logic works as follows:

  • Matrix Construction: The script organizes historical data into a numerical matrix.
  • Coefficient Calculation: Using a method similar to "Gaussian Elimination," it solves for the most accurate curve that fits the existing price points. It seeks to minimize the distance between the actual market prices and the calculated trend line.
  • Degree of Curve: The "degree" setting determines how complex this curve is. A lower degree creates a smoother, simpler arc, while a higher degree allows the line to follow more intricate fluctuations in the data.

3. Trend Projection (The Forecast) Once the script determines the ideal curve for the historical data, it projects that curve forward. This results in a "predicted" path of the market. The script generates two distinct boundaries based on this projection:

  • Upper Bound: The predicted trend line plus a calculated margin of deviation.
  • Lower Bound: The predicted trend line minus a calculated margin of deviation.

4. Volatility Adjustment (Standard Deviation) The thickness of the "channel" between the upper and lower bounds is not fixed. The script calculates a Standard Deviation, which measures how much the price typically fluctuates away from the trend line. By multiplying this by a user-defined factor, the script creates a dynamic "safety zone." If the market is volatile, the channel widens; if the market is stable, the channel narrows.

5. Alert System The final logic of the script is to monitor real-time prices against these calculated boundaries:

  • Overextended Highs: If the current asking price rises above the upper bound, it triggers an alert.
  • Overextended Lows: If the current bidding price falls below the lower bound, it triggers an alert.

The system is designed to notify the user via a sound file whenever the market price moves outside of the mathematically expected trend, suggesting that the market may be reaching an extreme point or potentially reversing.

Miscellaneous
Implements a curve of type %1It plays sound alerts
15 Views
0 Downloads
0 Favorites
i-Regr_alert[1]


// alert added by http://www.automatedtradingsoftware.com March 3rd, 2009

//----
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 LimeGreen
#property indicator_color2 Gold
#property indicator_color3 Gold


extern bool    AlertOn   = true;
extern string  SoundFile = "alert2.wav"; // name of sound file 
extern int     degree    = 3;
extern double  kstd      = 2.0;
extern int     bars      = 250;
extern int     shift     = 0;


//-----
double fx[],sqh[],sql[];

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;

int i0 = 0;

/*  
void clear()
{
  int total = ObjectsTotal();  
  for (int i=total-1; i >= 0; i--) 
  {
    string name = ObjectName(i);
    if (StringFind(name, prefix) == 0) ObjectDelete(name);
  }
}
*/

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
  SetIndexBuffer(0, fx); // Áóôåðû ìàññèâîâ èíäèêàòîðà
  SetIndexBuffer(1, sqh);
  SetIndexBuffer(2, sql);

  SetIndexStyle(0, DRAW_LINE);
  SetIndexStyle(1, DRAW_LINE);
  SetIndexStyle(2, DRAW_LINE);

  SetIndexEmptyValue(0, 0.0);
  SetIndexEmptyValue(1, 0.0);
  SetIndexEmptyValue(2, 0.0);
  
  SetIndexShift(0, shift);
  SetIndexShift(1, shift);
  SetIndexShift(2, shift);

  return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
  //clear();
  return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
  if (Bars < bars) return;

//---- 
    
  int mi; // ïåðåìåííàÿ èñïîëüçóþùàÿñÿ òîëüêî â start
  ip = bars;
  p=ip; // òèïà ïðèñâàèâàíèå
  sx[1]=p+1; // ïðèìå÷àíèå - [] - îçíà÷àåò ìàññèâ
  nn = degree+1;

  SetIndexDrawBegin(0, Bars-p-1);
  SetIndexDrawBegin(1, Bars-p-1);
  SetIndexDrawBegin(2, Bars-p-1); 
   
//----------------------sx-------------------------------------------------------------------
  for(mi=1;mi<=nn*2-2;mi++) // ìàòåìàòè÷åñêîå âûðàæåíèå - äëÿ âñåõ mi îò 1 äî nn*2-2 
  {
    sum=0;
    for(n=i0;n<=i0+p;n++)
    {
       sum+=MathPow(n,mi);
    }
    sx[mi+1]=sum;
  }  
  //----------------------syx-----------
  for(mi=1;mi<=nn;mi++)
  {
    sum=0.00000;
    for(n=i0;n<=i0+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=i0;n<=i0+p;n++)
  {
    sum=0;
    for(kk=1;kk<=degree;kk++)
    {
       sum+=x[kk+1]*MathPow(n,kk);
    }
    fx[n]=x[1]+sum;
  } 
//-----------------------------------Std-----------------------------------------------------------------------------------
  sq=0.0;
  for(n=i0;n<=i0+p;n++)
  {
    sq+=MathPow(Close[n]-fx[n],2);
  }
  sq=MathSqrt(sq/(p+1))*kstd;

  for(n=i0;n<=i0+p;n++)
  {
    sqh[n]=fx[n]+sq;
    sql[n]=fx[n]-sq;
  }
  
  if(Ask >= sqh[i0] && AlertOn == true)Alerts(1);
  if(Bid <= sql[i0] && AlertOn == true)Alerts(2);
  

  return(0);
}
//+------------------------------------------------------------------+

void Alerts(int Direction)
{
   static datetime AlertLastTime = 0;

   if(Time[0] > AlertLastTime ) {
      AlertLastTime = Time[0];
      PlaySound(SoundFile);
   
   }
}

Comments