Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public override void OnEndOfAlgorithm()
/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public long DataPoints => 126222;
public long DataPoints => 17270;

/// <summary>
/// Data Points count of the algorithm history
Expand Down
153 changes: 153 additions & 0 deletions Algorithm.CSharp/EquityOptionsUniverseSettingsRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* 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.Data.UniverseSelection;
using QuantConnect.Securities;
using System;

namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Regression algorithm asserting that options from universe are added with the same resolution, fill forward and extended market hours settings as the universe settings.
/// </summary>
public class EquityOptionsUniverseSettingsRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
{
private SecurityType[] _securityTypes;
private HashSet<SecurityType> _checkedSecurityTypes = new();

protected virtual DateTime TestStartDate => new DateTime(2015, 12, 24);

public override void Initialize()
{
SetStartDate(TestStartDate);
SetEndDate(TestStartDate.AddDays(1));
SetCash(100000);

UniverseSettings.Resolution = Resolution.Daily;
UniverseSettings.FillForward = false;
UniverseSettings.ExtendedMarketHours = true;

_securityTypes = AddSecurity();
}

protected virtual SecurityType[] AddSecurity()
{
var equity = AddEquity("GOOG");
var option = AddOption(equity.Symbol);
option.SetFilter(u => u.StandardsOnly().Strikes(-2, +2).Expiration(0, 180));

return [option.Symbol.SecurityType];
}

public override void OnSecuritiesChanged(SecurityChanges changes)
{
var securities = changes.AddedSecurities.Where(x => _securityTypes.Contains(x.Type) && !x.Symbol.IsCanonical()).Select(x => x.Symbol).ToList();
var configs = SubscriptionManager.Subscriptions.Where(x => securities.Contains(x.Symbol));

foreach (var config in configs)
{
if (config.Resolution != UniverseSettings.Resolution)
{
throw new RegressionTestException($"Config '{config}' resolution {config.Resolution} does not match universe settings resolution {UniverseSettings.Resolution}");
}

if (config.FillDataForward != UniverseSettings.FillForward)
{
throw new RegressionTestException($"Config '{config}' fill forward {config.FillDataForward} does not match universe settings fill forward {UniverseSettings.FillForward}");
}

if (config.ExtendedMarketHours != UniverseSettings.ExtendedMarketHours)
{
throw new RegressionTestException($"Config '{config}' extended market hours {config.ExtendedMarketHours} does not match universe settings extended market hours {UniverseSettings.ExtendedMarketHours}");
}

_checkedSecurityTypes.Add(config.SecurityType);
}
}

public override void OnEndOfAlgorithm()
{
if (_checkedSecurityTypes.Count != _securityTypes.Length || !_securityTypes.All(_checkedSecurityTypes.Contains))
{
throw new RegressionTestException($"Not all security types were checked. Expected: {string.Join(", ", _securityTypes)}. Checked: {string.Join(", ", _checkedSecurityTypes)}");
}
}

/// <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 };

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

/// <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 virtual 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"}
};
}
}
32 changes: 16 additions & 16 deletions Algorithm.CSharp/FundamentalRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public override void OnSecuritiesChanged(SecurityChanges changes)
/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public long DataPoints => 77169;
public long DataPoints => 70972;

/// <summary>
/// Data Points count of the algorithm history
Expand All @@ -239,34 +239,34 @@ public override void OnSecuritiesChanged(SecurityChanges changes)
/// </summary>
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
{
{"Total Orders", "2"},
{"Total Orders", "3"},
{"Average Win", "0%"},
{"Average Loss", "0%"},
{"Compounding Annual Return", "-1.016%"},
{"Compounding Annual Return", "-1.169%"},
{"Drawdown", "0.100%"},
{"Expectancy", "0"},
{"Start Equity", "100000"},
{"End Equity", "99963.64"},
{"Net Profit", "-0.036%"},
{"Sharpe Ratio", "-4.731"},
{"Sortino Ratio", "-6.776"},
{"Probabilistic Sharpe Ratio", "24.373%"},
{"End Equity", "99958.14"},
{"Net Profit", "-0.042%"},
{"Sharpe Ratio", "-3.451"},
{"Sortino Ratio", "-4.933"},
{"Probabilistic Sharpe Ratio", "27.530%"},
{"Loss Rate", "0%"},
{"Win Rate", "0%"},
{"Profit-Loss Ratio", "0"},
{"Alpha", "-0.013"},
{"Beta", "0.023"},
{"Annual Standard Deviation", "0.003"},
{"Beta", "0.043"},
{"Annual Standard Deviation", "0.005"},
{"Annual Variance", "0"},
{"Information Ratio", "0.607"},
{"Tracking Error", "0.095"},
{"Treynor Ratio", "-0.654"},
{"Total Fees", "$2.00"},
{"Tracking Error", "0.093"},
{"Treynor Ratio", "-0.381"},
{"Total Fees", "$3.00"},
{"Estimated Strategy Capacity", "$1900000000.00"},
{"Lowest Capacity Asset", "IBM R735QTJ8XC9X"},
{"Portfolio Turnover", "0.30%"},
{"Drawdown Recovery", "5"},
{"OrderListHash", "9b3bf202c3d5707779f25e9c7f7fdc92"}
{"Portfolio Turnover", "0.45%"},
{"Drawdown Recovery", "4"},
{"OrderListHash", "63a37fcfe86bca2e037d9dbb9c531e43"}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.Securities;
using System;

namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Regression algorithm asserting that futures and future options from universe are added with the same resolution, fill forward and extended market hours settings as the universe settings.
/// </summary>
public class FuturesAndFutureOptionsUniverseSettingsRegressionAlgorithm : EquityOptionsUniverseSettingsRegressionAlgorithm
{
protected override DateTime TestStartDate => new DateTime(2020, 01, 03);

protected override SecurityType[] AddSecurity()
{
var futures = AddFuture(Futures.Indices.SP500EMini);
futures.SetFilter(0, 180);

AddFutureOption(futures.Symbol, universe => universe.Strikes(-5, +5));

return [SecurityType.Future, SecurityType.FutureOption];
}

/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public override long DataPoints => 456;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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;

namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Regression algorithm asserting that index options from universe are added with the same resolution, fill forward and extended market hours settings as the universe settings.
/// </summary>
public class IndexOptionsUniverseSettingsRegressionAlgorithm : EquityOptionsUniverseSettingsRegressionAlgorithm
{
protected override DateTime TestStartDate => new DateTime(2021, 01, 05);

protected override SecurityType[] AddSecurity()
{
var index = AddIndex("SPX");
var indexOption = AddOption(index.Symbol);
indexOption.SetFilter(u => u.Strikes(-2, +2).Expiration(0, 180));

return [indexOption.Symbol.SecurityType];
}

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

/// <summary>
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
/// </summary>
public override 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", "-16.713"},
{"Tracking Error", "0.067"},
{"Treynor Ratio", "0"},
{"Total Fees", "$0.00"},
{"Estimated Strategy Capacity", "$0"},
{"Lowest Capacity Asset", ""},
{"Portfolio Turnover", "0%"},
{"Drawdown Recovery", "0"},
{"OrderListHash", "d41d8cd98f00b204e9800998ecf8427e"}
};
}
}
3 changes: 1 addition & 2 deletions Algorithm.Python/IndexOptionCallButterflyAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,5 @@ def on_data(self, slice: Slice) -> None:
call_butterfly = OptionStrategies.call_butterfly(self.spxw, otm_strike, atm_strike, itm_strike, expiry)
price = sum([abs(self.securities[x.symbol].price * x.quantity) * self.multiplier for x in call_butterfly.underlying_legs])
if price > 0:
quantity = self.portfolio.total_portfolio_value // price
quantity = int(self.portfolio.total_portfolio_value // price)
self.tickets = self.buy(call_butterfly, quantity, asynchronous=True)

3 changes: 1 addition & 2 deletions Algorithm.Python/IndexOptionPutButterflyAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,5 @@ def on_data(self, slice: Slice) -> None:
put_butterfly = OptionStrategies.put_butterfly(self.spxw, itm_strike, atm_strike, otm_strike, expiry)
price = sum([abs(self.securities[x.symbol].price * x.quantity) * self.multiplier for x in put_butterfly.underlying_legs])
if price > 0:
quantity = self.portfolio.total_portfolio_value // price
quantity = int(self.portfolio.total_portfolio_value // price)
self.tickets = self.buy(put_butterfly, quantity, asynchronous=True)

Loading