public static string TimespanToDurationString(TimeSpan t, BarSize minFreq)
        {
            //   duration:
            //     This is the time span the request will cover, and is specified using the
            //     format: , i.e., 1 D, where valid units are: S (seconds) D (days) W (weeks)
            //     M (months) Y (years) If no unit is specified, seconds are used. "years" is
            //     currently limited to one.
            if (minFreq > BarSize.OneMonth)
            {
                return(Math.Ceiling(Math.Max(1, t.TotalDays / 365)).ToString("0") + " Y");
            }
            if (minFreq >= BarSize.OneMonth)
            {
                return(Math.Ceiling(Math.Max(1, t.TotalDays / 29)).ToString("0") + " M");
            }
            if (minFreq >= BarSize.OneWeek)
            {
                return(Math.Ceiling(Math.Max(1, t.TotalDays / 7)).ToString("0") + " W");
            }
            if (minFreq >= BarSize.OneDay || t.TotalSeconds > 86400)
            {
                if (t.TotalDays > 14)
                {
                    //This is a ridiculous hack made necessary by the incredibly bad TWS API
                    //For longer periods, if we specify the period as a # of days, the request is rejected!
                    //so instead we do it as the number of weeks and everything is A-OK
                    return(Math.Ceiling(t.TotalDays / 7).ToString("0") + " W");
                }
                return(Math.Ceiling(Math.Max(1, t.TotalDays)).ToString("0") + " D");
            }

            return(Math.Abs(Math.Ceiling(t.TotalSeconds)).ToString("0") + " S");
        }
 /// <summary>
 ///     Returns the maximum period length of a historical data request, in seconds, depending on the data frequency.
 /// </summary>
 /// <param name="frequency"></param>
 /// <returns>Maximum allowed length in </returns>
 public static int MaxRequestLength(BarSize frequency)
 {
     //The limitations are laid out here: https://www.interactivebrokers.com/en/software/api/apiguide/tables/historical_data_limitations.htm
     if (frequency <= BarSize.OneSecond)
     {
         return(1800);
     }
     if (frequency <= BarSize.FiveSeconds)
     {
         return(7200);
     }
     if (frequency <= BarSize.FifteenSeconds)
     {
         return(14400);
     }
     if (frequency <= BarSize.ThirtySeconds)
     {
         return(24 * 3600);
     }
     if (frequency <= BarSize.OneMinute)
     {
         return(2 * 24 * 3600);
     }
     if (frequency <= BarSize.ThirtyMinutes)
     {
         return(7 * 24 * 3600);
     }
     if (frequency <= BarSize.OneHour)
     {
         return(29 * 24 * 3600);
     }
     return(365 * 24 * 3600);
 }
        public static Krs.Ats.IBNet.BarSize BarSizeConverter(BarSize freq)
        {
            switch (freq)
            {
            case BarSize.Tick:
                throw new Exception("Bar size conversion impossible, TWS does not suppor tick BarSize");

            case BarSize.OneSecond:
                return(Krs.Ats.IBNet.BarSize.OneSecond);

            case BarSize.FiveSeconds:
                return(Krs.Ats.IBNet.BarSize.FiveSeconds);

            case BarSize.FifteenSeconds:
                return(Krs.Ats.IBNet.BarSize.FifteenSeconds);

            case BarSize.ThirtySeconds:
                return(Krs.Ats.IBNet.BarSize.ThirtySeconds);

            case BarSize.OneMinute:
                return(Krs.Ats.IBNet.BarSize.OneMinute);

            case BarSize.TwoMinutes:
                return(Krs.Ats.IBNet.BarSize.TwoMinutes);

            case BarSize.FiveMinutes:
                return(Krs.Ats.IBNet.BarSize.FiveMinutes);

            case BarSize.FifteenMinutes:
                return(Krs.Ats.IBNet.BarSize.FifteenMinutes);

            case BarSize.ThirtyMinutes:
                return(Krs.Ats.IBNet.BarSize.ThirtyMinutes);

            case BarSize.OneHour:
                return(Krs.Ats.IBNet.BarSize.OneHour);

            case BarSize.OneDay:
                return(Krs.Ats.IBNet.BarSize.OneDay);

            case BarSize.OneWeek:
                return(Krs.Ats.IBNet.BarSize.OneWeek);

            case BarSize.OneMonth:
                return(Krs.Ats.IBNet.BarSize.OneMonth);

            case BarSize.OneQuarter:
                throw new Exception("Bar size conversion impossible, TWS does not suppor quarter BarSize.");

            case BarSize.OneYear:
                return(Krs.Ats.IBNet.BarSize.OneYear);

            default:
                return(Krs.Ats.IBNet.BarSize.OneDay);
            }
        }
Beispiel #4
0
        /// <summary>
        ///     Sets the appropriate closing time for each bar, since IB only gives us the opening time.
        /// </summary>
        private static void GenerateIntradayBarClosingTimes(List <OHLCBar> bars, BarSize frequency)
        {
            var freqTs = frequency.ToTimeSpan();

            for (var i = 0; i < bars.Count; i++)
            {
                var bar = bars[i];

                if (i == bars.Count - 1)
                {
                    //if it's the last bar we are basically just guessing the
                    //closing time by adding the duration of the frequency
                    if (bar.DateTimeOpen != null)
                    {
                        bar.DateTimeClose = bar.DateTimeOpen.Value + freqTs;
                    }
                }
                else
                {
                    //if it's not the last bar, we set the closing time to the
                    //earliest of the open of the next bar and the period of the frequency
                    //e.g. if hourly bar opens at 9:30 and the next bar opens at 10:00
                    //we set the close at the earliest of 10:00 and 10:30
                    if (bar.DateTimeOpen != null)
                    {
                        var      openPlusBarSize = bar.DateTimeOpen.Value + freqTs;
                        DateTime?dateTime        = bars[i + 1].DateTimeOpen;

                        if (dateTime != null)
                        {
                            bar.DateTimeClose = dateTime.Value < openPlusBarSize ? dateTime.Value : openPlusBarSize;
                        }
                    }
                }
            }
        }