Determine if the current time exists between several time intervals.

I am having trouble figuring out which trading session has a specific time.

There are four possible sessions shown in this image taken with ForexFactory.com

enter image description here

I have this method, which I need to check, is the current time during the specified trading session.

public bool IsTradingSession(TradingSession tradingSession, DateTime currentTime) { //currentTime is in local time. //Regular session is 5PM - next day 5PM, this is the session in the picture. //Irregular sessions also occur for example late open (3AM - same day 5PM) or early close (5PM - next day 11AM) DateTime sessionStart = Exchange.ToLocalTime(Exchange.CurrentSessionOpen); DateTime sessionEnd = Exchange.ToLocalTime(Exchange.CurrentSessionClose); if(tradingSession == TradingSession.Sydney) return ....... ? true : false; if(tradingSession == TradingSession.Tokyo) return ....... ? true : false; if(tradingSession == TradingSession.London) return ....... ? true : false; if (tradingSession == TradingSession.NewYork) return ....... ? true : false; return false; } 

Using:

  bool isSydneySession = IsTradingSession(TradingSession.Sydney, CurrentTime); bool isTokyoSession = IsTradingSession(TradingSession.Tokyo, CurrentTime); bool isLondonSession = IsTradingSession(TradingSession.London, CurrentTime); bool isNewYorkSession = IsTradingSession(TradingSession.NewYork, CurrentTime); 

thanks

+4
source share
3 answers

I would suggest writing a simple function for each trading session that takes a DateTime and returns a bool indicating whether it will open at that time.

 var sydneyOpen = new TimeSpan(17, 0, 0); var sydneyClose = new TimeSpan(2, 0, 0); Func<DateTime, bool> isOpenInSydney = d => (d.TimeOfDay > sydneyOpen || d.TimeOfDay < sydneyClose); // same for other markets, write a function to check against two times 

Then put them in Dictionary<TradingSession, Func> , as it is for general search ...

 var marketHours = new Dictionary<TradingSession, Func<DateTime, bool>>(); marketHours.Add(TradingSession.Sydney, isOpenInSydney); // add other markets... 

And then your existing method simply selects the appropriate function for the given TradingSession and applies it

 public bool IsTradingSession(TradingSession tradingSession, DateTime currentTime) { var functionForSession = marketHours[tradingSession]; return functionForSession(currentTime); } 

I don’t think you need UTC time here, as long as your application only works in one time zone, but summer savings can cause problems.


A good way to take into account the problem of trading sessions that span two days, and not just one day, is to write an assistant who accurately considers whether it is an β€œintraday” trading session and applies a different rule for you:

 private bool IsBetween(DateTime now, TimeSpan open, TimeSpan close) { var nowTime = now.TimeOfDay; return (open < close // if open is before close, then now must be between them ? (nowTime > open && nowTime < close) // otherwise now must be *either* after open or before close : (nowTime > open || nowTime < close)); } 

and then

 var sydneyOpen = new TimeSpan(17, 0, 0); var sydneyClose = new TimeSpan(2, 0, 0); Func<DateTime, bool> isOpenInSydney = d => IsBetween(d, sydneyOpen, sydneyClose); 
+3
source

You can compare s> and <or compare ticks.

See related questions: Check if a datetime instance falls between two other datetime objects

To avoid multiple if statements, you can also create a TradingSession object with a start and end time and define a property / function to check if there is a session. When I have a large switch or blocks, it usually indicates a missed OO opportunity :)

 TradingSession sydneySession = new TradingSession { StartTimeUtc = ...; EndTimeUtc = ...; } 

Then the trading session object may have the IsInSession property.

 public bool IsInSession { get { return DateTime.UTCNow >= StartTimeUtc && DateTime.UTCNow <= EndTimeUtc; } } 

This uses UTC time to troubleshoot time zone issues.

+2
source

You need to normalize your local time in UTC. Then you can compare times by region.

For each trading session, you need to know the start and end time of the session in UTC.

You need the current time in UTC. DateTime.UtcNow , for example.

Then you can perform a range comparison for each window of the trading session:

 if(tradingSession == TradingSession.Sydney) return currentTimeUtc >= sydneyStartTimeUtc && currentTimeUtc <= sydneyEndTimeUtc; 

etc...

If you try to verify that the transaction time is valid for a transaction in a certain trading session, this will work fine.

If, however, you are trying to figure out which trading session a trade refers to based on its time, sometimes you will have several answers, because trading sessions overlap. Several trading sessions can be valid for a certain time.

0
source

All Articles