Exemple #1
0
        // 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);
        }
Exemple #2
0
        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);
        }
Exemple #3
0
 // Need different IP for different platforms, and different GwClientID for different developers (if they happen to code and connect to IbGateways at the same time)
 public static (string HostIp, GatewayClientID GwClientID) GetHostIpAndGatewayClientID(GatewayId p_gatewayId)
 {
     switch (Utils.RunningPlatform())
     {
     case Platform.Linux:
         return(p_gatewayId switch
         {
             GatewayId.CharmatMain => (ServerIp.LocalhostLoopbackWithIP, GatewayClientID.SqCoreToDcProd),
             GatewayId.DeBlanzacMain => (ServerIp.LocalhostLoopbackWithIP, GatewayClientID.SqCoreToDbProd),
             GatewayId.GyantalMain => (ServerIp.AtsVirtualBrokerServerPublicIpForClients, GatewayClientID.SqCoreToGaProd),
             _ => throw new NotImplementedException()
         });