Range Consolidators
Classic Range Consolidators
Introduction
Most Range consolidators aggregate bars based on a fixed price range movement. The ClassicRangeConsolidator
produces a different type of Range bars than the RangeConsolidator
. A ClassicRangeConsolidator
with a bar size of $1 produces a new bar that spans $1 every time an asset's price range moves $1. If the price jumps multiple dollars in a single tick, the ClassicRangeConsolidator
only produces one bar per time step where the open of each bar matches the close of the previous bar.
Consolidate Trade Bars
TradeBar
consolidators aggregate TradeBar
objects into RangeBar
objects. Follow these steps to create and manage a TradeBar
consolidator based on the preceding Range bar rules:
- Create the consolidator.
- It uses the
Value
value
property of theIBaseData
object it receives to build the Range bars - It ignores the volume of the input data
- It enforces the high and low of each bar to be a multiple of the bar size
- 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 classic Range consolidator, pass the bar size to the ClassicRangeConsolidator
constructor.
// Create a Classic Range consolidator that emits a bar when the price range moves $1 _consolidator = new ClassicRangeConsolidator(1m);
# Create a Classic Range consolidator that emits a bar when the price range moves $1 self._consolidator = ClassicRangeConsolidator(1)
The ClassicRangeConsolidator
has the following default behavior:
To build the Range bars with a different property than the Value
value
of the IBaseData
object, provide a selector
argument. The selector
should be a function that receives the IBaseData
object and returns a decimal value.
self._consolidator = ClassicRangeConsolidator(1, selector=lambda data: data.high)
_consolidator = new ClassicRangeConsolidator(1m, data => (data as TradeBar).High);
To add a non-zero Volume
volume
property to the Range bars, provide a volumeSelector
volume_selector
argument. The volumeSelector
volume_selector
should be a function that receives the IBaseData
object and returns a decimal value.
self._consolidator = ClassicRangeConsolidator(1, volume_selector=lambda data: data.volume)
_consolidator = new ClassicRangeConsolidator(1m, null, data => (data as TradeBar).Volume);
_consolidator.DataConsolidated += ConsolidationHandler;
self._consolidator.data_consolidated += 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, RangeBar consolidatedBar) { }
def _consolidation_handler(self, sender: object, consolidated_bar: RangeBar) -> None: pass
The consolidation event handler receives bars when the price movement forms a new classic Range bar.
You can automatically or manually update the consolidator.
To automatically update a consolidator with data from the security subscription, call the AddConsolidator
add_consolidator
method of the Subscription Manager.
self.subscription_manager.add_consolidator(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
update
method with a TradeBar
object. You can update the consolidator with data from the Slice object in the OnData
on_data
method or with data from a history request.
# Example 1: Update the consolidator with data from the Slice object def on_data(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.subscription_manager.remove_consolidator(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 Quote Bars
QuoteBar
consolidators aggregate QuoteBar
objects into RangeBar
objects. Follow these steps to create and manage a QuoteBar
consolidator based on the preceding Range bar rules:
- Create the consolidator.
- It uses the
Value
value
property of theIBaseData
object it receives to build the Range bars - It ignores the volume of the input data
- It enforces the high and low of each bar to be a multiple of the bar size
- 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 classic Range consolidator, pass the bar size to the ClassicRangeConsolidator
constructor.
// Create a Classic Range consolidator that emits a bar when the price range moves $1 _consolidator = new ClassicRangeConsolidator(1m);
# Create a Classic Range consolidator that emits a bar when the price range moves $1 self._consolidator = ClassicRangeConsolidator(1)
The ClassicRangeConsolidator
has the following default behavior:
The following arguments enable you to create Range bars that aggregate the excessive bid-ask spread on the asset. It can help market-making algorithms to track wider spread opportunities.
self._consolidator = ClassicRangeConsolidator(10, lambda data: data.value, lambda data: data.last_bid_size - data.last_ask_size)
_consolidator = new ClassicRangeConsolidator(10, null, data => (data as QuoteBar).LastBidSize - (data as QuoteBar).LastAskSize);
_consolidator.DataConsolidated += ConsolidationHandler;
self._consolidator.data_consolidated += 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, RangeBar consolidatedBar) { }
def _consolidation_handler(self, sender: object, consolidated_bar: RangeBar) -> None: pass
The consolidation event handler receives bars when the price movement forms a new classic Range bar.
You can automatically or manually update the consolidator.
To automatically update a consolidator with data from the security subscription, call the AddConsolidator
add_consolidator
method of the Subscription Manager.
self.subscription_manager.add_consolidator(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
update
method with a QuoteBar
object. You can update the consolidator with data from the Slice object in the OnData
on_data
method or with data from a history request.
# Example 1: Update the consolidator with data from the Slice object def on_data(self, slice: Slice) -> None: quote_bar = slice.quote_bars[self._symbol] self._consolidator.update(quote_bar) # Example 2: Update the consolidator with data from a history request history = self.history[QuoteBar](self._symbol, 30, Resolution.MINUTE) for quote_bar in history: self._consolidator.update(quote_bar)
// Example 1: Update the consolidator with data from the Slice object public override void OnData(Slice slice) { var quoteBar = slice.QuoteBars[_symbol]; _consolidator.Update(quoteBar); } // Example 2: Update the consolidator with data from a history request var history = History<QuoteBar>(_symbol, 30, Resolution.Minute); foreach (var quoteBar in history) { _consolidator.Update(quoteBar); }
SubscriptionManager.RemoveConsolidator(_symbol, _consolidator);
self.subscription_manager.remove_consolidator(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 RangeBar
objects. Follow these steps to create and manage a Tick
consolidator based on the preceding Range bar rules:
- Create the consolidator.
- It uses the
Value
value
property of theIBaseData
object it receives to build the Range bars - It ignores the volume of the input data
- It enforces the high and low of each bar to be a multiple of the bar size
- 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 classic Range consolidator, pass the bar size to the ClassicRangeConsolidator
constructor.
// Create a Classic Range consolidator that emits a bar when the price range moves $1 _consolidator = new ClassicRangeConsolidator(1m);
# Create a Classic Range consolidator that emits a bar when the price range moves $1 self._consolidator = ClassicRangeConsolidator(1)
The ClassicRangeConsolidator
has the following default behavior:
_consolidator.DataConsolidated += ConsolidationHandler;
self._consolidator.data_consolidated += 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, RangeBar consolidatedBar) { }
def _consolidation_handler(self, sender: object, consolidated_bar: RangeBar) -> None: pass
The consolidation event handler receives bars when the price movement forms a new classic Range bar.
You can automatically or manually update the consolidator.
To automatically update a consolidator with data from the security subscription, call the AddConsolidator
add_consolidator
method of the Subscription Manager.
self.subscription_manager.add_consolidator(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
update
method with a Tick
object. You can update the consolidator with data from the Slice object in the OnData
on_data
method or with data from a history request.
# Example 1: Update the consolidator with data from the Slice object def on_data(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.subscription_manager.remove_consolidator(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 Quote Ticks
Tick
quote bar consolidators aggregate Tick
objects that represent quotes into RangeBar
objects. Follow these steps to create and manage a Tick
quote bar consolidator based on the preceding Range bar rules:
- Create the consolidator.
- It uses the
Value
value
property of theIBaseData
object it receives to build the Range bars - It ignores the volume of the input data
- It enforces the high and low of each bar to be a multiple of the bar size
- 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 classic Range consolidator, pass the bar size to the ClassicRangeConsolidator
constructor.
// Create a Classic Range consolidator that emits a bar when the price range moves $1 _consolidator = new ClassicRangeConsolidator(1m);
# Create a Classic Range consolidator that emits a bar when the price range moves $1 self._consolidator = ClassicRangeConsolidator(1)
The ClassicRangeConsolidator
has the following default behavior:
_consolidator.DataConsolidated += ConsolidationHandler;
self._consolidator.data_consolidated += 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, RangeBar consolidatedBar) { }
def _consolidation_handler(self, sender: object, consolidated_bar: RangeBar) -> None: pass
The consolidation event handler receives bars when the price movement forms a new classic Range bar.
You can automatically or manually update the consolidator.
To automatically update a consolidator with data from the security subscription, call the AddConsolidator
add_consolidator
method of the Subscription Manager.
self.subscription_manager.add_consolidator(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
update
method with a Tick
object. You can update the consolidator with data from the Slice object in the OnData
on_data
method or with data from a history request.
# Example 1: Update the consolidator with data from the Slice object def on_data(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.subscription_manager.remove_consolidator(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.