41a730bc68
Research Regression Tests / build (push) Has been cancelled
Python Virtual Environments / 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
* Set fill price to zero in OTM exercise orders. Improved the OTM exercise orders message. * Update regression algorithms and unit tests * Add IsInTheMoney property to OrderEvent * Update SerializedOrderEvent * Properly setting the option exercise order price to strike price or zero * Minor changes * Minor changes
232 lines
9.3 KiB
C#
232 lines
9.3 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 System.Reflection;
|
|
using QuantConnect.Data;
|
|
using QuantConnect.Interfaces;
|
|
using QuantConnect.Orders;
|
|
using QuantConnect.Securities;
|
|
|
|
namespace QuantConnect.Algorithm.CSharp
|
|
{
|
|
/// <summary>
|
|
/// This regression algorithm tests In The Money (ITM) index option expiry for short calls.
|
|
/// We expect 2 orders from the algorithm, which are:
|
|
///
|
|
/// * Initial entry, sell SPX Call Option (expiring ITM)
|
|
/// * Option assignment
|
|
///
|
|
/// Additionally, we test delistings for index options and assert that our
|
|
/// portfolio holdings reflect the orders the algorithm has submitted.
|
|
/// </summary>
|
|
public class IndexOptionShortCallITMExpiryRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
|
|
{
|
|
private Symbol _spx;
|
|
private Symbol _esOption;
|
|
private Symbol _expectedContract;
|
|
|
|
public override void Initialize()
|
|
{
|
|
SetStartDate(2021, 1, 4);
|
|
SetEndDate(2021, 1, 31);
|
|
|
|
_spx = AddIndex("SPX", Resolution.Minute).Symbol;
|
|
|
|
// Select a index option expiring ITM, and adds it to the algorithm.
|
|
_esOption = AddIndexOptionContract(OptionChainProvider.GetOptionContractList(_spx, Time)
|
|
.Where(x => x.ID.StrikePrice <= 3200m && x.ID.OptionRight == OptionRight.Call && x.ID.Date.Year == 2021 && x.ID.Date.Month == 1)
|
|
.OrderByDescending(x => x.ID.StrikePrice)
|
|
.Take(1)
|
|
.Single(), Resolution.Minute).Symbol;
|
|
|
|
_expectedContract = QuantConnect.Symbol.CreateOption(_spx, Market.USA, OptionStyle.European, OptionRight.Call, 3200m, new DateTime(2021, 1, 15));
|
|
if (_esOption != _expectedContract)
|
|
{
|
|
throw new Exception($"Contract {_expectedContract} was not found in the chain");
|
|
}
|
|
|
|
Schedule.On(DateRules.Tomorrow, TimeRules.AfterMarketOpen(_spx, 1), () =>
|
|
{
|
|
MarketOrder(_esOption, -1);
|
|
});
|
|
}
|
|
|
|
public override void OnData(Slice data)
|
|
{
|
|
// Assert delistings, so that we can make sure that we receive the delisting warnings at
|
|
// the expected time. These assertions detect bug #4872
|
|
foreach (var delisting in data.Delistings.Values)
|
|
{
|
|
if (delisting.Type == DelistingType.Warning)
|
|
{
|
|
if (delisting.Time != new DateTime(2021, 1, 15))
|
|
{
|
|
throw new Exception($"Delisting warning issued at unexpected date: {delisting.Time}");
|
|
}
|
|
}
|
|
if (delisting.Type == DelistingType.Delisted)
|
|
{
|
|
if (delisting.Time != new DateTime(2021, 1, 16))
|
|
{
|
|
throw new Exception($"Delisting happened at unexpected date: {delisting.Time}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void OnOrderEvent(OrderEvent orderEvent)
|
|
{
|
|
if (orderEvent.Status != OrderStatus.Filled)
|
|
{
|
|
// There's lots of noise with OnOrderEvent, but we're only interested in fills.
|
|
return;
|
|
}
|
|
|
|
if (!Securities.ContainsKey(orderEvent.Symbol))
|
|
{
|
|
throw new Exception($"Order event Symbol not found in Securities collection: {orderEvent.Symbol}");
|
|
}
|
|
|
|
var security = Securities[orderEvent.Symbol];
|
|
if (security.Symbol == _spx)
|
|
{
|
|
AssertIndexOptionOrderExercise(orderEvent, security, Securities[_expectedContract]);
|
|
}
|
|
else if (security.Symbol == _expectedContract)
|
|
{
|
|
AssertIndexOptionContractOrder(orderEvent, security);
|
|
}
|
|
else
|
|
{
|
|
throw new Exception($"Received order event for unknown Symbol: {orderEvent.Symbol}");
|
|
}
|
|
|
|
Log($"{orderEvent}");
|
|
}
|
|
|
|
private void AssertIndexOptionOrderExercise(OrderEvent orderEvent, Security index, Security optionContract)
|
|
{
|
|
if (orderEvent.Message.Contains("Assignment"))
|
|
{
|
|
if (orderEvent.FillPrice != 3200m)
|
|
{
|
|
throw new Exception("Option was not assigned at expected strike price (3200)");
|
|
}
|
|
if (orderEvent.Direction != OrderDirection.Sell || index.Holdings.Quantity != 0)
|
|
{
|
|
throw new Exception($"Expected Qty: 0 index holdings for assigned index option {index.Symbol}, found {index.Holdings.Quantity}");
|
|
}
|
|
}
|
|
}
|
|
|
|
private void AssertIndexOptionContractOrder(OrderEvent orderEvent, Security option)
|
|
{
|
|
if (orderEvent.Direction == OrderDirection.Sell && option.Holdings.Quantity != -1)
|
|
{
|
|
throw new Exception($"No holdings were created for option contract {option.Symbol}");
|
|
}
|
|
if (orderEvent.IsAssignment && option.Holdings.Quantity != 0)
|
|
{
|
|
throw new Exception($"Holdings were found after option contract was assigned: {option.Symbol}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ran at the end of the algorithm to ensure the algorithm has no holdings
|
|
/// </summary>
|
|
/// <exception cref="Exception">The algorithm has holdings</exception>
|
|
public override void OnEndOfAlgorithm()
|
|
{
|
|
if (Portfolio.Invested)
|
|
{
|
|
throw new Exception($"Expected no holdings at end of algorithm, but are invested in: {string.Join(", ", Portfolio.Keys)}");
|
|
}
|
|
}
|
|
|
|
/// <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 Language[] Languages { get; } = { Language.CSharp, Language.Python };
|
|
|
|
/// <summary>
|
|
/// Data Points count of all timeslices of algorithm
|
|
/// </summary>
|
|
public long DataPoints => 20443;
|
|
|
|
/// <summary>
|
|
/// Data Points count of the algorithm history
|
|
/// </summary>
|
|
public int AlgorithmHistoryDataPoints => 0;
|
|
|
|
/// <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 Trades", "2"},
|
|
{"Average Win", "48.82%"},
|
|
{"Average Loss", "0%"},
|
|
{"Compounding Annual Return", "-80.089%"},
|
|
{"Drawdown", "13.400%"},
|
|
{"Expectancy", "0"},
|
|
{"Net Profit", "-10.734%"},
|
|
{"Sharpe Ratio", "-1.921"},
|
|
{"Probabilistic Sharpe Ratio", "0.511%"},
|
|
{"Loss Rate", "0%"},
|
|
{"Win Rate", "100%"},
|
|
{"Profit-Loss Ratio", "0"},
|
|
{"Alpha", "-0.671"},
|
|
{"Beta", "0.211"},
|
|
{"Annual Standard Deviation", "0.344"},
|
|
{"Annual Variance", "0.118"},
|
|
{"Information Ratio", "-1.963"},
|
|
{"Tracking Error", "0.36"},
|
|
{"Treynor Ratio", "-3.133"},
|
|
{"Total Fees", "$0.00"},
|
|
{"Estimated Strategy Capacity", "$0"},
|
|
{"Lowest Capacity Asset", "SPX XL80P3GHDZXQ|SPX 31"},
|
|
{"Fitness Score", "0.005"},
|
|
{"Kelly Criterion Estimate", "0"},
|
|
{"Kelly Criterion Probability Value", "0"},
|
|
{"Sortino Ratio", "-0.676"},
|
|
{"Return Over Maximum Drawdown", "-7.289"},
|
|
{"Portfolio Turnover", "0.024"},
|
|
{"Total Insights Generated", "0"},
|
|
{"Total Insights Closed", "0"},
|
|
{"Total Insights Analysis Completed", "0"},
|
|
{"Long Insight Count", "0"},
|
|
{"Short Insight Count", "0"},
|
|
{"Long/Short Ratio", "100%"},
|
|
{"Estimated Monthly Alpha Value", "$0"},
|
|
{"Total Accumulated Estimated Alpha Value", "$0"},
|
|
{"Mean Population Estimated Insight Value", "$0"},
|
|
{"Mean Population Direction", "0%"},
|
|
{"Mean Population Magnitude", "0%"},
|
|
{"Rolling Averaged Population Direction", "0%"},
|
|
{"Rolling Averaged Population Magnitude", "0%"},
|
|
{"OrderListHash", "e0c73c8f7b4769aec2fdc714e899cb25"}
|
|
};
|
|
}
|
|
}
|
|
|