// there are some weird IB errors that happen usually when IB server is down. 99% of the time it is at the weekend, or when pre or aftermarket. In this exceptional times, ignore errors. public static bool IgnoreErrorsBasedOnMarketTradingTime(int offsetToOpenMin = 0, int offsetToCloseMin = 40) { DateTime timeUtc = DateTime.UtcNow; DateTime timeEt = Utils.ConvertTimeFromUtcToEt(timeUtc); if (timeEt.DayOfWeek == DayOfWeek.Saturday || timeEt.DayOfWeek == DayOfWeek.Sunday) // if it is the weekend => no Error { return(true); } TimeSpan timeTodayEt = timeEt - timeEt.Date; // The NYSE and NYSE MKT are open from Monday through Friday 9:30 a.m. to 4:00 p.m. ET. // "Gateways are not connected" errors handled with more strictness. We expect that there is a connection to IBGateway at least 1 hour before open. At 8:30. if (timeTodayEt.TotalMinutes < 9 * 60 + 29 + offsetToOpenMin) // ignore errors before 9:30. { return(true); // if it is not Approximately around market hours => no Error } if (timeTodayEt.TotalMinutes > 16 * 60 + offsetToCloseMin) // IB: not executed shorting trades are cancelled 30min after market close. Monitor errors only until that. { return(true); // if it is not Approximately around market hours => no Error } // TODO: <not too important> you can skip holiday days too later; and use real trading hours, which sometimes are shortened, before or after holidays. return(false); }
public void DailyMaintenance() { DateTime etNow = Utils.ConvertTimeFromUtcToEt(DateTime.UtcNow); if (etNow.DayOfWeek == DayOfWeek.Sunday) { CheckFreeDiskSpace(null); CleanLogfiles(null); } }
public static bool IsCriticalTradingTime(GatewayId p_gatewayId, DateTime p_timeUtc) { DateTime timeEt = Utils.ConvertTimeFromUtcToEt(p_timeUtc); if (timeEt.DayOfWeek == DayOfWeek.Saturday || timeEt.DayOfWeek == DayOfWeek.Sunday) // quick check: if it is the weekend => not critical time. { return(false); } bool isMarketHoursValid = Utils.DetermineUsaMarketTradingHours(p_timeUtc, out bool isMarketTradingDay, out DateTime marketOpenTimeUtc, out DateTime marketCloseTimeUtc, TimeSpan.FromDays(3)); if (isMarketHoursValid && !isMarketTradingDay) { return(false); } foreach (var critTradingRange in GatewayExtensions.CriticalTradingPeriods) { if (critTradingRange.GatewayId != p_gatewayId) { continue; } if (!isMarketHoursValid) { return(true); // Caution: if DetermineUsaMarketTradingHours() failed, better to report that we are in a critical period. } DateTime critPeriodStartUtc = DateTime.MinValue; // Caution: if (critTradingRange.RelativeTimePeriod.Start.Base == RelativeTimeBase.BaseOnUsaMarketOpen) { critPeriodStartUtc = marketOpenTimeUtc + critTradingRange.RelativeTimePeriod.Start.TimeOffset; } else if (critTradingRange.RelativeTimePeriod.Start.Base == RelativeTimeBase.BaseOnUsaMarketClose) { critPeriodStartUtc = marketCloseTimeUtc + critTradingRange.RelativeTimePeriod.Start.TimeOffset; } DateTime critPeriodEndUtc = DateTime.MaxValue; if (critTradingRange.RelativeTimePeriod.End.Base == RelativeTimeBase.BaseOnUsaMarketOpen) { critPeriodEndUtc = marketOpenTimeUtc + critTradingRange.RelativeTimePeriod.End.TimeOffset; } else if (critTradingRange.RelativeTimePeriod.End.Base == RelativeTimeBase.BaseOnUsaMarketClose) { critPeriodEndUtc = marketCloseTimeUtc + critTradingRange.RelativeTimePeriod.End.TimeOffset; } if (critPeriodStartUtc <= p_timeUtc && p_timeUtc <= critPeriodEndUtc) // if p_timeUtc is between [Start, End] { return(true); } } return(false); }
public static bool IsInRegularUsaTradingHoursNow(TimeSpan p_maxAllowedStaleness) { DateTime utcNow = DateTime.UtcNow; // 1. quick response for trivial case that works most of the time, that don't need DetermineUsaMarketTradingHours() DateTime utcNowET = Utils.ConvertTimeFromUtcToEt(utcNow).Date; if (utcNowET.DayOfWeek == DayOfWeek.Saturday || utcNowET.DayOfWeek == DayOfWeek.Sunday) { return(false); } DateTime openInET = new DateTime(utcNowET.Year, utcNowET.Month, utcNowET.Day, 9, 30, 0); if (utcNowET < openInET) { return(false); } DateTime maxPossibleCloseInET = new DateTime(utcNowET.Year, utcNowET.Month, utcNowET.Day, 16, 0, 0); // usually it is 16:00, but when half-day trading, then it is 13:00 if (utcNowET > openInET) { return(false); } // 2. During RTH on weekdays, we have to be more thorough. bool isMarketTradingDay; DateTime openTimeUtc, closeTimeUtc; bool isTradingHoursOK = Utils.DetermineUsaMarketTradingHours(utcNow, out isMarketTradingDay, out openTimeUtc, out closeTimeUtc, p_maxAllowedStaleness); if (!isTradingHoursOK) { Utils.Logger.Error("DetermineUsaMarketTradingHours() was not OK."); return(false); } else { if (!isMarketTradingDay) { return(false); } if (utcNow < openTimeUtc) { return(false); } if (utcNow > closeTimeUtc) { return(false); } return(true); } }
public static DateTime FromUtcToEt(this DateTime p_dateTimeUtc) { return(Utils.ConvertTimeFromUtcToEt(p_dateTimeUtc)); }
static DateTime g_holidaysDownloadDate = DateTime.MinValue; // the last time we downloaded info from the internet public static TradingHours UsaTradingHoursNow_withoutHolidays() { DateTime etNow = Utils.ConvertTimeFromUtcToEt(DateTime.UtcNow); return(UsaTradingHours_withoutHolidays(etNow)); }