Implement RangeConsolidator (#7385)
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

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
This commit is contained in:
Ricardo Andrés Marino Rojas
2023-07-27 08:02:02 -05:00
committed by GitHub
parent e442586a0d
commit 3e9fc57bb0
16 changed files with 1304 additions and 125 deletions
@@ -0,0 +1,30 @@
# 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 *
from RangeConsolidatorAlgorithm import RangeConsolidatorAlgorithm
### <summary>
### Example algorithm of how to use ClassicRangeConsolidator
### </summary>
class ClassicRangeConsolidatorAlgorithm(RangeConsolidatorAlgorithm):
def CreateRangeConsolidator(self):
return ClassicRangeConsolidator(self.GetRange())
def OnDataConsolidated(self, sender, rangeBar):
super().OnDataConsolidated(sender, rangeBar)
if rangeBar.Volume == 0:
raise Exception("All RangeBar's should have non-zero volume, but this doesn't")
@@ -0,0 +1,16 @@
from AlgorithmImports import *
from RangeConsolidatorWithTickAlgorithm import RangeConsolidatorWithTickAlgorithm
### <summary>
### Example algorithm of how to use ClassicRangeConsolidator with Tick resolution
### </summary>
class ClassicRangeConsolidatorWithTickAlgorithm(RangeConsolidatorWithTickAlgorithm):
def CreateRangeConsolidator(self):
return ClassicRangeConsolidator(self.GetRange())
def OnDataConsolidated(self, sender, rangeBar):
super().OnDataConsolidated(sender, rangeBar)
if rangeBar.Volume == 0:
raise Exception("All RangeBar's should have non-zero volume, but this doesn't")
@@ -0,0 +1,51 @@
# 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)}")
@@ -0,0 +1,17 @@
from AlgorithmImports import *
from RangeConsolidatorAlgorithm import RangeConsolidatorAlgorithm
### <summary>
### Example algorithm of how to use RangeConsolidator with Tick resolution
### </summary>
class RangeConsolidatorWithTickAlgorithm(RangeConsolidatorAlgorithm):
def GetRange(self):
return 5
def GetResolution(self):
return Resolution.Tick
def SetStartAndEndDates(self):
self.SetStartDate(2013, 10, 7)
self.SetEndDate(2013, 10, 7)