Disable margin calls by default in live mode
This commit is contained in:
@@ -31,7 +31,7 @@ namespace QuantConnect.Algorithm.CSharp
|
||||
{
|
||||
// set our initializer to our custom type
|
||||
SetBrokerageModel(BrokerageName.TradierBrokerage);
|
||||
SetSecurityInitializer(new CustomSecurityInitializer(BrokerageModel, new FuncSecuritySeeder(GetLastKnownPrice), DataNormalizationMode.Raw));
|
||||
SetSecurityInitializer(new CustomSecurityInitializer(BrokerageModel, new FuncSecuritySeeder(GetLastKnownPrice), new SecurityMarginModel(1m), DataNormalizationMode.Raw));
|
||||
|
||||
SetStartDate(2012, 01, 01);
|
||||
SetEndDate(2013, 01, 01);
|
||||
@@ -62,9 +62,11 @@ namespace QuantConnect.Algorithm.CSharp
|
||||
/// with the specified normalization mode
|
||||
/// </summary>
|
||||
/// <param name="brokerageModel">The brokerage model used to get fill/fee/slippage/settlement models</param>
|
||||
/// <param name="securitySeeder">An <see cref="ISecuritySeeder"/> used to seed the initial price of the security</param>
|
||||
/// <param name="securityMarginModel">The margin model to be used for the security</param>
|
||||
/// <param name="dataNormalizationMode">The desired data normalization mode</param>
|
||||
public CustomSecurityInitializer(IBrokerageModel brokerageModel, ISecuritySeeder securitySeeder, DataNormalizationMode dataNormalizationMode)
|
||||
: base(brokerageModel, securitySeeder)
|
||||
public CustomSecurityInitializer(IBrokerageModel brokerageModel, ISecuritySeeder securitySeeder, ISecurityMarginModel securityMarginModel, DataNormalizationMode dataNormalizationMode)
|
||||
: base(brokerageModel, securitySeeder, securityMarginModel)
|
||||
{
|
||||
_dataNormalizationMode = dataNormalizationMode;
|
||||
}
|
||||
|
||||
@@ -141,7 +141,8 @@ namespace QuantConnect.Algorithm
|
||||
TradeBuilder = new TradeBuilder(FillGroupingMethod.FillToFill, FillMatchingMethod.FIFO);
|
||||
|
||||
SecurityInitializer = new BrokerageModelSecurityInitializer(new DefaultBrokerageModel(AccountType.Margin),
|
||||
new FuncSecuritySeeder(GetLastKnownPrice));
|
||||
new FuncSecuritySeeder(GetLastKnownPrice),
|
||||
new SecurityMarginModel(1m));
|
||||
|
||||
CandlestickPatterns = new CandlestickPatterns(this);
|
||||
}
|
||||
@@ -820,23 +821,28 @@ namespace QuantConnect.Algorithm
|
||||
/// </summary>
|
||||
/// <param name="brokerage">The brokerage to emulate</param>
|
||||
/// <param name="accountType">The account type (Cash or Margin)</param>
|
||||
public void SetBrokerageModel(BrokerageName brokerage, AccountType accountType = AccountType.Margin)
|
||||
/// <param name="marginModel">The margin model to use</param>
|
||||
public void SetBrokerageModel(BrokerageName brokerage, AccountType accountType = AccountType.Margin, ISecurityMarginModel marginModel = null)
|
||||
{
|
||||
SetBrokerageModel(Brokerages.BrokerageModel.Create(brokerage, accountType));
|
||||
SetBrokerageModel(Brokerages.BrokerageModel.Create(brokerage, accountType), marginModel);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the brokerage to emulate in backtesting or paper trading.
|
||||
/// This can be used to set a custom brokerage model.
|
||||
/// </summary>
|
||||
/// <param name="model">The brokerage model to use</param>
|
||||
public void SetBrokerageModel(IBrokerageModel model)
|
||||
/// <param name="brokerageModel">The brokerage model to use</param>
|
||||
/// <param name="marginModel">The margin model to use</param>
|
||||
public void SetBrokerageModel(IBrokerageModel brokerageModel, ISecurityMarginModel marginModel = null)
|
||||
{
|
||||
BrokerageModel = model;
|
||||
BrokerageModel = brokerageModel;
|
||||
if (!_userSetSecurityInitializer)
|
||||
{
|
||||
// purposefully use the direct setter vs Set method so we don't flip the switch :/
|
||||
SecurityInitializer = new BrokerageModelSecurityInitializer(model, new FuncSecuritySeeder(GetLastKnownPrice));
|
||||
SecurityInitializer = new BrokerageModelSecurityInitializer(
|
||||
brokerageModel,
|
||||
new FuncSecuritySeeder(GetLastKnownPrice),
|
||||
marginModel ?? new SecurityMarginModel(1m));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -309,9 +309,9 @@ namespace QuantConnect.Interfaces
|
||||
/// Sets the brokerage model used to resolve transaction models, settlement models,
|
||||
/// and brokerage specified ordering behaviors.
|
||||
/// </summary>
|
||||
/// <param name="brokerageModel">The brokerage model used to emulate the real
|
||||
/// brokerage</param>
|
||||
void SetBrokerageModel(IBrokerageModel brokerageModel);
|
||||
/// <param name="brokerageModel">The brokerage model used to emulate the real brokerage</param>
|
||||
/// <param name="marginModel">The margin model to use</param>
|
||||
void SetBrokerageModel(IBrokerageModel brokerageModel, ISecurityMarginModel marginModel = null);
|
||||
|
||||
// <summary>
|
||||
// v1.0 Handler for Tick Events [DEPRECATED June-2014]
|
||||
|
||||
@@ -318,6 +318,7 @@
|
||||
<Compile Include="Securities\FuncSecuritySeeder.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" />
|
||||
|
||||
@@ -28,6 +28,7 @@ namespace QuantConnect.Securities
|
||||
{
|
||||
private readonly IBrokerageModel _brokerageModel;
|
||||
private readonly ISecuritySeeder _securitySeeder;
|
||||
private readonly ISecurityMarginModel _securityMarginModel;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BrokerageModelSecurityInitializer"/> class
|
||||
@@ -35,10 +36,12 @@ namespace QuantConnect.Securities
|
||||
/// </summary>
|
||||
/// <param name="brokerageModel">The brokerage model used to initialize the security models</param>
|
||||
/// <param name="securitySeeder">An <see cref="ISecuritySeeder"/> used to seed the initial price of the security</param>
|
||||
public BrokerageModelSecurityInitializer(IBrokerageModel brokerageModel, ISecuritySeeder securitySeeder)
|
||||
/// <param name="securityMarginModel">The margin model to be used for the security</param>
|
||||
public BrokerageModelSecurityInitializer(IBrokerageModel brokerageModel, ISecuritySeeder securitySeeder, ISecurityMarginModel securityMarginModel)
|
||||
{
|
||||
_brokerageModel = brokerageModel;
|
||||
_securitySeeder = securitySeeder;
|
||||
_securityMarginModel = securityMarginModel;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -48,6 +51,7 @@ namespace QuantConnect.Securities
|
||||
public virtual void Initialize(Security security)
|
||||
{
|
||||
// set leverage and models
|
||||
security.MarginModel = _securityMarginModel;
|
||||
security.SetLeverage(_brokerageModel.GetLeverage(security));
|
||||
security.FillModel = _brokerageModel.GetFillModel(security);
|
||||
security.FeeModel = _brokerageModel.GetFeeModel(security);
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ using QuantConnect.Lean.Engine.Results;
|
||||
using QuantConnect.Lean.Engine.TransactionHandlers;
|
||||
using QuantConnect.Logging;
|
||||
using QuantConnect.Packets;
|
||||
using QuantConnect.Securities;
|
||||
using QuantConnect.Util;
|
||||
|
||||
namespace QuantConnect.Lean.Engine.Setup
|
||||
@@ -183,7 +184,9 @@ namespace QuantConnect.Lean.Engine.Setup
|
||||
try
|
||||
{
|
||||
//Set the default brokerage model before initialize
|
||||
algorithm.SetBrokerageModel(_factory.BrokerageModel);
|
||||
algorithm.SetBrokerageModel(_factory.BrokerageModel, new NoMarginCallMarginModel(1m));
|
||||
//Margin calls are disabled by default in live mode
|
||||
algorithm.Portfolio.MarginCallModel = new NullMarginCallModel(algorithm.Portfolio);
|
||||
//Set our parameters
|
||||
algorithm.SetParameters(job.Parameters);
|
||||
//Algorithm is live, not backtesting:
|
||||
|
||||
@@ -1,4 +1,20 @@
|
||||
using System;
|
||||
/*
|
||||
* 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 NodaTime;
|
||||
using NUnit.Framework;
|
||||
using QuantConnect.Algorithm;
|
||||
@@ -49,7 +65,8 @@ namespace QuantConnect.Tests.Common.Securities
|
||||
SymbolProperties.GetDefault(CashBook.AccountCurrency));
|
||||
|
||||
_brokerageInitializer = new BrokerageModelSecurityInitializer(new DefaultBrokerageModel(),
|
||||
new FuncSecuritySeeder(_algo.GetLastKnownPrice));
|
||||
new FuncSecuritySeeder(_algo.GetLastKnownPrice),
|
||||
new SecurityMarginModel(1m));
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
Reference in New Issue
Block a user