Renko Consolidators

Classic Renko Consolidators

Introduction

Renko consolidators aggregate bars based on a fixed price movement. The ClassicRenkoConsolidator produces a different type of Renko bars than the RenkoConsolidator. A ClassicRenkoConsolidator with a bar size of $1 produces a new bar that spans $1 every time an asset closes $1 away from the close of the previous bar. If the price jumps multiple dollars in a single tick, the ClassicRenkoConsolidator 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 RenkoBar objects. Follow these steps to create and manage a TradeBar consolidator based on the preceding Renko bar rules:

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

    // Create a Classic Renko consolidator that emits a bar when the price moves $1
    _consolidator = new ClassicRenkoConsolidator(1m);
    # Create a Classic Renko consolidator that emits a bar when the price moves $1
    self.consolidator = ClassicRenkoConsolidator(1)

    The ClassicRenkoConsolidator has the following default behavior:

    • It uses the Value property of the IBaseData object it receives to build the Renko bars
    • It ignores the volume of the input data
    • It enforces the open and close of each bar to be a multiple of the bar size

    To build the Renko bars with a different property than the 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 = ClassicRenkoConsolidator(1, selector = lambda data: data.High)
    _consolidator = new ClassicRenkoConsolidator(1m, data => (data as TradeBar).High);

    To add a non-zero Volume property to the Renko bars, provide a volumeSelector argument. The volumeSelector should be a function that receives the IBaseData object and returns a decimal value.

    self.consolidator = ClassicRenkoConsolidator(1, volumeSelector = lambda data: data.Volume)
    _consolidator = new ClassicRenkoConsolidator(1m, null, data => (data as TradeBar).Volume);

    To relax the requirement that the open and close of the Renko bars must be a multiple of bar size, disable the evenBars argument. If you disable evenBars, the open value of the first Renko bar is set to the first value from the selector. The following opening and closing Renko bar values are all multiples of the first value from the selector

    self.consolidator = ClassicRenkoConsolidator(1, evenBars = False)
    _consolidator = new ClassicRenkoConsolidator(1m, evenBars: false);
  3. Add an event handler to the consolidator.
  4. _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 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 classic 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 AddConsolidator method of the Subscription Manager.

      self.SubscriptionManager.AddConsolidator(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 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);
      }
  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.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 Quote Bars

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

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

    // Create a Classic Renko consolidator that emits a bar when the price moves $1
    _consolidator = new ClassicRenkoConsolidator(1m);
    # Create a Classic Renko consolidator that emits a bar when the price moves $1
    self.consolidator = ClassicRenkoConsolidator(1)

    The ClassicRenkoConsolidator has the following default behavior:

    • It uses the Value property of the IBaseData object it receives to build the Renko bars
    • It ignores the volume of the input data
    • It enforces the open and close of each bar to be a multiple of the bar size

    The following arguments enable you to create Renko bars that aggregate the excess liquidity on the bid.

    self.consolidator = ClassicRenkoConsolidator(10, lambda data: data.Value, lambda data: data.LastBidSize - data.LastAskSize)
    _consolidator = new ClassicRenkoConsolidator(10, null, 
        data => (data as QuoteBar).LastBidSize - (data as QuoteBar).LastAskSize);

    To relax the requirement that the open and close of the Renko bars must be a multiple of bar size, disable the evenBars argument. If you disable evenBars, the open value of the first Renko bar is set to the first value from the selector. The following opening and closing Renko bar values are all multiples of the first value from the selector

    self.consolidator = ClassicRenkoConsolidator(1, evenBars = False)
    _consolidator = new ClassicRenkoConsolidator(1m, evenBars: false);
  3. Add an event handler to the consolidator.
  4. _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 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 classic 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 AddConsolidator method of the Subscription Manager.

      self.SubscriptionManager.AddConsolidator(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 Update method with a QuoteBar 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:
          quote_bar = slice.QuoteBars[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.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 RenkoBar objects. Follow these steps to create and manage a Tick consolidator based on the preceding Renko bar rules:

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

    // Create a Classic Renko consolidator that emits a bar when the price moves $1
    _consolidator = new ClassicRenkoConsolidator(1m);
    # Create a Classic Renko consolidator that emits a bar when the price moves $1
    self.consolidator = ClassicRenkoConsolidator(1)

    The ClassicRenkoConsolidator has the following default behavior:

    • It uses the Value property of the IBaseData object it receives to build the Renko bars
    • It ignores the volume of the input data
    • It enforces the open and close of each bar to be a multiple of the bar size

    To relax the requirement that the open and close of the Renko bars must be a multiple of bar size, disable the evenBars argument. If you disable evenBars, the open value of the first Renko bar is set to the first value from the selector. The following opening and closing Renko bar values are all multiples of the first value from the selector

    self.consolidator = ClassicRenkoConsolidator(1, evenBars = False)
    _consolidator = new ClassicRenkoConsolidator(1m, evenBars: false);
  3. Add an event handler to the consolidator.
  4. _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 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 classic 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 AddConsolidator method of the Subscription Manager.

      self.SubscriptionManager.AddConsolidator(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 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);
      }
  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.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 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 preceding Renko bar rules:

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

    // Create a Classic Renko consolidator that emits a bar when the price moves $1
    _consolidator = new ClassicRenkoConsolidator(1m);
    # Create a Classic Renko consolidator that emits a bar when the price moves $1
    self.consolidator = ClassicRenkoConsolidator(1)

    The ClassicRenkoConsolidator has the following default behavior:

    • It uses the Value property of the IBaseData object it receives to build the Renko bars
    • It ignores the volume of the input data
    • It enforces the open and close of each bar to be a multiple of the bar size

    To relax the requirement that the open and close of the Renko bars must be a multiple of bar size, disable the evenBars argument. If you disable evenBars, the open value of the first Renko bar is set to the first value from the selector. The following opening and closing Renko bar values are all multiples of the first value from the selector

    self.consolidator = ClassicRenkoConsolidator(1, evenBars = False)
    _consolidator = new ClassicRenkoConsolidator(1m, evenBars: false);
  3. Add an event handler to the consolidator.
  4. _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 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 classic 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 AddConsolidator method of the Subscription Manager.

      self.SubscriptionManager.AddConsolidator(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 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);
      }
  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.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.

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: