2012-07-11 96 views
4

我无法找出哪些人交易会话任何特定时间在不在。确定是否多时间之间当前的时间跨度

有四种可能的会议,显示在这张照片从ForexFactory.com

采取enter image description here

我有这个方法,我需要检查的是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); 

谢谢

+1

.NET日期时间是非常困难的正确使用,尤其是在不同的时区。如果您可以影响使用标准日期时间的决定,请考虑查看http://code.google.com/p/noda-time/。 – 2012-07-11 03:06:45

+1

如果'currentTime'始终处于一致的时区,那么UTC不是必需的;坚持当地时间为了简单不会伤害。 – 2012-07-11 03:58:06

+0

@Kirk Broadhurst是的,这是我的问题与dthorpe的答案,因为我在我的评论中提到 – parliament 2012-07-11 03:59:52

回答

3

我会建议为每个交易时段编写一个简单的函数,该函数需要一个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); 
+0

使用时间范围对象sydneyStart ,sydneyEnd,tokyoStart ...等是我最初开始的,这个问题是d.TimeOfDay(可以说是8AM)在sydneyClose = new TimeSpan(2,0,0) – parliament 2012-07-11 04:06:56

+0

之前和之后为什么不是?if currentTime =新的DateTime(2012,7,10,8,00,00)then currentTime.TimeOfDay(8AM)是在2012年7月10日之后2AM sydneyClose和之前7/11/2012 2AM sydney关闭,没有? – parliament 2012-07-11 04:17:29

+0

看着你的更新no w – parliament 2012-07-11 04:18:29

2

您可以比较> & <或比较蜱。

查看相关问题:Check if datetime instance falls in between other two datetime objects

为了避免多if语句,你还可以创建一个开始和结束时间的TradingSession对象,定义属性/功能检查是否开庭。当我有很大的开关或者块,通常表明错失机会OO :)

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

交易时段对象可以再有一个属性IsInSession。

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

这使用UTC时间来消除时区问题。

0

您需要将当地时间标准化为UTC。然后您可以比较不同地区的时间。

对于每个交易时段,您需要知道UTC的会话开始和结束时间。

您需要UTC的当前时间。例如,DateTime.UtcNow

然后,您可以执行范围比较每个交易时段窗口:

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

等等

如果你想验证一个交易时间有效期为在特定交易交易时段,这将工作正常。

如果您想要根据交易时间来确定交易所属的交易时段,有时会因为交易时段重叠而有多个答案。多个交易时段可能在给定时间内有效。

+1

我仍然没有围绕这个包裹我的头。请忽略会议蓝色框内的图片时间,这可能会造成一些混淆。方法内部的所有时间都在本地时间ei CurrentTime,sessionStart,sessionEnd等都在当地时间(我编辑我的方法来反映这一点)。没有看到它是多么重要的比较是在什么时区进行的,只要它在任何时候都是一致的比较。我想我仍然错过了一些东西。 – parliament 2012-07-11 03:56:58

+0

也许我很困惑如何基于总体会话的开始和结束时间(下午5点 - 下一天下午5点)定义每个单独会话的实际时间(sydneyStartTime,sydneyEndTime,tokyoStartTime,tokyoEndTime等),这可能是也可能不是不规则(对于一个晚上开放的凌晨3点 - 同一天下午5点将跳过整个悉尼会议和东京会议的一部分,只在伦敦会议开始时开放。 – parliament 2012-07-11 03:57:15

+0

您需要更好地定义您的问题。 – dthorpe 2012-07-11 07:15:24

相关问题