Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

BarBrushes has 0 items at all times

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

    BarBrushes has 0 items at all times

    Hi, I am trying to create an indicator that paints candles differently when some conditions are met. Previous candle's colors can change on each bar close, so I can't really use BarBrush, so I'm trying to use BarBrushes instead.


    The problem is: The BarBrushes array is empty anywhere I try to use it (OnStateChange when State == State.DataLoaded, and OnBarUpdate with Calculate.OnBarClose). Am I doing something wrong or is it a bug?

    #2
    Hello Matheusfx,
    Thanks for your post.

    BarBrushes is going to return null unless an explicit color overwrite is used first. Are you you setting a color first before you check the array?
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      I'm getting out of bounds exceptions when trying to set the colors, not null returns :/
      I'm using the array directly to set the colors, I expected for it to have an item count equal to the amount of loaded bars, with null values where the candle's color wasn't overriden.
      How should I go about setting these colors, if this is not how it works?

      Comment


        #4
        Can you provide a short sample that produces this error? I think it may help to see how you are setting BarBrushes.
        Josh G.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_JoshG View Post
          Can you provide a short sample that produces this error? I think it may help to see how you are setting BarBrushes.
          I don't have access to the editor at the moment, but I'll try to give some directions. I'm not setting BarBrushes at all, I think an accurate representation of the problem would be:
          1. Create an empty indicator.
          2.Inside of the OnStateChange() method:
          Code:
          if(State == State.DataLoaded) {
              BarBrushes[0] = Brushes.Blue
          }
          This produces an Out of Bounds exception, instead of returning null.
          Printing BarBrushes.Count shows that it's item count is 0.

          Comment


            #6
            I would not expect for you to be able to access that index from State.DataLoaded

            How are you setting BarBrushes inside OnBarUpdate() to produce the bounds error? The following snippet would set the current bar to the color orange when my condition is met.

            Code:
            protected override void OnBarUpdate()
            {
            	if (Close[0] > Open[0])
            		BarBrushes[0] = Brushes.Orange;
            }
            Josh G.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_JoshG View Post
              I would not expect for you to be able to access that index from State.DataLoaded

              How are you setting BarBrushes inside OnBarUpdate() to produce the bounds error? The following snippet would set the current bar to the color orange when my condition is met.

              Code:
              protected override void OnBarUpdate()
              {
              	if (Close[0] > Open[0])
              		BarBrushes[0] = Brushes.Orange;
              }
              BarBrushes is also empty on the OnBarUpdate() method. If I recall correctly the error happens before the script even runs OnBarUpdate(), I also set the colors in order from oldest to the most recent candle, which means it begins trying to set something like
              Code:
              BarBrushes[99] = Brushes.Orange
              and then continues setting the colors of the candles that match the criteria, until it reaches the current one, which is 0.

              If the explanation is too messy I'll try to post a proper code sample when I get home.

              Comment


                #8
                Your sample is not too messy. The reason an out of bounds error is occurring is because a value is not being assigned to BarBrushes on each bar. I am just not certain why that is, so I will need to look into it.

                If you describe what it is you are trying to accomplish I may be able to assist you more on how to achieve your goal.
                Josh G.NinjaTrader Customer Service

                Comment


                  #9
                  I managed to fix the out of bounds error in the OnBarUpdate() method by creating another indicator from scratch and moving it to the new one, I have absolutely no idea why that worked, as I made sure I was copying every piece of code I wrote to the other file.


                  The BarBrushes array really wasn't ready in the State.DataLoaded, and I had to populate the BarBrushes array with null values inside of OnBarUpdate() by doing:
                  Code:
                  BarBrushes[0] = null;
                  I was able to do the stuff that I needed to do inside the OnStateChange() method by checking if the State == State.RealTime, as the array was already pre-populated with null values at this point.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by aa731, Today, 02:54 AM
                  0 responses
                  4 views
                  0 likes
                  Last Post aa731
                  by aa731
                   
                  Started by thanajo, 05-04-2021, 02:11 AM
                  3 responses
                  470 views
                  0 likes
                  Last Post tradingnasdaqprueba  
                  Started by Christopher_R, Today, 12:29 AM
                  0 responses
                  10 views
                  0 likes
                  Last Post Christopher_R  
                  Started by sidlercom80, 10-28-2023, 08:49 AM
                  166 responses
                  2,237 views
                  0 likes
                  Last Post sidlercom80  
                  Started by thread, Yesterday, 11:58 PM
                  0 responses
                  5 views
                  0 likes
                  Last Post thread
                  by thread
                   
                  Working...
                  X