Lean  $LEAN_TAG$
BacktestingTransactionHandler.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 using System;
21 using QuantConnect.Logging;
22 using QuantConnect.Orders;
23 using QuantConnect.Util;
24 
26 {
27  /// <summary>
28  /// This transaction handler is used for processing transactions during backtests
29  /// </summary>
31  {
32  // save off a strongly typed version of the brokerage
33  private BacktestingBrokerage _brokerage;
34  private IAlgorithm _algorithm;
35  private Delistings _lastestDelistings;
36 
37  /// <summary>
38  /// Gets current time UTC. This is here to facilitate testing
39  /// </summary>
40  protected override DateTime CurrentTimeUtc => _algorithm.UtcTime;
41 
42  /// <summary>
43  /// Creates a new BacktestingTransactionHandler using the BacktestingBrokerage
44  /// </summary>
45  /// <param name="algorithm">The algorithm instance</param>
46  /// <param name="brokerage">The BacktestingBrokerage</param>
47  /// <param name="resultHandler"></param>
48  public override void Initialize(IAlgorithm algorithm, IBrokerage brokerage, IResultHandler resultHandler)
49  {
50  if (!(brokerage is BacktestingBrokerage))
51  {
52  throw new ArgumentException("Brokerage must be of type BacktestingBrokerage for use wth the BacktestingTransactionHandler");
53  }
54 
55  _brokerage = (BacktestingBrokerage) brokerage;
56  _algorithm = algorithm;
57 
58  base.Initialize(algorithm, brokerage, resultHandler);
59 
60  // non blocking implementation
62  }
63 
64  /// <summary>
65  /// Processes all synchronous events that must take place before the next time loop for the algorithm
66  /// </summary>
67  public override void ProcessSynchronousEvents()
68  {
69  // we process pending order requests our selves
70  Run();
71 
72  base.ProcessSynchronousEvents();
73 
74  _brokerage.Scan();
75 
76  // Run our delistings processing, only do this once a slice
77  if (_algorithm.CurrentSlice != null && _algorithm.CurrentSlice.Delistings != _lastestDelistings)
78  {
79  _lastestDelistings = _algorithm.CurrentSlice.Delistings;
80  _brokerage.ProcessDelistings(_algorithm.CurrentSlice.Delistings);
81  }
82  }
83 
84  /// <summary>
85  /// Processes asynchronous events on the transaction handler's thread
86  /// </summary>
87  public override void ProcessAsynchronousEvents()
88  {
89  base.ProcessAsynchronousEvents();
90 
91  _brokerage.Scan();
92  }
93 
94  /// <summary>
95  /// For backtesting we will submit the order ourselves
96  /// </summary>
97  /// <param name="ticket">The <see cref="OrderTicket"/> expecting to be submitted</param>
98  protected override void WaitForOrderSubmission(OrderTicket ticket)
99  {
100  // we submit the order request our selves
101  Run();
102 
103  if (!ticket.OrderSet.WaitOne(0))
104  {
105  // this could happen if there was some error handling the order
106  // and it was not set
107  Log.Error("BacktestingTransactionHandler.WaitForOrderSubmission(): " +
108  $"The order request (Id={ticket.OrderId}) was not submitted. " +
109  "See the OrderRequest.Response for more information");
110  }
111  }
112 
113  /// <summary>
114  /// For backtesting order requests will be processed by the algorithm thread
115  /// sequentially at <see cref="WaitForOrderSubmission"/> and <see cref="ProcessSynchronousEvents"/>
116  /// </summary>
117  protected override void InitializeTransactionThread()
118  {
119  // nop
120  }
121  }
122 }