FastStochastic.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 FastStochastic : Indicator
        {
                private Series fastK;
                private double d;
                private int x, y;
                private int lastIndex;
                private SMA sma;
                private SerializableColor kColor = new SerializableColor(Color.Blue);
                private SerializableColor dColor = new SerializableColor(Color.Red);

                /// <summary>
                /// ctor.
                /// </summary>
                public FastStochastic() : base("FastStochastic") {
                        this.x = x;
                        this.y = 3;
                        this.sma = new SMA(3);
                        this.fastK = new Series();
                        base.Position = ChartPosition.Lower;
                        this.NeedQuotes = true;
                }

                /// <summary>
                /// ctor.
                /// </summary>
                /// <param name="x"></param>
                /// <param name="y"></param>
                public FastStochastic(int x, int y)  : base("FastStochastic") {
                        this.x = x;
                        this.y = y;
                        this.sma = new SMA(y);
                        this.fastK = 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;
                        }
                }

                /// <summary>
                /// Gets or sets the y.
                /// </summary>
                [IndicatorParameter]
                public int Y {
                        get {
                                return this.y;
                        }
                        set {
                                this.y = value;
                                this.sma.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.d = this.fastK.Accept(index, this.sma);

                        return this.d;

                }

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

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

                /// <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;
                        }
                }

        }
}