Files
T
Martin-Molinero 83f9499b4a Option Margin Strategies (#5511)
* Refactor HasSufficientBuyingPowerForOrder implementations

Adds Sufficient and Insufficient helper methods to HashSufficientbuyingPowerForOrderParameters
enabling syntax like:

return paraeeters.Sufficient()
returnparameters.Insufficient(reason)

The next change will add the initial margin required which will simply require
updating both of these helper methods to accept the value.

* IBuyingPowerModel: Add margin functions Maintenance/Initial/ForOrder

These were originally hidden in an effort to only expose what's necessary
for the engine to perform its work. Additionally, we encapsulated all of
the method arguments into parameters classes to prevent having to break
anyone in the future. Not including these foundational methods turns out to
be an oversight. These methods are not required by the engine, but rather by
other models. Another possible solution here is to add an additional abstraction
and include these methods on this new abstraction. BuyingPowerModel would then
explicitly implement these methods and models that depend on them would require
two code paths, one for when the buying power model implements this interface
and another for when it doesn't.

Tests were additionally updated to remove test model implementations created for
the sole purpose of exposing these private methods.

* Add ConstantBuyingPowerModel

Provides an implementation of IBuyingPowerModel that returns the same
constant value

* Update BuyingPowerModelPythonWrapper to use reflection for method names

Having a bunch of hard-coded strings is a sure fire way for someone to
overlook when changing methods. This change ensures that noone needs to
remember that this code exists :)

Cleans up the syntax around verifying a python object implements a particular
C# interface via the ValidateImplementationOf<T> method by having it return a
value since the only use cases are in constructors when setting the models.

I was initially going to update ALL python wrappers to validate the passed
in models, but such a change could break many things that are 'working' right
now. Such an effort should be saved for its own dedicated PR.

* Add Parameters/Result types for new buying power model methods

* Support computing maintenance margin for arbitrary quantities

The existing GetMaintenanceMargin function assumes that we're only interested
in the maintenance margin for the entirety of the provided security's holdings.
This makes it impossible to perform what-if analysis or to even ask how much
maintenance margin is devoted to a particular subset of the security's holdings.
This change adds the quantity to the MaintenanceMarginParameters class. Futures
and Options models also depend on holdings cost and holdings value, so they have
also been added to the parameters type. Finally, static factory methods were
added to improve discernment of intent: ForCurrentHoldings provides the existing
behavior and then ForQuantityAtCurrentPrice to support what-if scenarios where
we're looking for the change in maintenance margin if we were to execute an order
for the securiy at the current time step. Obviously a constructor is provided to
set all of the values explicitly, using any price metric the caller desires.

* Address review

- Fix BPM xml documentation
- Fix python unit tests and PythonWrapper validate method

* Add SecurityHolding.QuantityChanged event

Adding event handlers will allow us to orchestrate complex
events from distant parts of the codebase through wiring
them up. If we continue down this path, it will move us away
from the current, very 'mechanical' data flows expressed in
LEAN and towards a more modern, event processing based system.
This is but a baby step in that direction and the initial use
case is using this QuantityChanged event to trigger resolution
of the algoritm's positions groups. This is part of an effort
to improve the fidelity of options margin modeling where we'll
model an OptionStrategy as an IPositionGroup. This will allow
us to compute the margin requirements of an OptionStrategy as
a unit instead of computing margin of each security individually
in isolation.

See #4065

* PortfolioManager: Group fields and remove unused field

This codebase generally places fields as the first members, but
this class had some fields at the top, then some properties, and
then some more fields. This change brings all the fields together
at the top of the file and also removes pointless comments placed
directly above some of the fields. Additionally, an unused field
was removed.

* Remove unused _currencyConverter from Security

Looks like at some point the only code using this member variable was removed
and the necessary clean up was overlooked.

* Add Parse.Enum functions

* Support disabling regression algorithms by language via config.json

Adds 'regression-test-languages' to config.json and filters regerssion algorithms to
run based on this value. When cycling on a particular feature, it's nice to be able
to run the entire regression set while ignoring the python algorithms. Once the C#
algorithms are all passing, one can then go back and run C# and Python in a final run,
since 99% of feature work doesn't impact python specifically.

* Implement IComparable in SecurityIdentitfier

This can be used to deterministically sort securities and symbols

* Add .editorconfig to enforce common formatting for json/sh files

* Fix typo in IBuyingPowerModel.GetBuyingPower xml docs

* Add ListEquals/GetListHashCode and OrderDirection.Closes(PositionSide)

ListEquals and GetListHashCode are designed to be used together as they
complement each other according to C#'s requirements for Equals and
GetHashCode functions.

PositionSide.ToOrderDirection() extension simply converts a PositionSide
to its logical equivalent OrderDirection. Long->Buy, Short->Sell, None->Hold

OrderDirection.Closes(PositionSide) determines if a particular OrderDirection
would have the effect of reducing a position's absolute size. This function
greatly improves the readability of buying power functions that must provide
adjustments when an order/contemplated trade reduces/closes an existing position.
OrderDirection.Buy.Closes(PositionSide.Short)
OrderDirection.Sell.Closes(PositionSide.Long)
All other combinations return false

Adds ToArray/ToImmutableArray convenience functions that combine a call
to Select followed by To(Immutable)Array all in one function call.

* Add decimal.DiscretelyRoundBy extension method

Supports rounding a decimal value by an arbitrarily chosen maximum precision,
or 'quanta'

* Update FutureMarginBuyingPowerModelTests to respect the security's lot size

* Add core position group classes and abstractions

* Add initial/maintenance margin support, buying power model consistency tests

* Add SufficientBuyingPower and GetReservedBuyingPower to position group model

Includes update to BrokerageTransactionHandler to use position group BPM for
sufficient buying power checks.

* Resolve position groups on each fill

We need to update the state of our position groups on each fill so that
we can properly handle multiple orders within the same time step. We
also limit the number of positions sent into the resolver by removing
securities without any holdings.

* fixup! Add SufficientBuyingPower and GetReservedBuyingPower to position group model

* Add GetMaximumLotsFor{Target|Delta}BuyingPower

Instead of computing order quantity, these functions compute the
maximum number of position group lots, which is the position group
quantity, and is guaranteed to be a whole number, for the provided
target/delta buying power parameters.

The SecurityPositionGroupBuyingPowerModel delegates to the security's
IBuyingPowerModel by applying a scaling factor equal to the security's
lot size.

This change also updates references to IBuyingPowerModel.GetMaximum...
to use the new position group model methods.

* Convert remaining IBuyingPowerModel call sites to position groups

* Rename PositionManasger.CreateDefaultGroup -> GetOrCreateDefaultGroup

Better describes its behavior

* Add Position Groups readme.md

* Add Option Strategy BuyingPowerModel

- Adding CompositePrositionGroupResolver and
  OptionStrategyPositionGroupResolver
- Adding OptionStrategyPositionGroupBuyingPowerModel handling option
  strategies based on IBs margin table. Adding regression algorithms
- Few changes so that option strategies executed by multiple orders are
  detected
- Adjust OptionStrategyDefinitionMatch to include equity legs in the
  matching result
- Minor tweaks fixing previous rebase
- Minor fixes for existing option strategies definitions, adding new
  missing strategies.
- Fixing minor bugs in option strategy matcher. Adding more unit tests

* Address self reviews

- Fixing bug in 'PositionGroupCollection'
- Few minor simplificaitons
- Adding BasicTemplateOptionEquityStrategyAlgorithm

* Address reviews

- Improve regression algorithms margin remaining and used assert logic to be exact. Taking into account spread and fees

Co-authored-by: Michael Handschuh <mhandschuh@gmail.com>
2021-04-30 18:45:27 -03:00

461 lines
17 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.ComponentModel;
using System.Linq;
using System.Threading;
using Newtonsoft.Json;
using QuantConnect.Interfaces;
using QuantConnect.Orders.Serialization;
using QuantConnect.Orders.TimeInForces;
using QuantConnect.Securities;
using QuantConnect.Securities.Positions;
using static QuantConnect.StringExtensions;
namespace QuantConnect.Orders
{
/// <summary>
/// Order struct for placing new trade
/// </summary>
public abstract class Order
{
private volatile int _incrementalId;
private decimal _quantity;
private decimal _price;
/// <summary>
/// Order ID.
/// </summary>
public int Id { get; internal set; }
/// <summary>
/// Order id to process before processing this order.
/// </summary>
public int ContingentId { get; internal set; }
/// <summary>
/// Brokerage Id for this order for when the brokerage splits orders into multiple pieces
/// </summary>
public List<string> BrokerId { get; internal set; }
/// <summary>
/// Symbol of the Asset
/// </summary>
public Symbol Symbol { get; internal set; }
/// <summary>
/// Price of the Order.
/// </summary>
public decimal Price
{
get { return _price; }
internal set { _price = value.Normalize(); }
}
/// <summary>
/// Currency for the order price
/// </summary>
public string PriceCurrency { get; internal set; }
/// <summary>
/// Gets the utc time the order was created.
/// </summary>
public DateTime Time { get; internal set; }
/// <summary>
/// Gets the utc time this order was created. Alias for <see cref="Time"/>
/// </summary>
public DateTime CreatedTime => Time;
/// <summary>
/// Gets the utc time the last fill was received, or null if no fills have been received
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public DateTime? LastFillTime { get; internal set; }
/// <summary>
/// Gets the utc time this order was last updated, or null if the order has not been updated.
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public DateTime? LastUpdateTime { get; internal set; }
/// <summary>
/// Gets the utc time this order was canceled, or null if the order was not canceled.
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public DateTime? CanceledTime { get; internal set; }
/// <summary>
/// Number of shares to execute.
/// </summary>
public decimal Quantity
{
get { return _quantity; }
internal set { _quantity = value.Normalize(); }
}
/// <summary>
/// Order Type
/// </summary>
public abstract OrderType Type { get; }
/// <summary>
/// Status of the Order
/// </summary>
public OrderStatus Status { get; set; }
/// <summary>
/// Order Time In Force
/// </summary>
[JsonIgnore]
public TimeInForce TimeInForce => Properties.TimeInForce;
/// <summary>
/// Tag the order with some custom data
/// </summary>
[DefaultValue(""), JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Tag { get; internal set; }
/// <summary>
/// Additional properties of the order
/// </summary>
public IOrderProperties Properties { get; private set; }
/// <summary>
/// The symbol's security type
/// </summary>
public SecurityType SecurityType => Symbol.ID.SecurityType;
/// <summary>
/// Order Direction Property based off Quantity.
/// </summary>
public OrderDirection Direction
{
get
{
if (Quantity > 0)
{
return OrderDirection.Buy;
}
if (Quantity < 0)
{
return OrderDirection.Sell;
}
return OrderDirection.Hold;
}
}
/// <summary>
/// Get the absolute quantity for this order
/// </summary>
[JsonIgnore]
public decimal AbsoluteQuantity => Math.Abs(Quantity);
/// <summary>
/// Gets the executed value of this order. If the order has not yet filled,
/// then this will return zero.
/// </summary>
public decimal Value => Quantity * Price;
/// <summary>
/// Gets the price data at the time the order was submitted
/// </summary>
public OrderSubmissionData OrderSubmissionData { get; internal set; }
/// <summary>
/// Returns true if the order is a marketable order.
/// </summary>
public bool IsMarketable
{
get
{
if (Type == OrderType.Limit)
{
// check if marketable limit order using bid/ask prices
var limitOrder = (LimitOrder)this;
return OrderSubmissionData != null &&
(Direction == OrderDirection.Buy && limitOrder.LimitPrice >= OrderSubmissionData.AskPrice ||
Direction == OrderDirection.Sell && limitOrder.LimitPrice <= OrderSubmissionData.BidPrice);
}
return Type == OrderType.Market;
}
}
/// <summary>
/// Added a default constructor for JSON Deserialization:
/// </summary>
protected Order()
{
Time = new DateTime();
Price = 0;
PriceCurrency = string.Empty;
Quantity = 0;
Symbol = Symbol.Empty;
Status = OrderStatus.None;
Tag = "";
BrokerId = new List<string>();
ContingentId = 0;
Properties = new OrderProperties();
}
/// <summary>
/// New order constructor
/// </summary>
/// <param name="symbol">Symbol asset we're seeking to trade</param>
/// <param name="quantity">Quantity of the asset we're seeking to trade</param>
/// <param name="time">Time the order was placed</param>
/// <param name="tag">User defined data tag for this order</param>
/// <param name="properties">The order properties for this order</param>
protected Order(Symbol symbol, decimal quantity, DateTime time, string tag = "", IOrderProperties properties = null)
{
Time = time;
Price = 0;
PriceCurrency = string.Empty;
Quantity = quantity;
Symbol = symbol;
Status = OrderStatus.None;
Tag = tag;
BrokerId = new List<string>();
ContingentId = 0;
Properties = properties ?? new OrderProperties();
}
/// <summary>
/// Creates an enumerable containing each position resulting from executing this order.
/// </summary>
/// <remarks>
/// This is provided in anticipation of a new combo order type that will need to override this method,
/// returning a position for each 'leg' of the order.
/// </remarks>
/// <returns>An enumerable of positions matching the results of executing this order</returns>
public virtual IEnumerable<IPosition> CreatePositions(SecurityManager securities)
{
var security = securities[Symbol];
yield return new Position(security, Quantity);
}
/// <summary>
/// Gets the value of this order at the given market price in units of the account currency
/// NOTE: Some order types derive value from other parameters, such as limit prices
/// </summary>
/// <param name="security">The security matching this order's symbol</param>
/// <returns>The value of this order given the current market price</returns>
public decimal GetValue(Security security)
{
var value = GetValueImpl(security);
return value*security.QuoteCurrency.ConversionRate*security.SymbolProperties.ContractMultiplier;
}
/// <summary>
/// Gets the order value in units of the security's quote currency for a single unit.
/// A single unit here is a single share of stock, or a single barrel of oil, or the
/// cost of a single share in an option contract.
/// </summary>
/// <param name="security">The security matching this order's symbol</param>
protected abstract decimal GetValueImpl(Security security);
/// <summary>
/// Gets a new unique incremental id for this order
/// </summary>
/// <returns>Returns a new id for this order</returns>
internal int GetNewId()
{
return Interlocked.Increment(ref _incrementalId);
}
/// <summary>
/// Modifies the state of this order to match the update request
/// </summary>
/// <param name="request">The request to update this order object</param>
public virtual void ApplyUpdateOrderRequest(UpdateOrderRequest request)
{
if (request.OrderId != Id)
{
throw new ArgumentException("Attempted to apply updates to the incorrect order!");
}
if (request.Quantity.HasValue)
{
Quantity = request.Quantity.Value;
}
if (request.Tag != null)
{
Tag = request.Tag;
}
}
/// <summary>
/// Returns a string that represents the current object.
/// </summary>
/// <returns>
/// A string that represents the current object.
/// </returns>
/// <filterpriority>2</filterpriority>
public override string ToString()
{
var tag = string.IsNullOrEmpty(Tag) ? string.Empty : $": {Tag}";
return Invariant($"OrderId: {Id} (BrokerId: {string.Join(",", BrokerId)}) {Status} {Type} order for {Quantity} unit{(Quantity == 1 ? "" : "s")} of {Symbol}{tag}");
}
/// <summary>
/// Creates a deep-copy clone of this order
/// </summary>
/// <returns>A copy of this order</returns>
public abstract Order Clone();
/// <summary>
/// Copies base Order properties to the specified order
/// </summary>
/// <param name="order">The target of the copy</param>
protected void CopyTo(Order order)
{
order.Id = Id;
order.Time = Time;
order.LastFillTime = LastFillTime;
order.LastUpdateTime = LastUpdateTime;
order.CanceledTime = CanceledTime;
order.BrokerId = BrokerId.ToList();
order.ContingentId = ContingentId;
order.Price = Price;
order.PriceCurrency = PriceCurrency;
order.Quantity = Quantity;
order.Status = Status;
order.Symbol = Symbol;
order.Tag = Tag;
order.Properties = Properties.Clone();
order.OrderSubmissionData = OrderSubmissionData?.Clone();
}
/// <summary>
/// Creates a new Order instance from a SerializedOrder instance
/// </summary>
/// <remarks>Used by the <see cref="SerializedOrderJsonConverter"/></remarks>
public static Order FromSerialized(SerializedOrder serializedOrder)
{
var sid = SecurityIdentifier.Parse(serializedOrder.Symbol);
var symbol = new Symbol(sid, sid.Symbol);
TimeInForce timeInForce = null;
var type = System.Type.GetType($"QuantConnect.Orders.TimeInForces.{serializedOrder.TimeInForceType}", throwOnError: false, ignoreCase: true);
if (type != null)
{
timeInForce = (TimeInForce) Activator.CreateInstance(type, true);
if (timeInForce is GoodTilDateTimeInForce)
{
var expiry = QuantConnect.Time.UnixTimeStampToDateTime(serializedOrder.TimeInForceExpiry.Value);
timeInForce = new GoodTilDateTimeInForce(expiry);
}
}
var createdTime = QuantConnect.Time.UnixTimeStampToDateTime(serializedOrder.CreatedTime);
var order = CreateOrder(serializedOrder.OrderId, serializedOrder.Type, symbol, serializedOrder.Quantity,
DateTime.SpecifyKind(createdTime, DateTimeKind.Utc),
serializedOrder.Tag,
new OrderProperties { TimeInForce = timeInForce },
serializedOrder.LimitPrice ?? 0,
serializedOrder.StopPrice ?? 0,
serializedOrder.TriggerPrice ?? 0);
order.OrderSubmissionData = new OrderSubmissionData(serializedOrder.SubmissionBidPrice,
serializedOrder.SubmissionAskPrice,
serializedOrder.SubmissionLastPrice);
order.BrokerId = serializedOrder.BrokerId;
order.ContingentId = serializedOrder.ContingentId;
order.Price = serializedOrder.Price;
order.PriceCurrency = serializedOrder.PriceCurrency;
order.Status = serializedOrder.Status;
if (serializedOrder.LastFillTime.HasValue)
{
var time = QuantConnect.Time.UnixTimeStampToDateTime(serializedOrder.LastFillTime.Value);
order.LastFillTime = DateTime.SpecifyKind(time, DateTimeKind.Utc);
}
if (serializedOrder.LastUpdateTime.HasValue)
{
var time = QuantConnect.Time.UnixTimeStampToDateTime(serializedOrder.LastUpdateTime.Value);
order.LastUpdateTime = DateTime.SpecifyKind(time, DateTimeKind.Utc);
}
if (serializedOrder.CanceledTime.HasValue)
{
var time = QuantConnect.Time.UnixTimeStampToDateTime(serializedOrder.CanceledTime.Value);
order.CanceledTime = DateTime.SpecifyKind(time, DateTimeKind.Utc);
}
return order;
}
/// <summary>
/// Creates an <see cref="Order"/> to match the specified <paramref name="request"/>
/// </summary>
/// <param name="request">The <see cref="SubmitOrderRequest"/> to create an order for</param>
/// <returns>The <see cref="Order"/> that matches the request</returns>
public static Order CreateOrder(SubmitOrderRequest request)
{
return CreateOrder(request.OrderId, request.OrderType, request.Symbol, request.Quantity, request.Time,
request.Tag, request.OrderProperties, request.LimitPrice, request.StopPrice, request.TriggerPrice);
}
private static Order CreateOrder(int orderId, OrderType type, Symbol symbol, decimal quantity, DateTime time,
string tag, IOrderProperties properties, decimal limitPrice, decimal stopPrice, decimal triggerPrice)
{
Order order;
switch (type)
{
case OrderType.Market:
order = new MarketOrder(symbol, quantity, time, tag, properties);
break;
case OrderType.Limit:
order = new LimitOrder(symbol, quantity, limitPrice, time, tag, properties);
break;
case OrderType.StopMarket:
order = new StopMarketOrder(symbol, quantity, stopPrice, time, tag, properties);
break;
case OrderType.StopLimit:
order = new StopLimitOrder(symbol, quantity, stopPrice, limitPrice, time, tag, properties);
break;
case OrderType.LimitIfTouched:
order = new LimitIfTouchedOrder(symbol, quantity, triggerPrice, limitPrice, time, tag, properties);
break;
case OrderType.MarketOnOpen:
order = new MarketOnOpenOrder(symbol, quantity, time, tag, properties);
break;
case OrderType.MarketOnClose:
order = new MarketOnCloseOrder(symbol, quantity, time, tag, properties);
break;
case OrderType.OptionExercise:
order = new OptionExerciseOrder(symbol, quantity, time, tag, properties);
break;
default:
throw new ArgumentOutOfRangeException();
}
order.Status = OrderStatus.New;
order.Id = orderId;
return order;
}
}
}