Exemplo n.º 1
0
        public static Result Calculate(double[] inputData, int period, double mfactor, double?[] outData)
        {
            Result result = new Result();

            result.Status = ResultStatus.Success;

            int len = inputData.Length;

            double?[] sma = new double?[len];
            try
            {
                var resultSMA = SMA.Calculate(inputData, period, sma);

                double multiplier = mfactor / (period + 1);

                outData[period - 1] = sma[period - 1].Value;

                for (int i = period; i < len; i++)
                {
                    double emaPre = outData[i - 1].Value;
                    outData[i] = Math.Round(((inputData[i] - emaPre) * multiplier + emaPre), 4);
                }
            }
            catch (Exception ex)
            {
                result.Status  = ResultStatus.Fail;
                result.Message = ex.ToString();
            }

            return(result);
        }
Exemplo n.º 2
0
        public static Result Calculate(double[] inputData, int period, double factor, double?[] outMiddle, double?[] outHigh, double?[] outLow)
        {
            Result res = new Result();

            res.Status = ResultStatus.Success;

            int len = inputData.Length;

            double[] sd = new double[len];

            try
            {
                SMA.Calculate(inputData, period, outMiddle);

                for (int i = period - 1; i < len; i++)
                {
                    double[] d = new double[period];

                    for (int j = 0; j < period; j++)
                    {
                        int bIndex = i + 1 - period;

                        d[j] = inputData[bIndex + j];
                    }

                    sd[i] = Math.Round(GenericHelper.StandardDeviation(d), 4);

                    outHigh[i] = Math.Round((outMiddle[i] + factor * sd[i]).Value, 4);

                    outLow[i] = Math.Round((outMiddle[i] - factor * sd[i]).Value, 4);
                }
            }
            catch (Exception ex)
            {
                res.Status  = ResultStatus.Fail;
                res.Message = ex.ToString();
                return(res);
            }

            return(res);
        }
Exemplo n.º 3
0
        public static Result CalculateSlow(double[] inputClose, double[] inputHigh, double[] inputLow, int period, int slow, double?[] outDataK, double?[] outDataD)
        {
            Result res = new Result();

            res.Status = ResultStatus.Success;

            int len = inputClose.Length;

            double[] tmpK = new double[len];
            try
            {
                for (int i = period - 1; i < len; i++)
                {
                    double[] tmpHigh = new double[period];
                    double[] tmpLow  = new double[period];

                    for (int j = 0; j < period; j++)
                    {
                        int bIndex = i - period + 1;

                        tmpHigh[j] = inputHigh[bIndex + j];
                        tmpLow[j]  = inputLow[bIndex + j];
                    }
                    tmpK[i] = GetBasicK(tmpHigh, tmpLow, inputClose[i]);
                }

                SMA.Calculate(tmpK, slow, outDataK);

                double[] outDataK2 = new double[len];

                for (int k = 0; k < len; k++)
                {
                    if (outDataK[k].HasValue)
                    {
                        outDataK2[k] = outDataK[k].Value;
                    }
                }

                SMA.Calculate(outDataK2, slow, outDataD);

                for (int j = 0; j < period; j++)
                {
                    outDataK[j] = null;
                    outDataD[j] = null;
                }


                for (int i = 0; i < len; i++)
                {
                    if (outDataD[i].HasValue)
                    {
                        outDataD[i] = Math.Round(outDataD[i].Value, 4);
                    }

                    if (outDataK[i].HasValue)
                    {
                        outDataK[i] = Math.Round(outDataK[i].Value, 4);
                    }
                }
            }
            catch (Exception ex)
            {
                res.Status  = ResultStatus.Fail;
                res.Message = ex.ToString();
                return(res);
            }
            return(res);
        }