Ejemplo n.º 1
0
            ///<summary>
            /// Mass Index
            ///</summary>
            ///<param name="pNav">Navigator</param>
            ///<param name="pOHLCV">OHLCV Recordset</param>
            ///<param name="periods">Periods</param>
            ///<param name="Alias">Alias</param>
            ///<returns>Recordset</returns>
            public Recordset MassIndex(Navigator pNav, Recordset pOHLCV, int periods, string Alias)
            {
                Recordset     Results = new Recordset();
                General       GE      = new General();
                MovingAverage MA      = new MovingAverage();
                int           Record;

                int RecordCount = pNav.RecordCount;

                if (periods < 1 || periods > RecordCount)
                {
                    return(null);
                }

                Field Field1 = new Field(RecordCount, Alias);

                Recordset HML  = GE.HighMinusLow(pNav, pOHLCV, "HML");
                Field     Temp = HML.GetField("HML");
                Recordset EMA1 = MA.ExponentialMovingAverage(pNav, Temp, 9, "EMA");

                Temp = EMA1.GetField("EMA");
                Recordset EMA2 = MA.ExponentialMovingAverage(pNav, Temp, 9, "EMA");

                int Start = (periods * 2) + 1;

                pNav.Position = Start;
                for (Record = Start; Record < RecordCount + 2; Record++)
                {
                    double?Sum = 0;

                    pNav.Position = Record - periods;
                    int Period;
                    for (Period = 1; Period < periods + 1; Period++)
                    {
                        Sum = Sum + (EMA1.Value("EMA", pNav.Position) / EMA2.Value("EMA", pNav.Position));
                        pNav.MoveNext();
                    }//Period
                    pNav.MovePrevious();
                    Field1.Value(pNav.Position, Sum);

                    pNav.MoveNext();
                }//Record

                pNav.MoveFirst();
                Results.AddField(Field1);

                return(Results);
            }
Ejemplo n.º 2
0
        /// <summary>
        /// Action to be executd for calculating indicator
        /// </summary>
        /// <returns>for future usage. Must be ignored at this time.</returns>
        protected override bool TrueAction()
        {
// Validate
            int size = _chartPanel._chartX.RecordCount;

            if (size == 0)
            {
                return(false);
            }

            int paramInt1 = ParamInt(1);

            if (paramInt1 < 1 || paramInt1 > size / 2)
            {
                ProcessError("Invalid Periods for indicator " + FullName, IndicatorErrorType.ShowErrorMessage);
                return(false);
            }


            // Get the data
            string paramStr0 = ParamStr(0);
            Field  pSource   = SeriesToField("Source", paramStr0, size);

            if (!EnsureField(pSource, paramStr0))
            {
                return(false);
            }

            Navigator pNav = new Navigator();
            Recordset pRS  = new Recordset();

            pRS.AddField(pSource);
            pNav.Recordset_ = pRS;


            // Calculate the indicator
            MovingAverage ta   = new MovingAverage();
            Recordset     pInd = ta.ExponentialMovingAverage(pNav, pSource, paramInt1, FullName);


            // Output the indicator values
            Clear();
            for (int n = 0; n < size; ++n)
            {
                AppendValue(DM.GetTimeStampByIndex(n), n < paramInt1 ? null : pInd.Value(FullName, n + 1));
            }
            return(_calculateResult = PostCalculate());
        }
Ejemplo n.º 3
0
            ///<summary>
            /// Stochastic Momentum Index
            ///</summary>
            ///<param name="pNav">Navigator</param>
            ///<param name="pOHLCV">OHLCV Recordser</param>
            ///<param name="KPeriods">K Periods</param>
            ///<param name="KSmooth">K Smooth</param>
            ///<param name="KDoubleSmooth">K Double Smooth</param>
            ///<param name="DPeriods">D Periods</param>
            ///<param name="MAType">Moving Average Type</param>
            ///<param name="PctD_MAType">%D Movering Average Type</param>
            ///<returns>Recordset</returns>
            public Recordset StochasticMomentumIndex(Navigator pNav, Recordset pOHLCV, int KPeriods, int KSmooth, int KDoubleSmooth,
                                                     int DPeriods, IndicatorType MAType, IndicatorType PctD_MAType)
            {
                MovingAverage MA      = new MovingAverage();
                Recordset     Results = new Recordset();
                General       GN      = new General();
                int           Record;
                double?       Value = 0;

                KSmooth += 1;
                int RecordCount = pNav.RecordCount;

                Field Field1 = new Field(RecordCount, "%K");


                Recordset Temp = GN.HHV(pNav, pOHLCV.GetField("High"), KPeriods, "HHV");
                Field     HHV  = new Field(RecordCount, "HHV");

                Temp.CopyField(HHV, "HHV");

                Temp = GN.LLV(pNav, pOHLCV.GetField("Low"), KPeriods, "LLV");
                Field LLV = new Field(RecordCount, "LLV");

                Temp.CopyField(LLV, "LLV");


                Field HHLL = new Field(RecordCount, "HHLL");

                for (Record = 1; Record != RecordCount + 1; ++Record)
                {
                    Value = HHV.Value(Record) - LLV.Value(Record);
                    HHLL.Value(Record, Value);
                }


                Field CHHLL = new Field(RecordCount, "CHHLL");

                for (Record = 1; Record != RecordCount + 1; ++Record)
                {
                    Value = pOHLCV.Value("Close", Record) - (0.5f * (HHV.Value(Record) + LLV.Value(Record)));
                    CHHLL.Value(Record, Value);
                }

                if (KSmooth > 1)
                {
                    switch (MAType)
                    {
                    case IndicatorType.SimpleMovingAverage:
                        Temp = MA.SimpleMovingAverage(pNav, CHHLL, KSmooth, "CHHLL");
                        break;

                    case IndicatorType.ExponentialMovingAverage:
                        Temp = MA.ExponentialMovingAverage(pNav, CHHLL, KSmooth, "CHHLL");
                        break;

                    case IndicatorType.TimeSeriesMovingAverage:
                        Temp = MA.TimeSeriesMovingAverage(pNav, CHHLL, KSmooth, "CHHLL");
                        break;

                    case IndicatorType.TriangularMovingAverage:
                        Temp = MA.TriangularMovingAverage(pNav, CHHLL, KSmooth, "CHHLL");
                        break;

                    case IndicatorType.VariableMovingAverage:
                        Temp = MA.VariableMovingAverage(pNav, CHHLL, KSmooth, "CHHLL");
                        break;

                    case IndicatorType.WeightedMovingAverage:
                        Temp = MA.WeightedMovingAverage(pNav, CHHLL, KSmooth, "CHHLL");
                        break;

                    case IndicatorType.VIDYA:
                        Temp = MA.VIDYA(pNav, CHHLL, KSmooth, 0.65, "CHHLL");
                        break;
                    }

                    Temp.CopyField(CHHLL, "CHHLL");
                }


                if (KDoubleSmooth > 1)
                {
                    switch (MAType)
                    {
                    case IndicatorType.SimpleMovingAverage:
                        Temp = MA.SimpleMovingAverage(pNav, CHHLL, KDoubleSmooth, "CHHLL");
                        break;

                    case IndicatorType.ExponentialMovingAverage:
                        Temp = MA.ExponentialMovingAverage(pNav, CHHLL, KDoubleSmooth, "CHHLL");
                        break;

                    case IndicatorType.TimeSeriesMovingAverage:
                        Temp = MA.TimeSeriesMovingAverage(pNav, CHHLL, KDoubleSmooth, "CHHLL");
                        break;

                    case IndicatorType.TriangularMovingAverage:
                        Temp = MA.TriangularMovingAverage(pNav, CHHLL, KDoubleSmooth, "CHHLL");
                        break;

                    case IndicatorType.VariableMovingAverage:
                        Temp = MA.VariableMovingAverage(pNav, CHHLL, KDoubleSmooth, "CHHLL");
                        break;

                    case IndicatorType.WeightedMovingAverage:
                        Temp = MA.WeightedMovingAverage(pNav, CHHLL, KDoubleSmooth, "CHHLL");
                        break;

                    case IndicatorType.VIDYA:
                        Temp = MA.VIDYA(pNav, CHHLL, KDoubleSmooth, 0.65, "CHHLL");
                        break;
                    }

                    Temp.CopyField(CHHLL, "CHHLL");
                }

                if (KSmooth > 1)
                {
                    switch (MAType)
                    {
                    case IndicatorType.SimpleMovingAverage:
                        Temp = MA.SimpleMovingAverage(pNav, HHLL, KSmooth, "HHLL");
                        break;

                    case IndicatorType.ExponentialMovingAverage:
                        Temp = MA.ExponentialMovingAverage(pNav, HHLL, KSmooth, "HHLL");
                        break;

                    case IndicatorType.TimeSeriesMovingAverage:
                        Temp = MA.TimeSeriesMovingAverage(pNav, HHLL, KSmooth, "HHLL");
                        break;

                    case IndicatorType.TriangularMovingAverage:
                        Temp = MA.TriangularMovingAverage(pNav, HHLL, KSmooth, "HHLL");
                        break;

                    case IndicatorType.VariableMovingAverage:
                        Temp = MA.VariableMovingAverage(pNav, HHLL, KSmooth, "HHLL");
                        break;

                    case IndicatorType.WeightedMovingAverage:
                        Temp = MA.WeightedMovingAverage(pNav, HHLL, KSmooth, "HHLL");
                        break;

                    case IndicatorType.VIDYA:
                        Temp = MA.VIDYA(pNav, HHLL, KSmooth, 0.65, "HHLL");
                        break;
                    }

                    Temp.CopyField(HHLL, "HHLL");
                }


                if (KDoubleSmooth > 1)
                {
                    switch (MAType)
                    {
                    case IndicatorType.SimpleMovingAverage:
                        Temp = MA.SimpleMovingAverage(pNav, HHLL, KDoubleSmooth, "HHLL");
                        break;

                    case IndicatorType.ExponentialMovingAverage:
                        Temp = MA.ExponentialMovingAverage(pNav, HHLL, KDoubleSmooth, "HHLL");
                        break;

                    case IndicatorType.TimeSeriesMovingAverage:
                        Temp = MA.TimeSeriesMovingAverage(pNav, HHLL, KDoubleSmooth, "HHLL");
                        break;

                    case IndicatorType.TriangularMovingAverage:
                        Temp = MA.TriangularMovingAverage(pNav, HHLL, KDoubleSmooth, "HHLL");
                        break;

                    case IndicatorType.VariableMovingAverage:
                        Temp = MA.VariableMovingAverage(pNav, HHLL, KDoubleSmooth, "HHLL");
                        break;

                    case IndicatorType.WeightedMovingAverage:
                        Temp = MA.WeightedMovingAverage(pNav, HHLL, KDoubleSmooth, "HHLL");
                        break;

                    case IndicatorType.VIDYA:
                        Temp = MA.VIDYA(pNav, HHLL, KDoubleSmooth, 0.65, "HHLL");
                        break;
                    }

                    Temp.CopyField(HHLL, "HHLL");
                }


                for (Record = KPeriods + 1; Record != RecordCount + 1; ++Record)
                {
                    double?a = CHHLL.Value(Record);
                    double?b = (0.5f * HHLL.Value(Record));
                    if (a != b && b != 0)
                    {
                        Value = 100.0f * (a / b);
                    }
                    Field1.Value(Record, Value);
                }


                if (DPeriods > 1)
                {
                    switch (PctD_MAType)
                    {
                    case IndicatorType.SimpleMovingAverage:
                        Temp = MA.SimpleMovingAverage(pNav, Field1, DPeriods, "%D");
                        break;

                    case IndicatorType.ExponentialMovingAverage:
                        Temp = MA.ExponentialMovingAverage(pNav, Field1, DPeriods, "%D");
                        break;

                    case IndicatorType.TimeSeriesMovingAverage:
                        Temp = MA.TimeSeriesMovingAverage(pNav, Field1, DPeriods, "%D");
                        break;

                    case IndicatorType.TriangularMovingAverage:
                        Temp = MA.TriangularMovingAverage(pNav, Field1, DPeriods, "%D");
                        break;

                    case IndicatorType.VariableMovingAverage:
                        Temp = MA.VariableMovingAverage(pNav, Field1, DPeriods, "%D");
                        break;

                    case IndicatorType.WeightedMovingAverage:
                        Temp = MA.WeightedMovingAverage(pNav, Field1, DPeriods, "%D");
                        break;

                    case IndicatorType.VIDYA:
                        Temp = MA.VIDYA(pNav, Field1, DPeriods, 0.65, "%D");
                        break;
                    }

                    Field Field2 = new Field(RecordCount, "%D");
                    Temp.CopyField(Field2, "%D");
                    Results.AddField(Field2);
                }
                Results.AddField(Field1);

                return(Results);
            }
Ejemplo n.º 4
0
            /// <summary>
            /// Bollinger bands
            /// </summary>
            /// <param name="pNav">Navigator</param>
            /// <param name="pSource">Field source</param>
            /// <param name="periods">Periods</param>
            /// <param name="StandardDeviations">Standard deviation</param>
            /// <param name="MAType">Moving Average Type</param>
            /// <returns>Recordset</returns>
            public Recordset BollingerBands(Navigator pNav, Field pSource, int periods, int StandardDeviations, IndicatorType MAType)
            {
                MovingAverage MA      = new MovingAverage();
                Recordset     Results = null;
                int           Record;

                int RecordCount = pNav.RecordCount;

                if (MAType < Constants.MA_START || MAType > Constants.MA_END)
                {
                    return(null);
                }

                if (periods < 1 || periods > RecordCount)
                {
                    return(null);
                }

                if (StandardDeviations < 0 || StandardDeviations > 100)
                {
                    return(null);
                }

                Field Field1 = new Field(RecordCount, "Bollinger Band Bottom");
                Field Field2 = new Field(RecordCount, "Bollinger Band Top");

                switch (MAType)
                {
                case IndicatorType.SimpleMovingAverage:
                    Results = MA.SimpleMovingAverage(pNav, pSource, periods, "Bollinger Band Median");
                    break;

                case IndicatorType.ExponentialMovingAverage:
                    Results = MA.ExponentialMovingAverage(pNav, pSource, periods, "Bollinger Band Median");
                    break;

                case IndicatorType.TimeSeriesMovingAverage:
                    Results = MA.TimeSeriesMovingAverage(pNav, pSource, periods, "Bollinger Band Median");
                    break;

                case IndicatorType.TriangularMovingAverage:
                    Results = MA.TriangularMovingAverage(pNav, pSource, periods, "Bollinger Band Median");
                    break;

                case IndicatorType.VariableMovingAverage:
                    Results = MA.VariableMovingAverage(pNav, pSource, periods, "Bollinger Band Median");
                    break;

                case IndicatorType.WeightedMovingAverage:
                    Results = MA.WeightedMovingAverage(pNav, pSource, periods, "Bollinger Band Median");
                    break;

                case IndicatorType.VIDYA:
                    Results = MA.VIDYA(pNav, pSource, periods, 0.65, "Bollinger Band Median");
                    break;
                }
                if (Results == null)
                {
                    return(null);
                }

                int Start = periods + 1;

                pNav.Position = Start;

                for (Record = Start; Record < RecordCount + 1; Record++)
                {
                    double Sum   = 0;
                    double Value = Results.ValueEx("Bollinger Band Median", pNav.Position);

                    int Period;
                    for (Period = 1; Period < periods + 1; Period++)
                    {
                        Sum += (pSource.Value(pNav.Position).Value - Value) *
                               (pSource.Value(pNav.Position).Value - Value);
                        pNav.MovePrevious();
                    }//Period

                    pNav.Position = pNav.Position + periods;

                    Value = StandardDeviations * Math.Sqrt(Sum / periods);
                    Field1.Value(pNav.Position,
                                 Results.Value("Bollinger Band Median", pNav.Position) - Value);
                    Field2.Value(pNav.Position,
                                 Results.Value("Bollinger Band Median", pNav.Position) + Value);

                    pNav.MoveNext();
                }//Record

                //Append fields to recordset
                Results.AddField(Field1);
                Results.AddField(Field2);
                return(Results);
            }
Ejemplo n.º 5
0
            ///<summary>
            /// Moving Average Enveloper
            ///</summary>
            ///<param name="pNav">Navigator</param>
            ///<param name="pSource">Field Source</param>
            ///<param name="periods">Periods</param>
            ///<param name="MAType">Moving Average Type</param>
            ///<param name="Shift">Shift</param>
            ///<returns>Recordset</returns>
            public Recordset MovingAverageEnvelope(Navigator pNav, Field pSource, int periods, IndicatorType MAType, double Shift)
            {
                MovingAverage MA      = new MovingAverage();
                Recordset     Results = null;
                int           Record;

                int RecordCount = pNav.RecordCount;

                if (MAType < Constants.MA_START || MAType > Constants.MA_END)
                {
                    return(null);
                }

                if (periods < 1 || periods > RecordCount)
                {
                    return(null);
                }

                if (Shift < 0 || Shift > 100)
                {
                    return(null);
                }

                Field Field1 = new Field(RecordCount, "Envelope Top");
                Field Field2 = new Field(RecordCount, "Envelope Bottom");

                switch (MAType)
                {
                case IndicatorType.SimpleMovingAverage:
                    Results = MA.SimpleMovingAverage(pNav, pSource, periods, "Temp");
                    break;

                case IndicatorType.ExponentialMovingAverage:
                    Results = MA.ExponentialMovingAverage(pNav, pSource, periods, "Temp");
                    break;

                case IndicatorType.TimeSeriesMovingAverage:
                    Results = MA.TimeSeriesMovingAverage(pNav, pSource, periods, "Temp");
                    break;

                case IndicatorType.TriangularMovingAverage:
                    Results = MA.TriangularMovingAverage(pNav, pSource, periods, "Temp");
                    break;

                case IndicatorType.VariableMovingAverage:
                    Results = MA.VariableMovingAverage(pNav, pSource, periods, "Temp");
                    break;

                case IndicatorType.WeightedMovingAverage:
                    Results = MA.WeightedMovingAverage(pNav, pSource, periods, "Temp");
                    break;

                case IndicatorType.VIDYA:
                    Results = MA.VIDYA(pNav, pSource, periods, 0.65, "Temp");
                    break;
                }
                if (Results == null)
                {
                    return(null);
                }

                pNav.MoveFirst();
                Shift = Shift / 100;

                for (Record = 1; Record < RecordCount + 1; Record++)
                {
                    double Value = Results.ValueEx("Temp", pNav.Position);
                    Field1.Value(pNav.Position, Value + (Value * Shift));

                    Value = Results.ValueEx("Temp", pNav.Position);
                    Field2.Value(pNav.Position, Value - (Value * Shift));

                    pNav.MoveNext();
                }//Record

                //Append fields to recordset
                Results.AddField(Field1);
                Results.AddField(Field2);

                return(Results);
            }
Ejemplo n.º 6
0
            ///<summary>
            /// Standard deviation
            ///</summary>
            ///<param name="pNav">Navigator</param>
            ///<param name="pSource">Field source</param>
            ///<param name="periods">Periods</param>
            ///<param name="StandardDeviations">Standard Deviation</param>
            ///<param name="MAType">Moving Average Type</param>
            ///<param name="Alias">Alias</param>
            ///<returns>RecordSet</returns>
            public Recordset StandardDeviation(Navigator pNav, Field pSource, int periods, double StandardDeviations,
                                               IndicatorType MAType, string Alias)
            {
                Recordset     Results = null;
                MovingAverage MA      = new MovingAverage();
                int           Record;

                int   RecordCount = pNav.RecordCount;
                Field Field1      = new Field(RecordCount, Alias);

                switch (MAType)
                {
                case IndicatorType.SimpleMovingAverage:
                    Results = MA.SimpleMovingAverage(pNav, pSource, periods, "Temp");
                    break;

                case IndicatorType.ExponentialMovingAverage:
                    Results = MA.ExponentialMovingAverage(pNav, pSource, periods, "Temp");
                    break;

                case IndicatorType.TimeSeriesMovingAverage:
                    Results = MA.TimeSeriesMovingAverage(pNav, pSource, periods, "Temp");
                    break;

                case IndicatorType.TriangularMovingAverage:
                    Results = MA.TriangularMovingAverage(pNav, pSource, periods, "Temp");
                    break;

                case IndicatorType.VariableMovingAverage:
                    Results = MA.VariableMovingAverage(pNav, pSource, periods, "Temp");
                    break;

                case IndicatorType.WeightedMovingAverage:
                    Results = MA.WeightedMovingAverage(pNav, pSource, periods, "Temp");
                    break;

                case IndicatorType.VIDYA:
                    Results = MA.VIDYA(pNav, pSource, periods, 0.65, "Temp");
                    break;
                }
                if (Results == null)
                {
                    return(null);
                }

                int Start = periods + 1;

                pNav.Position = Start;

                for (Record = Start; Record < RecordCount + 1; Record++)
                {
                    double Sum   = 0;
                    double Value = Results.ValueEx("Temp", pNav.Position);

                    int Period;
                    for (Period = 1; Period < periods + 1; Period++)
                    {
                        Sum += (pSource.ValueEx(pNav.Position) - Value) * (pSource.ValueEx(pNav.Position) - Value);
                        pNav.MovePrevious();
                    }//Period

                    pNav.Position = pNav.Position + periods;

                    Value = StandardDeviations * Math.Sqrt(Sum / periods);
                    Field1.Value(pNav.Position, Value);

                    pNav.MoveNext();
                } //Record

                Results.AddField(Field1);

                return(Results);
            }