cf9b547e2e
* Improve information tracked in regression's {algorithm}.{lang}.details.log
The details.log file aims at providing a diff-able document that quickly and
easily provides actionable information. Since many regression algorithms use
the algorithm's debug/error messaging facilities to log various pieces of algo
state. This document also support a configuration option: regression-high-fidelity-logging'
that logs EVERY piece of data, again, with the aim of providing an easily diff-able
documenbt to quickly highlight actionable information. I may have missed omse key
pieces of information here, but now that the entire QC knows about this regression
tool, if additional information is required then hopefully it's easy enough at this
point to extend the RegressionResultHandler to suit our needs.
The RegressionResultHandler was initially implemented to provide a concise log of
all orders. This was achieved by simply using the Order.ToString method. While
testing/investigating OptionExerciseOrder behavior, it became evident that more
information was required to properly identify the source of potential failures or
differences between previous regression test runs. This change adds logging for
almost every IResultHandler method and additionally attempts to capture the
actual portfolio impact of every OrderEvent. This is accomplished by logging
the portfolio's TotalPortfolioValue, Cash properties and the security's
SecurityHolding.Quantity property.
This change also standardizes the timestamps used to folloow the ISO-8601 format.
When using the RegressionResultHandler, it is highly recommeded to also disable
'forward-console-message' configuration option to ensure algorithm Debug/Error
message logging is done synchronously to ensure correct ordering with respect to
log messages via Log.Debug/Trace/Error.
* Fix typo in options OrderTests test case name
* Update SymbolRepresentation.GenerationOptionTickerOSI to extension method
Far more convenient as an extension method
* Improve R# default code formatting rules
Many of these rule changes focus on improving the readability of code,
with a particular emphasis on multi-line constructs, chained method calls
and multi-line method invocations/declarations.
* Add braces, use string interpolation and limit long lines
* Refactor OptionExerciseOrder.Quantity to indicate change in #contracts
For all other order types, the Order.Quantity indicates the change in the algorithm's
holdings upon order execution for the order's symbol. For OptionExerciseOrder, this
convention was broken. It appears as though only exercise was initially implemented,
in which case only long positions were supported and a code comment indicated that
only positive values of quantity were acceptable, indicating the number of contracts
to exercise. At a later date, assignment simulation was added and utilized a negative
order quantity. This caused some major inconsistencies in how models view exercise
orders compared to all other order types. This change brings OptionExerciseOrder.Quantity
into alignment with the other order types by making it represent the change in holdings
quantity upon order execution.
This change was originally going to be much larger, but in order to minimize risks and to
make for an easier review experience, the additional changes will be committed separately
and pushed in their own PR. Some of the issues identified include:
* Manual Exercise (especially for OTM) is not covered
* Margin Calculations (in particular taking into account opposing contracts held)
* IBrokerage.OptionPositionAssigned is raised for exercise (later filtered by tx handler)
Fixes OptionPortfolioModelTests to use exercise model to properly model exercise of
non-account quote currency option contract.
304 lines
14 KiB
C#
304 lines
14 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.Securities.Future;
|
|
using static QuantConnect.StringExtensions;
|
|
|
|
namespace QuantConnect
|
|
{
|
|
/// <summary>
|
|
/// Public static helper class that does parsing/generation of symbol representations (options, futures)
|
|
/// </summary>
|
|
public static class SymbolRepresentation
|
|
{
|
|
/// <summary>
|
|
/// Class contains future ticker properties returned by ParseFutureTicker()
|
|
/// </summary>
|
|
public class FutureTickerProperties
|
|
{
|
|
/// <summary>
|
|
/// Underlying name
|
|
/// </summary>
|
|
public string Underlying { get; set; }
|
|
|
|
/// <summary>
|
|
/// Short expiration year
|
|
/// </summary>
|
|
public int ExpirationYearShort { get; set; }
|
|
|
|
/// <summary>
|
|
/// Expiration month
|
|
/// </summary>
|
|
public int ExpirationMonth { get; set; }
|
|
|
|
/// <summary>
|
|
/// Expiration day
|
|
/// </summary>
|
|
public int ExpirationDay { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Class contains option ticker properties returned by ParseOptionTickerIQFeed()
|
|
/// </summary>
|
|
public class OptionTickerProperties
|
|
{
|
|
/// <summary>
|
|
/// Underlying name
|
|
/// </summary>
|
|
public string Underlying { get; set; }
|
|
|
|
/// <summary>
|
|
/// Option right
|
|
/// </summary>
|
|
public OptionRight OptionRight { get; set; }
|
|
|
|
/// <summary>
|
|
/// Option strike
|
|
/// </summary>
|
|
public decimal OptionStrike { get; set; }
|
|
|
|
/// <summary>
|
|
/// Expiration date
|
|
/// </summary>
|
|
public DateTime ExpirationDate { get; set; }
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Function returns underlying name, expiration year, expiration month, expiration day for the future contract ticker. Function detects if
|
|
/// the format used is either 1 or 2 digits year, and if day code is present (will default to 1rst day of month). Returns null, if parsing failed.
|
|
/// Format [Ticker][2 digit day code OPTIONAL][1 char month code][2/1 digit year code]
|
|
/// </summary>
|
|
/// <param name="ticker"></param>
|
|
/// <returns>Results containing 1) underlying name, 2) short expiration year, 3) expiration month</returns>
|
|
public static FutureTickerProperties ParseFutureTicker(string ticker)
|
|
{
|
|
var doubleDigitYear = char.IsDigit(ticker.Substring(ticker.Length - 2, 1)[0]);
|
|
var doubleDigitOffset = doubleDigitYear ? 1 : 0;
|
|
|
|
var expirationDayOffset = 0;
|
|
var expirationDay = 1;
|
|
if (ticker.Length > 4 + doubleDigitOffset)
|
|
{
|
|
var potentialExpirationDay = ticker.Substring(ticker.Length - 4 - doubleDigitOffset, 2);
|
|
var containsExpirationDay = char.IsDigit(potentialExpirationDay[0]) && char.IsDigit(potentialExpirationDay[1]);
|
|
expirationDayOffset = containsExpirationDay ? 2 : 0;
|
|
if (containsExpirationDay && !int.TryParse(potentialExpirationDay, out expirationDay))
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
var expirationYearString = ticker.Substring(ticker.Length - 1 - doubleDigitOffset, 1 + doubleDigitOffset);
|
|
var expirationMonthString = ticker.Substring(ticker.Length - 2 - doubleDigitOffset, 1);
|
|
var underlyingString = ticker.Substring(0, ticker.Length - 2 - doubleDigitOffset - expirationDayOffset);
|
|
|
|
int expirationYearShort;
|
|
|
|
if (!int.TryParse(expirationYearString, out expirationYearShort))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (!_futuresMonthCodeLookup.ContainsKey(expirationMonthString))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var expirationMonth = _futuresMonthCodeLookup[expirationMonthString];
|
|
|
|
return new FutureTickerProperties
|
|
{
|
|
Underlying = underlyingString,
|
|
ExpirationYearShort = expirationYearShort,
|
|
ExpirationMonth = expirationMonth,
|
|
ExpirationDay = expirationDay
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns future symbol ticker from underlying and expiration date. Function can generate tickers of two formats: one and two digits year.
|
|
/// Format [Ticker][2 digit day code][1 char month code][2/1 digit year code], more information at http://help.tradestation.com/09_01/tradestationhelp/symbology/futures_symbology.htm
|
|
/// </summary>
|
|
/// <param name="underlying">String underlying</param>
|
|
/// <param name="expiration">Expiration date</param>
|
|
/// <param name="doubleDigitsYear">True if year should represented by two digits; False - one digit</param>
|
|
/// <returns></returns>
|
|
public static string GenerateFutureTicker(string underlying, DateTime expiration, bool doubleDigitsYear = true)
|
|
{
|
|
var year = doubleDigitsYear ? expiration.Year % 100 : expiration.Year % 10;
|
|
var month = expiration.Month;
|
|
|
|
// These futures expire in the month before the contract month
|
|
month += FuturesExpiryUtilityFunctions.ExpiresInPreviousMonth(underlying);
|
|
// Get the month back into the allowable range, allowing for a wrap
|
|
// Below is a little algorithm for wrapping numbers with a certain bounds.
|
|
// In this case, were dealing with months, wrapping to years once we get to January
|
|
// As modulo works for [0, x), it's best to subtract 1 (as months are [1, 12] to convert to [0, 11]),
|
|
// do the modulo/integer division, then add 1 back on to get into the correct range again
|
|
month--;
|
|
year += month / 12;
|
|
month %= 12;
|
|
month++;
|
|
|
|
return $"{underlying}{expiration.Day:00}{_futuresMonthLookup[month]}{year}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns option symbol ticker in accordance with OSI symbology
|
|
/// More information can be found at http://www.optionsclearing.com/components/docs/initiatives/symbology/symbology_initiative_v1_8.pdf
|
|
/// </summary>
|
|
/// <param name="symbol">Symbol object to create OSI ticker from</param>
|
|
/// <returns>The OSI ticker representation</returns>
|
|
public static string GenerateOptionTickerOSI(this Symbol symbol)
|
|
{
|
|
if (symbol.SecurityType != SecurityType.Option)
|
|
{
|
|
throw new ArgumentException(Invariant($"{nameof(GenerateOptionTickerOSI)} returns symbol to be an option, received {symbol.SecurityType}."));
|
|
}
|
|
|
|
return GenerateOptionTickerOSI(symbol.Underlying.Value, symbol.ID.OptionRight, symbol.ID.StrikePrice, symbol.ID.Date);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns option symbol ticker in accordance with OSI symbology
|
|
/// More information can be found at http://www.optionsclearing.com/components/docs/initiatives/symbology/symbology_initiative_v1_8.pdf
|
|
/// </summary>
|
|
/// <param name="underlying">Underlying string</param>
|
|
/// <param name="right">Option right</param>
|
|
/// <param name="strikePrice">Option strike</param>
|
|
/// <param name="expiration">Option expiration date</param>
|
|
/// <returns>The OSI ticker representation</returns>
|
|
public static string GenerateOptionTickerOSI(string underlying, OptionRight right, decimal strikePrice, DateTime expiration)
|
|
{
|
|
if (underlying.Length > 5) underlying += " ";
|
|
return Invariant($"{underlying,-6}{expiration.ToStringInvariant(DateFormat.SixCharacter)}{right.ToStringPerformance()[0]}{(strikePrice * 1000m):00000000}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Parses the specified OSI options ticker into a Symbol object
|
|
/// </summary>
|
|
/// <param name="ticker">The OSI compliant option ticker string</param>
|
|
/// <returns>Symbol object for the specified OSI option ticker string</returns>
|
|
public static Symbol ParseOptionTickerOSI(string ticker)
|
|
{
|
|
var underlying = ticker.Substring(0, 6).Trim();
|
|
var expiration = DateTime.ParseExact(ticker.Substring(6, 6), DateFormat.SixCharacter, null);
|
|
OptionRight right;
|
|
if (ticker[12] == 'C') right = OptionRight.Call;
|
|
else if (ticker[12] == 'P') right = OptionRight.Put;
|
|
else throw new FormatException($"Expected 12th character to be 'C' or 'P' for OptionRight: {ticker}");
|
|
var strike = Parse.Decimal(ticker.Substring(13, 8)) / 1000m;
|
|
var underlyingSid = SecurityIdentifier.GenerateEquity(underlying, Market.USA);
|
|
var sid = SecurityIdentifier.GenerateOption(expiration, underlyingSid, Market.USA, strike, right, OptionStyle.American);
|
|
return new Symbol(sid, ticker, new Symbol(underlyingSid, underlying));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Function returns option contract parameters (underlying name, expiration date, strike, right) from IQFeed option ticker
|
|
/// Symbology details: http://www.iqfeed.net/symbolguide/index.cfm?symbolguide=guide&displayaction=support%C2%A7ion=guide&web=iqfeed&guide=options&web=IQFeed&type=stock
|
|
/// </summary>
|
|
/// <param name="ticker">IQFeed option ticker</param>
|
|
/// <returns>Results containing 1) underlying name, 2) option right, 3) option strike 4) expiration date</returns>
|
|
public static OptionTickerProperties ParseOptionTickerIQFeed(string ticker)
|
|
{
|
|
// This table describes IQFeed option symbology
|
|
var symbology = new Dictionary<string, Tuple<int, OptionRight>>
|
|
{
|
|
{ "A", Tuple.Create(1, OptionRight.Call) }, { "M", Tuple.Create(1, OptionRight.Put) },
|
|
{ "B", Tuple.Create(2, OptionRight.Call) }, { "N", Tuple.Create(2, OptionRight.Put) },
|
|
{ "C", Tuple.Create(3, OptionRight.Call) }, { "O", Tuple.Create(3, OptionRight.Put) },
|
|
{ "D", Tuple.Create(4, OptionRight.Call) }, { "P", Tuple.Create(4, OptionRight.Put) },
|
|
{ "E", Tuple.Create(5, OptionRight.Call) }, { "Q", Tuple.Create(5, OptionRight.Put) },
|
|
{ "F", Tuple.Create(6, OptionRight.Call) }, { "R", Tuple.Create(6, OptionRight.Put) },
|
|
{ "G", Tuple.Create(7, OptionRight.Call) }, { "S", Tuple.Create(7, OptionRight.Put) },
|
|
{ "H", Tuple.Create(8, OptionRight.Call) }, { "T", Tuple.Create(8, OptionRight.Put) },
|
|
{ "I", Tuple.Create(9, OptionRight.Call) }, { "U", Tuple.Create(9, OptionRight.Put) },
|
|
{ "J", Tuple.Create(10, OptionRight.Call) }, { "V", Tuple.Create(10, OptionRight.Put) },
|
|
{ "K", Tuple.Create(11, OptionRight.Call) }, { "W", Tuple.Create(11, OptionRight.Put) },
|
|
{ "L", Tuple.Create(12, OptionRight.Call) }, { "X", Tuple.Create(12, OptionRight.Put) },
|
|
|
|
};
|
|
|
|
var letterRange = symbology.Keys
|
|
.Select(x => x[0])
|
|
.ToArray();
|
|
var optionTypeDelimiter = ticker.LastIndexOfAny(letterRange);
|
|
var strikePriceString = ticker.Substring(optionTypeDelimiter + 1, ticker.Length - optionTypeDelimiter - 1);
|
|
|
|
var lookupResult = symbology[ticker[optionTypeDelimiter].ToStringInvariant()];
|
|
var month = lookupResult.Item1;
|
|
var optionRight = lookupResult.Item2;
|
|
|
|
var dayString = ticker.Substring(optionTypeDelimiter - 2, 2);
|
|
var yearString = ticker.Substring(optionTypeDelimiter - 4, 2);
|
|
var underlying = ticker.Substring(0, optionTypeDelimiter - 4);
|
|
|
|
// if we cannot parse strike price, we ignore this contract, but log the information.
|
|
decimal strikePrice;
|
|
if (!Decimal.TryParse(strikePriceString, out strikePrice))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
int day;
|
|
|
|
if (!int.TryParse(dayString, out day))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
int year;
|
|
|
|
if (!int.TryParse(yearString, out year))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var expirationDate = new DateTime(2000 + year, month, day);
|
|
|
|
return new OptionTickerProperties
|
|
{
|
|
Underlying = underlying,
|
|
OptionRight = optionRight,
|
|
OptionStrike = strikePrice,
|
|
ExpirationDate = expirationDate
|
|
};
|
|
}
|
|
|
|
|
|
private static IReadOnlyDictionary<string, int> _futuresMonthCodeLookup = new Dictionary<string, int>
|
|
{
|
|
{ "F", 1 },
|
|
{ "G", 2 },
|
|
{ "H", 3 },
|
|
{ "J", 4 },
|
|
{ "K", 5 },
|
|
{ "M", 6 },
|
|
{ "N", 7 },
|
|
{ "Q", 8 },
|
|
{ "U", 9 },
|
|
{ "V", 10 },
|
|
{ "X", 11 },
|
|
{ "Z", 12 }
|
|
};
|
|
|
|
private static IReadOnlyDictionary<int, string> _futuresMonthLookup = _futuresMonthCodeLookup.ToDictionary(kv => kv.Value, kv => kv.Key);
|
|
}
|
|
}
|