Lean  $LEAN_TAG$
PortfolioLooperAlgorithm.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 
17 using QuantConnect.Orders;
19 using System.Collections.Generic;
20 using System.Linq;
21 using System.Reflection;
22 
23 namespace QuantConnect.Report
24 {
25  /// <summary>
26  /// Fake algorithm that initializes portfolio and algorithm securities. Never ran.
27  /// </summary>
29  {
30  private decimal _startingCash;
31  private List<Order> _orders;
32  private AlgorithmConfiguration _algorithmConfiguration;
33 
34  /// <summary>
35  /// Initialize an instance of <see cref="PortfolioLooperAlgorithm"/>
36  /// </summary>
37  /// <param name="startingCash">Starting algorithm cash</param>
38  /// <param name="orders">Orders to use</param>
39  /// <param name="algorithmConfiguration">Optional parameter to override default algorithm configuration</param>
40  public PortfolioLooperAlgorithm(decimal startingCash, IEnumerable<Order> orders, AlgorithmConfiguration algorithmConfiguration = null) : base()
41  {
42  _startingCash = startingCash;
43  _orders = orders.ToList();
44  _algorithmConfiguration = algorithmConfiguration;
45  }
46 
47  /// <summary>
48  /// Initializes all the proper Securities from the orders provided by the user
49  /// </summary>
50  /// <param name="orders">Orders to use</param>
51  public void FromOrders(IEnumerable<Order> orders)
52  {
53  foreach (var symbol in orders.Select(x => x.Symbol).Distinct())
54  {
55  Resolution resolution;
56  switch (symbol.SecurityType)
57  {
58  case SecurityType.Option:
59  case SecurityType.Future:
60  resolution = Resolution.Minute;
61  break;
62  default:
63  resolution = Resolution.Daily;
64  break;
65  }
66 
67  var configs = SubscriptionManager.SubscriptionDataConfigService.Add(symbol, resolution, false, false);
68  var security = Securities.CreateSecurity(symbol, configs, 0m);
69  if (symbol.SecurityType == SecurityType.Crypto)
70  {
71  security.BuyingPowerModel = new SecurityMarginModel();
72  }
73 
74  // Set leverage to 10000 to account for unknown leverage values in user algorithms
75  security.SetLeverage(10000m);
76 
77  var method = typeof(QCAlgorithm).GetMethod("AddToUserDefinedUniverse", BindingFlags.NonPublic | BindingFlags.Instance);
78  method.Invoke(this, new object[] { security, configs });
79  }
80  }
81 
82  /// <summary>
83  /// Initialize this algorithm
84  /// </summary>
85  public override void Initialize()
86  {
87  if (_algorithmConfiguration != null)
88  {
89  SetAccountCurrency(_algorithmConfiguration.AccountCurrency);
90  SetBrokerageModel(_algorithmConfiguration.Brokerage, _algorithmConfiguration.AccountType);
91  }
92 
93  SetCash(_startingCash);
94 
95  if (_orders.Count != 0)
96  {
97  SetStartDate(_orders.First().Time);
98  SetEndDate(_orders.Last().Time);
99  }
100 
101  SetBenchmark(b => 0);
102  }
103  }
104 }