Standard Deviation free

by qualitiedx2 in category Oscilator at 25/05/2012
Description

Standard deviation is calculated with the following formula: σ = √[ ∑(x-mean)2 / N ]

Standard Deviation

Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgocTrader
using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(AccessRights = AccessRights.None)]
    public class StandardDeviation : Indicator
    {
        [Parameter]
        public DataSeries Source { get; set; }

        [Parameter(DefaultValue = 14, MinValue = 2)]
        public int Periods { get; set; }

        [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MAType { get; set; }

        [Output("Result", Color = Colors.Orange)]
        public IndicatorDataSeries Result { get; set; }


        private MovingAverage movingAverage;

        protected override void Initialize()
        {
            movingAverage = Indicators.MovingAverage(Source, Periods, MAType);
        }

        public override void Calculate(int index)
        {
            var average = movingAverage.Result[index];

            double sum = 0;

            for (var period = 0; period < Periods; period++)
            {
                sum += Math.Pow(Source[index - period] - average, 2.0);
            }

            Result[index] = Math.Sqrt(sum / Periods);
        }
    }
}
Comments
5