Lean  $LEAN_TAG$
PaperBrokerage.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 
17 using System;
18 using System.Collections.Generic;
21 using QuantConnect.Packets;
23 
25 {
26  /// <summary>
27  /// Paper Trading Brokerage
28  /// </summary>
30  {
31  private DateTime _lastScanTime;
32  private readonly LiveNodePacket _job;
33 
34  /// <summary>
35  /// Creates a new PaperBrokerage
36  /// </summary>
37  /// <param name="algorithm">The algorithm under analysis</param>
38  /// <param name="job">The job packet</param>
39  public PaperBrokerage(IAlgorithm algorithm, LiveNodePacket job)
40  : base(algorithm, "Paper Brokerage")
41  {
42  _job = job;
43  }
44 
45  /// <summary>
46  /// Gets the current cash balance for each currency held in the brokerage account
47  /// </summary>
48  /// <returns>The current cash balance for each currency available for trading</returns>
49  public override List<CashAmount> GetCashBalance()
50  {
51  return GetCashBalance(_job.BrokerageData, Algorithm.Portfolio.CashBook);
52  }
53 
54  /// <summary>
55  /// Gets all holdings for the account
56  /// </summary>
57  /// <returns>The current holdings from the account</returns>
58  public override List<Holding> GetAccountHoldings()
59  {
60  return base.GetAccountHoldings(_job.BrokerageData, Algorithm.Securities.Values);
61  }
62 
63  /// <summary>
64  /// Scans all the outstanding orders and applies the algorithm model fills to generate the order events.
65  /// This override adds dividend detection and application
66  /// </summary>
67  public override void Scan()
68  {
69  // Scan is called twice per time loop, this check enforces that we only check
70  // on the first call for each time loop
71  if (Algorithm.UtcTime != _lastScanTime && Algorithm.CurrentSlice != null)
72  {
73  _lastScanTime = Algorithm.UtcTime;
74 
75  // apply each dividend directly to the quote cash holdings of the security
76  // this assumes dividends are paid out in a security's quote cash (reasonable assumption)
77  foreach (var dividend in Algorithm.CurrentSlice.Dividends.Values)
78  {
79  Security security;
80  if (Algorithm.Securities.TryGetValue(dividend.Symbol, out security))
81  {
82  // compute the total distribution and apply as security's quote currency
83  var distribution = security.Holdings.Quantity * dividend.Distribution;
84  security.QuoteCurrency.AddAmount(distribution);
85  }
86  }
87  }
88 
89  base.Scan();
90  }
91  }
92 }