コード例 #1
0
ファイル: OverlapStudies.cs プロジェクト: BryanW1215/Forex
        public static double MovingAverage(float[] inReal, int period, Core.MAType type)
        {
            int begIndex;
            int numElements;

            double[] outReal = new double[inReal.Length];
            Core.MovingAverage(0, inReal.Length - 1, inReal, period, type, out begIndex, out numElements, outReal);
            return(outReal[0]);
        }
コード例 #2
0
        public OperationResult <IndicatorSeries> CalculateStoch(List <double> highs,
                                                                List <double> lows,
                                                                List <double> closes,
                                                                int optInFastKPeriod,
                                                                int optInSlowKPeriod,
                                                                Core.MAType optInSlowKMAType,
                                                                int optInSlowDPeriod,
                                                                Core.MAType optInSlowDMAType)
        {
            try
            {
                var length = closes.Count();

                var endIdx  = length - 1;
                var inHigh  = highs.ToArray();
                var inLow   = lows.ToArray();
                var inClose = closes.ToArray();

                int outBegIdx;
                int outNBElement;
                var outSlowK = new double[length];
                var outSlowD = new double[length];

                var retCode = Core.Stoch(0,
                                         endIdx,
                                         inHigh,
                                         inLow,
                                         inClose,
                                         optInFastKPeriod,
                                         optInSlowKPeriod,
                                         optInSlowKMAType,
                                         optInSlowDPeriod,
                                         optInSlowDMAType,
                                         out outBegIdx,
                                         out outNBElement,
                                         outSlowK,
                                         outSlowD);
                if (retCode == Core.RetCode.Success)
                {
                    return(new OperationResult <IndicatorSeries>(new IndicatorDoubleSeries(outBegIdx, outNBElement, outSlowK, outSlowD)));
                }
                else
                {
                    return(new OperationResult <IndicatorSeries>(false, retCode.ToString()));
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                throw new TALibException(ex);
            }
        }
コード例 #3
0
        public OperationResult <IndicatorSeries> CalculateBbands(List <double> closes,
                                                                 int optInTimePeriod,
                                                                 double optInNbDevUp,
                                                                 double optInNbDevDn,
                                                                 Core.MAType optInMAType)
        {
            try
            {
                var length = closes.Count();

                var endIdx = length - 1;
                var inReal = closes.ToArray();

                int outBegIdx;
                int outNBElement;
                var outRealUpperBand  = new double[length];
                var outRealMiddleBand = new double[length];
                var outRealLowerBand  = new double[length];

                var retCode = Core.Bbands(0,
                                          endIdx,
                                          inReal,
                                          optInTimePeriod,
                                          optInNbDevUp,
                                          optInNbDevDn,
                                          optInMAType,
                                          out outBegIdx,
                                          out outNBElement,
                                          outRealUpperBand,
                                          outRealMiddleBand,
                                          outRealLowerBand);

                if (retCode == Core.RetCode.Success)
                {
                    return(new OperationResult <IndicatorSeries>(new IndicatorTripleSeries(outBegIdx, outNBElement, outRealUpperBand, outRealMiddleBand, outRealLowerBand)));
                }
                else
                {
                    return(new OperationResult <IndicatorSeries>(false, retCode.ToString()));
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                throw new TALibException(ex);
            }
        }
コード例 #4
0
        public BBand(string pair, int periods, int stdDev, Core.MAType mAType = Core.MAType.Sma)
        {
            this.pair = pair;

            this.periods = periods;
            this.stdDev  = stdDev;
            prices       = new List <float>();
            this.mAType  = mAType;

            Upper  = new double[periods];
            Middle = new double[periods];
            Lower  = new double[periods];

            UpdatePrices();
            Update();

            Events.CandleEvent += Events_CandleEvent;
        }
コード例 #5
0
ファイル: Oscillators.cs プロジェクト: BryanW1215/Forex
        public static stochasticResult Stochastic(float[] inHigh, float[] inLow, float[] inClose, int FastK_Period,
                                                  int SlowK_Period, int SlowD_Period, Core.MAType FastK_MAType = Core.MAType.Sma,
                                                  Core.MAType SlowD_MAType = Core.MAType.Sma)
        {
            var startIdx = 0;
            var endIdx   = inHigh.Length - 1;
            var slowD    = new double[inHigh.Length];
            var slowK    = new double[inHigh.Length];
            int begIndex;
            int nbElements;

            Core.Stoch(startIdx, endIdx, inHigh, inLow, inClose, FastK_Period, SlowK_Period, FastK_MAType, SlowD_Period,
                       SlowD_MAType, out begIndex, out nbElements, slowK, slowD);
            return(new stochasticResult()
            {
                slowD = slowD[0], slowK = slowK[0]
            });
        }
コード例 #6
0
        public static MovingAverageType FromTaLib(Core.MAType maType)
        {
            switch (maType)
            {
            case Core.MAType.Ema:
                return(MovingAverageType.Ema);

            case Core.MAType.Kama:
                return(MovingAverageType.Kama);

            case Core.MAType.Sma:
                return(MovingAverageType.Sma);

            case Core.MAType.Tema:
                return(MovingAverageType.Tema);

            case Core.MAType.Wma:
                return(MovingAverageType.Wma);

            default:
                return(MovingAverageType.Sma);
            }
        }
コード例 #7
0
        public ITaLibOutput <(double[] macd, double[] macdSignal, double[] macdHistory)> MovingAverageConvergenceDivergence(Frequency unitType, int historiesLength, Core.MAType fastMaType = Core.MAType.Sma, Core.MAType slowMaType = Core.MAType.Sma, Core.MAType signalMaType = Core.MAType.Sma, int fastPeriod = 12, int slowPeriod = 26, int signalPeriod = 9)
        {
            var inputs = new IndicatorsInputs(database, historiesLength);

            database.FillMarketHistory(unitType, inputs.OutputArrayLength);

            double[] closingHistory = database.CurrentHistory
                                      .Select(bar => bar.ClosingPrice)
                                      .ToArray();

            var output = new TaLibOutput <(double[] macd, double[] macdSignal, double[] macdHistory)> {
                Series = (macd : new double[inputs.OutputArrayLength],
                          macdSignal : new double[inputs.OutputArrayLength],
                          macdHistory : new double[inputs.OutputArrayLength]),
                HistoryWasCustom = database.HistoryIsCustom
            };
            int outBeginningIndex = 0;
            int outNBElement      = 0;

            output.Outcome = Core.MacdExt(startIdx: inputs.StartingIndex,
                                          endIdx: inputs.EndIndex,
                                          inReal: closingHistory,
                                          optInFastPeriod: fastPeriod,
                                          optInSignalPeriod: signalPeriod,
                                          optInSlowPeriod: slowPeriod,
                                          outBegIdx: out outBeginningIndex,
                                          outNBElement: out outNBElement,
                                          outMACD: output.Series.macd,
                                          outMACDHist: output.Series.macdHistory,
                                          outMACDSignal: output.Series.macdSignal,
                                          optInFastMAType: fastMaType,
                                          optInSignalMAType: signalMaType,
                                          optInSlowMAType: slowMaType);
            output.AlgorithmsBeginningIndex = outBeginningIndex;
            output.NBElement = outNBElement;
            return(output);
        }
コード例 #8
0
        public void MovingAverageWithVariablePeriod()
        {
            var goodOutputsLine1  = new double[HistoriesLength];
            int outBeginningIndex = 0;
            int outNBElement      = 0;

            Core.MAType movingAverageType = Core.MAType.Sma;
            int         minPeriod         = 2;
            int         maxPeriod         = 30;

            var GoodOutputsRetCode1 = Core.MovingAverageVariablePeriod(
                FakeDataBase.ManualStartingIndex,
                EndingIndex,
                ClosingPrices,
                PeriodsPerPrices,
                minPeriod,
                maxPeriod,
                movingAverageType,
                out outBeginningIndex,
                out outNBElement,
                goodOutputsLine1);

            var possiblyBadOutput1 = objectToTest.MovingAverageWithVariablePeriod(
                TestDataFrequency,
                PeriodsPerPrices,
                HistoriesLength
                );

            Assert.Equal(GoodOutputsRetCode1, possiblyBadOutput1.Outcome);
            Assert.Equal(outBeginningIndex, possiblyBadOutput1.AlgorithmsBeginningIndex);
            Assert.Equal(outNBElement, possiblyBadOutput1.NBElement);

            for (int i = 0; i < goodOutputsLine1.Length; i++)
            {
                Assert.Equal(goodOutputsLine1[i], possiblyBadOutput1.Series[i]);
            }

            //CustomHistory test
            FakeDataBase.HistoryIsCustom     = true;
            FakeDataBase.ManualStartingIndex = 5;
            goodOutputsLine1 = new double[HistoriesLength];

            var GoodOutputsRetCode2 = Core.MovingAverageVariablePeriod(
                FakeDataBase.ManualStartingIndex,
                EndingIndex,
                ClosingPrices,
                PeriodsPerPrices,
                minPeriod,
                maxPeriod,
                movingAverageType,
                out outBeginningIndex,
                out outNBElement,
                goodOutputsLine1);

            var possiblyBadOutput2 = objectToTest.MovingAverageWithVariablePeriod(
                TestDataFrequency,
                PeriodsPerPrices,
                HistoriesLength
                );

            Assert.Equal(GoodOutputsRetCode2, possiblyBadOutput2.Outcome);
            Assert.Equal(outBeginningIndex, possiblyBadOutput2.AlgorithmsBeginningIndex);
            Assert.Equal(outNBElement, possiblyBadOutput2.NBElement);

            for (int i = 0; i < goodOutputsLine1.Length; i++)
            {
                Assert.Equal(goodOutputsLine1[i], possiblyBadOutput2.Series[i]);
            }
        }
コード例 #9
0
        public void BollingerBands()
        {
            var goodOutputsLine1 = new double[HistoriesLength];
            var goodOutputsLine2 = new double[HistoriesLength];
            var goodOutputsLine3 = new double[HistoriesLength];

            int outBeginningIndex = 0;
            int outNBElement      = 0;

            Core.MAType movingAverageType = Core.MAType.Sma;
            int         timePeriod        = 5;
            int         nbDevUp           = 2;
            int         nbDevDown         = 2;

            var GoodOutputsRetCode1 = Core.Bbands(
                FakeDataBase.ManualStartingIndex,
                EndingIndex,
                ClosingPrices,
                timePeriod,
                nbDevUp,
                nbDevDown,
                movingAverageType,
                out outBeginningIndex,
                out outNBElement,
                goodOutputsLine1,
                goodOutputsLine2,
                goodOutputsLine3);

            var possiblyBadOutput1 = objectToTest.BollingerBands(
                TestDataFrequency,
                HistoriesLength
                );

            Assert.Equal(GoodOutputsRetCode1, possiblyBadOutput1.Outcome);
            Assert.Equal(outBeginningIndex, possiblyBadOutput1.AlgorithmsBeginningIndex);
            Assert.Equal(outNBElement, possiblyBadOutput1.NBElement);

            for (int i = 0; i < goodOutputsLine1.Length; i++)
            {
                Assert.Equal(goodOutputsLine1[i], possiblyBadOutput1.Series.upperBand[i]);
                Assert.Equal(goodOutputsLine2[i], possiblyBadOutput1.Series.middleBand[i]);
                Assert.Equal(goodOutputsLine3[i], possiblyBadOutput1.Series.lowerBand[i]);
            }

            //CustomHistory test
            FakeDataBase.HistoryIsCustom     = true;
            FakeDataBase.ManualStartingIndex = 5;
            goodOutputsLine1 = new double[HistoriesLength];
            goodOutputsLine2 = new double[HistoriesLength];
            goodOutputsLine3 = new double[HistoriesLength];

            var GoodOutputsRetCode2 = Core.Bbands(
                FakeDataBase.ManualStartingIndex,
                EndingIndex,
                ClosingPrices,
                timePeriod,
                nbDevUp,
                nbDevDown,
                movingAverageType,
                out outBeginningIndex,
                out outNBElement,
                goodOutputsLine1,
                goodOutputsLine2,
                goodOutputsLine3);

            var possiblyBadOutput2 = objectToTest.BollingerBands(
                TestDataFrequency,
                HistoriesLength
                );

            Assert.Equal(GoodOutputsRetCode2, possiblyBadOutput2.Outcome);
            Assert.Equal(outBeginningIndex, possiblyBadOutput2.AlgorithmsBeginningIndex);
            Assert.Equal(outNBElement, possiblyBadOutput2.NBElement);

            for (int i = 0; i < goodOutputsLine1.Length; i++)
            {
                Assert.Equal(goodOutputsLine1[i], possiblyBadOutput2.Series.upperBand[i]);
                Assert.Equal(goodOutputsLine2[i], possiblyBadOutput2.Series.middleBand[i]);
                Assert.Equal(goodOutputsLine3[i], possiblyBadOutput2.Series.lowerBand[i]);
            }
        }
コード例 #10
0
        public ITaLibOutput <double[]> MovingAverageWithVariablePeriod(Frequency unitType, double[] periodsPerPrices, int historiesLength, Core.MAType movingAverageType = Core.MAType.Sma, int minPeriod = 2, int maxPeriod = 30)
        {
            var inputs = new IndicatorsInputs(database, historiesLength);

            database.FillMarketHistory(unitType, inputs.OutputArrayLength);

            double[] closingHistory = database.CurrentHistory
                                      .Select(bar => bar.ClosingPrice)
                                      .ToArray();

            var output = new TaLibOutput <double[]> {
                Series           = new double[inputs.OutputArrayLength],
                HistoryWasCustom = database.HistoryIsCustom
            };
            int outBeginningIndex = 0;
            int outNBElement      = 0;

            output.Outcome = Core.MovingAverageVariablePeriod(startIdx: inputs.StartingIndex,
                                                              endIdx: inputs.EndIndex,
                                                              inReal: closingHistory,
                                                              inPeriods: periodsPerPrices,
                                                              optInMinPeriod: minPeriod,
                                                              optInMaxPeriod: maxPeriod,
                                                              optInMAType: movingAverageType,
                                                              outBegIdx: out outBeginningIndex,
                                                              outNBElement: out outNBElement,
                                                              outReal: output.Series);
            output.AlgorithmsBeginningIndex = outBeginningIndex;
            output.NBElement = outNBElement;
            return(output);
        }
コード例 #11
0
        public ITaLibOutput <(double[] upperBand, double[] middleBand, double[] lowerBand)> BollingerBands(Frequency unitType, int historiesLength, Core.MAType movingAverageType = Core.MAType.Sma, int timePeriod = 5, int nbDevUp = 2, int nbDevDown = 2)
        {
            var inputs = new IndicatorsInputs(database, historiesLength);

            database.FillMarketHistory(unitType, inputs.OutputArrayLength);

            double[] closingHistory = database.CurrentHistory
                                      .Select(bar => bar.ClosingPrice)
                                      .ToArray();

            var output = new TaLibOutput <(double[] upperBand, double[] middleBand, double[] lowerBand)> {
                Series = (upperBand : new double[inputs.OutputArrayLength],
                          middleBand : new double[inputs.OutputArrayLength],
                          lowerBand : new double[inputs.OutputArrayLength]),
                HistoryWasCustom = database.HistoryIsCustom
            };
            int outBeginningIndex = 0;
            int outNBElement      = 0;

            output.Outcome = Core.Bbands(startIdx: inputs.StartingIndex,
                                         endIdx: inputs.EndIndex,
                                         inReal: closingHistory,
                                         optInTimePeriod: timePeriod,
                                         optInNbDevUp: nbDevUp,
                                         optInNbDevDn: nbDevDown,
                                         optInMAType: movingAverageType,
                                         outBegIdx: out outBeginningIndex,
                                         outNBElement: out outNBElement,
                                         outRealUpperBand: output.Series.upperBand,
                                         outRealMiddleBand: output.Series.middleBand,
                                         outRealLowerBand: output.Series.lowerBand);
            output.AlgorithmsBeginningIndex = outBeginningIndex;
            output.NBElement = outNBElement;
            return(output);
        }
コード例 #12
0
ファイル: Extension.cs プロジェクト: RomanS2N/trading
        private static Tuple <double[], double[], double[]> RunTA12(this double[] series, TA12 handler, int period, double devUp, double devDn, Core.MAType maType)
        {
            int startIdx = 0;
            int endIdx   = series.Length - 1;

            double[] inReal     = new double[series.Length];
            double[] outRealUp  = new double[series.Length];
            double[] outRealMid = new double[series.Length];
            double[] outRealLo  = new double[series.Length];
            series.CopyTo(inReal, 0);
            int optInTimePeriod = period;
            int outBegIdx;
            int outNbElement;

            handler(startIdx, endIdx, inReal, optInTimePeriod, devUp, devDn, maType, out outBegIdx, out outNbElement, outRealUp, outRealMid, outRealLo);
            return(new Tuple <double[], double[], double[]>(outRealUp, outRealMid, outRealLo));
        }
コード例 #13
0
        public static List <BollingerBand> createBollingerBand(int N, double P, List <Price> Price, Core.MAType MovingAverageType)
        {
            List <BollingerBand> BB = new List <BollingerBand>();


            double[] _close = new double[Price.Count];
            double[] _high  = new double[Price.Count];
            double[] _low   = new double[Price.Count];
            double[] _open  = new double[Price.Count];

            double[] _upperBand = new double[Price.Count];
            double[] _midBand   = new double[Price.Count];
            double[] _lowerBand = new double[Price.Count];

            for (int x = 0; x < Price.Count; x++)
            {
                _close[x] = Price[x].Close;
                _high[x]  = Price[x].High;
                _low[x]   = Price[x].Low;
                _open[x]  = Price[x].Open;
            }

            int a, b;


            Core.Bbands(0, Price.Count - 1, _close, N, P, P, MovingAverageType, out a, out b, _upperBand, _midBand, _lowerBand);


            for (int x = 0; x < _upperBand.Count() - a; x++)
            {
                //Debug.WriteLine("UPPER " + _upperBand[x]);
                //Debug.WriteLine("MID " + _midBand[x]);
                //Debug.WriteLine("LOWER " + _lowerBand[x]);
                //Debug.WriteLine(Price[x + a].TimeStamp + "   " + Price[x + a].Close);
                //Debug.WriteLine("========" + x + "===========");

                BollingerBand bb = new BollingerBand();
                bb.Mid         = _midBand[x];
                bb.Lower       = _lowerBand[x];
                bb.Upper       = _upperBand[x];
                bb.N           = N;
                bb.P           = P;
                bb.TimeStamp   = Price[x + a].TimeStamp;
                bb.Price_Close = Price[x + a].Close;
                bb.Price_High  = Price[x + a].High;
                bb.Price_Low   = Price[x + a].Low;
                bb.Price_Open  = Price[x + a].Open;
                BB.Add(bb);
            }

            return(BB);
        }
コード例 #14
0
        public ITaLibOutput <double[]> PercentagePriceOscillator(Frequency unitType, int historiesLength, Core.MAType MovingAverageType = Core.MAType.Sma, int fastPeriod = 12, int slowPeriod = 26)
        {
            var inputs = new IndicatorsInputs(database, historiesLength);

            database.FillMarketHistory(unitType, inputs.OutputArrayLength);

            double[] closingHistory = database.CurrentHistory
                                      .Select(bar => bar.ClosingPrice)
                                      .ToArray();

            var output = new TaLibOutput <double[]> {
                Series           = new double[inputs.OutputArrayLength],
                HistoryWasCustom = database.HistoryIsCustom
            };
            int outBeginningIndex = 0;
            int outNBElement      = 0;

            output.Outcome = Core.Ppo(startIdx: inputs.StartingIndex,
                                      endIdx: inputs.EndIndex,
                                      inReal: closingHistory,
                                      optInFastPeriod: fastPeriod,
                                      optInSlowPeriod: slowPeriod,
                                      optInMAType: MovingAverageType,
                                      outBegIdx: out outBeginningIndex,
                                      outNBElement: out outNBElement,
                                      outReal: output.Series);
            output.AlgorithmsBeginningIndex = outBeginningIndex;
            output.NBElement = outNBElement;
            return(output);
        }
コード例 #15
0
        public ITaLibOutput <(double[] slowK, double[] slowD)> Stochastic(Frequency unitType, int historiesLength, Core.MAType slowKMaType = Core.MAType.Sma, Core.MAType slowDMaType = Core.MAType.Sma, int fastKPeriod = 5, int slowKPeriod = 3, int slowDPeriod = 3)
        {
            var inputs = new IndicatorsInputs(database, historiesLength);

            database.FillMarketHistory(unitType, inputs.OutputArrayLength);

            double[] closingHistory = database.CurrentHistory
                                      .Select(bar => bar.ClosingPrice)
                                      .ToArray();

            double[] highs = database.CurrentHistory
                             .Select(bar => bar.PriceHigh)
                             .ToArray();

            double[] lows = database.CurrentHistory
                            .Select(bar => bar.PriceLow)
                            .ToArray();

            var output = new TaLibOutput <(double[] slowK, double[] slowD)> {
                Series           = (slowK : new double[inputs.OutputArrayLength], slowD : new double[inputs.OutputArrayLength]),
                HistoryWasCustom = database.HistoryIsCustom
            };
            int outBeginningIndex = 0;
            int outNBElement      = 0;

            output.Outcome = Core.Stoch(startIdx: inputs.StartingIndex,
                                        endIdx: inputs.EndIndex,
                                        inHigh: highs,
                                        inLow: lows,
                                        inClose: closingHistory,
                                        optInFastK_Period: fastKPeriod,
                                        optInSlowK_Period: slowKPeriod,
                                        optInSlowD_Period: slowDPeriod,
                                        optInSlowD_MAType: slowDMaType,
                                        optInSlowK_MAType: slowKMaType,
                                        outBegIdx: out outBeginningIndex,
                                        outNBElement: out outNBElement,
                                        outSlowD: output.Series.slowD,
                                        outSlowK: output.Series.slowK);
            output.AlgorithmsBeginningIndex = outBeginningIndex;
            output.NBElement = outNBElement;
            return(output);
        }
コード例 #16
0
        /// <summary>
        /// Returns the Stochastic Oscillator indicator
        /// </summary>
        /// <param name="high"></param>
        /// <param name="low"></param>
        /// <param name="close"></param>
        /// <param name="fastperiod"></param>
        /// <param name="slowperiod"></param>
        /// <param name="slowDperiod"></param>
        /// <param name="matype"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        public TaLibStochResult Stoch(double[] high, double[] low, double[] close,
                                      int fastperiod, int slowperiod, int slowDperiod, Core.MAType matype, int offset = 1)
        {
            double[] outputK = new double[high.Length];
            double[] outputD = new double[high.Length];

            int begin;
            int length;

            Core.RetCode retCode = Core.Stoch(0, high.Length - 1 - offset, high, low, close,
                                              fastperiod, slowperiod, matype, slowDperiod, matype, out begin, out length, outputK, outputD);

            return(new TaLibStochResult(outputK, outputD, retCode, length));
        }
コード例 #17
0
        public ITaLibOutput <(double[] fastK, double[] fastD)> StochasticRelativeStrengthIndex(Frequency unitType, int historiesLength, Core.MAType fastDMaType = Core.MAType.Sma, int timePeriod = 14, int fastKPeriod = 5, int fastDPeriod = 0)
        {
            var inputs = new IndicatorsInputs(database, historiesLength);

            database.FillMarketHistory(unitType, inputs.OutputArrayLength);

            double[] closingHistory = database.CurrentHistory
                                      .Select(bar => bar.ClosingPrice)
                                      .ToArray();

            var output = new TaLibOutput <(double[] fastK, double[] fastD)> {
                Series           = (fastK : new double[inputs.OutputArrayLength], fastD : new double[inputs.OutputArrayLength]),
                HistoryWasCustom = database.HistoryIsCustom
            };
            int outBeginningIndex = 0;
            int outNBElement      = 0;

            output.Outcome = Core.StochRsi(startIdx: inputs.StartingIndex,
                                           endIdx: inputs.EndIndex,
                                           inReal: closingHistory,
                                           optInTimePeriod: timePeriod,
                                           optInFastK_Period: fastKPeriod,
                                           optInFastD_Period: fastDPeriod,
                                           optInFastD_MAType: fastDMaType,
                                           outBegIdx: out outBeginningIndex,
                                           outNBElement: out outNBElement,
                                           outFastK: output.Series.fastK,
                                           outFastD: output.Series.fastD);
            output.AlgorithmsBeginningIndex = outBeginningIndex;
            output.NBElement = outNBElement;
            return(output);
        }