Exemplo n.º 1
0
        public FloatSerie CalculateCorrelation(int period, FloatSerie serie)
        {
            if (this.Count != serie.Count) throw new ArgumentOutOfRangeException("Cannot Covariance on series with different size");

            FloatSerie ma1 = this.CalculateMA(period);
            FloatSerie ma2 = serie.CalculateMA(period);

            // Formula is there http://www.socscistatistics.com/tests/pearson/

            FloatSerie correl = new FloatSerie(this.Count, "CORREL");

            for (int i = period; i < this.Count; i++)
            {
                double sum1 = 0.0;
                double sum2 = 0.0;
                double sum3 = 0.0;
                for (int j = i - period; j <= i; j++)
                {
                    double diff1 = this[j] - ma1[i];
                    double diff2 = serie[j] - ma2[i];
                    sum1 += diff1 * diff2;
                    sum2 += diff1 * diff1;
                    sum3 += diff2 * diff2;
                }
                correl[i] = (float)(sum1 / (Math.Sqrt(sum2) * Math.Sqrt(sum3)));
            }

            return correl;
        }
Exemplo n.º 2
0
        public FloatSerie CalculateCovariance(int period, FloatSerie serie)
        {
            if (this.Count != serie.Count) throw new ArgumentOutOfRangeException("Cannot Covariance on series with different size");

            FloatSerie ma1 = this.CalculateMA(period);
            FloatSerie ma2 = serie.CalculateMA(period);
            FloatSerie variance = new FloatSerie(this.Count, "COVAR");
            float avg1, avg2;
            float sum;
            float spread1, spread2;
            int count;
            for (int i = period; i < this.Count; i++)
            {
                count = 0;
                sum = 0.0f;
                avg1 = ma1[i];
                avg2 = ma2[i];
                for (int j = i - period; j <= i; j++)
                {
                    count++;
                    spread1 = this.Values[j] - avg1;
                    spread2 = serie.Values[j] - avg2;
                    sum += spread1 * spread2;
                }
                variance[i] = sum / (float)count;
            }
            return variance;
        }
        public override void ApplyTo(StockSerie stockSerie)
        {
            int period = (int)this.parameters[0];
             FloatSerie nbUpDaysSerie = new FloatSerie(stockSerie.Count, "NBUPDAYS");
             FloatSerie closeSerie = stockSerie.GetSerie(StockDataType.CLOSE);

             float nbUpDays = 0;
             for (int i = 1; i <= period; i++)
             {
            if (closeSerie[i] >= closeSerie[i - 1])
            {
               nbUpDays++;
            }
            nbUpDaysSerie[i] = nbUpDays / i;
             }

             for (int i = period + 1; i < closeSerie.Count; i++)
             {
            nbUpDays = 0;
            for (int j = i - period + 1; j <= i; j++)
            {
               if (closeSerie[j] >= closeSerie[j - 1])
               {
                  nbUpDays++;
               }
            }
            nbUpDaysSerie[i] = nbUpDays / period;
             }
             this.Series[0] = nbUpDaysSerie.CalculateMA((int)this.parameters[1]).Sub(0.5f);
        }