Skip to content
Merged
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
143 changes: 143 additions & 0 deletions Algorithm.CSharp/FutureOptionContinuousFutureRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* 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.Collections.Generic;
using System.Linq;
using QuantConnect.Data;
using QuantConnect.Interfaces;
using QuantConnect.Securities;
using QuantConnect.Securities.Future;

namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Regression algorithm that validates that when using a continuous future (without a filter)
/// the option chains are correctly populated using the mapped symbol.
/// </summary>
public class FutureOptionContinuousFutureRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
{
protected Future Future { get; private set; }
private bool _hasAnyOptionChainForMappedSymbol;
public override void Initialize()
{
SetStartDate(2020, 1, 4);
SetEndDate(2020, 1, 8);

Future = AddFuture(Futures.Indices.SP500EMini, Resolution.Minute, Market.CME);
SetFilter();

AddFutureOption(Future.Symbol, universe => universe.Strikes(-1, 1));
}

public virtual void SetFilter()
{
}

public override void OnData(Slice slice)
{
if (slice.OptionChains.Count == 0)
{
return;
}

ValidateOptionChains(slice);

// OptionChain for the mapped symbol must exist with or without a future filter
if (!slice.OptionChains.TryGetValue(Future.Mapped, out var chain) || chain == null || !chain.Any())
{
throw new RegressionTestException("No option chain found for mapped symbol during algorithm execution");
}

// Mark that we successfully received a non-empty OptionChain for mapped symbol
_hasAnyOptionChainForMappedSymbol = true;
}

public virtual void ValidateOptionChains(Slice slice)
{
if (slice.OptionChains.Count != 1)
{
throw new RegressionTestException("Expected only one option chain for the mapped symbol");
}
}

public override void OnEndOfAlgorithm()
{
if (!_hasAnyOptionChainForMappedSymbol)
{
throw new RegressionTestException("No non-empty option chain found for mapped symbol during algorithm execution");
}
}

/// <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 virtual long DataPoints => 15767;

/// <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", "-8.363"},
{"Tracking Error", "0.059"},
{"Treynor Ratio", "0"},
{"Total Fees", "$0.00"},
{"Estimated Strategy Capacity", "$0"},
{"Lowest Capacity Asset", ""},
{"Portfolio Turnover", "0%"},
{"Drawdown Recovery", "0"},
{"OrderListHash", "d41d8cd98f00b204e9800998ecf8427e"}
};
}
}
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 QuantConnect.Data;

namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Regression algorithm that validates that when using a Future with filter
/// the option chains are correctly populated and are unique
/// </summary>
public class FutureOptionWithFutureFilterRegressionAlgorithm : FutureOptionContinuousFutureRegressionAlgorithm
{
public override void SetFilter()
{
Future.SetFilter(0, 368);
}

public override void ValidateOptionChains(Slice slice)
{
var futureContractsWithOptionChains = 0;
foreach (var futureChain in slice.FutureChains.Values)
{
foreach (var futureContract in futureChain)
{
// Not all future contracts have option chains, so we need to check if the contract is in the option chain
if (slice.OptionChains.ContainsKey(futureContract.Symbol))
{
var chain = slice.OptionChains[futureContract.Symbol];
if (chain.Count == 0)
{
throw new RegressionTestException($"Expected at least one option contract for {chain.Symbol}");
}
futureContractsWithOptionChains++;
}
}

}
if (futureContractsWithOptionChains < 1)
{
throw new RegressionTestException($"Expected at least two future contracts with option chains, but found {futureContractsWithOptionChains}");
}
}

/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public override long DataPoints => 29701;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# 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 that validates that when using a continuous future (without a filter)
### the option chains are correctly populated using the mapped symbol.
### </summary>
class FutureOptionContinuousFutureRegressionAlgorithm(QCAlgorithm):
def initialize(self):
self.set_start_date(2020, 1, 4)
self.set_end_date(2020, 1, 8)

self.future = self.add_future(Futures.Indices.SP_500_E_MINI, Resolution.MINUTE, Market.CME)
self.set_filter()

self.add_future_option(self.future.symbol, lambda universe: universe.strikes(-1, 1))

self._has_any_option_chain_for_mapped_symbol = False

def set_filter(self):
"""Set future filter - override in derived classes for filtered version"""
pass

def on_data(self, slice: Slice):
if len(slice.option_chains) == 0:
return

self.validate_option_chains(slice)

# OptionChain for mapped symbol
chain = slice.option_chains[self.future.mapped]
if chain is None or not any(chain):
raise RegressionTestException("No option chain found for mapped symbol during algorithm execution")

# Mark that we successfully received a non-empty OptionChain for mapped symbol
self._has_any_option_chain_for_mapped_symbol = True

def validate_option_chains(self, slice: Slice):
if len(slice.option_chains) != 1:
raise RegressionTestException("Expected only one option chain for the mapped symbol")

def on_end_of_algorithm(self):
if not self._has_any_option_chain_for_mapped_symbol:
raise RegressionTestException("No non-empty option chain found for mapped symbol during algorithm execution")
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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 *

from FutureOptionContinuousFutureRegressionAlgorithm import FutureOptionContinuousFutureRegressionAlgorithm

### <summary>
### Regression algorithm that validates that when using a Future with filter
### the option chains are correctly populated and are unique
### </summary>
class FutureOptionWithFutureFilterRegressionAlgorithm(FutureOptionContinuousFutureRegressionAlgorithm):
def set_filter(self):
"""Set future filter for specific contracts"""
self.future.set_filter(0, 368)

def validate_option_chains(self, slice: Slice):
future_contracts_with_option_chains = 0
for future_chain in slice.future_chains.values():
for future_contract in future_chain:
# Not all future contracts have option chains, so we need to check if the contract is in the option chain
if future_contract.symbol in slice.option_chains:
chain = slice.option_chains[future_contract.symbol]
if len(chain) == 0:
raise RegressionTestException("Expected at least one option contract for {}".format(chain.symbol))
future_contracts_with_option_chains += 1

if future_contracts_with_option_chains < 1:
raise RegressionTestException("Expected at least two future contracts with option chains, but found {}".format(future_contracts_with_option_chains))
7 changes: 7 additions & 0 deletions Algorithm/QCAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2206,6 +2206,13 @@ public void AddFutureOption(Symbol symbol, Func<OptionFilterUniverse, OptionFilt
}

AddUniverseOptions(symbol, optionFilter);

// Also add universe options for ContinuousContractUniverse to handle continuous futures
var continuousUniverseSymbol = ContinuousContractUniverse.CreateSymbol(symbol);
if (UniverseManager.ContainsKey(continuousUniverseSymbol))
{
AddUniverseOptions(continuousUniverseSymbol, optionFilter);
}
}

/// <summary>
Expand Down
12 changes: 6 additions & 6 deletions Common/Data/Market/DataDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public class DataDictionary<T> : ExtendedDictionary<Symbol, T>, IDictionary<Symb
// storage for the data
private readonly IDictionary<Symbol, T> _data = new Dictionary<Symbol, T>();

/// <summary>
/// Initializes a new instance of the <see cref="QuantConnect.Data.Market.DataDictionary{T}"/> class.
/// </summary>
/// <summary>
/// Initializes a new instance of the <see cref="QuantConnect.Data.Market.DataDictionary{T}"/> class.
/// </summary>
public DataDictionary()
{
}
Expand Down Expand Up @@ -114,7 +114,7 @@ public override void Clear()
/// true if <paramref name="item"/> is found in the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false.
/// </returns>
/// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
public bool Contains(KeyValuePair<Symbol, T> item)
public virtual bool Contains(KeyValuePair<Symbol, T> item)
{
return _data.Contains(item);
}
Expand All @@ -135,7 +135,7 @@ public void CopyTo(KeyValuePair<Symbol, T>[] array, int arrayIndex)
/// true if <paramref name="item"/> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"/>.
/// </returns>
/// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
public bool Remove(KeyValuePair<Symbol, T> item)
public virtual bool Remove(KeyValuePair<Symbol, T> item)
{
return _data.Remove(item);
}
Expand Down Expand Up @@ -184,7 +184,7 @@ public override bool ContainsKey(Symbol key)
/// Adds an element with the provided key and value to the <see cref="System.Collections.Generic.IDictionary{TKey, TValue}"/>.
/// </summary>
/// <param name="key">The object to use as the key of the element to add.</param><param name="value">The object to use as the value of the element to add.</param><exception cref="System.ArgumentNullException"><paramref name="key"/> is null.</exception><exception cref="T:System.ArgumentException">An element with the same key already exists in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.</exception><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.</exception>
public void Add(Symbol key, T value)
public virtual void Add(Symbol key, T value)
{
_data.Add(key, value);
}
Expand Down
Loading