forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCustomModelsAlgorithm.cs
More file actions
149 lines (127 loc) · 5.49 KB
/
CustomModelsAlgorithm.cs
File metadata and controls
149 lines (127 loc) · 5.49 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* 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 System.Collections.Generic;
using QuantConnect.Data.Market;
using QuantConnect.Orders;
using QuantConnect.Orders.Fees;
using QuantConnect.Orders.Fills;
using QuantConnect.Orders.Slippage;
using QuantConnect.Securities;
namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// This algorithm shows how you can define your own custom models.
/// </summary>
public class CustomModelsAlgorithm : QCAlgorithm
{
private Security _security;
private Symbol _spy = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA);
public override void Initialize()
{
SetStartDate(2012, 01, 01);
SetEndDate(2012, 02, 01);
AddSecurity(SecurityType.Equity, "SPY", Resolution.Hour);
// set our models
_security = Securities[_spy];
_security.FeeModel = new CustomFeeModel(this);
_security.FillModel = new CustomFillModel(this);
_security.SlippageModel = new CustomSlippageModel(this);
}
public void OnData(TradeBars data)
{
var openOrders = Transactions.GetOpenOrders(_spy);
if (openOrders.Count != 0) return;
if (Time.Day > 10 && _security.Holdings.Quantity <= 0)
{
var quantity = CalculateOrderQuantity(_spy, .5m);
Log("MarketOrder: " + quantity);
MarketOrder(_spy, quantity, asynchronous: true); // async needed for partial fill market orders
}
else if (Time.Day > 20 && _security.Holdings.Quantity >= 0)
{
var quantity = CalculateOrderQuantity(_spy, -.5m);
Log("MarketOrder: " + quantity);
MarketOrder(_spy, quantity, asynchronous: true); // async needed for partial fill market orders
}
}
public class CustomFillModel : ImmediateFillModel
{
private readonly QCAlgorithm _algorithm;
private readonly Random _random = new Random(387510346); // seed it for reproducibility
private readonly Dictionary<long, decimal> _absoluteRemainingByOrderId = new Dictionary<long, decimal>();
public CustomFillModel(QCAlgorithm algorithm)
{
_algorithm = algorithm;
}
public override OrderEvent MarketFill(Security asset, MarketOrder order)
{
// this model randomly fills market orders
decimal absoluteRemaining;
if (!_absoluteRemainingByOrderId.TryGetValue(order.Id, out absoluteRemaining))
{
absoluteRemaining = order.AbsoluteQuantity;
_absoluteRemainingByOrderId.Add(order.Id, order.AbsoluteQuantity);
}
var fill = base.MarketFill(asset, order);
var absoluteFillQuantity = (int) (Math.Min(absoluteRemaining, _random.Next(0, 2*(int)order.AbsoluteQuantity)));
fill.FillQuantity = Math.Sign(order.Quantity) * absoluteFillQuantity;
if (absoluteRemaining == absoluteFillQuantity)
{
fill.Status = OrderStatus.Filled;
_absoluteRemainingByOrderId.Remove(order.Id);
}
else
{
absoluteRemaining = absoluteRemaining - absoluteFillQuantity;
_absoluteRemainingByOrderId[order.Id] = absoluteRemaining;
fill.Status = OrderStatus.PartiallyFilled;
}
_algorithm.Log("CustomFillModel: " + fill);
return fill;
}
}
public class CustomFeeModel : IFeeModel
{
private readonly QCAlgorithm _algorithm;
public CustomFeeModel(QCAlgorithm algorithm)
{
_algorithm = algorithm;
}
public decimal GetOrderFee(Security security, Order order)
{
// custom fee math
var fee = Math.Max(1m, security.Price*order.AbsoluteQuantity*0.00001m);
_algorithm.Log("CustomFeeModel: " + fee);
return fee;
}
}
public class CustomSlippageModel : ISlippageModel
{
private readonly QCAlgorithm _algorithm;
public CustomSlippageModel(QCAlgorithm algorithm)
{
_algorithm = algorithm;
}
public decimal GetSlippageApproximation(Security asset, Order order)
{
// custom slippage math
var slippage = asset.Price*0.0001m*(decimal) Math.Log10(2*(double) order.AbsoluteQuantity);
_algorithm.Log("CustomSlippageModel: " + slippage);
return slippage;
}
}
}
}