Beispiel #1
0
        public static DataArray <double> ExponentialMovingAverage(StockSeriesData data, int periods)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data", "DataArray<double> must not be null.");
            }
            if (periods <= 0)
            {
                throw new ArgumentOutOfRangeException("periods", "Periods must not be negative or zero.");
            }
            var array = new DataArray <double>(data.Count);
            int num   = (((data.StartIndex + periods) - 1) < data.Count) ? ((data.StartIndex + periods) - 1) : data.Count;

            for (int i = 0; i < num; i++)
            {
                array.Add(0.0);
            }
            if (num < data.Count)
            {
                double num3 = 0.0;
                double num4 = 2.0 / (1 + periods);
                double num5 = 1.0 - num4;
                for (int j = data.StartIndex; j < (data.StartIndex + periods); j++)
                {
                    num3 += (double)data[j].close;
                }
                array.Add(num3 / periods);
                for (int k = num + 1; k < data.Count; k++)
                {
                    array.Add((num4 * (double)data[k].close) + (num5 * array[k - 1]));
                }
            }
            array.StartIndex = num;
            return(array);
        }
Beispiel #2
0
        public static DataArray <double> BollingerBandTop(StockSeriesData data, int periods, MovingAverageMethod method, double stddev)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data", "Data must not be null.");
            }
            if (periods <= 0)
            {
                throw new ArgumentOutOfRangeException("periods", "Periods must not be negative or zero.");
            }
            if (stddev <= 0.0)
            {
                throw new ArgumentOutOfRangeException("stddev", "Standard deviation must not be negative or zero.");
            }
            DataArray <double> array = null;

            switch (method)
            {
            case MovingAverageMethod.Simple:
                array = SimpleMovingAverage(data, periods);
                break;

            case MovingAverageMethod.Weighted:
                array = WeightedMovingAverage(data, periods);
                break;

            case MovingAverageMethod.Exponential:
                array = ExponentialMovingAverage(data, periods);
                break;
            }
            DataArray <double> array2 = ChartMath.StdDev(data, periods);

            return(ChartMath.Add(array, ChartMath.Multiply(array2, stddev)));
        }
Beispiel #3
0
        public static DataArray<double> BollingerBandTop(StockSeriesData data, int periods, MovingAverageMethod method, double stddev)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data", "Data must not be null.");
            }
            if (periods <= 0)
            {
                throw new ArgumentOutOfRangeException("periods", "Periods must not be negative or zero.");
            }
            if (stddev <= 0.0)
            {
                throw new ArgumentOutOfRangeException("stddev", "Standard deviation must not be negative or zero.");
            }
            DataArray<double> array = null;
            switch (method)
            {
                case MovingAverageMethod.Simple:
                    array = SimpleMovingAverage(data, periods);
                    break;

                case MovingAverageMethod.Weighted:
                    array = WeightedMovingAverage(data, periods);
                    break;

                case MovingAverageMethod.Exponential:
                    array = ExponentialMovingAverage(data, periods);
                    break;
            }
            DataArray<double> array2 = ChartMath.StdDev(data, periods);
            return ChartMath.Add(array, ChartMath.Multiply(array2, stddev));
        }
Beispiel #4
0
        public static DataArray <double> Sum(StockSeriesData data, int periods)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data", "DataArray<double> must not be null.");
            }
            if (periods <= 0)
            {
                throw new ArgumentOutOfRangeException("periods", "Periods must not be negative or zero.");
            }
            var array = new DataArray <double>(data.Count);
            int num   = (((data.StartIndex + periods) - 1) < data.Count) ? ((data.StartIndex + periods) - 1) : data.Count;

            for (int i = 0; i < num; i++)
            {
                array.Add(0.0);
            }
            if (num < data.Count)
            {
                double num3 = 0.0;
                for (int j = data.StartIndex; j < (data.StartIndex + periods); j++)
                {
                    num3 += (double)data[j].close;
                }
                array.Add(num3);
                for (int k = num + 1; k < data.Count; k++)
                {
                    num3 = (num3 - (double)data[k - periods].close) + (double)data[k].close;
                    array.Add(num3);
                }
            }
            array.StartIndex = num;
            return(array);
        }
Beispiel #5
0
 public static DataArray <double> MACD(StockSeriesData data, int shortPeriods, int longPeriods)
 {
     if (data == null)
     {
         throw new ArgumentNullException("data", "DataArray<double> must not be null.");
     }
     if (shortPeriods <= 0)
     {
         throw new ArgumentOutOfRangeException("shortPeriods", "Periods must not be negative or zero.");
     }
     if (longPeriods <= 0)
     {
         throw new ArgumentOutOfRangeException("longPeriods", "Periods must not be negative or zero.");
     }
     return(ChartMath.Subtract(ExponentialMovingAverage(data, shortPeriods), ExponentialMovingAverage(data, longPeriods)));
 }
Beispiel #6
0
        public static DataArray <double> Maximum(StockSeriesData data, double value)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data", "DataArray must not be null.");
            }
            var array = new DataArray <double>(data.Count);

            for (int i = 0; i < data.StartIndex; i++)
            {
                array.Add(0.0);
            }
            for (int j = data.StartIndex; j < data.Count; j++)
            {
                array.Add(((double)data[j].close > value) ? (double)data[j].close : value);
            }
            array.StartIndex = data.StartIndex;
            return(array);
        }
Beispiel #7
0
 public static DataArray<double> Maximum(StockSeriesData data, double value)
 {
     if (data == null)
     {
         throw new ArgumentNullException("data", "DataArray must not be null.");
     }
     var array = new DataArray<double>(data.Count);
     for (int i = 0; i < data.StartIndex; i++)
     {
         array.Add(0.0);
     }
     for (int j = data.StartIndex; j < data.Count; j++)
     {
         array.Add(((double)data[j].close > value) ? (double)data[j].close : value);
     }
     array.StartIndex = data.StartIndex;
     return array;
 }
Beispiel #8
0
 public static DataArray <double> SimpleMovingAverage(StockSeriesData data, int periods)
 {
     return(ChartMath.Divide(ChartMath.Sum(data, periods), periods));
 }
Beispiel #9
0
 // Methods
 protected StockSeries(string name)
     : base(name)
 {
     data = new StockSeriesData();
 }
Beispiel #10
0
 public static DataArray<double> SimpleMovingAverage(StockSeriesData data, int periods)
 {
     return ChartMath.Divide(ChartMath.Sum(data, periods), periods);
 }
Beispiel #11
0
 public static DataArray<double> WeightedMovingAverage(StockSeriesData data, int periods)
 {
     if (data == null)
     {
         throw new ArgumentNullException("data", "DataArray<double> must not be null.");
     }
     if (periods <= 0)
     {
         throw new ArgumentOutOfRangeException("periods", "Periods must not be negative or zero.");
     }
     DataArray<double> array = new DataArray<double>(data.Count);
     int num = (((data.StartIndex + periods) - 1) < data.Count) ? ((data.StartIndex + periods) - 1) : data.Count;
     for (int i = 0; i < num; i++)
     {
         array.Add(0.0);
     }
     if (num < data.Count)
     {
         double num3 = 0.0;
         double num4 = 0.0;
         double num5 = (periods * (periods + 1)) / 2;
         for (int j = data.StartIndex; j < (data.StartIndex + periods); j++)
         {
             num4 += ((j - data.StartIndex) + 1) * (double)data[j].close;
             num3 += (double)data[j].close;
         }
         array.Add(num4 / num5);
         for (int k = num + 1; k < data.Count; k++)
         {
             num4 = (num4 - num3) + (periods * (double)data[k].close);
             num3 = (num3 - (double)data[k - periods].close) + (double)data[k].close;
             array.Add(num4 / num5);
         }
     }
     array.StartIndex = num;
     return array;
 }
Beispiel #12
0
 public static DataArray<double> MACD(StockSeriesData data, int shortPeriods, int longPeriods)
 {
     if (data == null)
     {
         throw new ArgumentNullException("data", "DataArray<double> must not be null.");
     }
     if (shortPeriods <= 0)
     {
         throw new ArgumentOutOfRangeException("shortPeriods", "Periods must not be negative or zero.");
     }
     if (longPeriods <= 0)
     {
         throw new ArgumentOutOfRangeException("longPeriods", "Periods must not be negative or zero.");
     }
     return ChartMath.Subtract(ExponentialMovingAverage(data, shortPeriods), ExponentialMovingAverage(data, longPeriods));
 }
Beispiel #13
0
 public static DataArray<double> ExponentialMovingAverage(StockSeriesData data, int periods)
 {
     if (data == null)
     {
         throw new ArgumentNullException("data", "DataArray<double> must not be null.");
     }
     if (periods <= 0)
     {
         throw new ArgumentOutOfRangeException("periods", "Periods must not be negative or zero.");
     }
     var array = new DataArray<double>(data.Count);
     int num = (((data.StartIndex + periods) - 1) < data.Count) ? ((data.StartIndex + periods) - 1) : data.Count;
     for (int i = 0; i < num; i++)
     {
         array.Add(0.0);
     }
     if (num < data.Count)
     {
         double num3 = 0.0;
         double num4 = 2.0 / (1 + periods);
         double num5 = 1.0 - num4;
         for (int j = data.StartIndex; j < (data.StartIndex + periods); j++)
         {
             num3 += (double)data[j].close;
         }
         array.Add(num3 / periods);
         for (int k = num + 1; k < data.Count; k++)
         {
             array.Add((num4 * (double)data[k].close) + (num5 * array[k - 1]));
         }
     }
     array.StartIndex = num;
     return array;
 }
Beispiel #14
0
 /// <summary>
 /// цветовая схема, если null - берется из VisualSettings
 /// </summary>
 /*public Color? CustomColorFillUp { get; set; }
 public Color? CustomColorFillDn { get; set; }
 public Color? CustomColorFillEmpty { get; set; }
 public Color? CustomColorOutlineUp { get; set; }
 public Color? CustomColorOutlineDn { get; set; }*/
 internal void CopyFrom(StockSeries stockSeries)
 {
     if (stockSeries == null) return;
     Owner = stockSeries.Owner;
     Name = stockSeries.Name;
     BackColor = stockSeries.BackColor;
     ForeColor = stockSeries.ForeColor;
     LineWidth = stockSeries.LineWidth;
     NumberDecimalDigits = stockSeries.NumberDecimalDigits;
     upFillColor = stockSeries.upFillColor;
     upLineColor = stockSeries.upLineColor;
     downFillColor = stockSeries.downFillColor;
     downLineColor = stockSeries.downLineColor;
     BarNeutralColor = stockSeries.BarNeutralColor;
     data = stockSeries.Data;
 }
Beispiel #15
0
 public static DataArray<double> StdDev(StockSeriesData data, int periods)
 {
     return Sqrt(Variance(data, periods));
 }
Beispiel #16
0
 public static DataArray <double> StdDev(StockSeriesData data, int periods)
 {
     return(Sqrt(Variance(data, periods)));
 }
Beispiel #17
0
 public static DataArray<double> Variance(StockSeriesData data, int periods)
 {
     if (data == null)
     {
         throw new ArgumentNullException("data", "DataArray<double> must not be null.");
     }
     if (periods <= 0)
     {
         throw new ArgumentOutOfRangeException("periods", "Periods must not be negative or zero.");
     }
     var array = new DataArray<double>(data.Count);
     int num = (((data.StartIndex + periods) - 1) < data.Count) ? ((data.StartIndex + periods) - 1) : data.Count;
     for (int i = 0; i < num; i++)
     {
         array.Add(0.0);
     }
     if (num < data.Count)
     {
         double num3 = 0.0;
         double num4 = 0.0;
         for (int j = data.StartIndex; j < (data.StartIndex + periods); j++)
         {
             num3 += (double)data[j].close;
             num4 += (double)(data[j].close * data[j].close);
         }
         array.Add((num4 / (periods)) - ((num3 / (periods)) * (num3 / (periods))));
         for (int k = num + 1; k < data.Count; k++)
         {
             num3 = (num3 - (double)data[k - periods].close) + (double)data[k].close;
             num4 = (num4 - (double)(data[k - periods].close * data[k - periods].close)) + (double)(data[k].close * data[k].close);
             array.Add((num4 / (periods)) - ((num3 / (periods)) * (num3 / (periods))));
         }
     }
     array.StartIndex = num;
     return array;
 }
        /// <summary>
        /// Получить описывающий визуальный
        /// </summary>
        /// <param name="candles"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        private ChartEllipse BuildVisualInclusiveEllipse(StockSeriesData candles, int start, int end)
        {
            if (start == end) return null;
            float startvalue, endvalue;
            if (candles[start].high > candles[end].high)
            {
                startvalue = candles[start].high;
                endvalue = candles[end].low;
            }
            else
            {
                startvalue = candles[start].low;
                endvalue = candles[end].high;
            }
            var A = Conversion.WorldToScreen(new PointD(start, Convert.ToDouble(startvalue)), worldRect, canvasRect).ToPointF();
            var B = Conversion.WorldToScreen(new PointD(end, Convert.ToDouble(endvalue)), worldRect, canvasRect).ToPointF();
            var C = new PointF();

            double angle;
            float cx, cy, a, b;
            var S = float.NaN;
            var correctEllipse = false;
            // поиск охватывающего эллипса
            for (var i = start; i <= end; i++)
            {
                var d = Conversion.WorldToScreen(new PointD(i, Convert.ToDouble(candles[i].high)), worldRect, canvasRect).ToPointF();
                correctEllipse = Geometry.GetEllipseParams(A, B, d, out angle, out a, out b, out cx, out cy);
                if (float.IsNaN(S) || (correctEllipse && S < b))
                {
                    S = b;
                    C = d;
                }

                d = Conversion.WorldToScreen(new PointD(i, Convert.ToDouble(candles[i].low)), worldRect, canvasRect).ToPointF();
                correctEllipse = Geometry.GetEllipseParams(A, B, d, out angle, out a, out b, out cx, out cy);
                if (float.IsNaN(S) || (correctEllipse && S < b))
                {
                    S = b;
                    C = d;
                }
            }

            correctEllipse = // пересчитываем параметры эллипса
                Geometry.GetEllipseParams(A, B, C, out angle, out a, out b, out cx, out cy);
            if (correctEllipse)
            {   // можно построить эллипс - рисуем его
                var newEllipse = new ChartEllipse { BuildTangent = true, angle = angle, a = a, b = b, cx = cx, cy = cy };

                newEllipse.points.Add(Conversion.ScreenToWorld(new PointD(A.X, A.Y), worldRect, canvasRect).ToPointF());
                newEllipse.points.Add(Conversion.ScreenToWorld(new PointD(B.X, B.Y), worldRect, canvasRect).ToPointF());
                newEllipse.points.Add(Conversion.ScreenToWorld(new PointD(C.X, C.Y), worldRect, canvasRect).ToPointF());

                var minIndex = newEllipse.points.Min(p => p.X);
                newEllipse.DateStart = owner.StockSeries.GetCandleOpenTimeByIndex((int)minIndex);
                return newEllipse;
            }
            return null;
        }