forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassicRenkoConsolidatorAlgorithm.py
71 lines (55 loc) · 3.05 KB
/
ClassicRenkoConsolidatorAlgorithm.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
# 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>
### Demonstration of how to initialize and use the Classic RenkoConsolidator
### </summary>
### <meta name="tag" content="renko" />
### <meta name="tag" content="indicators" />
### <meta name="tag" content="using data" />
### <meta name="tag" content="consolidating data" />
class ClassicRenkoConsolidatorAlgorithm(QCAlgorithm):
'''Demonstration of how to initialize and use the RenkoConsolidator'''
def initialize(self):
self.set_start_date(2012, 1, 1)
self.set_end_date(2013, 1, 1)
self.add_equity("SPY", Resolution.DAILY)
# this is the simple constructor that will perform the
# renko logic to the Value property of the data it receives.
# break SPY into $2.5 renko bricks and send that data to our 'OnRenkoBar' method
renko_close = ClassicRenkoConsolidator(2.5)
renko_close.data_consolidated += self.handle_renko_close
self.subscription_manager.add_consolidator("SPY", renko_close)
# this is the full constructor that can accept a value selector and a volume selector
# this allows us to perform the renko logic on values other than Close, even computed values!
# break SPY into (2*o + h + l + 3*c)/7
renko7bar = ClassicRenkoConsolidator(2.5, lambda x: (2 * x.open + x.high + x.low + 3 * x.close) / 7, lambda x: x.volume)
renko7bar.data_consolidated += self.handle_renko7_bar
self.subscription_manager.add_consolidator("SPY", renko7bar)
# We're doing our analysis in the on_renko_bar method, but the framework verifies that this method exists, so we define it.
def on_data(self, data):
pass
def handle_renko_close(self, sender, data):
'''This function is called by our renko_close consolidator defined in Initialize()
Args:
data: The new renko bar produced by the consolidator'''
if not self.portfolio.invested:
self.set_holdings(data.symbol, 1)
self.log(f"CLOSE - {data.time} - {data.open} {data.close}")
def handle_renko7_bar(self, sender, data):
'''This function is called by our renko7bar consolidator defined in Initialize()
Args:
data: The new renko bar produced by the consolidator'''
if self.portfolio.invested:
self.liquidate(data.symbol)
self.log(f"7BAR - {data.time} - {data.open} {data.close}")