Files
quantconnect--lean/Algorithm.CSharp/OptionAssignmentStatisticsRegressionAlgorithm.cs
T
Jhonathan Abreu e29bb2c5e0
API Tests / build (push) Has been cancelled
Benchmarks / build (push) Has been cancelled
Build & Test Lean / build (push) Has been cancelled
Regression Tests / build (push) Has been cancelled
Report Generator Tests / build (push) Has been cancelled
Research Regression Tests / build (push) Has been cancelled
Python Virtual Environments / build (push) Has been cancelled
File-based options universe (#8212)
* Initial options universe with greeks implementation

* Options universe improvements

* Address peer review

* File based options universe fixes and improvements.

- Adjust OptionUniverse start-end times and period.
- Adapt unit tests and some algorithms to pass with new options universe selection.

* Updated options regression algorithms stats for new universe data

* Updated options regression algorithms stats for new universe data

* Updated options regression algorithms stats for new universe data

* Updated options regression algorithms stats for new universe data

* Updated options regression algorithms stats for new universe data

* Option chain provider with new options universe

* Allow canonical option history requests

* Address peer review

* Address peer review

* Fix symbols parsing in OptionUniverse

* Fix universe selection subscriptions start time to not include extended market hours

* Minor changes

* Minor changes

* Peer recommended changes and fixes

* Update regression algorithm stats

* Update regression algorithms stats and minor fixes

* Fix option chain provider history request

* Round option indicators values

* Added option universe csv header property

* Update regression algorithms stats

* Update regression algorithms stats

* Data fixes and regression algos stats update

* Unit test fixes

* Minor changes

* Option chain handling in live trading data feed

* Minor changes

* Added processed data provider

* Fix thread-safety violation in Slice class

* Minor change

* Update options filter universe API to use OptionUniverse data

Add new filter methods for greeks, IV and open interest

* Option filter universe api updates

* Add OptionUniverse history regression algorithms

* Add regression algorithms for new options filter universe api methods

* Added options greeks data and updated regression algorithms

* Address peer review

* Address peer review

* Add more assertions to new options filter api regression algorithms

* Minor performance improvement.

Reduce greeks binomial model steps to 140

* Minor tests updates

* Greeks numerical models performance improvements

* Greeks numerical models performance improvements

* Revert array pool change for option pricing numerical models

* Update default dividend yield provider depending on option type

* [TEST]

* Add helper method con calculate time till expiration

* Use double in price option numerical models

* Implied volatility calculation improvements

- Adjust root finding method accuracy as a factor of the option price
- Use BSM to get a first guess

* Cleanup

* Some regression algorithms and unit tests cleanup

* Regression tests updates after rebasing from master

* Add universe files

* Self review and cleanup

* Minor regression tests updates after rebase

* Fix: set data time zone to same as exchange tz for options universes

* Minor change

* Minor change

* Fix for live trading options universe selection

* Keep underlying when aggregating collections in BaseDataCollectionAggregatorEnumerator

* Update index options regression algorithms stats

* Minor change

* Address peer review

* Memory usage improvements

* Minor build fix

* Minor changes and test fixes

* Cache symbols in OptionUniverse

* Cleanup

* Fix index option creation in OptionUniverse

* Use cached underlying SID when parsing from string

* Abstract symbols cache to BaseDataCollection

* Return actual underlying symbol when mapping decomposing ICO ticker

* Address peer review

* Minor performance improvements reduce garbage

* Limit Symbols and SIDs cache size to help with memory usage

* Minor fix in symbols and sid cache cleanup

* Build fix

* Lazily parse greeks on individual access

* Cleanup and tests

* Address peer review

* Minor greeks fix

---------

Co-authored-by: Martin Molinero <martin.molinero1@gmail.com>
2024-09-09 12:39:31 -03:00

316 lines
13 KiB
C#

/*
* 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 System.Linq;
using QuantConnect.Data;
using QuantConnect.Interfaces;
using QuantConnect.Orders;
using QuantConnect.Securities;
using QuantConnect.Statistics;
namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Regression algorithm asserting final trade statistics for options assignment
///
/// Expected win/loss rate statistics for the regression algorithm:
/// Loss Rate 25%
/// Win Rate 75%
/// </summary>
public class OptionAssignmentStatisticsRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
{
private Security _goog;
private Security _googCall600;
private Symbol _googCall600Symbol;
private Security _googCall650;
private Symbol _googCall650Symbol;
public override void Initialize()
{
SetStartDate(2015, 12, 23);
SetEndDate(2015, 12, 28);
SetCash(100000);
_goog = AddEquity("GOOG", Resolution.Minute);
var contracts = OptionChainProvider.GetOptionContractList(_goog.Symbol, UtcTime).ToList();
_googCall600Symbol = contracts
.Where(c => c.ID.OptionRight == OptionRight.Call)
.OrderBy(c => c.ID.Date)
.First(c => c.ID.StrikePrice == 600m);
_googCall600 = AddOptionContract(_googCall600Symbol);
_googCall600["closed"] = false;
_googCall650Symbol = contracts
.Where(c => c.ID.OptionRight == OptionRight.Call)
.OrderBy(c => c.ID.Date)
.First(c => c.ID.StrikePrice == 650m);
_googCall650 = AddOptionContract(_googCall650Symbol);
_googCall650["closed"] = false;
_googCall650["bought"] = false;
}
public override void OnData(Slice slice)
{
if (_goog.Price == 0 || _googCall600.Price == 0 || _googCall650.Price == 0)
{
return;
}
if (!Portfolio.Invested)
{
if (Time < _googCall600Symbol.ID.Date)
{
// This option assignment is expected to be a losing trade. The option is ITM but the premium paid is higher than the pay off
MarketOrder(_googCall600Symbol, 1);
}
if (Time < _googCall650Symbol.ID.Date && !(bool)_googCall650["bought"])
{
// This option assignment is expected to be a winning trade
LimitOrder(_googCall650Symbol, 1, 0.95m * _googCall650.Price);
// This is to avoid placing another order for this option
_googCall650["bought"] = true;
}
}
else if (_goog.Invested && (bool)_googCall600["closed"] && (bool)_googCall650["closed"])
{
Liquidate(_goog.Symbol);
}
}
public override void OnOrderEvent(OrderEvent orderEvent)
{
if (orderEvent.Status == OrderStatus.Filled && orderEvent.Symbol.SecurityType.IsOption())
{
Securities[orderEvent.Symbol]["closed"] = true;
}
}
public override void OnEndOfAlgorithm()
{
AssertTradeStatistics();
AssertPortfolioStatistics();
}
private void AssertTradeStatistics()
{
var trades = TradeBuilder.ClosedTrades;
if (trades.Count != 4)
{
throw new RegressionTestException($@"AssertTradeStatistics(): Expected 4 closed trades: 2 for the options, 2 for the underlying. Actual: {
trades.Count}");
}
var statistics = new TradeStatistics(trades);
if (statistics.TotalNumberOfTrades != 4)
{
throw new RegressionTestException($@"AssertTradeStatistics(): Expected 4 total trades: 2 for the options, 2 for the underlying. Actual: {
statistics.TotalNumberOfTrades}");
}
if (statistics.NumberOfWinningTrades != 3)
{
throw new RegressionTestException($@"AssertTradeStatistics(): Expected 3 winning trades (the ITM 650 strike option and the underlying trades). Actual {
statistics.NumberOfWinningTrades}");
}
if (statistics.NumberOfLosingTrades != 1)
{
throw new RegressionTestException($@"AssertTradeStatistics(): Expected 1 losing trade (the 600 strike option). Actual {
statistics.NumberOfLosingTrades}");
}
if (statistics.WinRate != 0.75m)
{
throw new RegressionTestException($"AssertTradeStatistics(): Expected win rate to be 0.75. Actual {statistics.WinRate}");
}
if (statistics.LossRate != 0.25m)
{
throw new RegressionTestException($"AssertTradeStatistics(): Expected loss rate to be 0.25. Actual {statistics.LossRate}");
}
if (statistics.WinLossRatio != 3)
{
throw new RegressionTestException($"AssertTradeStatistics(): Expected win-loss ratio to be 3. Actual {statistics.WinLossRatio}");
}
// Let's assert the trades per symbol just to be sure
// We expect the first option (600 strike) to be a losing trade
var googCall600Trade = trades.Where(t => t.Symbol == _googCall600Symbol).FirstOrDefault();
if (googCall600Trade == null)
{
throw new RegressionTestException("AssertTradeStatistics(): Expected a closed trade for the 600 strike option");
}
if (googCall600Trade.IsWin)
{
throw new RegressionTestException("AssertTradeStatistics(): Expected the 600 strike option to be a losing trade");
}
// We expect the second option (650 strike) to be a winning trade
var googCall650Trade = trades.Where(t => t.Symbol == _googCall650Symbol).FirstOrDefault();
if (googCall650Trade == null)
{
throw new RegressionTestException("AssertTradeStatistics(): Expected a closed trade for the 650 strike option");
}
if (!googCall650Trade.IsWin)
{
throw new RegressionTestException("AssertTradeStatistics(): Expected the 650 strike option to be a winning trade");
}
// We expect the both underlying trades to be winning trades
var googTrades = trades.Where(t => t.Symbol == _goog.Symbol).ToList();
if (googTrades.Count != 2)
{
throw new RegressionTestException(
$@"AssertTradeStatistics(): Expected 2 closed trades for the underlying, one for each option assignment. Actual: {
googTrades.Count}");
}
if (googTrades.Any(x => !x.IsWin || x.ProfitLoss < 0))
{
throw new RegressionTestException("AssertTradeStatistics(): Expected both underlying trades to be winning trades");
}
}
private void AssertPortfolioStatistics()
{
// First, let's check the transactions, which are used to build the portfolio statistics
// We expected 2 winning transactions (one of the options assignment and the underlying liquidation)
// and 1 losing transaction (the other option assignment)
if (Transactions.WinCount != 2)
{
throw new RegressionTestException($"AssertPortfolioStatistics(): Expected 2 winning transactions. Actual {Transactions.WinCount}");
}
if (Transactions.LossCount != 1)
{
throw new RegressionTestException($"AssertPortfolioStatistics(): Expected 1 losing transaction. Actual {Transactions.LossCount}");
}
var portfolioStatistics = Statistics.TotalPerformance.PortfolioStatistics;
if (portfolioStatistics.WinRate != 2m / 3m)
{
throw new RegressionTestException($"AssertPortfolioStatistics(): Expected win rate to be 2/3. Actual {portfolioStatistics.WinRate}");
}
if (portfolioStatistics.LossRate != 1m / 3m)
{
throw new RegressionTestException($"AssertPortfolioStatistics(): Expected loss rate to be 1/3. Actual {portfolioStatistics.LossRate}");
}
var expectedAverageWinRate = 0.32962000910479m;
if (!AreEqual(expectedAverageWinRate, portfolioStatistics.AverageWinRate))
{
throw new RegressionTestException($@"AssertPortfolioStatistics(): Expected average win rate to be {expectedAverageWinRate}. Actual {
portfolioStatistics.AverageWinRate}");
}
var expectedAverageLossRate = -0.13556638257576m;
if (!AreEqual(expectedAverageLossRate, portfolioStatistics.AverageLossRate))
{
throw new RegressionTestException($@"AssertPortfolioStatistics(): Expected average loss rate to be {expectedAverageLossRate}. Actual {
portfolioStatistics.AverageLossRate}");
}
var expectedProfitLossRatio = 2.43142881621545m;
if (!AreEqual(expectedProfitLossRatio, portfolioStatistics.ProfitLossRatio))
{
throw new RegressionTestException($@"AssertPortfolioStatistics(): Expected profit loss ratio to be {expectedProfitLossRatio}. Actual {
portfolioStatistics.ProfitLossRatio}");
}
var totalNetProfit = -0.00697m;
if (!AreEqual(totalNetProfit, portfolioStatistics.TotalNetProfit))
{
throw new RegressionTestException($@"AssertPortfolioStatistics(): Expected total net profit to be {totalNetProfit}. Actual {
portfolioStatistics.TotalNetProfit}");
}
}
private static bool AreEqual(decimal expected, decimal actual)
{
return Math.Abs(expected - actual) < 1e-12m;
}
/// <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 virtual List<Language> Languages { get; } = new() { Language.CSharp };
/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public long DataPoints => 4358;
/// <summary>
/// Data Points count of the algorithm history
/// </summary>
public int AlgorithmHistoryDataPoints => 1;
/// <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", "6"},
{"Average Win", "32.96%"},
{"Average Loss", "-13.56%"},
{"Compounding Annual Return", "-36.270%"},
{"Drawdown", "1.400%"},
{"Expectancy", "1.288"},
{"Start Equity", "100000"},
{"End Equity", "99303"},
{"Net Profit", "-0.697%"},
{"Sharpe Ratio", "-8.675"},
{"Sortino Ratio", "-6.769"},
{"Probabilistic Sharpe Ratio", "0.012%"},
{"Loss Rate", "33%"},
{"Win Rate", "67%"},
{"Profit-Loss Ratio", "2.43"},
{"Alpha", "-0.011"},
{"Beta", "0.825"},
{"Annual Standard Deviation", "0.02"},
{"Annual Variance", "0"},
{"Information Ratio", "1.705"},
{"Tracking Error", "0.014"},
{"Treynor Ratio", "-0.207"},
{"Total Fees", "$3.00"},
{"Estimated Strategy Capacity", "$0"},
{"Lowest Capacity Asset", "GOOCV VP83T1ZUHROL"},
{"Portfolio Turnover", "50.31%"},
{"OrderListHash", "c8fafa07bb56dc1a253690449787df54"}
};
}
}