public IBHistoricalDataRequest( IBAdapter adapter, IHistoryDataConsumer consumer, Contract contract, DateTime begin, DateTime end, HistoryProviderSpan span, string whatToShow, int useRth, int formatDate) { this.adapter = adapter; Consumer = consumer; this.contract = contract; this.begin = begin; this.end = end; this.span = span; this.whatToShow = whatToShow; this.useRth = useRth; this.formatDate = formatDate; TimeSpan?minDuration, maxDuration; IBHistoryDataLimits.GetHistoryDataLimits(span, out minDuration, out maxDuration); var durationTimespan = end - begin; switch (span) { case HistoryProviderSpan.Minute: case HistoryProviderSpan.Minute5: case HistoryProviderSpan.Minute10: case HistoryProviderSpan.Minute15: case HistoryProviderSpan.Minute30: if (durationTimespan >= maxDuration || durationTimespan > IBHistoryDataLimits.MaxBarsPerRequest) { duration = (int)Math.Ceiling(durationTimespan.TotalDays); durationSuffix = " D"; } else { duration = (int)Math.Ceiling(durationTimespan.TotalSeconds); durationSuffix = " S"; } break; case HistoryProviderSpan.Hour: case HistoryProviderSpan.Hour4: case HistoryProviderSpan.Day: case HistoryProviderSpan.Week: case HistoryProviderSpan.Month: duration = (int)Math.Ceiling(durationTimespan.TotalDays); durationSuffix = " D"; break; default: throw new ArgumentOutOfRangeException(nameof(span), span, null); } }
private static void ApplyHistoryDataLimits(HistoryProviderSpan span, ref DateTime begin, ref DateTime end) { // SEE https://www.interactivebrokers.com/en/software/api/apiguide/tables/historical_data_limitations.htm // В IB есть ограничения по историческим данным. Если их превысить, то данные не придут. // Применяем эти ограничения, чтобы не было ошибок TimeSpan?min, max; IBHistoryDataLimits.GetHistoryDataLimits(span, out min, out max); ClampHistoryDataDuration(ref begin, ref end, min, max); }
/// <summary> /// Определить оптимальную длину блока для запроса исторических данных /// </summary> /// <param name="instrument"> /// Инструмент /// </param> /// <param name="span"> /// Интервал свечей для исторических данных /// </param> /// <returns> /// Оптимальная длина блока либо null /// </returns> public TimeSpan?GetBestFetchBlockLength(Instrument instrument, HistoryProviderSpan span) { var size = IBHistoryDataLimits.GetFetchBlockSize(span); return(size); }