Stochastic.cs // Copyright (C) 2002-2005 Competo Solutions, Inc.
// All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR
// FITNESS FOR A PARTICULAR PURPOSE.

using System;
using System.Drawing;

using Trader.Data;

namespace Trader.Study
{
        /// <summary>
        /// Represents a Stochastic indicator.
        /// </summary>
        public class Stochastic : Indicator
        {
                private Series fastK;
                private Series k;
                private double d;
                private int x, y, z;
                private int lastIndex;
                private SMA smaY;
                private SMA smaZ;
                private SerializableColor kColor = new SerializableColor(Color.Blue);
                private SerializableColor dColor = new SerializableColor(Color.Red);

                /// <summary>
                /// ctor.
                /// </summary>
                public Stochastic() : base("Stochastic") {
                        this.x = 14;
                        this.y = 3;
                        this.z = 7;
                        this.smaY = new SMA(3);
                        this.smaZ = new SMA(7);
                        this.fastK = new Series();
                        this.k = new Series();
                        base.Position = ChartPosition.Lower;
                        this.NeedQuotes = true;
                }

                /// <summary>
                /// ctor.
                /// </summary>
                /// <param name="x"></param>
                /// <param name="y"></param>
                /// <param name="z"></param>
                public Stochastic(int x, int y, int z)  : base("Stochastic") {
                        this.x = x;
                        this.y = y;
                        this.z = z;
                        this.smaY = new SMA(y);
                        this.smaZ = new SMA(z);
                        this.fastK = new Series();
                        this.k = new Series();
                        base.Position = ChartPosition.Lower;
                        this.NeedQuotes = true;
                }

                /// <summary>
                /// Gets or sets the x.
                /// </summary>
                [IndicatorParameter]
                public int X {
                        get {
                                return this.x;
                        }
                        set {
                                this.x = value;
                                this.smaY.Period = value;
                        }
                }

                /// <summary>
                /// Gets or sets the y.
                /// </summary>
                [IndicatorParameter]
                public int Y {
                        get {
                                return this.y;
                        }
                        set {
                                this.y = value;
                                this.smaY.Period = value;
                        }
                }

                /// <summary>
                /// Gets or sets the Z.
                /// </summary>
                [IndicatorParameter]
                public int Z {
                        get {
                                return this.z;
                        }
                        set {
                                this.z = value;
                                this.smaZ.Period = value;
                        }
                }

                /// <summary>
                /// Calculates the Stochastic values from a quotes.
                /// </summary>
                /// <remarks>
                /// It returns the %D.
                /// </remarks>
                /// <param name="index"></param>
                /// <param name="quotes"></param>
                /// <returns></returns>
                public override double Calculate(int index, Quotes quotes) {
                        Series closes = quotes.Closes;
                        Series lows = quotes.Lows;
                        Series highs = quotes.Highs;

                        this.lastIndex = index;

                        this.fastK[index] = 100*(closes[index] - lows.GetLow(index, this.x))
                                /(highs.GetHigh(index, this.x) - lows.GetLow(index, this.x));

                        this.k[index] = this.fastK.Accept(index, this.smaY);

                        this.d = this.k.Accept(index, this.smaZ);

                        return this.d;
                }

                /// <summary>
                /// Gets the full K.
                /// </summary>
                [IndicatorValue]
                public double K {
                        get {
                                return this.k[this.lastIndex];
                        }
                }

                /// <summary>
                /// Gets or sets the color for K.
                /// </summary>
                [IndicatorParameter]
                public SerializableColor KColor {
                        get {
                                return this.kColor;
                        }
                        set {
                                this.kColor = value;
                        }
                }

                /// <summary>
                /// Gets or sets the color for D.
                /// </summary>
                [IndicatorParameter]
                public SerializableColor DColor {
                        get {
                                return this.dColor;
                        }
                        set {
                                this.dColor = value;
                        }
                }

                /// <summary>
                /// Gets the full D.
                /// </summary>
                [IndicatorValue]
                public double D {
                        get {
                                return this.d;
                        }
                }

        }
}