SSRC_Alert_001

Download script
Author:
SSRC_Alert_001

This script functions as a specialized technical analysis indicator designed to measure the strength and direction of price trends by evaluating how consistently price movements correlate over time. Rather than simply looking at where the price is now, it analyzes the "rank" or order of price changes to determine the underlying momentum.

The logic can be broken down into three primary stages:

1. Data Smoothing and Weighting (The "Snake" and "Mart")

The script begins by cleaning and processing raw market data (such as Open, High, Low, or Close prices) to create a more stable foundation for analysis.

  • Weighted Moving Averages: It uses a custom calculation—referred to in the code as a "Snake"—to calculate weighted averages. This ensures that more recent price actions carry different significance than older ones, helping to filter out minor market noise.
  • Volatility Adjustment (Mart): The script then takes these averages and applies a smoothing formula. It identifies the highest and lowest points over a specific period and uses them to dampen extreme fluctuations. This creates a "smoothed" baseline that represents the core trend of the asset.

2. Statistical Correlation (Spearman Rank)

This is the core "intelligence" of the script. Instead of using standard linear math, it employs a statistical method called Spearman’s Rank Correlation.

  • Ranking Logic: The script looks at a specific window of time and assigns a rank to each price point (e.g., 1st place, 2nd place, etc.).
  • Correlation Coefficient: It calculates how well the sequence of these ranks follows a consistent path. If the prices are consistently moving in one direction, the correlation score will be high (approaching +1.0). If the price is moving randomly or reversing, the score will drop toward 0. If the movement is consistently downward, it will approach -1.0.
  • Boundaries: The script ensures that these results stay within a professional range of -1.0 to 1.0, providing a clear visual representation of trend strength.

3. Signal Generation

The final layer of the logic translates these complex statistical calculations into actionable alerts for the user.

  • Cross-Threshold Detection: The script monitors the correlation value as it moves in real-time. It specifically looks for moments when the value crosses a "trigger" threshold (set at 0.9).
  • Automated Alerts: When the correlation strength shifts rapidly across these levels—indicating a significant and confirmed trend change—the script triggers an automated alert. This notifies the user of a potential "Buy" or "Sell" opportunity based on the statistical exhaustion or acceleration of the market move.

Summary for the User: In simple terms, this tool ignores minor price "wiggles" and instead looks at the mathematical consistency of the trend. It calculates how "ordered" the market movement is; when that order reaches a peak and begins to shift, the script alerts you to the potential reversal or continuation of the trend.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screen
11 Views
0 Downloads
0 Favorites
SSRC_Alert_001
//+------------+-----------------------------------------------------+
//| v.12.04.07 |                                            SSRC.mq4 |
//+------------+              Spearman,                              |
//|            |              Rosh                                   |
//|            |              È ïðèìàçàâøèéñÿ ê íèì                  |
//|            |              Bookkeeper, 2007, yuzefovich@gmail.com |
//+------------+-----------------------------------------------------+
// http://www.improvedoutcomes.com/docs/WebSiteDocs/Clustering/
// Clustering_Parameters/Spearman_Rank_Correlation_Distance_Metric.htm
// http://www.infamed.com/stat/s05.html
// http://www.metaquotes.net
// http://codebase.mql4.com/ru/1023
#property copyright ""
#property link      "http://www.forexter.land.ru/indicators.htm"
//----
#property indicator_separate_window
#property indicator_maximum 1.3
#property indicator_minimum -1.3
#property indicator_level1 0.9
#property indicator_level2 -0.9
#property indicator_level3 0.75
#property indicator_level4 -0.75
#property indicator_level5 0
#property indicator_levelcolor Black
#property indicator_buffers  1
#property indicator_color1   Blue
//---- input parameters
//---- Snake 
extern int    SnakeRange   =14; 
extern int    FilterPeriod =21; 
extern double MartFiltr    =2;
extern int    PriceConst   =6; // 0 - Close
                               // 1 - Open
                               // 2 - High
                               // 3 - Low
                               // 4 - (H+L)/2
                               // 5 - (H+L+C)/3
                               // 6 - (H+L+2*C)/4
//---- buffers
double SRCBuffer[];
double Axis[];
double Mart[];
#define SIGNAL_BAR 1

//---- SpearmanRankCorrelation
int    rangeN = 14; //   = 30 maximum
double R2[];
double multiply;
int    PriceInt[];
int    SortInt[];
//+------------------------------------------------------------------+
void MartAxis(int Pos) { int SnakeWeight,i,w,ww,Shift;double SnakeSum;
switch(PriceConst) {
case  0: 
  Axis[Pos]=iMA(NULL,0,SnakeRange+1,0,MODE_LWMA,PRICE_CLOSE,Pos);
  break;
case  1: 
  Axis[Pos]=iMA(NULL,0,SnakeRange+1,0,MODE_LWMA,PRICE_OPEN,Pos);
  break;
case  2: 
  Axis[Pos]=iMA(NULL,0,SnakeRange+1,0,MODE_LWMA,PRICE_HIGH,Pos);
  break;
case  3: 
  Axis[Pos]=iMA(NULL,0,SnakeRange+1,0,MODE_LWMA,PRICE_LOW,Pos);
  break;
case  4: 
  Axis[Pos]=iMA(NULL,0,SnakeRange+1,0,MODE_LWMA,PRICE_MEDIAN,Pos);
  break;
case  5: 
  Axis[Pos]=iMA(NULL,0,SnakeRange+1,0,MODE_LWMA,PRICE_TYPICAL,Pos);
  break;
case  6: 
  Axis[Pos]=iMA(NULL,0,SnakeRange+1,0,MODE_LWMA,PRICE_WEIGHTED,Pos);
  break;
default: 
  Axis[Pos]=iMA(NULL,0,SnakeRange+1,0,MODE_LWMA,PRICE_WEIGHTED,Pos);
  break; }
for(Shift=Pos+SnakeRange+2;Shift>Pos;Shift--) { SnakeSum=0.0;
SnakeWeight=0; i=0; w=Shift+SnakeRange; ww=Shift-SnakeRange;
if(ww<Pos) ww=Pos;
while(w>=Shift) { i++; SnakeSum=SnakeSum+i*SnakePrice(w); 
SnakeWeight=SnakeWeight+i; w--; }
while(w>=ww) { i--; SnakeSum=SnakeSum+i*SnakePrice(w);
SnakeWeight=SnakeWeight+i; w--; }
Axis[Shift]=SnakeSum/SnakeWeight; } return; }
//----
double SnakePrice(int Shift) {
switch(PriceConst) {
   case  0: return(Close[Shift]);
   case  1: return(Open[Shift]);
   case  2: return(High[Shift]);
   case  3: return(Low[Shift]);
   case  4: return((High[Shift]+Low[Shift])/2);
   case  5: return((Close[Shift]+High[Shift]+Low[Shift])/3);
   case  6: return((2*Close[Shift]+High[Shift]+Low[Shift])/4);
   default: return(Close[Shift]); } }
//+------------------------------------------------------------------+
void SmoothOverMart(int Shift) { double t,b;
t=Axis[ArrayMaximum(Axis,FilterPeriod,Shift)];
b=Axis[ArrayMinimum(Axis,FilterPeriod,Shift)];
Mart[Shift]=(2*(2+MartFiltr)*Axis[Shift]-(t+b))/2/(1+MartFiltr);
return; }
//+------------------------------------------------------------------+
double SpearmanRankCorrelation(double Ranks[], int N) { double res,z2;
for(int i=0;i<N;i++) { z2 += MathPow(Ranks[i] - i - 1, 2); }
res=1-6*z2/(MathPow(N,3)-N); return(res); }
//+------------------------------------------------------------------+
void RankPrices(int InitialArray[]) { double dcounter, averageRank;
int i, k, m, dublicat, counter, etalon; double TrueRanks[];
ArrayResize(TrueRanks, rangeN); ArrayCopy(SortInt, InitialArray);
for(i=0;i<rangeN;i++) TrueRanks[i]=i+1; 
ArraySort(SortInt, 0, 0, MODE_DESCEND);
for(i=0;i<rangeN-1;i++) { if(SortInt[i]!=SortInt[i+1]) continue;
dublicat=SortInt[i]; k=i+1; counter=1; averageRank=i+1;
while(k<rangeN) { if(SortInt[k]==dublicat) {
counter++; averageRank+=k+1; k++; } else break; }
dcounter=counter; averageRank=averageRank/dcounter;
for(m=i;m<k;m++) TrueRanks[m]=averageRank; i=k; }
for(i=0;i<rangeN;i++) { etalon=InitialArray[i]; k=0;
while(k<rangeN) { if(etalon==SortInt[k]) { 
R2[i]=TrueRanks[k]; break; } k++; } } return; }
//+------------------------------------------------------------------+
int init() { IndicatorBuffers(3);
SetIndexBuffer(0,SRCBuffer); SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(1,Axis); SetIndexStyle(1,DRAW_NONE);
SetIndexBuffer(2,Mart); SetIndexStyle(2,DRAW_NONE);
ArrayResize(R2,rangeN); ArrayResize(PriceInt,rangeN);
ArrayResize(SortInt,rangeN);
if(rangeN>30) IndicatorShortName("Decrease rangeN input!");
else 
IndicatorShortName("SSRC( SR:"+SnakeRange+", FP:"+FilterPeriod+" )");
multiply=MathPow(10,Digits); return(0); }
//+------------------------------------------------------------------+
int deinit() { return(0); }
//+------------------------------------------------------------------+
int start() { int i,k,limit,limit2,limit3;
int counted_bars=IndicatorCounted(); if(rangeN>30) return(-1);
if(counted_bars==0) {
limit=Bars-(rangeN+FilterPeriod+SnakeRange+4);
limit2=Bars-(SnakeRange+2);
limit3=Bars-(FilterPeriod+SnakeRange+3); }
if(counted_bars>0) { 
limit=Bars-counted_bars+1; limit2=limit; limit3=limit; }
for(i=limit2;i>=0;i--) MartAxis(i);
for(i=limit3;i>=0;i--) SmoothOverMart(i);
for(i=limit;i>=0;i--) { 
for(k=0;k<rangeN;k++) PriceInt[k]=Mart[i+k]*multiply;
RankPrices(PriceInt); SRCBuffer[i]=SpearmanRankCorrelation(R2,rangeN);
if(SRCBuffer[i]>1.0) SRCBuffer[i]=1.0; 
if(SRCBuffer[i]<-1.0) SRCBuffer[i]=-1.0; } 

    //---- Ñòàòè÷åñêèå ïåðåìåííûå, â êîòîðûõ õðàíÿòñÿ
    //---- âðåìÿ ïîñëåäíåãî áàðà è íàïðàâëåíèå ïîñëåäíåãî ñèãíàëà
    static int PrevTime = 0;
 
    //---- Åñëè áàðîì äëÿ àíàëèçà âûáðàí íå 0-é, íàì íåò ñìûñëà ïðîâåðÿòü ñèãíàë
    //---- íåñêîëüêî ðàç. Åñëè íå íà÷àëñÿ íîâûé áàð, âûõîäèì.
    if(SIGNAL_BAR > 0 && Time[0] <= PrevTime ) 
        return(0);
    //---- Îòìå÷àåì, ÷òî ýòîò áàð ïðîâåðåí
    PrevTime = Time[0];
    
    
      {
        if(SRCBuffer[SIGNAL_BAR+1] - 0.9 >= 0 && 
           SRCBuffer[SIGNAL_BAR]-0.9 <= 0)
          {
           
            Alert("SSRC lvl 0.9 (", Symbol(), ", ", Period(), ")  -  SELL!!!");
          }
      }
   
      {
        if(SRCBuffer[SIGNAL_BAR] + 0.9 >= 0 && 
           SRCBuffer[SIGNAL_BAR+1] + 0.9 <= 0)
          {
          
            Alert("SSRC lvl 0.9 (", Symbol(), ", ", Period(), ")  -  BUY!!!");
          }
      }
      
return(0); }
//+------------------------------------------------------------------+

Comments