Add regression algorithms
This commit is contained in:
@@ -26,7 +26,7 @@ class FutureOptionChainFullDataRegressionAlgorithm(QCAlgorithm):
|
||||
|
||||
future_contract = self.add_future_contract(
|
||||
Symbol.create_future(Futures.Indices.SP_500_E_MINI, Market.CME, datetime(2020, 3, 20)),
|
||||
Resolution.MINUTE).symbol;
|
||||
Resolution.MINUTE).symbol
|
||||
|
||||
option_chain = self.option_chain(future_contract, flatten=True)
|
||||
|
||||
@@ -34,7 +34,7 @@ class FutureOptionChainFullDataRegressionAlgorithm(QCAlgorithm):
|
||||
df = option_chain.data_frame
|
||||
# Get contracts expiring within 4 months, with the latest expiration date, highest strike and lowest price
|
||||
contracts = df.loc[(df.expiry <= self.time + timedelta(days=120))]
|
||||
contracts = df.sort_values(['expiry', 'strike', 'lastprice'], ascending=[False, False, True])
|
||||
contracts = contracts.sort_values(['expiry', 'strike', 'lastprice'], ascending=[False, False, True])
|
||||
self._option_contract = contracts.index[0]
|
||||
|
||||
self.add_future_option_contract(self._option_contract)
|
||||
|
||||
@@ -26,11 +26,11 @@ class FutureOptionChainsMultipleFullDataRegressionAlgorithm(QCAlgorithm):
|
||||
|
||||
es_future_contract = self.add_future_contract(
|
||||
Symbol.create_future(Futures.Indices.SP_500_E_MINI, Market.CME, datetime(2020, 3, 20)),
|
||||
Resolution.MINUTE).symbol;
|
||||
Resolution.MINUTE).symbol
|
||||
|
||||
gc_future_contract = self.add_future_contract(
|
||||
Symbol.create_future(Futures.Metals.GOLD, Market.COMEX, datetime(2020, 4, 28)),
|
||||
Resolution.MINUTE).symbol;
|
||||
Resolution.MINUTE).symbol
|
||||
|
||||
chains = self.option_chains([es_future_contract, gc_future_contract], flatten=True)
|
||||
|
||||
@@ -46,17 +46,18 @@ class FutureOptionChainsMultipleFullDataRegressionAlgorithm(QCAlgorithm):
|
||||
# Index by the requested underlying, by getting all data with canonicals which underlying is the requested underlying symbol:
|
||||
canonicals = df.index.get_level_values('canonical')
|
||||
condition = [canonical for canonical in canonicals if canonical.underlying == underlying]
|
||||
df = df.loc[condition]
|
||||
contracts = df.loc[condition]
|
||||
|
||||
# Get contracts expiring within 4 months, with the latest expiration date, highest strike and lowest price
|
||||
contracts = df.loc[(df.expiry <= self.time + timedelta(days=120))]
|
||||
contracts = df.sort_values(['expiry', 'strike', 'lastprice'], ascending=[False, False, True])
|
||||
contracts = contracts.loc[(df.expiry <= self.time + timedelta(days=120))]
|
||||
contracts = contracts.sort_values(['expiry', 'strike', 'lastprice'], ascending=[False, False, True])
|
||||
|
||||
return contracts.index[0][1]
|
||||
|
||||
def on_data(self, data):
|
||||
# Do some trading with the selected contract for sample purposes
|
||||
if not self.portfolio.invested:
|
||||
self.set_holdings(self._es_option_contract, 0.5)
|
||||
self.set_holdings(self._es_option_contract, 0.25)
|
||||
self.set_holdings(self._gc_option_contract, 0.25)
|
||||
else:
|
||||
self.liquidate()
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
# 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 testing history requests for <see cref="FutureUniverse"/> type work as expected
|
||||
### and return the same data as the futures chain provider.
|
||||
### </summary>
|
||||
class OptionUniverseHistoryRegressionAlgorithm(QCAlgorithm):
|
||||
|
||||
def initialize(self):
|
||||
self.set_start_date(2013, 10, 11)
|
||||
self.set_end_date(2013, 10, 11)
|
||||
|
||||
future = self.add_future(Futures.Indices.SP_500_E_MINI).symbol
|
||||
|
||||
historical_futures_data_df = self.history(FutureUniverse, future, 3, flatten=True)
|
||||
|
||||
# Level 0 of the multi-index is the date, we expect 3 dates, 3 future chains
|
||||
if historical_futures_data_df.index.levshape[0] != 3:
|
||||
raise RegressionTestException(f"Expected 3 futures chains from history request, "
|
||||
f"but got {historical_futures_data_df.index.levshape[1]}")
|
||||
|
||||
for date in historical_futures_data_df.index.levels[0]:
|
||||
expected_chain = list(self.future_chain_provider.get_future_contract_list(future, date))
|
||||
expected_chain_count = len(expected_chain)
|
||||
|
||||
actual_chain = historical_futures_data_df.loc[date]
|
||||
actual_chain_count = len(actual_chain)
|
||||
|
||||
if expected_chain_count != actual_chain_count:
|
||||
raise RegressionTestException(f"Expected {expected_chain_count} futures in chain on {date}, "
|
||||
f"but got {actual_chain_count}")
|
||||
|
||||
for i, symbol in enumerate(actual_chain.index):
|
||||
expected_symbol = expected_chain[i]
|
||||
if symbol != expected_symbol:
|
||||
raise RegressionTestException(f"Expected symbol {expected_symbol} at index "
|
||||
f" {i} on {date}, but got {symbol}")
|
||||
@@ -0,0 +1,45 @@
|
||||
# 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 datetime import timedelta
|
||||
|
||||
### <summary>
|
||||
### Regression algorithm illustrating the usage of the <see cref="QCAlgorithm.FuturesChain(Symbol, bool)"/>
|
||||
### method to get a future chain.
|
||||
### </summary>
|
||||
class FuturesChainFullDataRegressionAlgorithm(QCAlgorithm):
|
||||
|
||||
def initialize(self):
|
||||
self.set_start_date(2013, 10, 7)
|
||||
self.set_end_date(2013, 10, 7)
|
||||
|
||||
future = self.add_future(Futures.Indices.SP_500_E_MINI, Resolution.MINUTE).symbol
|
||||
|
||||
chain = self.futures_chain(future, flatten=True)
|
||||
|
||||
# Demonstration using data frame:
|
||||
df = chain.data_frame
|
||||
# Get contracts expiring within 6 months, with the latest expiration date, and lowest price
|
||||
contracts = df.loc[(df.expiry <= self.time + timedelta(days=180))]
|
||||
contracts = contracts.sort_values(['expiry', 'lastprice'], ascending=[False, True])
|
||||
self._future_contract = contracts.index[0]
|
||||
|
||||
self.add_future_contract(self._future_contract)
|
||||
|
||||
def on_data(self, data):
|
||||
# Do some trading with the selected contract for sample purposes
|
||||
if not self.portfolio.invested:
|
||||
self.set_holdings(self._future_contract, 0.5)
|
||||
else:
|
||||
self.liquidate()
|
||||
@@ -0,0 +1,58 @@
|
||||
# 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 datetime import timedelta
|
||||
|
||||
### <summary>
|
||||
### Regression algorithm illustrating the usage of the <see cref="QCAlgorithm.FuturesChains(IEnumerable{Symbol}, bool)"/>
|
||||
### method to get multiple futures chains.
|
||||
### </summary>
|
||||
class FuturesChainsMultipleFullDataRegressionAlgorithm(QCAlgorithm):
|
||||
|
||||
def initialize(self):
|
||||
self.set_start_date(2013, 10, 7)
|
||||
self.set_end_date(2013, 10, 7)
|
||||
|
||||
es_future = self.add_future(Futures.Indices.SP_500_E_MINI).symbol
|
||||
gc_future = self.add_future(Futures.Metals.GOLD).symbol
|
||||
|
||||
chains = self.futures_chains([es_future, gc_future], flatten=True)
|
||||
|
||||
self._es_contract = self.get_contract(chains, es_future)
|
||||
self._gc_contract = self.get_contract(chains, gc_future)
|
||||
|
||||
self.add_future_contract(self._es_contract)
|
||||
self.add_future_contract(self._gc_contract)
|
||||
|
||||
def get_contract(self, chains: FuturesChains, canonical: Symbol) -> Symbol:
|
||||
df = chains.data_frame
|
||||
|
||||
# Index by the requested underlying, by getting all data with canonicals which underlying is the requested underlying symbol:
|
||||
canonicals = df.index.get_level_values('canonical')
|
||||
condition = [symbol for symbol in canonicals if symbol == canonical]
|
||||
contracts = df.loc[condition]
|
||||
|
||||
# Get contracts expiring within 6 months, with the latest expiration date, and lowest price
|
||||
contracts = contracts.loc[(df.expiry <= self.time + timedelta(days=180))]
|
||||
contracts = contracts.sort_values(['expiry', 'lastprice'], ascending=[False, True])
|
||||
|
||||
return contracts.index[0][1]
|
||||
|
||||
def on_data(self, data):
|
||||
# Do some trading with the selected contract for sample purposes
|
||||
if not self.portfolio.invested:
|
||||
self.set_holdings(self._es_contract, 0.25)
|
||||
self.set_holdings(self._gc_contract, 0.25)
|
||||
else:
|
||||
self.liquidate()
|
||||
@@ -43,6 +43,7 @@
|
||||
<Content Include="OptionUniverseFilterGreeksShortcutsRegressionAlgorithm.py" />
|
||||
<Content Include="OptionUniverseFilterOptionsDataRegressionAlgorithm.py" />
|
||||
<Content Include="OptionUniverseFilterGreeksRegressionAlgorithm.py" />
|
||||
<Content Include="FutureUniverseHistoryRegressionAlgorithm.py" />
|
||||
<Content Include="OptionUniverseHistoryRegressionAlgorithm.py" />
|
||||
<Content Include="FundamentalUniverseSelectionAlgorithm.py" />
|
||||
<Content Include="AccumulativeInsightPortfolioRegressionAlgorithm.py" />
|
||||
|
||||
Reference in New Issue
Block a user