/
SpreadExecutionModel.cs
91 lines (82 loc) · 4.11 KB
/
SpreadExecutionModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* 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 QuantConnect.Orders;
using QuantConnect.Securities;
using QuantConnect.Algorithm.Framework.Portfolio;
namespace QuantConnect.Algorithm.Framework.Execution
{
/// <summary>
/// Execution model that submits orders while the current spread is in desirably tight extent.
/// </summary>
/// <remarks>Note this execution model will not work using <see cref="Resolution.Daily"/>
/// since Exchange.ExchangeOpen will be false, suggested resolution is <see cref="Resolution.Minute"/></remarks>
public class SpreadExecutionModel : ExecutionModel
{
private readonly decimal _acceptingSpreadPercent;
private readonly PortfolioTargetCollection _targetsCollection;
/// <summary>
/// Initializes a new instance of the <see cref="SpreadExecutionModel"/> class
/// </summary>
/// <param name="acceptingSpreadPercent">Maximum spread accepted comparing to current price in percentage.</param>
public SpreadExecutionModel(decimal acceptingSpreadPercent = 0.005m)
{
_acceptingSpreadPercent = Math.Abs(acceptingSpreadPercent);
_targetsCollection = new PortfolioTargetCollection();
}
/// <summary>
/// Submit orders for the specified portfolio targets if the spread is tighter/equal to preset level
/// </summary>
/// <param name="algorithm">The algorithm instance</param>
/// <param name="targets">The portfolio targets to be ordered</param>
public override void Execute(QCAlgorithm algorithm, IPortfolioTarget[] targets)
{
// update the complete set of portfolio targets with the new targets
_targetsCollection.AddRange(targets);
// for performance we check count value, OrderByMarginImpact and ClearFulfilled are expensive to call
if (!_targetsCollection.IsEmpty)
{
foreach (var target in _targetsCollection.OrderByMarginImpact(algorithm))
{
var symbol = target.Symbol;
// calculate remaining quantity to be ordered
var unorderedQuantity = OrderSizing.GetUnorderedQuantity(algorithm, target);
if (unorderedQuantity != 0)
{
// get security object
var security = algorithm.Securities[symbol];
// check order entry conditions
if (PriceIsFavorable(security))
{
algorithm.MarketOrder(symbol, unorderedQuantity);
}
}
}
_targetsCollection.ClearFulfilled(algorithm);
}
}
/// <summary>
/// Determines if the current spread is equal or tighter than preset level
/// </summary>
protected virtual bool PriceIsFavorable(Security security)
{
// Has to be in opening hours of exchange to avoid extreme spread in OTC period
// Price has to be larger than zero to avoid zero division error, or negative price causing the spread percentage lower than preset value by accident
return security.Exchange.ExchangeOpen
&& security.Price > 0 && security.AskPrice > 0 && security.BidPrice > 0
&& (security.AskPrice - security.BidPrice) / security.Price <= _acceptingSpreadPercent;
}
}
}