Example #1
0
        public async Task <SupportAndResistanceViewModel> GetSupportAndResistance(string symbol, string timeframe = null)
        {
            SupportAndResistance supportAndResistance = await _signalsProxyService.GetSupportAndResistance(symbol, timeframe);

            SupportAndResistanceViewModel result = Mapper.Map <SupportAndResistance, SupportAndResistanceViewModel>(supportAndResistance);

            return(result);
        }
        public object GetSyrahSentiments(string stockCode)
        {
            List <HistoricalQuote> historicalQuotes2YearsResponse = _marketDataService.GetHistoricalQuotes(stockCode, "2y");

            List <Signal> syrahShortTermSignals;
            List <Signal> syrahLongTermSignals;

            int?sentimentShortTermValue = null;
            int?sentimentLongTermValue  = null;

            int?technicalRank = null;
            SupportAndResistance supportAndResistance = null;
            string sentence = string.Empty;

            Tuple <List <Signal>, List <Signal> > signals = _signalHelpers.GetSentiments(historicalQuotes2YearsResponse);

            syrahShortTermSignals = signals.Item1;
            syrahLongTermSignals  = signals.Item2;

            Tuple <int?, int?> sentimentTermValues = _signalHelpers.GetSentimentTermValues(syrahShortTermSignals, syrahLongTermSignals);

            sentimentShortTermValue = sentimentTermValues.Item1;
            sentimentLongTermValue  = sentimentTermValues.Item2;

            DateTime yearAgo = DateTime.UtcNow.Date.AddYears(-1);
            List <HistoricalQuote> historicalQuotes1Year = historicalQuotes2YearsResponse.Where(h => h.TradeDate >= yearAgo).ToList();
            HistoricalData         historicalData1Year   = null;

            if (!historicalQuotes1Year.IsNullOrEmpty())
            {
                historicalData1Year = new HistoricalData(historicalQuotes1Year);
            }

            StockQuoteInfo quote = _marketDataService.GetStockQuote(stockCode);

            technicalRank = _signalHelpers.GetTechnicalRank(historicalData1Year);
            var last = 0d;

            if (quote.LastPrice != null)
            {
                last = (double)quote.LastPrice.Value;
            }
            SupportAndResistanceMath srCalc = new SupportAndResistanceMath(historicalData1Year);

            supportAndResistance = srCalc.GetSupportAndResistance();
            return(new
            {
                quote = quote,
                technicalRank = technicalRank,
                supportAndResistance = supportAndResistance,
                sentimentShortTermValue = sentimentShortTermValue,
                sentimentLongTermValue = sentimentLongTermValue,
                syrahShortTermSignals = syrahShortTermSignals,
                syrahLongTermSignals = syrahLongTermSignals,
                historicalQuotes2Years = historicalQuotes2YearsResponse
            });
        }
        /// <summary>
        /// Gets closest resistance level
        /// </summary>
        public static double GetClosestResistance(this SupportAndResistance data, double price)
        {
            double resistance = data.MajorResistance.Where(d => d > price).OrderBy(d => d - price).First();

            return(resistance);
        }
        /// <summary>
        /// Gets closest support level
        /// </summary>
        public static double GetClosestSupport(this SupportAndResistance data, double price)
        {
            double support = data.MajorSupport.Where(d => d < price).OrderBy(d => price - d).First();

            return(support);
        }
        public SupportAndResistance GetSupportAndResistance(double?currentPriceParam = null, double?percentRangeParam = null)
        {
            double percentRange = currentPriceParam ?? 0.4;
            double currentPrice = percentRangeParam ?? _data.Close.Last();

            Tuple <int, double> highMax = _data.High.MaximumAndIndex();
            Tuple <int, double> lowMin  = _data.Low.MinimumAndIndex();

            double        increment;
            List <double> priceChainResult    = GeneratePriceChain(highMax.Item2, lowMin.Item2, out increment);
            List <double> volumeProfileResult = VolumeAddition(priceChainResult);

            priceChainResult.RemoveAt(priceChainResult.Count - 1);

            List <double> priceChain = priceChainResult.ToList(), volumeProfile = volumeProfileResult.ToList();
            List <SupportAndResistanceValue> srValues = FindGaps(ref volumeProfile, ref priceChain);

            PriceBounds bounds = UpAndDownPrice(highMax.Item2, lowMin.Item2, percentRange, currentPrice);

            List <double> localPrices;
            List <double> localVolume;

            SRpoints(bounds.UpPrice, bounds.DownPrice, priceChain, volumeProfile, increment, SRPointsLocalization.TwoLocal,
                     out localPrices, out localVolume);

            List <double> idenSR = IdenticalVolumeFilter(ref localPrices, ref localVolume);

            double priceRangeStdDev = _data.GetPriceRangeStdDev();
            ////////////////////////////////////
            // SRLocalHighFilter mat lab method
            List <Tuple <int, double> > srPoints = ApplyLocalFilters(localPrices, idenSR, priceRangeStdDev);

            //global max is always resistence, and global min is always support
            if (srPoints.All(i => !i.Item2.Equals(highMax.Item2)))
            {
                srPoints.Add(highMax);
            }
            if (srPoints.All(i => !i.Item2.Equals(lowMin.Item2)))
            {
                srPoints.Add(lowMin);
            }
            ////////////////////////////////////

            CrossOverFilter(ref srPoints);

            ////////////////////////////////////
            // TODO: consider move to FindGaps
            IEnumerable <SupportAndResistanceValue> gaps = PartialGap(priceRangeStdDev);

            srValues.AddRange(gaps);
            ////////////////////////////////////

            srValues.AddRange(srPoints.Select((d, i) =>
            {
                DateTime date = _data.Date[d.Item1];
                SupportAndResistanceValueType type = d.Item2 >= currentPrice
                                        ? SupportAndResistanceValueType.MajorResistance
                                        : SupportAndResistanceValueType.MajorSupport;
                SupportAndResistanceValue srVal = new SupportAndResistanceValue(d.Item2, date, type);
                return(srVal);
            }));

            SupportAndResistance result = new SupportAndResistance(srValues);

            return(result);
        }