Ejemplo n.º 1
0
        public Candle(decimal open, decimal close, decimal max, decimal min)
        {
            if (open < close)
            {
                Type = CandleTypeEnum.Increase;
            }
            else
            {
                Type = CandleTypeEnum.Decrease;
            }

            OpenPrice  = open;
            ClosePrice = close;
            MaxPrice   = max;
            MinPrice   = min;
        }
        public async Task <ICollection <MovingAverageDataPoint> > Ema(ExchangeEnum exchange, SymbolCodeEnum symbolCode, IntervalKey intervalKey, Epoch from, int dataPoints, CandleTypeEnum candleType, int period)
        {
            var periodOffset = period - 1;

            var fromOffset = IntervalFactory.GetInterval(intervalKey, from, periodOffset * -1);

            var aggValues = await MarketRepository.GetTradeAggregates(exchange, symbolCode, intervalKey, fromOffset.From, periodOffset + dataPoints);

            var values = aggValues.GetValues(candleType);

            int outBegIdx, outNbElement;

            var smaValues = new double[dataPoints];

            var retCode = TicTacTec.TA.Library.Core.Ema(0, values.Length - 1, values, period, out outBegIdx, out outNbElement, smaValues);

            var validSmaValues = smaValues.Skip(outNbElement - dataPoints).Take(dataPoints).ToArray();

            var validAggValues = aggValues.Skip(aggValues.Count - dataPoints).Take(dataPoints).ToArray();

            if (retCode == RetCode.Success)
            {
                var dp = new List <MovingAverageDataPoint>();

                for (var i = 0; i < validAggValues.Length; i++)
                {
                    var agg = validAggValues[i];

                    dp.Add(new MovingAverageDataPoint
                    {
                        Epoch = agg.Epoch,
                        Value = validSmaValues[i]
                    });
                }

                return(dp);
            }

            throw new Exception("Unable to calculate EMA - " + retCode);
        }
        public async Task <ICollection <BollingerBandsDataPoint> > BollingerBands(ExchangeEnum exchange, SymbolCodeEnum symbolCode, IntervalKey intervalKey, Epoch from, int dataPoints, CandleTypeEnum candleType, int period, MovingAverageTypeEnum maType, double stdDevUp, double stdDevDown)
        {
            var maTypeConverted = maType.ToTaLib();

            var periodOffset = period - 1;

            var fromOffset = IntervalFactory.GetInterval(intervalKey, from, periodOffset * -1);

            var aggValues = await MarketRepository.GetTradeAggregates(exchange, symbolCode, intervalKey, fromOffset.From, periodOffset + dataPoints);

            var values = aggValues.GetValues(candleType);

            int outBegIdx, outNbElement;

            var upperBandValues  = new double[dataPoints];
            var middleBandValues = new double[dataPoints];
            var lowerBandValues  = new double[dataPoints];

            var retCode = Bbands(0, values.Length - 1, values, period, stdDevUp, stdDevDown, maTypeConverted, out outBegIdx, out outNbElement, upperBandValues, middleBandValues, lowerBandValues);

            var validUpperBandValues  = upperBandValues.Skip(outNbElement - dataPoints).Take(dataPoints).ToArray();
            var validMiddleBandValues = middleBandValues.Skip(outNbElement - dataPoints).Take(dataPoints).ToArray();
            var validLowerBandValues  = lowerBandValues.Skip(outNbElement - dataPoints).Take(dataPoints).ToArray();

            var validAggValues = aggValues.Skip(aggValues.Count - dataPoints).Take(dataPoints).ToArray();

            if (retCode == RetCode.Success)
            {
                var dp = new List <BollingerBandsDataPoint>();

                for (var i = 0; i < validAggValues.Length; i++)
                {
                    var agg = validAggValues[i];

                    dp.Add(new BollingerBandsDataPoint
                    {
                        Epoch  = agg.Epoch,
                        Upper  = upperBandValues[i],
                        Middle = middleBandValues[i],
                        Lower  = lowerBandValues[i]
                    });
                }

                return(dp);
            }

            throw new Exception("Unable to calculate Bollinger Bands - " + retCode);
        }
        public async Task <ICollection <MacdDataPoint> > Macd(ExchangeEnum exchange, SymbolCodeEnum symbolCode, IntervalKey intervalKey, Epoch from, int dataPoints, CandleTypeEnum candleType, int fastEmaPeriod, int slowEmaPeriod, int signalPeriod)
        {
            var periodOffset = (slowEmaPeriod + signalPeriod) - 2;

            var fromOffset = IntervalFactory.GetInterval(intervalKey, from, periodOffset * -1);

            var aggValues = await MarketRepository.GetTradeAggregates(exchange, symbolCode, intervalKey, fromOffset.From, periodOffset + dataPoints);

            var values = aggValues.GetValues(candleType);

            int outBegIdx, outNbElement;

            var macdValues      = new double[dataPoints];
            var signalValues    = new double[dataPoints];
            var histogramValues = new double[dataPoints];

            var retCode = TicTacTec.TA.Library.Core.Macd(0, values.Length - 1, values, fastEmaPeriod, slowEmaPeriod, signalPeriod, out outBegIdx, out outNbElement, macdValues, signalValues, histogramValues);

            var validMacdValues      = macdValues.Skip(outNbElement - dataPoints).Take(dataPoints).ToArray();
            var validSignalValues    = signalValues.Skip(outNbElement - dataPoints).Take(dataPoints).ToArray();
            var validHistogramValues = histogramValues.Skip(outNbElement - dataPoints).Take(dataPoints).ToArray();
            var validAggValues       = aggValues.Skip(aggValues.Count - dataPoints).Take(dataPoints).ToArray();

            if (retCode == RetCode.Success)
            {
                var dp = new List <MacdDataPoint>();

                for (var i = 0; i < validAggValues.Length; i++)
                {
                    var agg = validAggValues[i];

                    dp.Add(new MacdDataPoint
                    {
                        Epoch     = agg.Epoch,
                        Macd      = validMacdValues[i],
                        Signal    = validSignalValues[i],
                        Histogram = validHistogramValues[i]
                    });
                }

                return(dp);
            }

            throw new Exception("Unable to calculate MACD - " + retCode);
        }
        public static double[] GetValues(this ICollection <MarketAggregate> aggregates, CandleTypeEnum candleType)
        {
            switch (candleType)
            {
            case CandleTypeEnum.Open:
                return(aggregates.Select(a => (double)a.Open).ToArray());

            case CandleTypeEnum.High:
                return(aggregates.Select(a => (double)a.High).ToArray());

            case CandleTypeEnum.Low:
                return(aggregates.Select(a => (double)a.Low).ToArray());

            case CandleTypeEnum.Close:
                return(aggregates.Select(a => (double)a.Close).ToArray());

            default:
                throw new ArgumentException($"Unhandled candle type: {candleType}", "candleType");
            }
        }