【发布时间】:2012-07-10 14:46:47
【问题描述】:
我无法确定任何特定时间处于哪个交易时段。
有四个可能的会话,显示在这张来自 ForexFactory.com 的图片中
我有这个方法,我需要检查当前时间是否在指定的交易时段内。
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);
谢谢
【问题讨论】:
-
.NET DateTime 是出了名的难以正确使用,尤其是在不同时区的情况下。如果您能影响是否使用标准 DateTime 的决定,请考虑查看 code.google.com/p/noda-time。
-
如果
currentTime始终处于一致的时区,则不需要UTC;为简单起见,坚持当地时间不会有什么坏处。 -
@Kirk Broadhurst 是的,这是我在评论中提到的 dthorpe 回答的问题
-
@user1267778 根据交易时段是一天还是两天,您需要自定义功能。检查我的更新答案
-
您在下面有很好的答案。但是,如果您想了解更多关于时间编程的基础知识,这可能会有所帮助 - blogs.windwardreports.com/davidt/2009/11/…