Example #1
0
 private bool IsANewRange(ArrayItem currentMin, ArrayItem currentMax, ArrayItem previousMin)
 {
     return(currentMin != new ArrayItem() &
            currentMax != new ArrayItem() &
            currentMin.location >= previousMin.location &
            currentMax.Value != 0 &
            currentMax.Value != currentMin.Value);
 }
Example #2
0
        public List <Range> GetRangesOfAscendingNumbers(int[] stockPriceArrayOrderedByDateTime)
        {
            var ranges = new List <Range>();

            var previousMin = new ArrayItem();
            var previousMax = new ArrayItem();

            var currentMin = new ArrayItem();
            var currentMax = new ArrayItem();

            int minMarkerLocation = 0;
            int maxMarkerLocation = 0;

            int locationInArray = 0;

            foreach (var price in stockPriceArrayOrderedByDateTime)
            {
                if (currentMin.Value == 0 || price < previousMin.Value)
                {
                    currentMin.location = locationInArray;
                    currentMin.Value    = price;

                    //start over
                    minMarkerLocation = locationInArray;
                    maxMarkerLocation = locationInArray;
                }

                if (price > currentMax.Value || currentMax.Value > previousMax.Value || previousMax.Value == 0)
                {
                    currentMax.location = locationInArray;
                    currentMax.Value    = price;

                    maxMarkerLocation = locationInArray;
                }

                if (locationInArray < stockPriceArrayOrderedByDateTime.Length)
                {
                    if (this.IsAMaxValue(currentMin, currentMax,
                                         minMarkerLocation, maxMarkerLocation,
                                         price, stockPriceArrayOrderedByDateTime, locationInArray))
                    {
                        if (this.IsANewRange(currentMin, currentMax, previousMin))
                        {
                            currentMin = this.CreateAndAddRange(currentMin, currentMax, ranges, ref minMarkerLocation);
                        }
                    }
                }

                currentMax = new ArrayItem();

                previousMin = currentMin;
                previousMax = currentMax;

                locationInArray += 1;
            }

            return(ranges);
        }
Example #3
0
        private bool IsAMaxValue(ArrayItem currentMin, ArrayItem currentMax, int minMarkerLocation, int maxMarkerLocation, int price, int [] stockPriceArrayOrderedByDateTime, int locationInArray)
        {
            if (locationInArray + 1 == stockPriceArrayOrderedByDateTime.Length &&
                currentMax.Value >= currentMin.Value)
            {
                return(true); //because we are at the end.

                //todo: there might be a corner case here that needs to be addressed.
            }
            else
            {
                int nextPrice = stockPriceArrayOrderedByDateTime[locationInArray + 1];

                return(maxMarkerLocation > minMarkerLocation &&
                       nextPrice < price &&
                       currentMax.Value >= currentMin.Value);
            }
        }
Example #4
0
        private ArrayItem CreateAndAddRange(ArrayItem currentMin,
                                            ArrayItem currentMax,
                                            ICollection <Range> ranges,
                                            ref int minMarkerLocation)
        {
            var currentRange = new Range()
            {
                Min = new ArrayItem {
                    location = currentMin.location, Value = currentMin.Value
                },
                Max = new ArrayItem {
                    location = currentMax.location, Value = currentMax.Value
                },
            };

            ranges.Add(currentRange);

            minMarkerLocation = 0;
            currentMin        = new ArrayItem();
            return(currentMin);
        }