Ejemplo n.º 1
0
        public static DataItem[] getDataItemsArray(TimeframeSymbol timeframe, DateTime startDate, DateTime endDate, List<AnalysisType> types)
        {
            DateTime minDate = startDate.CompareTo(endDate) < 0 ? startDate : endDate;
            DateTime d = new DateTime(minDate.Ticks);
            List<DataItem> items = new List<DataItem>();

            while (d.CompareTo(endDate) <= 0)
            {
                var item = generateDataItem(d, types);
                d = d.getNext(timeframe);
                items.Add(item);
            }

            var array = items.ToArray();
            array.AppendIndexNumbers();
            return array;
        }
Ejemplo n.º 2
0
        public static DateTime addTimeUnits(DateTime date, TimeframeSymbol timeframe, int units)
        {
            switch (timeframe)
            {
                case TimeframeSymbol.MN1:
                    return addTimeUnits_month(date, units);
                case TimeframeSymbol.W1:
                    return addTimeUnits_weeks(date.Proper(timeframe), units);
                case TimeframeSymbol.D1:
                    return addTimeUnits_days(date.Proper(timeframe), units);
                case TimeframeSymbol.H4:
                case TimeframeSymbol.H1:
                case TimeframeSymbol.M30:
                case TimeframeSymbol.M15:
                case TimeframeSymbol.M5:
                    return addTimeUnits_shortPeriods(date.Proper(timeframe), timeframe, units);
            }

            return date;
        }
Ejemplo n.º 3
0
 private static int getTimeframeHolidayInactiveUnits(TimeframeSymbol timeframe)
 {
     switch (timeframe)
     {
         case TimeframeSymbol.H4: return 0;
         case TimeframeSymbol.H1: return 2;
         case TimeframeSymbol.M30: return 5;
         case TimeframeSymbol.M15: return 11;
         case TimeframeSymbol.M5: return 35;
         default: return 0;
     }
 }
Ejemplo n.º 4
0
 private static int dayUnitsForTimeframe(TimeframeSymbol timeframe)
 {
     switch (timeframe)
     {
         case TimeframeSymbol.D1: return 1;
         case TimeframeSymbol.H4: return 6;
         case TimeframeSymbol.H1: return 24;
         case TimeframeSymbol.M30: return 48;
         case TimeframeSymbol.M15: return 96;
         case TimeframeSymbol.M5: return 288;
         default: return 0;
     }
 }
Ejemplo n.º 5
0
 private static int countWeekendItems(DateTime startDate, DateTime endDate, TimeframeSymbol timeframe)
 {
     DateTime realStartDate = startDate.Proper(TimeframeSymbol.W1);
     DateTime realEndDate = endDate.Proper(TimeframeSymbol.W1);
     int weeks = countTimeUnits_weeks(realStartDate, realEndDate);
     return weeks * dayUnitsForTimeframe(timeframe) * 2;
 }
Ejemplo n.º 6
0
        private static int countTimeUnits_shortPeriod(DateTime baseDate, DateTime comparedDate, TimeframeSymbol timeframe)
        {
            DateTime properBaseDate = baseDate.Proper(timeframe);
            DateTime properComparedDate = comparedDate.Proper(timeframe);
            TimeSpan span = getTimespan(timeframe);
            int spanMinutes = span.Hours * 60 + span.Minutes;

            long datesMinutesDifference = (properComparedDate - properBaseDate).Ticks / 600000000;
            int result = (int) datesMinutesDifference / spanMinutes;
            int excluded = countExcludedItems(baseDate, comparedDate, timeframe);
            return result - countExcludedItems(baseDate, comparedDate, timeframe);
        }
Ejemplo n.º 7
0
        private static int countExcludedItems(DateTime startDate, DateTime endDate, TimeframeSymbol timeframe)
        {
            int weekends = 0;
            int christmas = 0;
            int newYears = 0;
            int sign = (startDate.CompareTo(endDate) < 0 ? 1 : -1);

            weekends = countWeekendItems(startDate, endDate, timeframe);
            newYears = sign * startDate.countNewYearBreaks(endDate, false) * (dayUnitsForTimeframe(timeframe) + getTimeframeHolidayInactiveUnits(timeframe));
            christmas = sign * startDate.countChristmas(endDate, false) * (dayUnitsForTimeframe(timeframe) + getTimeframeHolidayInactiveUnits(timeframe));

            return (weekends + christmas + newYears);
        }
Ejemplo n.º 8
0
        private static DateTime addTimeUnits_shortPeriods(DateTime date, TimeframeSymbol timeframe, int units)
        {
            DateTime startDate = new DateTime(date.Ticks).Proper(timeframe);
            TimeSpan span = (Math.Sign(units) == 1 ? getTimespan(timeframe) : getTimespan(timeframe).invert());
            int sign = Math.Sign(units);

            for(var i = 1; i <= Math.Abs(units); i++){
                startDate = startDate.Add(span);

                if (!startDate.isOpenMarketTime())
                {
                    DateTime nextOpenMarketTime = startDate.ifNotOpenMarketGetNext();
                    DateTime proper = startDate.Proper(timeframe);
                    startDate = (sign > 0 ? startDate.ifNotOpenMarketGetNext() : startDate.Proper(timeframe));
                }
            }

            return startDate;
        }
Ejemplo n.º 9
0
 public static DateTime getNext(this DateTime date, TimeframeSymbol timeframe)
 {
     DateTime d = date.Proper(timeframe);
     switch (timeframe)
     {
         case TimeframeSymbol.MN1: return new DateTime(d.Year, d.Month + 1, 1, 0, 0, 0);
         case TimeframeSymbol.W1: return d.AddDays(7);
         case TimeframeSymbol.D1:
             return d.AddDays(1).ifNotOpenMarketGetNext();
         default:
             return d.Add(Timeframe.getTimespan(timeframe)).ifNotOpenMarketGetNext();
     }
 }
Ejemplo n.º 10
0
 public static TimeSpan getTimespan(TimeframeSymbol timeframe)
 {
     return getTimespan(timeframe, 1);
 }
Ejemplo n.º 11
0
        public static Timeframe GetTimeframe(TimeframeSymbol symbol)
        {
            if (timeframes == null) LoadTimeframes();

            Timeframe timeframe = null;
            timeframes.TryGetValue(symbol, out timeframe);

            return timeframe;
        }
Ejemplo n.º 12
0
 public static DateTime getNewYearProperDate(DateTime date, TimeframeSymbol timeframe)
 {
     DateTime d = date.getNewYearExactDate();
     int units = getTimeframeHolidayInactiveUnits(timeframe);
     TimeSpan span = getTimespan(timeframe, (-1) * units - 1);
     return d.Add(span);
 }
Ejemplo n.º 13
0
 public static int countTimeUnits(this DateTime date, DateTime compared, TimeframeSymbol timeframe)
 {
     return Timeframe.countTimeUnits(date, compared, timeframe);
 }
Ejemplo n.º 14
0
        private static DateTime ProperShortTime(this DateTime d, TimeframeSymbol timeframe)
        {
            //To full time period.
            TimeSpan span = Timeframe.getTimespan(timeframe, 1);

            //include weekends
            if (d.isWeekend())
            {
                var dt = (d.DayOfWeek == DayOfWeek.Sunday ? d.AddDays(-1) : d.AddDays(0));
                var saturdayMidnight = new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0);
                return saturdayMidnight.Add(span.invert()).ProperShortTime(timeframe);

            }

            //include christmas & new year
            if (d.IsChristmas())
            {
                return Timeframe.getChristmasProperDate(d, timeframe).ProperShortTime(timeframe);
            }

            if (d.IsNewYear())
            {
                return Timeframe.getNewYearProperDate(d, timeframe).ProperShortTime(timeframe);
            }

            //To full time format.
            var hours = -1 * (span.Hours == 0 && span.Minutes == 0 ? d.Hour : (span.Minutes == 0 ? d.Hour % span.Hours : 0));
            var minutes = -1 * (span.Minutes == 0 ? d.Minute : d.Minute % span.Minutes);
            var seconds = -1 * (span.Seconds == 0 ? d.Second : d.Second % span.Seconds);
            TimeSpan toAdd = new TimeSpan(hours, minutes, seconds);

            return d.Add(toAdd);
        }
Ejemplo n.º 15
0
 public static DateTime Proper(this DateTime d, TimeframeSymbol timeframe)
 {
     switch (timeframe)
     {
         case TimeframeSymbol.MN1: return d.ProperMonth();
         case TimeframeSymbol.W1:  return d.ProperWeek();
         case TimeframeSymbol.D1:  return d.ProperDay();
         default: return d.ProperShortTime(timeframe);
     }
 }
Ejemplo n.º 16
0
        public static TimeSpan getTimespan(TimeframeSymbol timeframe, int units)
        {
            switch (timeframe)
            {
                case TimeframeSymbol.H4: return new TimeSpan(units * 4, 0, 0);
                case TimeframeSymbol.H1: return new TimeSpan(units * 1, 0, 0);
                case TimeframeSymbol.M30: return new TimeSpan(0, units * 30, 0);
                case TimeframeSymbol.M15: return new TimeSpan(0, units * 15, 0);
                case TimeframeSymbol.M5: return new TimeSpan(0, units * 5, 0);
            }

            return new TimeSpan(0);
        }
Ejemplo n.º 17
0
        public static int countTimeUnits(DateTime baseDate, DateTime comparedDate, TimeframeSymbol timeframe)
        {
            DateTime properBaseDate = baseDate.Proper(timeframe);
            DateTime properComparedDate = comparedDate.Proper(timeframe);
            switch (timeframe)
            {
                case TimeframeSymbol.MN1:
                    return countTimeUnits_month(properBaseDate, properComparedDate);
                case TimeframeSymbol.W1:
                    return countTimeUnits_weeks(properBaseDate, properComparedDate);
                case TimeframeSymbol.D1:
                    return countTimeUnits_days(properBaseDate, properComparedDate);
                case TimeframeSymbol.H4:
                case TimeframeSymbol.H1:
                case TimeframeSymbol.M30:
                case TimeframeSymbol.M15:
                case TimeframeSymbol.M5:
                    return countTimeUnits_shortPeriod(properBaseDate, properComparedDate, timeframe);
            }

            return 0;
        }
Ejemplo n.º 18
0
 public static DateTime addTimeUnits(this DateTime date, TimeframeSymbol timeframe, int units)
 {
     if (units == 0) return date.AddMilliseconds(0);
     return Timeframe.addTimeUnits(date, timeframe, units);
 }