简体   繁体   English

确定当前时间是否在多个时间跨度之间

[英]Determine if current time between multiple time spans

I'm having trouble figuring out which tradings session any particular time is in. 我无法确定任何特定时间在哪个交易时段。

There are four possible sessions, show in this picture taken from ForexFactory.com 该图中显示了四个可能的会话,取自ForexFactory.com

在此处输入图片说明

I have this method that I need to check is currentTime is during the specified trading session. 我有这种方法需要检查的是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;
}

Use: 使用:

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

Thank you 谢谢

I'd suggest writing a simple function for each trading session, which takes a DateTime and returns a bool indicating if it's open at that time. 我建议为每个交易时段编写一个简单的函数,该函数需要一个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

Then place these into a Dictionary<TradingSession, Func> like this for generic retrieval... 然后将它们放入像这样的Dictionary<TradingSession, Func>以进行通用检索...

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 然后,您现有的方法只需为给定的TradingSession选择适当的函数并将其应用

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

I don't believe you need UTC time here as long as your application only runs in a single timezone, but daylight savings might cause problems. 只要您的应用程序仅在单个时区运行 ,我认为您不需要在这里使用UTC时间,但是夏令时可能会导致问题。


A nice way to account for the problem of trading sessions which cover two days, as opposed to just one day, is to write a helper that precisely considers whether it's a 'cross-day' 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);

You can compare with > & < or compare ticks. 您可以使用>&<进行比较,也可以比较刻度线。

See related questions: Check if datetime instance falls in between other two datetime objects 请参阅相关问题: 检查datetime实例是否介于其他两个datetime对象之间

To avoid the multiple if statements, you could also create a TradingSession object with start and end time and define a property/function to check if in session. 为了避免使用多个if语句,您还可以创建一个包含开始时间和结束时间的TradingSession对象,并定义一个属性/函数以检查是否在会话中。 When I have big switch or if blocks, it usually indicates a missed OO opportunity :) 当我有很大的选择或障碍时,通常表示错过了OO机会:)

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

The trading session object could then have a property IsInSession. 然后,交易会话对象可以具有属性IsInSession。

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

This uses UTC time to eliminate time zone issues. 这使用UTC时间来消除时区问题。

You need to normalize your local times to UTC. 您需要将当地时间标准化为UTC。 You can then compare times across regions. 然后,您可以比较各个地区的时间。

For each trading session, you need to know the session start and end time in UTC. 对于每个交易时段,您需要以UTC知道交易时段的开始和结束时间。

You need the current time in UTC. 您需要使用UTC的当前时间。 DateTime.UtcNow , for example. 例如, DateTime.UtcNow

You can then perform range comparisons for each trading session window: 然后,您可以为每个交易时段窗口执行范围比较:

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

etc... 等等...

If you're trying to validate that a transaction time is valid for a transaction on a particular trading session, this will work fine. 如果您要尝试验证某个交易时间对于特定交易时段的交易有效,则可以正常使用。

If however you're trying to figure out what trading session a trade belongs to based on its time, you will have multiple answers sometimes because the trading sessions overlap. 但是,如果您尝试根据时间确定某笔交易所属的交易时段,则有时会遇到多个答案,因为交易时段重叠。 Multiple trading sessions may be valid for a given time. 在给定时间内,多个交易时段可能有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM