[英]Determine if current time between multiple time spans
我无法确定任何特定时间在哪个交易时段。
该图中显示了四个可能的会话,取自ForexFactory.com
我有这种方法需要检查的是currentTime在指定的交易时段内。
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;
}
使用:
bool isSydneySession = IsTradingSession(TradingSession.Sydney, CurrentTime);
bool isTokyoSession = IsTradingSession(TradingSession.Tokyo, CurrentTime);
bool isLondonSession = IsTradingSession(TradingSession.London, CurrentTime);
bool isNewYorkSession = IsTradingSession(TradingSession.NewYork, CurrentTime);
谢谢
我建议为每个交易时段编写一个简单的函数,该函数需要一个DateTime并返回一个布尔值,以指示该时间是否打开。
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
然后将它们放入像这样的Dictionary<TradingSession, Func>
以进行通用检索...
var marketHours = new Dictionary<TradingSession, Func<DateTime, bool>>();
marketHours.Add(TradingSession.Sydney, isOpenInSydney);
// add other markets...
然后,您现有的方法只需为给定的TradingSession
选择适当的函数并将其应用
public bool IsTradingSession(TradingSession tradingSession, DateTime currentTime)
{
var functionForSession = marketHours[tradingSession];
return functionForSession(currentTime);
}
只要您的应用程序仅在单个时区运行 ,我认为您不需要在这里使用UTC时间,但是夏令时可能会导致问题。
解决涵盖两天而不是一天的交易时段的一种好方法是编写一个帮助程序,该助手精确地考虑这是否是“跨日”交易时段并为您应用不同的规则:
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));
}
然后
var sydneyOpen = new TimeSpan(17, 0, 0);
var sydneyClose = new TimeSpan(2, 0, 0);
Func<DateTime, bool> isOpenInSydney = d => IsBetween(d, sydneyOpen, sydneyClose);
您可以使用>&<进行比较,也可以比较刻度线。
请参阅相关问题: 检查datetime实例是否介于其他两个datetime对象之间
为了避免使用多个if语句,您还可以创建一个包含开始时间和结束时间的TradingSession对象,并定义一个属性/函数以检查是否在会话中。 当我有很大的选择或障碍时,通常表示错过了OO机会:)
TradingSession sydneySession = new TradingSession
{
StartTimeUtc = ...;
EndTimeUtc = ...;
}
然后,交易会话对象可以具有属性IsInSession。
public bool IsInSession
{
get {
return DateTime.UTCNow >= StartTimeUtc && DateTime.UTCNow <= EndTimeUtc;
}
}
这使用UTC时间来消除时区问题。
您需要将当地时间标准化为UTC。 然后,您可以比较各个地区的时间。
对于每个交易时段,您需要以UTC知道交易时段的开始和结束时间。
您需要使用UTC的当前时间。 例如, DateTime.UtcNow
。
然后,您可以为每个交易时段窗口执行范围比较:
if(tradingSession == TradingSession.Sydney)
return currentTimeUtc >= sydneyStartTimeUtc && currentTimeUtc <= sydneyEndTimeUtc;
等等...
如果您要尝试验证某个交易时间对于特定交易时段的交易有效,则可以正常使用。
但是,如果您尝试根据时间确定某笔交易所属的交易时段,则有时会遇到多个答案,因为交易时段重叠。 在给定时间内,多个交易时段可能有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.