Ejemplo n.º 1
0
        public static void FindGaps(this HistoricalData data, out List <int> gapUp, out List <int> gapDown)
        {
            double stdDev = data.GetPriceRangeStdDev();

            gapUp   = new List <int>();
            gapDown = new List <int>();

            for (int j = 1; j < data.Count; j++)
            {
                if (data.Low[j] - data.High[j - 1] >= stdDev)
                {
                    gapUp.Add(j);
                }

                if (data.Low[j - 1] - data.High[j] >= stdDev)
                {
                    gapDown.Add(j);
                }
            }
        }
        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);
        }