Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Bar Color

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

    Bar Color

    I wish to set bar colors using the following formula

    if(Close[0]>h1)
    BarColor=color.green;
    if(close[0]<l1)
    BarColor=color.red;

    What section do I need to include code in and what would I need to declare for the code to work.

    Thanks

    Craig

    #2
    Hi Craig,

    Logic generally goes inside the OnBarUpdate() method. You would need to declare the h1 and l1 variables. Also, Color needs to be capitalized.

    BarColor = Color.Red;
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Bar Colors 2

      I placed this code in the
      OnBarUpdate() section of code and it shows no errors.
      It does however not overide the normal Candlestick colors that are the default.
      What would I need to do to achieve this?
      {
      // Condition set 1
      if (Close[0] > High[1])
      {
      BarColor=Color.Green;
      }
      // Condition set 2
      if (Close[0] < Low[1])
      {
      BarColor=Color.Red;
      }
      }
      Thanks

      Craig

      Comment


        #4
        Its a good habit to check the Log tab since you will see an error in there when applying this indicator to a chart. Please review the content in the following link which outlines what you need to do to fix your code.

        RayNinjaTrader Customer Service

        Comment


          #5
          The following now works. Thanks for your help
          {
          if (CurrentBar < 2)
          return;
          // Condition set 1
          if (Close[0] > High[1])
          {
          BarColor=Color.Green;
          }
          // Condition set 2
          if (Close[0] < Low[1])
          {
          BarColor=Color.Red;
          }}

          I now wish to code the following. Can you please suggest ways I might approach this.
          if condition 1 is true then subsequent bars to be colored green until condition 2 is true
          if condition 2 is true then subsequent bars to be colored red until condition 1 is true

          Thanks

          Craig

          Comment


            #6
            Create a variable in the variables section of your script,

            private Color myColor = Color.Green;

            then in OnBarUpdate()

            Code:
            if (CurrentBar < 2)
                return;
             
            if (Close[0] > High[1])
                myColor = Color.Green;
            else if (Close[0] < Low[1])
                myColor = Color.Red;
             
            BarColor = myColor;
            RayNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            599 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            344 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
            558 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            557 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X