Added IMarginCallModel interface

- Renamed MarginCallModel to DefaultMarginCallModel
- Private null implementation exposed as MarginCallModel.Null
This commit is contained in:
Stefano Raggi
2017-01-05 19:03:08 +01:00
parent cb8f451ebb
commit 517ba7db90
6 changed files with 63 additions and 51 deletions
+2 -2
View File
@@ -316,9 +316,9 @@
<Compile Include="Securities\DelayedSettlementModel.cs" />
<Compile Include="Securities\AdjustedPriceVariationModel.cs" />
<Compile Include="Securities\FuncSecuritySeeder.cs" />
<Compile Include="Securities\IMarginCallModel.cs" />
<Compile Include="Securities\ISecuritySeeder.cs" />
<Compile Include="Securities\NoMarginCallMarginModel.cs" />
<Compile Include="Securities\NullMarginCallModel.cs" />
<Compile Include="Securities\SecurityPriceVariationModel.cs" />
<Compile Include="Securities\FuncSecurityDerivativeFilter.cs" />
<Compile Include="Securities\FuncSecurityInitializer.cs" />
@@ -350,7 +350,7 @@
<Compile Include="Securities\ISecurityMarginModel.cs" />
<Compile Include="Securities\IOrderProvider.cs" />
<Compile Include="Securities\ISecurityPortfolioModel.cs" />
<Compile Include="Securities\MarginCallModel.cs" />
<Compile Include="Securities\DefaultMarginCallModel.cs" />
<Compile Include="Securities\SecurityPortfolioModel.cs" />
<Compile Include="Securities\SecurityMarginModel.cs" />
<Compile Include="Securities\ImmediateSettlementModel.cs" />
@@ -27,7 +27,7 @@ namespace QuantConnect.Securities
/// This is a default implementation that orders the generated margin call orders by the unrealized
/// profit (losers first) and executes each order synchronously until we're within the margin requirements
/// </remarks>
public class MarginCallModel
public class DefaultMarginCallModel : IMarginCallModel
{
/// <summary>
/// Gets the portfolio that margin calls will be transacted against
@@ -35,10 +35,10 @@ namespace QuantConnect.Securities
protected SecurityPortfolioManager Portfolio { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="MarginCallModel"/> class
/// Initializes a new instance of the <see cref="DefaultMarginCallModel"/> class
/// </summary>
/// <param name="portfolio">The portfolio object to receive margin calls</param>
public MarginCallModel(SecurityPortfolioManager portfolio)
public DefaultMarginCallModel(SecurityPortfolioManager portfolio)
{
Portfolio = portfolio;
}
+55
View File
@@ -0,0 +1,55 @@
/*
* 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.Collections.Generic;
using QuantConnect.Orders;
namespace QuantConnect.Securities
{
/// <summary>
/// Represents the model responsible for picking which orders should be executed during a margin call
/// </summary>
public interface IMarginCallModel
{
/// <summary>
/// Executes synchronous orders to bring the account within margin requirements.
/// </summary>
/// <param name="generatedMarginCallOrders">These are the margin call orders that were generated
/// by individual security margin models.</param>
/// <returns>The list of orders that were actually executed</returns>
List<OrderTicket> ExecuteMarginCall(IEnumerable<SubmitOrderRequest> generatedMarginCallOrders);
}
/// <summary>
/// Provides access to a null implementation for <see cref="IMarginCallModel"/>
/// </summary>
public static class MarginCallModel
{
/// <summary>
/// Gets an instance of <see cref="IMarginCallModel"/> that will always
/// return an empty list of executed orders.
/// </summary>
public static readonly IMarginCallModel Null = new NullMarginCallModel();
private sealed class NullMarginCallModel : IMarginCallModel
{
public List<OrderTicket> ExecuteMarginCall(IEnumerable<SubmitOrderRequest> generatedMarginCallOrders)
{
return new List<OrderTicket>();
}
}
}
}
-43
View File
@@ -1,43 +0,0 @@
/*
* 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.Collections.Generic;
using QuantConnect.Orders;
namespace QuantConnect.Securities
{
/// <summary>
/// Represents a margin call model which will always return no margin call orders
/// </summary>
public class NullMarginCallModel : MarginCallModel
{
/// <summary>
/// Initializes a new instance of the <see cref="NullMarginCallModel"/> class
/// </summary>
/// <param name="portfolio">The portfolio object to receive margin calls</param>
public NullMarginCallModel(SecurityPortfolioManager portfolio) : base(portfolio)
{
}
/// <summary>
/// Returns an empty list of executed orders
/// </summary>
public override List<OrderTicket> ExecuteMarginCall(IEnumerable<SubmitOrderRequest> generatedMarginCallOrders)
{
return new List<OrderTicket>();
}
}
}
@@ -67,7 +67,7 @@ namespace QuantConnect.Securities
{
Securities = securityManager;
Transactions = transactions;
MarginCallModel = new MarginCallModel(this);
MarginCallModel = new DefaultMarginCallModel(this);
CashBook = new CashBook();
UnsettledCashBook = new CashBook();
@@ -437,7 +437,7 @@ namespace QuantConnect.Securities
/// Gets or sets the <see cref="MarginCallModel"/> for the portfolio. This
/// is used to executed margin call orders.
/// </summary>
public MarginCallModel MarginCallModel { get; set; }
public IMarginCallModel MarginCallModel { get; set; }
/// <summary>
/// Indexer for the PortfolioManager class to access the underlying security holdings objects.
+1 -1
View File
@@ -186,7 +186,7 @@ namespace QuantConnect.Lean.Engine.Setup
//Set the default brokerage model before initialize
algorithm.SetBrokerageModel(_factory.BrokerageModel, new NoMarginCallMarginModel(1m));
//Margin calls are disabled by default in live mode
algorithm.Portfolio.MarginCallModel = new NullMarginCallModel(algorithm.Portfolio);
algorithm.Portfolio.MarginCallModel = MarginCallModel.Null;
//Set our parameters
algorithm.SetParameters(job.Parameters);
//Algorithm is live, not backtesting: