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
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public override void OnEndOfAlgorithm()
/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public long DataPoints => 1658168;
public long DataPoints => 2349547;

/// <summary>
/// Data Points count of the algorithm history
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public override void Initialize()
_optionSymbol = option.Symbol;

// set our strike/expiry filter for this option chain
option.SetFilter(u => u.Strikes(-2, +2)
option.SetFilter(u => u.StandardsOnly().Strikes(-2, +2)
// Expiration method accepts TimeSpan objects or integer for days.
// The following statements yield the same filtering criteria
.Expiration(0, 180));
Expand Down Expand Up @@ -73,7 +73,7 @@ public override void OnData(Slice slice)
var higherStrike = callContracts[2].Strike;

var optionStrategy = OptionStrategies.CallButterfly(_optionSymbol, higherStrike, middleStrike, lowerStrike, expiry);

Order(optionStrategy, 10);
}
}
Expand Down
7 changes: 4 additions & 3 deletions Algorithm.CSharp/BasicTemplateOptionStrategyAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ public override void Initialize()
// set our strike/expiry filter for this option chain
// SetFilter method accepts TimeSpan objects or integer for days.
// The following statements yield the same filtering criteria
option.SetFilter(-2, +2, 0, 180);
// option.SetFilter(-2, +2, TimeSpan.Zero, TimeSpan.FromDays(180));
option.SetFilter(u => u.StandardsOnly()
.Strikes(-2, +2)
.Expiration(0, 180));

// Adding this to reproduce GH issue #2314
SetWarmup(TimeSpan.FromMinutes(1));
Expand Down Expand Up @@ -83,7 +84,7 @@ public override void OnData(Slice slice)
Liquidate();
}

foreach(var kpv in slice.Bars)
foreach (var kpv in slice.Bars)
{
Log($"---> OnData: {Time}, {kpv.Key.Value}, {kpv.Value.Close:0.00}");
}
Expand Down
5 changes: 2 additions & 3 deletions Algorithm.CSharp/BasicTemplateOptionsAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ public override void Initialize()
_optionSymbol = option.Symbol;

// set our strike/expiry filter for this option chain
option.SetFilter(u => u.Strikes(-2, +2)
option.SetFilter(u => u.StandardsOnly().Strikes(-2, +2)
// Expiration method accepts TimeSpan objects or integer for days.
// The following statements yield the same filtering criteria
.Expiration(0, 180));
// .Expiration(TimeSpan.Zero, TimeSpan.FromDays(180)));
.Expiration(0, 180)); // .Expiration(TimeSpan.Zero, TimeSpan.FromDays(180)));

// use the underlying equity as the benchmark
SetBenchmark(equity.Symbol);
Expand Down
2 changes: 1 addition & 1 deletion Algorithm.CSharp/BasicTemplateOptionsHourlyAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public override void Initialize()
_optionSymbol = option.Symbol;

// set our strike/expiry filter for this option chain
option.SetFilter(u => u.Strikes(-2, +2)
option.SetFilter(u => u.StandardsOnly().Strikes(-2, +2)
// Expiration method accepts TimeSpan objects or integer for days.
// The following statements yield the same filtering criteria
.Expiration(0, 180));
Expand Down
2 changes: 1 addition & 1 deletion Algorithm.CSharp/ComboOrderAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public override void Initialize()
var option = AddOption(equity.Symbol, fillForward: true);
_optionSymbol = option.Symbol;

option.SetFilter(u => u.Strikes(-2, +2)
option.SetFilter(u => u.StandardsOnly().Strikes(-2, +2)
.Expiration(0, 180));
}

Expand Down
8 changes: 3 additions & 5 deletions Algorithm.CSharp/ComboOrderTicketDemoAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override void Initialize()
var option = AddOption(equity.Symbol, fillForward: true);
_optionSymbol = option.Symbol;

option.SetFilter(u => u.Strikes(-2, +2)
option.SetFilter(u => u.StandardsOnly().Strikes(-2, +2)
.Expiration(0, 180));
}

Expand Down Expand Up @@ -243,8 +243,7 @@ public override void OnOrderEvent(OrderEvent orderEvent)
}
if (orderEvent.Quantity != order.Quantity)
{
throw new RegressionTestException($@"OrderEvent quantity should hold the current order Quantity. Got {orderEvent.Quantity
}, expected {order.Quantity}");
throw new RegressionTestException($@"OrderEvent quantity should hold the current order Quantity. Got {orderEvent.Quantity}, expected {order.Quantity}");
}
if (order is ComboLegLimitOrder && orderEvent.LimitPrice == 0)
{
Expand Down Expand Up @@ -303,8 +302,7 @@ public override void OnEndOfAlgorithm()
{
throw new RegressionTestException(
"There were expected 6 filled market orders, 3 filled combo limit orders and 6 filled combo leg limit orders, " +
$@"but there were {filledComboMarketOrders.Count} filled market orders, {filledComboLimitOrders.Count
} filled combo limit orders and {filledComboLegLimitOrders.Count} filled combo leg limit orders");
$@"but there were {filledComboMarketOrders.Count} filled market orders, {filledComboLimitOrders.Count} filled combo limit orders and {filledComboLegLimitOrders.Count} filled combo leg limit orders");
}

if (openOrders.Count != 0 || openOrderTickets.Count != 0)
Expand Down
4 changes: 2 additions & 2 deletions Algorithm.CSharp/CoveredCallComboLimitOrderAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public override void Initialize()
var option = AddOption(equity.Symbol);
_optionSymbol = option.Symbol;

option.SetFilter(u => u.Strikes(-1, +1).Expiration(0, 30));
option.SetFilter(u => u.StandardsOnly().Strikes(-1, +1).Expiration(0, 30));
}
/// <summary>
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
Expand All @@ -70,7 +70,7 @@ public override void OnData(Slice slice)
var legs = new List<Leg> { Leg.Create(atmContract.Symbol, -1), Leg.Create(atmContract.Symbol.Underlying, 100) };

var comboPrice = underlyingPrice - optionPrice;
if(comboPrice < 734m)
if (comboPrice < 734m)
{
// just to make sure the price makes sense
throw new RegressionTestException($"Unexpected combo price {comboPrice}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override void Initialize()
SetEndDate(2014, 07, 06);

var option = AddOption("AAPL", Resolution.Daily);
option.SetFilter(-5, +5, 0, 365);
option.SetFilter(u => u.StandardsOnly().Strikes(-5, +5).Expiration(0, 365));

_symbol = option.Symbol;
}
Expand Down
2 changes: 1 addition & 1 deletion Algorithm.CSharp/LargeQuantityOptionStrategyAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public override void Initialize()
var option = AddOption("GOOG");
_optionSymbol = option.Symbol;

option.SetFilter(-2, +2, 0, 180);
option.SetFilter(u => u.StandardsOnly().Strikes(-2, +2).Expiration(0, 180));
}

public override void OnData(Slice slice)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public override void Initialize()
SetCash(500000);

var option = AddOption("GOOG");
option.SetFilter(universe => universe.Strikes(-3, 3).Expiration(0, 180));
option.SetFilter(universe => universe.StandardsOnly().Strikes(-3, 3).Expiration(0, 180));

_symbol = option.Symbol;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public override void Initialize()
var option = AddOption("GOOG");
_optionSymbol = option.Symbol;

option.SetFilter(-2, +2, 0, 180);
option.SetFilter(u => u.StandardsOnly().Strikes(-2, +2).Expiration(0, 180));

SetBenchmark("GOOG");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public override void Initialize()
var option = AddOption(UnderlyingTicker);
_optionSymbol = option.Symbol;

option.SetFilter(u => u.Strikes(-2, +2).Expiration(0, 180 * 3));
option.SetFilter(u => u.StandardsOnly().Strikes(-2, +2).Expiration(0, 180 * 3));

SetBenchmark(equity.Symbol);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override void Initialize()
var option = AddOption(equity.Symbol);
_optionSymbol = option.Symbol;

option.SetFilter(-2, +2, 0, 180);
option.SetFilter(u => u.StandardsOnly().Strikes(-2, +2).Expiration(0, 180));
}

public override void OnData(Slice slice)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public override void Initialize()
var option = AddOption(equity.Symbol);
OptionSymbol = option.Symbol;

option.SetFilter(u => u.Strikes(-2, +2).Expiration(0, 180));
option.SetFilter(u => u.StandardsOnly().Strikes(-2, +2).Expiration(0, 180));
}

protected virtual void OverrideMarginModels()
Expand Down Expand Up @@ -75,7 +75,7 @@ public override void OnData(Slice slice)
.ThenBy(x => x.Strike)
.First();

if(!_placedTrades)
if (!_placedTrades)
{
_placedTrades = true;
PlaceTrades(atmContracts);
Expand All @@ -100,7 +100,7 @@ protected virtual void AssertState(OrderTicket ticket, int expectedGroupCount, i
{
throw new RegressionTestException($"Unexpected position group count {Portfolio.Positions.Groups.Count} for symbol {ticket.Symbol} and quantity {ticket.Quantity}");
}
if(Portfolio.TotalMarginUsed != expectedMarginUsed)
if (Portfolio.TotalMarginUsed != expectedMarginUsed)
{
throw new RegressionTestException($"Unexpected margin used {expectedMarginUsed}");
}
Expand Down
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 QuantConnect.Data;
using QuantConnect.Data.Market;
using QuantConnect.Interfaces;
using QuantConnect.Securities.Option;
using System.Collections.Generic;

namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Verifies that weekly option contracts are included when no standard contracts are available.
/// </summary>
public class OptionChainIncludeWeeklysByDefaultRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
{
private Option _option;
private Symbol _optionSymbol;
private int _weeklyCount;
private int _totalCount;

/// <summary>
/// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
/// </summary>
public override void Initialize()
{
SetStartDate(2015, 12, 24);
SetEndDate(2015, 12, 24);

_option = AddOption("GOOG");
_optionSymbol = _option.Symbol;

_option.SetFilter((optionFilter) =>
{
return optionFilter.Strikes(-8, +8).Expiration(0, 0);
});
}

/// <summary>
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
/// </summary>
/// <param name="data">Slice object keyed by symbol containing the stock data</param>
public override void OnData(Slice slice)
{
OptionChain chain;
if (slice.OptionChains.TryGetValue(_optionSymbol, out chain))
{
_totalCount += chain.Contracts.Count;
foreach (var contract in chain.Contracts.Values)
{
if (!OptionSymbol.IsStandard(contract.Symbol))
{
_weeklyCount++;
}
}
}
}

public override void OnEndOfAlgorithm()
{
if (_weeklyCount == 0)
{
throw new RegressionTestException("No weekly contracts found");
}
if (_totalCount != _weeklyCount)
{
throw new RegressionTestException("When no standard option expirations are available, the option chain must fall back to weekly contracts only");
}
}

/// <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 => 22702;

/// <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"}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public TestOptionUniverseSelectionModel(Func<DateTime, IEnumerable<Symbol>> opti

protected override OptionFilterUniverse Filter(OptionFilterUniverse filter)
{
return filter.BackMonth().Contracts(contracts => contracts.Take(15));
return filter.StandardsOnly().BackMonth().Contracts(contracts => contracts.Take(15));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public override void Initialize()
SetStartDate(2014, 06, 05);
SetEndDate(2014, 06, 09);

_aaplOption = AddOption("AAPL").Symbol;
var option = AddOption("AAPL");
option.SetFilter(u => u.StandardsOnly().Strikes(-1, 1).Expiration(0, 35));
_aaplOption = option.Symbol;
AddUniverseSelection(new DailyUniverseSelectionModel("MyCustomSelectionModel", time => new[] { "AAPL" }, this));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace QuantConnect.Algorithm.CSharp
/// <summary>
/// Regression algorithm to test the OptionChainedUniverseSelectionModel class
/// </summary>
public class OptionChainedUniverseSelectionModelRegressionAlgorithm: QCAlgorithm, IRegressionAlgorithmDefinition
public class OptionChainedUniverseSelectionModelRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
{
public override void Initialize()
{
Expand All @@ -39,7 +39,7 @@ public override void Initialize()

var universe = AddUniverse("my-minute-universe-name", time => new List<string> { "AAPL", "TWX" });

AddUniverseSelection(new OptionChainedUniverseSelectionModel(universe, u => u.Strikes(-2, +2)
AddUniverseSelection(new OptionChainedUniverseSelectionModel(universe, u => u.StandardsOnly().Strikes(-2, +2)
// Expiration method accepts TimeSpan objects or integer for days.
// The following statements yield the same filtering criteria
.Expiration(0, 180)));
Expand Down
Loading
Loading