Correlation market state

Author: copyright© mladen
2 Views
0 Downloads
0 Favorites
Correlation market state
ÿþ//------------------------------------------------------------------

#property copyright   "copyright© mladen"

#property link        "mladenfx@gmail.com"

//------------------------------------------------------------------

#property indicator_separate_window

#property indicator_buffers  5

#property indicator_plots    2

#property indicator_label1   "Trend"

#property indicator_type1    DRAW_FILLING

#property indicator_color1   clrLightGreen,clrOrange

#property indicator_label2   "State"

#property indicator_type2    DRAW_COLOR_LINE

#property indicator_color2   clrMediumSeaGreen,clrOrangeRed

#property indicator_width2   2



//

//

//



input int  inpPeriod = 35; // Period

double val[],valc[],fillu[],filld[],angle[],tcos[],tsin[];



//------------------------------------------------------------------

//

//------------------------------------------------------------------

//

//

//



int OnInit()

{

   SetIndexBuffer(0,fillu,INDICATOR_DATA); 

   SetIndexBuffer(1,filld,INDICATOR_DATA); 

   SetIndexBuffer(2,val  ,INDICATOR_DATA); 

   SetIndexBuffer(3,valc ,INDICATOR_COLOR_INDEX); 

   SetIndexBuffer(4,angle,INDICATOR_CALCULATIONS); 

      

      //

      //

      //

      

      ArrayResize(tcos,inpPeriod); for (int i=0; i<inpPeriod; i++) tcos[i] =  MathCos(i*2*M_PI/(double)inpPeriod); 

      ArrayResize(tsin,inpPeriod); for (int i=0; i<inpPeriod; i++) tsin[i] = -MathSin(i*2*M_PI/(double)inpPeriod); 

      IndicatorSetString(INDICATOR_SHORTNAME,StringFormat("Market state (%i)",inpPeriod));

   return(INIT_SUCCEEDED);

}

void OnDeinit(const int reason) {  return; }



//------------------------------------------------------------------

//

//------------------------------------------------------------------

//

//

//



int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long& tick_volume[],

                const long& volume[],

                const int& spread[])

{

   int limit = prev_calculated-1; if (limit<0) limit = 0;



   //

   //

   //

   

   for (int i=limit; i<rates_total; i++)

   {

      val[i] = filld[i] = 0;

      

      //

      //

      //

      

         double real = 0;

         double imag = 0;

         double cSx  = 0, sSx  = 0; 

         double cSy  = 0, sSy  = 0; 

         double cSxx = 0, sSxx = 0; 

         double cSxy = 0, sSxy = 0; 

         double cSyy = 0, sSyy = 0; 

         for (int k=0; k<inpPeriod && i>=k; k++)

         {

	         double  X = close[i-k] ; 

	         double cY = tcos[k];

	         double sY = tsin[k];

	            cSx  = cSx  +  X ; 

	            cSy  = cSy  + cY ; 

	            cSxx = cSxx +  X *  X; 

	            cSxy = cSxy +  X * cY; 

	            cSyy = cSyy + cY * cY; 

	            sSx  = sSx  +  X ; 

	            sSy  = sSy  + cY ; 

	            sSxx = sSxx +  X *  X; 

	            sSxy = sSxy +  X * sY; 

	            sSyy = sSyy + sY * sY; 

         }

         if ((inpPeriod*cSxx-cSx*cSx > 0 ) && (inpPeriod*cSyy-cSy*cSy > 0 )) real = (inpPeriod*cSxy-cSx*cSy) / MathSqrt((inpPeriod*cSxx-cSx*cSx) * (inpPeriod*cSyy-cSy*cSy)); 

         if ((inpPeriod*sSxx-sSx*sSx > 0 ) && (inpPeriod*sSyy-sSy*sSy > 0 )) imag = (inpPeriod*sSxy-sSx*sSy) / MathSqrt((inpPeriod*sSxx-sSx*sSx) * (inpPeriod*sSyy-sSy*sSy)); 

         

         if (imag!=0) angle[i] = 90.0 + 90.0*MathArctan(real/imag) ; 

         if (imag >0) angle[i] = val[i] - 180.0; 

         if (i>0 && (angle[i-1]-angle[i]<270 && angle[i]<angle[i-1])) angle[i] = angle[i-1];

         

         

         if (i>0)

         {

            if ((MathAbs(angle[i]-angle[i-1])<9.0 && angle[i]<0)) val[i] = -1; 

            if ((MathAbs(angle[i]-angle[i-1])<9.0 && angle[i]>0)) val[i] =  1; 

         }

         else val[i]   = 0;            

              fillu[i] = val[i];

              valc[i]  = val[i]>0 ? 0 : 1;

   }      

   return(rates_total);

}

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---