Renko Consolidators

Renko Consolidators

Introduction

Most Renko consolidators aggregate bars based on a fixed price movement. The RenkoConsolidator produces Renko bars by their traditional definition. In the case of a $1 bar size, the RenkoConsolidator produces bars that have a body spanning $1. The opening price of the first bar is set to the closest $1 multiple of the first trade. When the price moves by at least $1, the first bar closes. If a bar is a rising bar, the following bar closes when the price moves $1 above the closing price of the previous bar or $1 below the opening price of the previous bar. If a bar is a falling bar, the following bar closes when the price moves $1 below the closing price of the previous bar or $1 above the opening price of the previous bar. If the price jumps multiple dollars in a single tick, the RenkoConsolidator produces multiple $1 bars in a single time step.

RenkoBar sample

Consolidate Trade Bars

TradeBar consolidators aggregate TradeBar objects into RenkoBar objects. Follow these steps to create and manage a TradeBar consolidator based on the traditional Renko bar rules:

  1. Create the consolidator.
  2. To create a Renko consolidator, pass the bar size to the RenkoConsolidator constructor.

    // Create a Renko consolidator that emits a bar when the price moves $1
    _consolidator = new RenkoConsolidator(1m);
    # Create a Renko consolidator that emits a bar when the price moves $1
    self._consolidator = RenkoConsolidator(1)
  3. Add an event handler to the consolidator.
  4. _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 ConsolidationHandlerself._consolidation_handler, not ConsolidationHandler()self._consolidation_handler().

  5. Define the consolidation handler.
  6. void ConsolidationHandler(object sender, RenkoBar consolidatedBar)
    {
    
    }
    def _consolidation_handler(self, sender: object, consolidated_bar: RenkoBar) -> None:
        pass

    The consolidation event handler receives bars when the price movement forms a new Renko bar.

  7. Update the consolidator.
  8. You can automatically or manually update the consolidator.

    • Automatic Updates
    • To automatically update a consolidator with data from the security subscription, call the AddConsolidatoradd_consolidator method of the Subscription Manager.

      self.subscription_manager.add_consolidator(self._symbol, self._consolidator)
      SubscriptionManager.AddConsolidator(_symbol, _consolidator);
    • Manual Updates
    • 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 Updateupdate method with a TradeBar object. You can update the consolidator with data from the Slice object in the OnDataon_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);
      }
  9. 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.
  10. 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 RenkoBar objects. Follow these steps to create and manage a QuoteBar consolidator based on the traditional Renko bar rules:

  1. Create the consolidator.
  2. To create a Renko consolidator, pass the bar size to the RenkoConsolidator constructor.

    // Create a Renko consolidator that emits a bar when the price moves $1
    _consolidator = new RenkoConsolidator(1m);
    # Create a Renko consolidator that emits a bar when the price moves $1
    self._consolidator = RenkoConsolidator(1)
  3. Add an event handler to the consolidator.
  4. _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 ConsolidationHandlerself._consolidation_handler, not ConsolidationHandler()self._consolidation_handler().

  5. Define the consolidation handler.
  6. void ConsolidationHandler(object sender, RenkoBar consolidatedBar)
    {
    
    }
    def _consolidation_handler(self, sender: object, consolidated_bar: RenkoBar) -> None:
        pass

    The consolidation event handler receives bars when the price movement forms a new Renko bar.

  7. Update the consolidator.
  8. You can automatically or manually update the consolidator.

    • Automatic Updates
    • To automatically update a consolidator with data from the security subscription, call the AddConsolidatoradd_consolidator method of the Subscription Manager.

      self.subscription_manager.add_consolidator(self._symbol, self._consolidator)
      SubscriptionManager.AddConsolidator(_symbol, _consolidator);
    • Manual Updates
    • 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 Updateupdate method with a QuoteBar object. You can update the consolidator with data from the Slice object in the OnDataon_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);
      }
  9. 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.
  10. 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 RenkoBar objects. Follow these steps to create and manage a Tick consolidator based on the traditional Renko bar rules:

  1. Create the consolidator.
  2. To create a Renko consolidator, pass the bar size to the RenkoConsolidator constructor.

    // Create a Renko consolidator that emits a bar when the price moves $1
    _consolidator = new RenkoConsolidator(1m);
    # Create a Renko consolidator that emits a bar when the price moves $1
    self._consolidator = RenkoConsolidator(1)
  3. Add an event handler to the consolidator.
  4. _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 ConsolidationHandlerself._consolidation_handler, not ConsolidationHandler()self._consolidation_handler().

  5. Define the consolidation handler.
  6. void ConsolidationHandler(object sender, RenkoBar consolidatedBar)
    {
    
    }
    def _consolidation_handler(self, sender: object, consolidated_bar: RenkoBar) -> None:
        pass

    The consolidation event handler receives bars when the price movement forms a new Renko bar.

  7. Update the consolidator.
  8. You can automatically or manually update the consolidator.

    • Automatic Updates
    • To automatically update a consolidator with data from the security subscription, call the AddConsolidatoradd_consolidator method of the Subscription Manager.

      self.subscription_manager.add_consolidator(self._symbol, self._consolidator)
      SubscriptionManager.AddConsolidator(_symbol, _consolidator);
    • Manual Updates
    • 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 Updateupdate method with a Tick object. You can update the consolidator with data from the Slice object in the OnDataon_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);
      }
  9. 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.
  10. 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 RenkoBar objects. Follow these steps to create and manage a Tick quote bar consolidator based on the traditional Renko bar rules:

  1. Create the consolidator.
  2. To create a Renko consolidator, pass the bar size to the RenkoConsolidator constructor.

    // Create a Renko consolidator that emits a bar when the price moves $1
    _consolidator = new RenkoConsolidator(1m);
    # Create a Renko consolidator that emits a bar when the price moves $1
    self._consolidator = RenkoConsolidator(1)
  3. Add an event handler to the consolidator.
  4. _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 ConsolidationHandlerself._consolidation_handler, not ConsolidationHandler()self._consolidation_handler().

  5. Define the consolidation handler.
  6. void ConsolidationHandler(object sender, RenkoBar consolidatedBar)
    {
    
    }
    def _consolidation_handler(self, sender: object, consolidated_bar: RenkoBar) -> None:
        pass

    The consolidation event handler receives bars when the price movement forms a new Renko bar.

  7. Update the consolidator.
  8. You can automatically or manually update the consolidator.

    • Automatic Updates
    • To automatically update a consolidator with data from the security subscription, call the AddConsolidatoradd_consolidator method of the Subscription Manager.

      self.subscription_manager.add_consolidator(self._symbol, self._consolidator)
      SubscriptionManager.AddConsolidator(_symbol, _consolidator);
    • Manual Updates
    • 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 Updateupdate method with a Tick object. You can update the consolidator with data from the Slice object in the OnDataon_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);
      }
  9. 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.
  10. 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.

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: