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

Why BackBrush.Opacity is in read-only mode?

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

    Why BackBrush.Opacity is in read-only mode?

    Hi there,

    I try to change an opacity property for chart background color in OnBarUpdate() like this:
    Code:
    BackBrush.Opacity = (float)(20/100);
    But when I attach my indicator to chart - I receive an error:
    Code:
    Error on calling 'OnBarUpdate' method on bar 95: Cannot set a property on object '#FF3E3D42' because it is in a read-only state.
    P.S.
    I tried
    Code:
    BackBrush.Opacity = 0.2f;
    but the result is the same - an error.
    Handlar
    NinjaTrader Ecosystem Vendor - Handlar

    #2
    Hello handlar,

    Thank you for the post.

    This is due to the nature of Brushes. You would need to recreate the brush with a new opacity and Freeze the brush after doing so.

    We have some infomration on working with brushes in the following page: https://ninjatrader.com/support/help...th_brushes.htm

    The sample in the help guide shows applying opacity and freezing the brush:

    Code:
    MyBrush = new SolidColorBrush(Color.FromArgb(100, 56, 120, 153));
    myBrush.Freeze();
    If you are only changing the opacity and want to keep the existing brush, you could use the above code but reference the existing BackBrush to create a new color:

    Code:
    if(BackBrush != null)
    {
    	SolidColorBrush existingBrush = (SolidColorBrush)BackBrush;
    	Brush newBackBrush = new SolidColorBrush(existingBrush.Color);
    	newBackBrush.Opacity = 0.2f;
    	newBackBrush.Freeze();
    	BackBrush = newBackBrush;
    }
    BackBrush is also a user variable meaning that if you dont set it in this script specifically, it will be a null value so a nullcheck is required here.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      NinjaTrader_Jesse, thank you so much for the answer!
      Handlar
      NinjaTrader Ecosystem Vendor - Handlar

      Comment


        #4
        Hello Jesse,

        I'm using this code to set the BackBrush with opacity, but after certain number of iterations of a drawing object (greater than 500) the color stops changing, even though its changing in the code. Is there a limit of how many frozen brushes can be used on a chart?

        Regards
        Ulises
        ulisesguerrero
        NinjaTrader Ecosystem Vendor - Thrifty Coders

        Comment


          #5
          Hello ulisesguerrero,

          Thank you for the reply.

          There is a limit however it should generally be much greater than 500 bars it would be 65535 for BackBrush.
          Warning: You may have up to 65,535 unique BackBrush instances, therefore, using static predefined brushes should be favored. Alternatively, in order to use fewer brushes, please try to cache your custom brushes until a new brush would actually need to be created.
          https://ninjatrader.com/support/help...tsub=backbrush

          If you are seeing ~500 brushes and it stops my guesses would be this somehow relates to logic or potentially a setting being used in the script. I could suggest to try just setting the BackBrush on every bar at the top of OnBarUpdate and try to use a chart with greater than 500 bars and less than 65000 to see how many are painted. You would also have to comment out your other uses of BackBrush to avoid resetting it in case some logic is affecting the coloring.

          The more efficient way to color many bars is to use OnRender instead of BackBrush however that is more involved to do. Rather than having X number of back brush instances you would have a set of pre defined brushes and then render the color based on the bar data or however you currently determine the color for the bar. This happens only for the visible bars which helps to not require a lot of instances of the brush at once like BackBrush.

          I look forward to being of further assistance.


          JesseNinjaTrader Customer Service

          Comment


            #6
            I have spent many hours looking everywhere and have read all the help stuff for a simple example how to add opacity to my Backbrush timeframe to no avail. I just want to be able to add some opacity to the solid DimGray in this code piece. Any help is appreciated.

            Thanks FuturesPhantom

            HTML Code:
            protected override void OnBarUpdate()
            
            {
            
            
            
            if (CurrentBars[0] < BarsRequiredToTrade || CurrentBars[1] < BarsRequiredToTrade)
            return;
            {
            
            if ((Times[0][0].TimeOfDay >= new TimeSpan(05, 0, 0))
            && (Times[0][0].TimeOfDay <= new TimeSpan(14, 30, 0)))
            
            
            BackBrush = Brushes.DimGray;
            
            
            
            
            
            {

            Comment


              #7
              Hello FuturesPhantom,

              In C# a Brush needs to be created and then frozen if you wanted to have opacity, you can find a sample of creating a brush in the help guide link here: https://ninjatrader.com/support/help...ightsub=freeze

              You can also do new SolidColorBrush(Brushes.DimGray.Color); for standard brushes.



              Please let me know if I may be of further assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                I have read that already about 50 times today to no avail. The help pages often give a broad overview only intelligible for advanced programmers. Sometimes the help pages give a code snippet example and that helps a multitude. If one of the members here has any idea how to add opacity I would appreciate it.

                FuturesPhantom

                Comment


                  #9
                  Hello FuturesPhantom,

                  What part are you having difficult with in that section of the help guide? There are a few samples there, you could copy and paste them from the help guide to test them out. Here is one that includes using opacity from that page:

                  Code:
                  Brush MyBrush = new SolidColorBrush(Color.FromArgb(100, 56, 120, 153));
                  myBrush.Freeze();
                  This uses the ARGB color code, you could alternatively use a Color in place of Color.FromArgb:

                  Code:
                  Brush MyBrush = new SolidColorBrush(Brushes.DimGray.Color);
                  If you specify a solid color you can use the Opacity property:

                  Code:
                  MyBrush.Opacity = 0.5;
                  As a side note the help guide for NinjaScript is directed toward C# developers so it may be difficult to read if you don't have any existing C# experience. We always recommend going through some general C# tutorials or courses before trying to manually code NinjaScript as that will greatly assist with reviewing samples and other help content.

                  Please let me know if I may be of further assistance.
                  Last edited by NinjaTrader_Jesse; 08-11-2021, 03:22 PM.
                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    Has anyone used the onrender format to change backbrush color and attach a variable to the colors opacity?
                    My attmpts to determine color in onrender returns errors. I already have it set in onbarupdate although there is def a limit to how long it can update the color, historically it has no issue but when it is dynamic changing in realtime or playback there is simply a lmit to amount it can produce and req. loading new depending on the type of logic that is affecting the backbrush color.

                    Comment


                      #11
                      Hello LoganJKTrader,

                      OnRender generally wouldn't be used to change bar based plots/series because there is no concept of BarsAgo in OnRender. Those items won't make sense in that use case in regard to setting data, you can still set data using a BarsAgo however OnRender happens out of sync with OnBarUpdate so you may see variable results. You can reliably access values from series in OnRender by using the series GetValueAt method.

                      If you wanted to do variable opacity you could use a variable to control the Opacity property on a brush. You would have to create and freeze the brush every time that value changes.

                      If you have something in OnRender that depends on logic which sets the BackBrushColor that would be a good use case for a Series<double>. You can set a value to the series in OnBarUpdate and then from OnRender you can check what values each bar has in that series to do rendering logic. Indicators like the ZigZag uses this type of logic.
                      JesseNinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by judysamnt7, 03-13-2023, 09:11 AM
                      4 responses
                      59 views
                      0 likes
                      Last Post DynamicTest  
                      Started by ScottWalsh, Today, 06:52 PM
                      4 responses
                      36 views
                      0 likes
                      Last Post ScottWalsh  
                      Started by olisav57, Today, 07:39 PM
                      0 responses
                      7 views
                      0 likes
                      Last Post olisav57  
                      Started by trilliantrader, Today, 03:01 PM
                      2 responses
                      21 views
                      0 likes
                      Last Post helpwanted  
                      Started by cre8able, Today, 07:24 PM
                      0 responses
                      10 views
                      0 likes
                      Last Post cre8able  
                      Working...
                      X