Renko Consolidators
Volume Renko Consolidators
Introduction
Volume Renko consolidators aggregate bars based on a fixed trading volume. These types of bars are commonly known as volume bars. A VolumeRenkoConsolidator
with a bar size of 10,000 produces a new bar every time 10,000 units of the security trades in the market. If the trading volume of a single time step exceeds two step sizes (i.e. >20,000), the VolumeRenkoConsolidator
produces multiple bars in a single time step.
Consolidate Trade Bars
TradeBar
consolidators aggregate TradeBar
objects into VolumeRenkoBar
objects. Follow these steps to create and manage a TradeBar
consolidator based on custom set volume traded:
- Create the consolidator.
- Add an event handler to the consolidator.
- Define the consolidation handler.
- Update the consolidator.
- Automatic Updates
- Manual Updates
- If you create consolidators for securities in a dynamic universe and register them for automatic updates, remove the consolidator when the security leaves the universe.
To create a Volume Renko consolidator, pass the bar size to the VolumeRenkoConsolidator
constructor.
// Create a Volume Renko consolidator that emits a bar every time 10,000 units trade _consolidator = new VolumeRenkoConsolidator(10000m);
# Create a Volume Renko consolidator that emits a bar every time 10,000 units trade self.consolidator = VolumeRenkoConsolidator(10000)
_consolidator.DataConsolidated += ConsolidationHandler;
self.consolidator.DataConsolidated += self.consolidation_handler
LEAN passes consolidated bars to the consolidator event handler in your algorithm. The most common error when creating consolidators is to put parenthesis ()
at the end of your method name when setting the event handler of the consolidator. If you use parenthesis, the method executes and the result is passed as the event handler instead of the method itself. Remember to pass the name of your method to the event system. Specifically, it should be ConsolidationHandler
self.consolidation_handler
, not ConsolidationHandler()
self.consolidation_handler()
.
void ConsolidationHandler(object sender, VolumeRenkoBar consolidatedBar) { }
def consolidation_handler(self, sender: object, consolidated_bar: VolumeRenkoBar) -> None: pass
The consolidation event handler receives bars when the trading volume forms a new Volume Renko bar.
You can automatically or manually update the consolidator.
To automatically update a consolidator with data from the security subscription, call the AddConsolidator
method of the Subscription Manager.
self.SubscriptionManager.AddConsolidator(self.symbol, self.consolidator)
SubscriptionManager.AddConsolidator(_symbol, _consolidator);
Manual updates let you control when the consolidator updates and what data you use to update it. If you need to warm up a consolidator with data outside of the warm-up period, you can manually update the consolidator. To manually update a consolidator, call its Update
method with a TradeBar
object. You can update the consolidator with data from the Slice object in the OnData
method or with data from a history request.
# Example 1: Update the consolidator with data from the Slice object def OnData(self, slice: Slice) -> None: trade_bar = slice.Bars[self.symbol] self.consolidator.Update(trade_bar) # Example 2: Update the consolidator with data from a history request history = self.History[TradeBar](self.symbol, 30, Resolution.Minute) for trade_bar in history: self.consolidator.Update(trade_bar)
// Example 1: Update the consolidator with data from the Slice object public override void OnData(Slice slice) { var tradeBar = slice.Bars[_symbol]; _consolidator.Update(tradeBar); } // Example 2: Update the consolidator with data from a history request var history = History<TradeBar>(_symbol, 30, Resolution.Minute); foreach (var tradeBar in history) { _consolidator.Update(tradeBar); }
SubscriptionManager.RemoveConsolidator(_symbol, _consolidator);
self.SubscriptionManager.RemoveConsolidator(self.symbol, self.consolidator)
If you have a dynamic universe and don't remove consolidators, they compound internally, causing your algorithm to slow down and eventually die once it runs out of RAM. For an example of removing consolidators from universe subscriptions, see the GasAndCrudeOilEnergyCorrelationAlphaGasAndCrudeOilEnergyCorrelationAlpha in the LEAN GitHub repository.
Consolidate Trade Ticks
Tick
consolidators aggregate Tick
objects into VolumeRenkoBar
objects. Follow these steps to create and manage a Tick
consolidator based on custom set volume traded:
- Create the consolidator.
- Add an event handler to the consolidator.
- Define the consolidation handler.
- Update the consolidator.
- Automatic Updates
- Manual Updates
- If you create consolidators for securities in a dynamic universe and register them for automatic updates, remove the consolidator when the security leaves the universe.
To create a Volume Renko consolidator, pass the bar size to the VolumeRenkoConsolidator
constructor.
// Create a Volume Renko consolidator that emits a bar every time 10,000 units trade _consolidator = new VolumeRenkoConsolidator(10000m);
# Create a Volume Renko consolidator that emits a bar every time 10,000 units trade self.consolidator = VolumeRenkoConsolidator(10000)
_consolidator.DataConsolidated += ConsolidationHandler;
self.consolidator.DataConsolidated += self.consolidation_handler
LEAN passes consolidated bars to the consolidator event handler in your algorithm. The most common error when creating consolidators is to put parenthesis ()
at the end of your method name when setting the event handler of the consolidator. If you use parenthesis, the method executes and the result is passed as the event handler instead of the method itself. Remember to pass the name of your method to the event system. Specifically, it should be ConsolidationHandler
self.consolidation_handler
, not ConsolidationHandler()
self.consolidation_handler()
.
void ConsolidationHandler(object sender, VolumeRenkoBar consolidatedBar) { }
def consolidation_handler(self, sender: object, consolidated_bar: VolumeRenkoBar) -> None: pass
The consolidation event handler receives bars when the trading volume forms a new Volume Renko bar.
You can automatically or manually update the consolidator.
To automatically update a consolidator with data from the security subscription, call the AddConsolidator
method of the Subscription Manager.
self.SubscriptionManager.AddConsolidator(self.symbol, self.consolidator)
SubscriptionManager.AddConsolidator(_symbol, _consolidator);
Manual updates let you control when the consolidator updates and what data you use to update it. If you need to warm up a consolidator with data outside of the warm-up period, you can manually update the consolidator. To manually update a consolidator, call its Update
method with a Tick
object. You can update the consolidator with data from the Slice object in the OnData
method or with data from a history request.
# Example 1: Update the consolidator with data from the Slice object def OnData(self, slice: Slice) -> None: ticks = slice.Ticks[self.symbol] for tick in ticks: self.consolidator.Update(tick) # Example 2: Update the consolidator with data from a history request ticks = self.History[Tick](self.symbol, timedelta(minutes=3), Resolution.Tick) for tick in ticks: self.consolidator.Update(tick)
// Example 1: Update the consolidator with data from the Slice object public override void OnData(Slice slice) { var ticks = slice.Ticks[_symbol]; foreach (var tick in ticks) { _consolidator.Update(tick); } } // Example 2: Update the consolidator with data from a history request var ticks = History<Tick>(_symbol, TimeSpan.FromMinutes(3), Resolution.Tick); foreach (var tick in ticks) { _consolidator.Update(tick); }
SubscriptionManager.RemoveConsolidator(_symbol, _consolidator);
self.SubscriptionManager.RemoveConsolidator(self.symbol, self.consolidator)
If you have a dynamic universe and don't remove consolidators, they compound internally, causing your algorithm to slow down and eventually die once it runs out of RAM. For an example of removing consolidators from universe subscriptions, see the GasAndCrudeOilEnergyCorrelationAlphaGasAndCrudeOilEnergyCorrelationAlpha in the LEAN GitHub repository.