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 CarlTrading, 03-31-2026, 09:41 PM
            1 response
            152 views
            1 like
            Last Post NinjaTrader_ChelseaB  
            Started by CarlTrading, 04-01-2026, 02:41 AM
            0 responses
            89 views
            1 like
            Last Post CarlTrading  
            Started by CaptainJack, 03-31-2026, 11:44 PM
            0 responses
            131 views
            2 likes
            Last Post CaptainJack  
            Started by CarlTrading, 03-30-2026, 11:51 AM
            0 responses
            127 views
            1 like
            Last Post CarlTrading  
            Started by CarlTrading, 03-30-2026, 11:48 AM
            0 responses
            107 views
            0 likes
            Last Post CarlTrading  
            Working...
            X