Lean
$LEAN_TAG$
FuturesContract.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
QuantConnect
.
Data
.
UniverseSelection
;
17
18
namespace
QuantConnect.Data.Market
19
{
20
/// <summary>
21
/// Defines a single futures contract at a specific expiration
22
/// </summary>
23
public
class
FuturesContract
:
BaseContract
24
{
25
private
FutureUniverse
_universeData;
26
private
TradeBar
_tradeBar;
27
private
QuoteBar
_quoteBar;
28
private
Tick
_tradeTick;
29
private
Tick
_quoteTick;
30
private
Tick
_openInterest;
31
32
/// <summary>
33
/// Gets the open interest
34
/// </summary>
35
public
override
decimal
OpenInterest
36
{
37
get
38
{
39
// Contract universe data is prioritized
40
if
(_universeData !=
null
)
41
{
42
return
_universeData.
OpenInterest
;
43
}
44
return
_openInterest?.
Value
?? decimal.Zero;
45
}
46
}
47
48
/// <summary>
49
/// Gets the last price this contract traded at
50
/// </summary>
51
public
override
decimal
LastPrice
52
{
53
get
54
{
55
if
(_universeData !=
null
)
56
{
57
return
_universeData.
Close
;
58
}
59
60
if
(_tradeBar ==
null
&& _tradeTick ==
null
)
61
{
62
return
decimal.Zero;
63
}
64
if
(_tradeBar !=
null
)
65
{
66
return
_tradeTick !=
null
&& _tradeTick.
EndTime
> _tradeBar.
EndTime
? _tradeTick.
Price
: _tradeBar.
Close
;
67
}
68
return
_tradeTick.
Price
;
69
}
70
}
71
72
/// <summary>
73
/// Gets the last volume this contract traded at
74
/// </summary>
75
public
override
long
Volume
76
{
77
get
78
{
79
if
(_universeData !=
null
)
80
{
81
return
(
long
)_universeData.
Volume
;
82
}
83
return
(
long
)(_tradeBar?.
Volume
?? 0);
84
}
85
}
86
87
/// <summary>
88
/// Get the current bid price
89
/// </summary>
90
public
override
decimal
BidPrice
91
{
92
get
93
{
94
if
(_universeData !=
null
)
95
{
96
return
_universeData.
Close
;
97
}
98
if
(_quoteBar ==
null
&& _quoteTick ==
null
)
99
{
100
return
decimal.Zero;
101
}
102
if
(_quoteBar !=
null
)
103
{
104
return
_quoteTick !=
null
&& _quoteTick.
EndTime
> _quoteBar.
EndTime
? _quoteTick.
BidPrice
: _quoteBar.
Bid
.
Close
;
105
}
106
return
_quoteTick.
BidPrice
;
107
}
108
}
109
110
/// <summary>
111
/// Get the current bid size
112
/// </summary>
113
public
override
long
BidSize
114
{
115
get
116
{
117
if
(_quoteBar ==
null
&& _quoteTick ==
null
)
118
{
119
return
0;
120
}
121
if
(_quoteBar !=
null
)
122
{
123
return
(
long
)(_quoteTick !=
null
&& _quoteTick.
EndTime
> _quoteBar.
EndTime
? _quoteTick.
BidSize
: _quoteBar.
LastBidSize
);
124
}
125
return
(
long
)_quoteTick.
BidSize
;
126
}
127
}
128
129
/// <summary>
130
/// Gets the current ask price
131
/// </summary>
132
public
override
decimal
AskPrice
133
{
134
get
135
{
136
if
(_universeData !=
null
)
137
{
138
return
_universeData.
Close
;
139
}
140
if
(_quoteBar ==
null
&& _quoteTick ==
null
)
141
{
142
return
decimal.Zero;
143
}
144
if
(_quoteBar !=
null
)
145
{
146
return
_quoteTick !=
null
&& _quoteTick.
EndTime
> _quoteBar.
EndTime
? _quoteTick.
AskPrice
: _quoteBar.
Ask
.
Close
;
147
}
148
return
_quoteTick.
AskPrice
;
149
}
150
}
151
152
/// <summary>
153
/// Get the current ask size
154
/// </summary>
155
public
override
long
AskSize
156
{
157
get
158
{
159
if
(_quoteBar ==
null
&& _quoteTick ==
null
)
160
{
161
return
0;
162
}
163
if
(_quoteBar !=
null
)
164
{
165
return
(
long
)(_quoteTick !=
null
&& _quoteTick.
EndTime
> _quoteBar.
EndTime
? _quoteTick.
AskSize
: _quoteBar.
LastAskSize
);
166
}
167
return
(
long
)_quoteTick.
AskSize
;
168
}
169
}
170
171
/// <summary>
172
/// Initializes a new instance of the <see cref="FuturesContract"/> class
173
/// </summary>
174
/// <param name="symbol">The futures contract symbol</param>
175
public
FuturesContract
(
Symbol
symbol)
176
: base(symbol)
177
{
178
}
179
180
/// <summary>
181
/// Initializes a new instance of the <see cref="FuturesContract"/> class
182
/// </summary>
183
/// <param name="contractData">The contract universe data</param>
184
public
FuturesContract
(
FutureUniverse
contractData)
185
: base(contractData.
Symbol
)
186
{
187
_universeData = contractData;
188
}
189
190
/// <summary>
191
/// Implicit conversion into <see cref="Symbol"/>
192
/// </summary>
193
/// <param name="contract">The option contract to be converted</param>
194
public
static
implicit
operator
Symbol
(
FuturesContract
contract)
195
{
196
return
contract.
Symbol
;
197
}
198
199
/// <summary>
200
/// Updates the future contract with the new data, which can be a <see cref="Tick"/> or <see cref="TradeBar"/> or <see cref="QuoteBar"/>
201
/// </summary>
202
internal
override
void
Update(
BaseData
data)
203
{
204
switch
(data)
205
{
206
case
TradeBar
tradeBar:
207
_tradeBar = tradeBar;
208
break
;
209
210
case
QuoteBar
quoteBar:
211
_quoteBar = quoteBar;
212
break
;
213
214
case
Tick
tick when tick.
TickType
==
TickType
.Trade:
215
_tradeTick = tick;
216
break
;
217
218
case
Tick
tick when tick.
TickType
==
TickType
.Quote:
219
_quoteTick = tick;
220
break
;
221
222
case
Tick
tick when tick.
TickType
==
TickType
.OpenInterest:
223
_openInterest = tick;
224
break
;
225
}
226
}
227
}
228
}
Common
Data
Market
FuturesContract.cs
Generated by
1.8.17