Skip to content

Commit

Permalink
Changed time rules to explicitly require a user to specify only posit…
Browse files Browse the repository at this point in the history
…ive numbers in offsets for both live and backtest modes. Tests.
  • Loading branch information
quant1729 committed Feb 8, 2017
1 parent 0e0e6a9 commit ecb95bb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Common/Scheduling/TimeRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ public ITimeRule Every(TimeSpan interval)
/// <returns>A time rule that fires the specified number of minutes after the symbol's market open</returns>
public ITimeRule AfterMarketOpen(Symbol symbol, double minutesAfterOpen = 0, bool extendedMarketOpen = false)
{
if (minutesAfterOpen < 0)
{
throw new ArgumentException("The time after market open is negative. Please make sure it is a non-negative value", "minutesAfterOpen");
}

var security = GetSecurity(symbol);

var type = extendedMarketOpen ? "ExtendedMarketOpen" : "MarketOpen";
Expand All @@ -163,6 +168,11 @@ where security.Exchange.DateIsOpen(date)
/// <returns>A time rule that fires the specified number of minutes before the symbol's market close</returns>
public ITimeRule BeforeMarketClose(Symbol symbol, double minutesBeforeClose = 0, bool extendedMarketClose = false)
{
if (minutesBeforeClose < 0)
{
throw new ArgumentException("The time before market close is negative. Please make sure it is a non-negative value", "minutesBeforeClose");
}

var security = GetSecurity(symbol);

var type = extendedMarketClose ? "ExtendedMarketClose" : "MarketClose";
Expand Down
14 changes: 14 additions & 0 deletions Tests/Common/Scheduling/TimeRulesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ public void ExtendedMarketOpenWithDelta()
Assert.AreEqual(1, count);
}

[Test]
public void RegularMarketOpenWithNegativeDelta()
{
var rules = GetTimeRules(TimeZones.Utc);
Assert.Throws<ArgumentException>(() => rules.AfterMarketOpen(Symbols.SPY, -30));
}

[Test]
public void RegularMarketCloseNoDelta()
{
Expand Down Expand Up @@ -156,6 +163,13 @@ public void RegularMarketCloseWithDelta()
Assert.AreEqual(1, count);
}

[Test]
public void RegularMarketCloseWithNegativeDelta()
{
var rules = GetTimeRules(TimeZones.Utc);
Assert.Throws<ArgumentException>(() => rules.BeforeMarketClose(Symbols.SPY, -30));
}

[Test]
public void ExtendedMarketCloseNoDelta()
{
Expand Down

0 comments on commit ecb95bb

Please sign in to comment.