Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Color bar if it has existed less than X amount of seconds

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Color bar if it has existed less than X amount of seconds

    Hi there,

    I am trying to color a renko bar if it only existed less than 30 seconds, effectively trying to measure the speed of a particular move.

    Here is currently what I was trying, however I'm unable to get any bar painting to occur. Thank you!

    Code:
    if(
    Close[0] > Open[0]
    && Time[0].Subtract(Time[1]).TotalSeconds >= 30
    )
    
    {BarBrush = Brushes.Red;}
    
    if(
    Close[0] < Open[0]
    && Time[0].Subtract(Time[1]).TotalSeconds >= 30
    )
    
    {BarBrush = Brushes.Green;}

    #2
    Hello,

    To color a Renko bar based on its existence time, you need to ensure your conditions are correctly checking the bar duration and then setting the color appropriately. The issue might be in the way the conditions are set. Here’s how you can do it: Step-by-Step Solution:
    1. Check the Duration of the Bar:
      • Use Time[0] and Time[1] to determine the duration of the current bar.
    2. Set the BarBrush Property:
      • Use BarBrush to set the color based on the duration.
    Corrected Code:

    Code:
    #region Using declarations
    using System;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript.StrategyAnalyzerColumns;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.NinjaScript.Strategies;
    using NinjaTrader.NinjaScript.Indicators;
    using System.Windows.Media;
    #endregion
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class ColorRenkoBars : Strategy
        {
            protected override void OnBarUpdate()
            {
                // Ensure there is a previous bar to compare with
                if (CurrentBar < 1)
                    return;
    
                // Calculate the duration of the bar in seconds
                double barDurationInSeconds = (Time[0] - Time[1]).TotalSeconds;
    
                // Check if the bar duration is less than 30 seconds
                if (barDurationInSeconds < 30)
                {
                    if (Close[0] > Open[0])
                    {
                        BarBrush = Brushes.Red;
                    }
                    else if (Close[0] < Open[0])
                    {
                        BarBrush = Brushes.Green;
                    }
                }
            }
        }
    }
    ​
    Explanation:
    1. Check Bar Duration:
      • double barDurationInSeconds = (Time[0] - Time[1]).TotalSeconds; calculates the duration of the current bar.
    2. Conditional Coloring:
      • The bar is colored red if it's a bullish bar (Close[0] > Open[0]) and its duration is less than 30 seconds.
      • The bar is colored green if it's a bearish bar (Close[0] < Open[0]) and its duration is less than 30 seconds.
    Additional Notes:
    • Ensure your strategy is set to calculate on each tick or bar close, depending on when you need the coloring to happen.
    • The coloring will only be applied to the bars where the conditions are met.

    This should properly color your Renko bars based on the duration they existed, effectively measuring the speed of a particular move.

    Comment


      #3
      Originally posted by NinjaTrader_RyanS View Post
      Hello,

      To color a Renko bar based on its existence time, you need to ensure your conditions are correctly checking the bar duration and then setting the color appropriately. The issue might be in the way the conditions are set. Here’s how you can do it: Step-by-Step Solution:
      1. Check the Duration of the Bar:
        • Use Time[0] and Time[1] to determine the duration of the current bar.
      2. Set the BarBrush Property:
        • Use BarBrush to set the color based on the duration.
      Corrected Code:

      Code:
      #region Using declarations
      using System;
      using NinjaTrader.Cbi;
      using NinjaTrader.Gui.Tools;
      using NinjaTrader.NinjaScript;
      using NinjaTrader.Data;
      using NinjaTrader.NinjaScript.StrategyAnalyzerColumns;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.NinjaScript.Strategies;
      using NinjaTrader.NinjaScript.Indicators;
      using System.Windows.Media;
      #endregion
      
      namespace NinjaTrader.NinjaScript.Strategies
      {
      public class ColorRenkoBars : Strategy
      {
      protected override void OnBarUpdate()
      {
      // Ensure there is a previous bar to compare with
      if (CurrentBar < 1)
      return;
      
      // Calculate the duration of the bar in seconds
      double barDurationInSeconds = (Time[0] - Time[1]).TotalSeconds;
      
      // Check if the bar duration is less than 30 seconds
      if (barDurationInSeconds < 30)
      {
      if (Close[0] > Open[0])
      {
      BarBrush = Brushes.Red;
      }
      else if (Close[0] < Open[0])
      {
      BarBrush = Brushes.Green;
      }
      }
      }
      }
      }
      ​
      Explanation:
      1. Check Bar Duration:
        • double barDurationInSeconds = (Time[0] - Time[1]).TotalSeconds; calculates the duration of the current bar.
      2. Conditional Coloring:
        • The bar is colored red if it's a bullish bar (Close[0] > Open[0]) and its duration is less than 30 seconds.
        • The bar is colored green if it's a bearish bar (Close[0] < Open[0]) and its duration is less than 30 seconds.
      Additional Notes:
      • Ensure your strategy is set to calculate on each tick or bar close, depending on when you need the coloring to happen.
      • The coloring will only be applied to the bars where the conditions are met.

      This should properly color your Renko bars based on the duration they existed, effectively measuring the speed of a particular move.
      Thank you so much! Very helpful.

      Comment


        #4
        Hello,

        If not using the declaration strategyanalyzercolumns you can remove this from your script, otherwise you'll need to add or correct the necessary 'using' statement.


        Error CS0616
        This indicates that you are trying to use Range as an attribute, but it isn't defined as such. Ensure you are using the correct attribute and that it is valid in this context.

        Here's an example of correct usage:
        Code:
        using System.ComponentModel.DataAnnotations;
        
        [Range(1, 100)]
        public int YourProperty { get; set; }
        ​

        If Range is not a valid attribute for your context, consider replacing it with a valid attribute or removing it.


        Error CS0246


        Message: The type or namespace name Display could not be found (are you missing a using directive or an assembly reference?).

        Solution: Ensure that the Display attribute is part of the namespaces you have included. It should likely be:
        Code:
        using System.ComponentModel.DataAnnotations;
        ​
        Make sure the correct namespaces are included in your file​​.

        Error CS0246 (DisplayAttribute)


        Message: The type or namespace name DisplayAttribute could not be found.

        Solution: Similar to the previous error, make sure that you have the appropriate using directive for DisplayAttribute. It should be:
        Code:
        using System.ComponentModel.DataAnnotations;
        ​
        Please let us know if you have any other questions.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        581 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        338 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        103 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        554 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        552 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X