Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions Algorithm.CSharp/ContractDaysToExpiryRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using QuantConnect.Data;
using QuantConnect.Data.Market;
using QuantConnect.Interfaces;

namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Regression algorithm asserting the behavior of <see cref="BaseContract.DaysToExpiry()"/> and <see cref="BaseContract.DTE"/>,
/// the supported alternative to manual expiry math mixing datetime and date values.
/// The Python version also asserts that the reference argument accepts both datetime and date instances.
/// </summary>
public class ContractDaysToExpiryRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
{
private Symbol _optionSymbol;
private int _contractsValidated;

public override void Initialize()
{
SetStartDate(2015, 12, 24);
SetEndDate(2015, 12, 24);
SetCash(100000);

var option = AddOption("GOOG");
option.SetFilter(u => u.Strikes(-2, +2).Expiration(0, 180));
_optionSymbol = option.Symbol;
}

public override void OnData(Slice slice)
{
OptionChain chain;
if (!slice.OptionChains.TryGetValue(_optionSymbol, out chain))
{
return;
}

foreach (var contract in chain)
{
var expected = (contract.Expiry.Date - Time.Date).Days;
if (contract.DaysToExpiry() != expected)
{
throw new RegressionTestException($"Expected DaysToExpiry() to be {expected} but was {contract.DaysToExpiry()}");
}
if (contract.DTE != expected)
{
throw new RegressionTestException($"Expected DTE to be {expected} but was {contract.DTE}");
}
if (contract.DaysToExpiry(Time.AddDays(-10)) != expected + 10)
{
throw new RegressionTestException($"Expected DaysToExpiry(reference) to be {expected + 10} but was {contract.DaysToExpiry(Time.AddDays(-10))}");
}
_contractsValidated++;
}
}

public override void OnEndOfAlgorithm()
{
if (_contractsValidated == 0)
{
throw new RegressionTestException("No contracts were validated");
}
}

/// <summary>
/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
/// </summary>
public bool CanRunLocally { get; } = true;

/// <summary>
/// This is used by the regression test system to indicate which languages this algorithm is written in.
/// </summary>
public List<Language> Languages { get; } = new() { Language.CSharp, Language.Python };

/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public long DataPoints => 37131;

/// <summary>
/// Data Points count of the algorithm history
/// </summary>
public int AlgorithmHistoryDataPoints => 0;

/// <summary>
/// Final status of the algorithm
/// </summary>
public AlgorithmStatus AlgorithmStatus => AlgorithmStatus.Completed;

/// <summary>
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
/// </summary>
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
{
{"Total Orders", "0"},
{"Average Win", "0%"},
{"Average Loss", "0%"},
{"Compounding Annual Return", "0%"},
{"Drawdown", "0%"},
{"Expectancy", "0"},
{"Start Equity", "100000"},
{"End Equity", "100000"},
{"Net Profit", "0%"},
{"Sharpe Ratio", "0"},
{"Sortino Ratio", "0"},
{"Probabilistic Sharpe Ratio", "0%"},
{"Loss Rate", "0%"},
{"Win Rate", "0%"},
{"Profit-Loss Ratio", "0"},
{"Alpha", "0"},
{"Beta", "0"},
{"Annual Standard Deviation", "0"},
{"Annual Variance", "0"},
{"Information Ratio", "0"},
{"Tracking Error", "0"},
{"Treynor Ratio", "0"},
{"Total Fees", "$0.00"},
{"Estimated Strategy Capacity", "$0"},
{"Lowest Capacity Asset", ""},
{"Portfolio Turnover", "0%"},
{"Drawdown Recovery", "0"},
{"OrderListHash", "d41d8cd98f00b204e9800998ecf8427e"}
};
}
}
55 changes: 55 additions & 0 deletions Algorithm.Python/ContractDaysToExpiryRegressionAlgorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from AlgorithmImports import *

### <summary>
### Regression algorithm asserting the behavior of 'days_to_expiry' and 'dte' on option/future contracts,
### the supported alternative to manual expiry math mixing datetime and date values.
### It also asserts that the reference argument accepts both datetime and date instances.
### </summary>
class ContractDaysToExpiryRegressionAlgorithm(QCAlgorithm):
def initialize(self):
self.set_start_date(2015, 12, 24)
self.set_end_date(2015, 12, 24)
self.set_cash(100000)

option = self.add_option("GOOG")
option.set_filter(lambda u: u.strikes(-2, +2).expiration(0, 180))
self._option_symbol = option.symbol
self._contracts_validated = 0

def on_data(self, slice):
chain = slice.option_chains.get(self._option_symbol)
if not chain:
return

for contract in chain:
# The manual shape, with the operand types correctly aligned
expected = (contract.expiry.date() - self.time.date()).days
if contract.days_to_expiry() != expected:
raise AssertionError(f"Expected days_to_expiry() to be {expected} but was {contract.days_to_expiry()}")
if contract.dte != expected:
raise AssertionError(f"Expected dte to be {expected} but was {contract.dte}")
# The reference argument accepts both datetime and date instances
if contract.days_to_expiry(self.time) != expected:
raise AssertionError(f"Expected days_to_expiry(datetime) to be {expected} but was {contract.days_to_expiry(self.time)}")
if contract.days_to_expiry(self.time.date()) != expected:
raise AssertionError(f"Expected days_to_expiry(date) to be {expected} but was {contract.days_to_expiry(self.time.date())}")
if contract.days_to_expiry(reference=self.time.date() - timedelta(days=10)) != expected + 10:
raise AssertionError(f"Expected days_to_expiry(reference=date) to be {expected + 10}")
self._contracts_validated += 1

def on_end_of_algorithm(self):
if self._contracts_validated == 0:
raise AssertionError("No contracts were validated")
1 change: 1 addition & 0 deletions Common/AlgorithmImports.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
pass

from datetime import date, time, datetime, timedelta
from zoneinfo import ZoneInfo
from typing import *
import math
import json
Expand Down
28 changes: 28 additions & 0 deletions Common/Data/Market/BaseContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,34 @@ public Symbol Symbol
/// </summary>
public DateTime Expiry => Symbol.ID.Date;

/// <summary>
/// Gets the number of whole days until the contract expires, based on the contract's current time.
/// Shorthand alias of <see cref="DaysToExpiry()"/>
/// </summary>
[PandasIgnore]
public int DTE => DaysToExpiry();

/// <summary>
/// Gets the number of whole days between the contract's current time (<see cref="Time"/>) and <see cref="Expiry"/>
/// </summary>
/// <returns>The number of whole days until the contract expires</returns>
public int DaysToExpiry()
{
return DaysToExpiry(Time);
}

/// <summary>
/// Gets the number of whole days between the given reference and <see cref="Expiry"/>.
/// From Python, the reference accepts both <c>datetime</c> and <c>date</c> instances, avoiding the
/// TypeError users hit when manually mixing them, e.g. <c>(contract.expiry - self.time.date()).days</c>
/// </summary>
/// <param name="reference">The date to measure from. Only the date part is used</param>
/// <returns>The number of whole days from the given reference until the contract expires</returns>
public int DaysToExpiry(DateTime reference)
{
return (Expiry.Date - reference.Date).Days;
}

/// <summary>
/// Gets the local date time this contract's data was last updated
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using Python.Runtime;
using QuantConnect.Util;

namespace QuantConnect.Exceptions
{
/// <summary>
/// Interprets the TypeError exceptions pythonnet raises when a .NET generic type or Array is indexed with
/// something that is not a .NET type, e.g. 'RollingWindow[datetime](10)' with Python's datetime type
/// </summary>
public class GenericTypeParameterPythonExceptionInterpreter : PythonExceptionInterpreter
{
/// <summary>
/// Determines the order that an instance of this class should be called
/// </summary>
public override int Order => 0;

/// <summary>
/// Determines if this interpreter should be applied to the specified exception.
/// </summary>
/// <param name="exception">The exception to check</param>
/// <returns>True if the exception can be interpreted, false otherwise</returns>
public override bool CanInterpret(Exception exception)
{
// "type(s) expected" is raised for generic types, "type expected" for Array
return base.CanInterpret(exception) &&
(exception.Message.Contains(Messages.GenericTypeParameterPythonExceptionInterpreter.TypesExpectedSubstring) ||
exception.Message.Contains(Messages.GenericTypeParameterPythonExceptionInterpreter.TypeExpectedSubstring));
}

/// <summary>
/// Interprets the specified exception into a new exception
/// </summary>
/// <param name="exception">The exception to be interpreted</param>
/// <param name="innerInterpreter">An interpreter that should be applied to the inner exception.</param>
/// <returns>The interpreted exception</returns>
public override Exception Interpret(Exception exception, IExceptionInterpreter innerInterpreter)
{
var pe = (PythonException)exception;

var message = Messages.GenericTypeParameterPythonExceptionInterpreter.InvalidGenericTypeParameter;
message += PythonUtil.PythonExceptionStackParser(pe.StackTrace);

return new Exception(message, pe);
}
}
}
63 changes: 63 additions & 0 deletions Common/Exceptions/TzInfoPythonExceptionInterpreter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using Python.Runtime;
using QuantConnect.Util;

namespace QuantConnect.Exceptions
{
/// <summary>
/// Interprets TypeError exceptions caused by passing a Lean time zone (a NodaTime DateTimeZone, like the
/// <see cref="TimeZones"/> values) where Python expects a tzinfo instance, e.g. 'datetime.now(TimeZones.NEW_YORK)'
/// </summary>
public class TzInfoPythonExceptionInterpreter : PythonExceptionInterpreter
{
/// <summary>
/// Determines the order that an instance of this class should be called
/// </summary>
public override int Order => 0;

/// <summary>
/// Determines if this interpreter should be applied to the specified exception.
/// </summary>
/// <param name="exception">The exception to check</param>
/// <returns>True if the exception can be interpreted, false otherwise</returns>
public override bool CanInterpret(Exception exception)
{
// CPython raises "tzinfo argument must be None or of a tzinfo subclass, not type 'CachedDateTimeZone'".
// Only interpret it when the offending type is a NodaTime time zone, so the hint below is accurate
return base.CanInterpret(exception) &&
exception.Message.Contains(Messages.TzInfoPythonExceptionInterpreter.TzInfoSubclassExpectedSubstring) &&
exception.Message.Contains(Messages.TzInfoPythonExceptionInterpreter.DateTimeZoneTypeSubstring);
}

/// <summary>
/// Interprets the specified exception into a new exception
/// </summary>
/// <param name="exception">The exception to be interpreted</param>
/// <param name="innerInterpreter">An interpreter that should be applied to the inner exception.</param>
/// <returns>The interpreted exception</returns>
public override Exception Interpret(Exception exception, IExceptionInterpreter innerInterpreter)
{
var pe = (PythonException)exception;

var message = Messages.TzInfoPythonExceptionInterpreter.LeanTimeZoneUsedAsTzInfo;
message += PythonUtil.PythonExceptionStackParser(pe.StackTrace);

return new Exception(message, pe);
}
}
}
Loading
Loading