Lean  $LEAN_TAG$
BrokerageModelSecurityInitializer.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15 */
16 
18 
20 {
21  /// <summary>
22  /// Provides an implementation of <see cref="ISecurityInitializer"/> that initializes a security
23  /// by settings the <see cref="Security.FillModel"/>, <see cref="Security.FeeModel"/>,
24  /// <see cref="Security.SlippageModel"/>, and the <see cref="Security.SettlementModel"/> properties
25  /// </summary>
27  {
28  private readonly IBrokerageModel _brokerageModel;
29  private readonly ISecuritySeeder _securitySeeder;
30 
31  /// <summary>
32  /// Initializes a new instance of the <see cref="BrokerageModelSecurityInitializer"/> class
33  /// for the specified algorithm
34  /// </summary>
36  {
37 
38  }
39 
40  /// <summary>
41  /// Initializes a new instance of the <see cref="BrokerageModelSecurityInitializer"/> class
42  /// for the specified algorithm
43  /// </summary>
44  /// <param name="brokerageModel">The brokerage model used to initialize the security models</param>
45  /// <param name="securitySeeder">An <see cref="ISecuritySeeder"/> used to seed the initial price of the security</param>
46  public BrokerageModelSecurityInitializer(IBrokerageModel brokerageModel, ISecuritySeeder securitySeeder)
47  {
48  _brokerageModel = brokerageModel;
49  _securitySeeder = securitySeeder;
50  }
51 
52  /// <summary>
53  /// Initializes the specified security by setting up the models
54  /// </summary>
55  /// <param name="security">The security to be initialized</param>
56  public virtual void Initialize(Security security)
57  {
58  // Sets the security models
59  security.FillModel = _brokerageModel.GetFillModel(security);
60  security.FeeModel = _brokerageModel.GetFeeModel(security);
61  security.SlippageModel = _brokerageModel.GetSlippageModel(security);
62  security.SettlementModel = _brokerageModel.GetSettlementModel(security);
63  security.BuyingPowerModel = _brokerageModel.GetBuyingPowerModel(security);
64  security.MarginInterestRateModel = _brokerageModel.GetMarginInterestRateModel(security);
65  // Sets the leverage after the buying power model. Otherwise we would set the leverage of the default model.
66  security.SetLeverage(_brokerageModel.GetLeverage(security));
67  security.SetShortableProvider(_brokerageModel.GetShortableProvider(security));
68 
69  _securitySeeder.SeedSecurity(security);
70  }
71  }
72 }