-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Expand file tree
/
Copy pathPythonDictionaryFeatureRegressionAlgorithm.py
More file actions
152 lines (113 loc) · 6.19 KB
/
Copy pathPythonDictionaryFeatureRegressionAlgorithm.py
File metadata and controls
152 lines (113 loc) · 6.19 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
150
151
152
# 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>
### Example algorithm showing that Slice, Securities and Portfolio behave as a Python Dictionary
### </summary>
class PythonDictionaryFeatureRegressionAlgorithm(QCAlgorithm):
'''Example algorithm showing that Slice, Securities and Portfolio behave as a Python Dictionary'''
def initialize(self):
self.set_start_date(2013,10, 7) #Set Start Date
self.set_end_date(2013,10,11) #Set End Date
self.set_cash(100000) #Set Strategy Cash
self.spy_symbol = self.add_equity("SPY").symbol
self.ibm_symbol = self.add_equity("IBM").symbol
self.aig_symbol = self.add_equity("AIG").symbol
self.aapl_symbol = Symbol.create("AAPL", SecurityType.EQUITY, Market.USA)
date_rules = self.date_rules.on(2013, 10, 7)
self.schedule.on(date_rules, self.time_rules.at(13, 0), self.test_securities_dictionary)
self.schedule.on(date_rules, self.time_rules.at(14, 0), self.test_portfolio_dictionary)
self.schedule.on(date_rules, self.time_rules.at(15, 0), self.test_slice_dictionary)
def test_slice_dictionary(self):
slice = self.current_slice
symbols = ', '.join([f'{x}' for x in slice.keys()])
slice_data = ', '.join([f'{x}' for x in slice.values()])
slice_bars = ', '.join([f'{x}' for x in slice.bars.values()])
if "SPY" not in slice:
raise AssertionError('SPY (string) is not in Slice')
if self.spy_symbol not in slice:
raise AssertionError('SPY (Symbol) is not in Slice')
spy = slice.get(self.spy_symbol)
if spy is None:
raise AssertionError('SPY is not in Slice')
if slice.contains_key(None):
raise AssertionError('Slice.contains_key(None) should return False instead of throwing')
if slice.get(None) is not None:
raise AssertionError('Slice.get(None) should return None instead of throwing')
if slice.bars.contains_key(None):
raise AssertionError('TradeBars.contains_key(None) should return False instead of throwing')
for symbol, bar in slice.bars.items():
self.plot(symbol, 'Price', bar.close)
def test_securities_dictionary(self):
symbols = ', '.join([f'{x}' for x in self.securities.keys()])
leverages = ', '.join([str(x.get_last_data()) for x in self.securities.values()])
if "IBM" not in self.securities:
raise AssertionError('IBM (string) is not in Securities')
if self.ibm_symbol not in self.securities:
raise AssertionError('IBM (Symbol) is not in Securities')
ibm = self.securities.get(self.ibm_symbol)
if ibm is None:
raise AssertionError('ibm is None')
aapl = self.securities.get(self.aapl_symbol)
if aapl is not None:
raise AssertionError('aapl is not None')
# A None key should behave like a missing key instead of throwing,
# e.g. when a symbol field is only assigned later in the algorithm
none_symbol = None
price = self.securities[none_symbol].price if self.securities.contains_key(none_symbol) else None
if price is not None:
raise AssertionError('Securities.contains_key(None) should return False instead of throwing')
if self.securities.get(none_symbol) is not None:
raise AssertionError('Securities.get(None) should return None instead of throwing')
for symbol, security in self.securities.items():
self.plot(symbol, 'Price', security.price)
def test_portfolio_dictionary(self):
symbols = ', '.join([f'{x}' for x in self.portfolio.keys()])
leverages = ', '.join([f'{x.symbol}: {x.leverage}' for x in self.portfolio.values()])
if "AIG" not in self.securities:
raise AssertionError('AIG (string) is not in Portfolio')
if self.aig_symbol not in self.securities:
raise AssertionError('AIG (Symbol) is not in Portfolio')
aig = self.portfolio.get(self.aig_symbol)
if aig is None:
raise AssertionError('aig is None')
aapl = self.portfolio.get(self.aapl_symbol)
if aapl is not None:
raise AssertionError('aapl is not None')
if self.portfolio.contains_key(None):
raise AssertionError('Portfolio.contains_key(None) should return False instead of throwing')
if self.portfolio.get(None) is not None:
raise AssertionError('Portfolio.get(None) should return None instead of throwing')
for symbol, holdings in self.portfolio.items():
msg = f'{symbol}: {holdings.leverage}'
def on_end_of_algorithm(self):
portfolio_copy = self.portfolio.copy()
try:
self.portfolio.clear() # Throws exception
except Exception as e:
self.debug(e)
bar = self.securities.pop("SPY")
length = len(self.securities)
if length != 2:
raise AssertionError(f'After popping SPY, Securities should have 2 elements, {length} found')
securities_copy = self.securities.copy()
self.securities.clear() # Does not throw
def on_data(self, data):
'''on_data event is the primary entry point for your algorithm. Each new data point will be pumped in here.
Arguments:
data: Slice object keyed by symbol containing the stock data
'''
if not self.portfolio.invested:
self.set_holdings("SPY", 1/3)
self.set_holdings("IBM", 1/3)
self.set_holdings("AIG", 1/3)