Stochastic Oscillator: Trend + Values

by Roman_Volodko in category Oscilator at 17/11/2016
Description

 

UP Trend begins when 2 lines cross DOWN level bottom-up and ends when 2 lines cross UP level top-down

DOWN Trend begins when 2 lines cross UP level top-down and ends when 2 lines cross DOWN level bottom-up

Also indicator shows changes between UP and DOWN trends.

 

/
Download
1370 downloads
How to install
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
#region Using declarations
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
#endregion

namespace cAlgo
{
    [Levels(10, 20, 50, 80, 90)]

    [Indicator(IsOverlay = false, AutoRescale = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SO : Indicator
    {

        #region Properties
        [Parameter("K Periods", DefaultValue = 200)]
        public int kPeriods { get; set; }

        [Parameter("K Slowing", DefaultValue = 3)]
        public int kSlowing { get; set; }

        [Parameter("D Periods", DefaultValue = 3)]
        public int dPeriods { get; set; }

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

        [Parameter("UP Level", DefaultValue = 90, MinValue = 80, MaxValue = 100, Step = 1)]
        public int Up_Level { get; set; }

        [Parameter("Down Level", DefaultValue = 10, MinValue = 0, MaxValue = 20, Step = 1)]
        public int Down_Level { get; set; }

        [Output("% D", Color = Colors.Aquamarine, Thickness = 2)]
        public IndicatorDataSeries Percent_D { get; set; }

        [Output("% K", Color = Colors.Purple, Thickness = 2)]
        public IndicatorDataSeries Percent_K { get; set; }

        [Output("Trend", Color = Colors.Yellow, Thickness = 1)]
        public IndicatorDataSeries Value { get; set; }
        #endregion


        #region Variables
        enum Direction
        {
            Up,
            Down
        }

        private Direction direction = Direction.Down;
        private StochasticOscillator so;

        private double Extremum_Value = 0.0;
        private int Extremum_Index = 0;
        private int Last_Up_Index = 0;
        private int Last_Down_Index = 0;

        #endregion

        protected override void Initialize()
        {
            so = Indicators.StochasticOscillator(kPeriods, kSlowing, dPeriods, maType);

            if (Lines_Are_Lower_Level(Down_Level, 0))
                direction = Direction.Up;

            if (Lines_Are_Higher_Level(Up_Level, 0))
                direction = Direction.Down;
        }

        public override void Calculate(int index)
        {
            Percent_K[index] = so.PercentK.LastValue;
            Percent_D[index] = so.PercentD.LastValue;

            /// Up Level
            if (Lines_Just_Crossed_From_Up_To_Down_Level(Up_Level, index))
            {
                if (direction == Direction.Up)
                {
                    direction = Direction.Down;
                    Last_Up_Index = index;
                    Set_Extremum_At(index, Up_Level);
                }

                if (direction == Direction.Down && Extremum_Value == Up_Level)
                {
                    Last_Up_Index = index;
                    Move_Extremum_At(index, Up_Level);
                }
            }

            /// Down Level
            if (Lines_Just_Crossed_From_Down_To_Up_Level(Down_Level, index))
            {
                if (direction == Direction.Down)
                {
                    direction = Direction.Up;
                    Last_Down_Index = index;
                    Set_Extremum_At(index, Down_Level);
                }

                if (direction == Direction.Up && Extremum_Value == Down_Level)
                {
                    Last_Down_Index = index;
                    Move_Extremum_At(index, Down_Level);
                }
            }

            if (index - 1 != Extremum_Index)
            {
                Value[index - 1] = Double.NaN;
                Value[index] = Percent_D[index];
            }
        }

        private bool Lines_Are_Lower_Level(double level, int index)
        {
            return Percent_K[index] < level && Percent_D[index] < level;
        }

        private bool Lines_Are_Higher_Level(double level, int index)
        {
            return Percent_K[index] > level && Percent_D[index] > level;
        }

        private bool Lines_Just_Crossed_From_Up_To_Down_Level(double level, int index)
        {
            return Lines_Are_Lower_Level(level, index) && (Percent_K[index - 1] >= level || Percent_D[index - 1] >= level);
        }

        private bool Lines_Just_Crossed_From_Down_To_Up_Level(double level, int index)
        {
            return Lines_Are_Higher_Level(level, index) && (Percent_K[index - 1] <= level || Percent_D[index - 1] <= level);
        }

        private void Move_Extremum_At(int index, double v)
        {
            Value[Extremum_Index] = Double.NaN;
            ChartObjects.RemoveObject("Info " + Extremum_Index);
            Set_Extremum_At(index, v);
        }

        private void Set_Extremum_At(int Index, double V)
        {
            Extremum_Index = Index;
            Extremum_Value = V;
            Value[Extremum_Index] = Extremum_Value;

            VerticalAlignment Point_VA = VerticalAlignment.Center;
            HorizontalAlignment Point_HA = HorizontalAlignment.Center;
            Colors Point_Color = Colors.Yellow;

            ChartObjects.DrawText("Direction " + Index, "•", Index, V, Point_VA, Point_HA, Point_Color);

            double Low = MarketSeries.Low[Last_Down_Index];
            double High = MarketSeries.High[Last_Up_Index];
            double Trend_Value = (High - Low) / Symbol.TickSize;
            string Dir = direction == Direction.Down ? "▲" : "▼";
            string Info = Dir + " " + Trend_Value.ToString("N0");

            VerticalAlignment Text_VA = VerticalAlignment.Center;
            HorizontalAlignment Text_HA = HorizontalAlignment.Center;
            Colors Text_Color = Colors.Yellow;

            ChartObjects.DrawText("Info " + Index, Info, Index, V >= 50 ? V - 10 : V + 10, Text_VA, Text_HA, Text_Color);
        }
    }
}
Comments
0