Files
quantconnect--lean/Algorithm.Python/RangeConsolidatorAlgorithm.py
T
Ricardo Andrés Marino Rojas 3e9fc57bb0
Benchmarks / build (push) Has been cancelled
Build & Test Lean / build (push) Has been cancelled
Regression Tests / build (push) Has been cancelled
Research Regression Tests / build (push) Has been cancelled
Python Virtual Environments / build (push) Has been cancelled
Implement RangeConsolidator (#7385)
* Implement RangeConsolidator

It turned out that the behavior of RangeConsolidator was similar to
ClassicRenkoConsolidator. Then, some of the ClassicRenkoConsolidator
methods, were abstracted to new class called BaseTimelessConsolidator.cs,
from which both RangeConsolidator and ClassicRenkoConsolidator could
inherit, therefore resusing code. The following tasks were done:

- Create RangeConsolidator.cs
- Create RangeBar.cs
- Create BaseTimelessConsolidator.cs
- Create RangeConsolidatorTests.cs
- Modify ClassicRenkoConsolidator.cs

* Allow intermediate/Phantom RangeBar's

- Enhance unit tests
- Nit changes
- Allow intermediate/Phantom RangeBar's on RangeConsolidator

* Nit changes

* Create ClassicRangeConsolidator and more changes

- Add regression tests
- Enhance unit tests

* Address required changes

* Address requested changes

* Address requested changes

* Add regression tests with Tick Resolution

* Address required changes

* Increase Range for RangeConsolidatorWithTickAlgo

* Add more unit tests and solve bugs

* Nit change
2023-07-27 10:02:02 -03:00

52 lines
2.2 KiB
Python

# 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 of how to use RangeConsolidator
### </summary>
class RangeConsolidatorAlgorithm(QCAlgorithm):
def GetResolution(self):
return Resolution.Daily
def GetRange(self):
return 100;
def Initialize(self):
self.SetStartAndEndDates();
self.AddEquity("SPY", self.GetResolution())
rangeConsolidator = self.CreateRangeConsolidator()
rangeConsolidator.DataConsolidated += self.OnDataConsolidated
self.firstDataConsolidated = None;
self.SubscriptionManager.AddConsolidator("SPY", rangeConsolidator)
def SetStartAndEndDates(self):
self.SetStartDate(2013, 10, 7)
self.SetEndDate(2013, 10, 11)
def OnEndOfAlgorithm(self):
if self.firstDataConsolidated == None:
raise Exception("The consolidator should have consolidated at least one RangeBar, but it did not consolidated any one")
def CreateRangeConsolidator(self):
return RangeConsolidator(self.GetRange())
def OnDataConsolidated(self, sender, rangeBar):
if (self.firstDataConsolidated is None):
self.firstDataConsolidated = rangeBar
if round(rangeBar.High - rangeBar.Low, 2) != self.GetRange() * 0.01: # The minimum price change for SPY is 0.01, therefore the range size of each bar equals Range * 0.01
raise Exception(f"The difference between the High and Low for all RangeBar's should be {self.GetRange() * 0.01}, but for this RangeBar was {round(rangeBar.Low - rangeBar.High, 2)}")