/
FutureOptionBuySellCallIntradayRegressionAlgorithm.py
77 lines (66 loc) · 3.54 KB
/
FutureOptionBuySellCallIntradayRegressionAlgorithm.py
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
# 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
from AlgorithmImports import *
### <summary>
### This regression algorithm tests In The Money (ITM) future option calls across different strike prices.
### We expect 6 orders from the algorithm, which are:
###
### * (1) Initial entry, buy ES Call Option (ES19M20 expiring ITM)
### * (2) Initial entry, sell ES Call Option at different strike (ES20H20 expiring ITM)
### * [2] Option assignment, opens a position in the underlying (ES20H20, Qty: -1)
### * [2] Future contract liquidation, due to impending expiry
### * [1] Option exercise, receive 1 ES19M20 future contract
### * [1] Liquidate ES19M20 contract, due to expiry
###
### Additionally, we test delistings for future options and assert that our
### portfolio holdings reflect the orders the algorithm has submitted.
### </summary>
class FutureOptionBuySellCallIntradayRegressionAlgorithm(QCAlgorithm):
def initialize(self):
self.set_start_date(2020, 1, 5)
self.set_end_date(2020, 6, 30)
self.es20h20 = self.add_future_contract(
Symbol.create_future(
Futures.Indices.SP_500_E_MINI,
Market.CME,
datetime(2020, 3, 20)
),
Resolution.MINUTE).symbol
self.es19m20 = self.add_future_contract(
Symbol.create_future(
Futures.Indices.SP_500_E_MINI,
Market.CME,
datetime(2020, 6, 19)
),
Resolution.MINUTE).symbol
# Select a future option expiring ITM, and adds it to the algorithm.
self.es_options = [
self.add_future_option_contract(i, Resolution.MINUTE).symbol
for i in (self.option_chain_provider.get_option_contract_list(self.es19m20, self.time) +
self.option_chain_provider.get_option_contract_list(self.es20h20, self.time))
if i.id.strike_price == 3200.0 and i.id.option_right == OptionRight.CALL
]
self.expected_contracts = [
Symbol.create_option(self.es20h20, Market.CME, OptionStyle.AMERICAN, OptionRight.CALL, 3200.0, datetime(2020, 3, 20)),
Symbol.create_option(self.es19m20, Market.CME, OptionStyle.AMERICAN, OptionRight.CALL, 3200.0, datetime(2020, 6, 19))
]
for es_option in self.es_options:
if es_option not in self.expected_contracts:
raise AssertionError(f"Contract {es_option} was not found in the chain")
self.schedule.on(self.date_rules.tomorrow, self.time_rules.after_market_open(self.es19m20, 1), self.schedule_callback_buy)
def schedule_callback_buy(self):
self.market_order(self.es_options[0], 1)
self.market_order(self.es_options[1], -1)
def on_end_of_algorithm(self):
if self.portfolio.invested:
raise AssertionError(f"Expected no holdings at end of algorithm, but are invested in: {', '.join([str(i.id) for i in self.portfolio.keys()])}")