Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Switching chart back from custom BarBrush

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

    Switching chart back from custom BarBrush

    My indicator is changing the colors of some bars base on a certain algorithm (recolor, using BarBrush = ... during OnUpdate().

    I would like the ability to change the color back to default color using a menu option (typically using ForceRefresh()).
    I know I need to change the BarBrush to null, but this is typically done is OnUpdate(), which is not called again.

    Any idea?

    Using OnRender?

    #2
    Hello Shai Samuel,

    Thanks for your post.

    I understand that you would like to set BarBrush back to null (default) outside of the OnBarUpdate() method.

    You could consider setting BarBrush = null in a menu-click event. You could find code samples in the forum thread linked below demonstrating using a menu-click event in a NinjaScript.

    https://ninjatrader.com/support/foru...lem#post792624

    Let me know if I may assist further.
    Last edited by NinjaTrader_BrandonH; 09-06-2022, 03:00 PM.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Thank you Brandon.

      Since I would like reset the appearance of historical bars, does that means that I need to loop over all bars and convert the BarBrush to null?

      Comment


        #4
        Hello Shai Samuel,

        Thanks for your note.

        Yes, that is correct. You would loop over all the bars on the chart using ChartBars.FromIndex and ChartBars.ToIndex and set BarBrush = null;

        Let me know if I may assist further.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Thank you Brandon.
          I am trying to reference bars back, and getting the error out-of-range, which does not make sense, as the bar is in range. Any idea?

          Render Bar=113, CurrentBar=1133, ChartBars.FromIndex=1021, ChartBars.ToIndex=1134
          renderBar=113

          Indicator 'BWMSBase': Error on calling 'OnRender' method on bar 1134: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
          Code:
          protected override void OnRender(Gui.Chart.ChartControl chartControl, Gui.Chart.ChartScale chartScale)
          {[INDENT]for (int i=ChartBars.FromIndex; i<=ChartBars.ToIndex; i++)
          {[/INDENT][INDENT=2]Print("Render Bar="+(CurrentBars[0]-i+1)+", CurrentBar="+CurrentBars[0]+", FromIndex="+ChartBars.FromIndex+", ToIndex="+ChartBars.ToIndex);
          renderBar(CurrentBars[STF]-i+1);[/INDENT][INDENT]}[/INDENT]
           }
          
          protected void renderBar(int barsAgo)
          {[INDENT]Print("renderBar="+barsAgo);
          double barKind = BarKind_STF[barsAgo];[/INDENT]
           }
          
          [Browsable(false)][XmlIgnore] public Series<double> BarKind_HTF { get { return Values[plotBarKind_HTF]; } }
          ​​​​​​​

          Comment


            #6
            Hello Shai Samuel,

            Thanks for your note.

            I see in the sample code you shared that you are using BarsAgo within OnRender. BarsAgo cannot be used within OnRender. When using OnRender, you would need to use specific bar indexes that are specifically for the primary series.

            For example, CurrentBars couldn't be used in OnRender since you can't use a BarsAgo value from OnRender.

            See the sample code in this help guide page for how to use ChartBars.FromIndex and ChartBars.ToIndex: https://ninjatrader.com/support/help..._fromindex.htm

            Note that GetValueAt is needed to get values off of a series when using OnRender. Again, these values are for the primary series only because the rendered indexes for ChartBars.FromIndex and ChartBars.ToIndex are primary bar indexes.

            See this help guide page for more information about GetValueAt and sample code: https://ninjatrader.com/support/help...sub=getvalueat

            Let me know if I may assist further.
            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment


              #7
              Thanks Brandon for your quick reply.
              I would like to use the same draw/rendered logic, for OnRender and the OnUpdate. Is there a way to convert between BarsAgo to index? BarsAgo 0 = CurrentBar?

              Comment


                #8
                Hello Shai Samuel,

                Thanks for your note.

                In NinjaScript you have two methods of retrieving information about a bar depending on the scope of where you are accessing that data.

                From OnBarUpdate you generally use the BarsAgo system to retrieve data from a number of Bars from now or the processing bar.

                When outside the OnBarUpdate context, such as OnRender, you instead deal with direct bar indexes.

                When using a price series, indicator, or any Series<T> you will have the GetValueAt method which allows you to get a value from a specific index:
                https://ninjatrader.com/support/help...sub=GetValueAt

                For example:

                Code:
                Close.GetValueAt(123);
                This would return the Close price for that index. Rather than gathering a bar, you would use the series to retrieve the value.

                If you are getting a BarsAgo returned from something you are doing, you can also convert that to an index by subtracting the BarsAgo from the CurrentBar:

                Code:
                CurrentBar - BarsAgo = index of bar
                Let me know if I may assist further.
                <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                Comment


                  #9
                  Originally posted by NinjaTrader_BrandonH View Post

                  BarsAgo cannot be used within OnRender. When using OnRender, you would need to use specific bar indexes that are specifically for the primary series.

                  For example, CurrentBars couldn't be used in OnRender since you can't use a BarsAgo value from OnRender.

                  .
                  Is this mentioned in the documentation anywhere? If it is not there please add it. It should be pointed out more clearly. ​

                  Comment


                    #10
                    Hello Borgen,

                    Thanks for your note.

                    This could be seen on the NinjaScript Best Practices help guide page linked below. See the section "barsAgo indexer vs. absolute bar Index" on the help guide page linked below.

                    From the help guide: "The internal indexer and pointers about the barsAgo value are only guaranteed to be correctly synced and updated during a market data event. As a result, you should favor using the absolute GetValueAt() methods during events which are not driven by price"

                    NinjaScript Best Practices: ​https://ninjatrader.com/support/help...#Errorhandling

                    Let me know if I may assist further.
                    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    649 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    370 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    109 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    573 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    576 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X