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

How to make plot color dynamically change transparency

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

    How to make plot color dynamically change transparency

    Hello, I've used this method to make dynamic color changes to the plot, not sure if this is the most optimized way
    Code:
    Brush myBrush = new SolidColorBrush(Oscillator[0] > 0 ?
                                        Color.FromArgb(Convert.ToByte(Math.Abs(osc[0])*255), 12, 181, 26) :
                                        Color.FromArgb(Convert.ToByte(Math.Abs(osc[0])*255), 255, 17, 0));
    myBrush.Freeze();
    PlotBrushes[0][0] = myBrush;​
    it works correctly, but now I have a problem

    I've used RGB values and this is hard coded and cannot be user changed

    now I want for the user to define a color using an input parameter, which I did and called it BullColor

    Code:
    [NinjaScriptProperty]
    [XmlIgnore]
    [Display(Name="BullColor", Order=2, GroupName="Parameters")]
    public Brush BullColor
    { get { return new SolidColorBrush(Color.FromRgb(8, 153, 129)); } set{} }
    
    [Browsable(false)]
    public string BullColorSerializable
    {
    get { return Serialize.BrushToString(BullColor); }
    set { BullColor = Serialize.StringToBrush(value); }
    }​
    this works correctly as it should of course.
    but now I want to change opacity/transparency of this color dynamically
    so that user can set the color, but the script will make it transparent based on a value.
    since I don't know RGB values from this color (not sure how to find it out)
    and since it's a Brush not a Color I am not sure how to continue with this.

    I am completely unsure of how to use .Opacity and where as whenever I've tried to use it I've gotten an object error
    Code:
    Error on calling 'OnBarUpdate' method on bar 14: Object reference not set to an instance of an object.
    and while we're here how can I access RGB values of custom brushes,

    #2
    Hello LuxSpuzy,

    Thanks for your post.

    The error message you shared indicates that an object you are referencing does not exist at the time you are accessing it.

    You could consider making sure that the object you are accessing is not null before you access it.

    Note that using .Opacity on a Brush would be a C# concept, not NinjaScript specific code, and is not documented/supported in our help guide. You could do a Google search for something like 'C# Brush .Opacity' to research this topic further.

    See this help guide page for more information about Working with Brushes: https://ninjatrader.com/support/help...nsparent+brush

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.

    https://ninjatrader.com/support/foru...121#post791121

    Something you could consider is creating a user-defined brush as you have shared. You could set PlotBrushes[0][0] to use the custom user-defined brush. Then, when a certain condition occurs, you could set the script to use a pre-defined Transparent brush, such as PlotBrushes[0][0] = Brushes.Transparent.

    This forum thread will also be open for other community members to share their insights on the topic.

    Let me know if I may assist further.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      ah okay thank you for your answer on .Opacity

      as for the transparent part my question wasn't correctly phrased, I don't want to make the plot fully transparent aka invisible
      what I want is for the user to set a color using the input parameters, and then I want to change the A value of ARGB of that color
      can I somehow extract the RGB values from a custom brush?

      something like
      Code:
      Brush myBrush = new SolidColorBrush(Color.FromArgb(myValue,bullColor.R,bullColor.G,bullColor.B));​​
      can you convert a Brush to Color? or a Color to brush?

      is the only way to convert a color to Brush using "new SolidColorBrush()" ?
      do I have to use "myBrush.Freeze()" after every "new SolidColorBrush()" call or only after defining a new brush?

      sorry I've read a lot of documentation, tried to experiment and google before asking here, but some things I can't figure out/find
      Last edited by LuxSpuzy; 02-20-2023, 10:29 AM.

      Comment


        #4
        Hello LuxSpuzy,

        Thanks for your note.

        The 'Creating a Transparent Solid Color Brush' help guide page linked below demonstrates how to create a custom brush using Color.FromArgb() where the A parameter defines alpha transparency.

        https://ninjatrader.com/support/help...gcustombrushes

        We do not have any documentation about how to get individual values from a custom Brush or how to programmatically modify a custom Brush in the way you described. This would require C# code and goes beyond the support we would be able to provide you.

        Anytime you create a custom brush that will be used by NinjaTrader rendering, it must be frozen using the .Freeze() method due to the multi-threaded nature of NinjaTrader. If you do not call .Freeze() on a custom-defined brush then it WILL eventually result in threading errors should you try to modify or access that brush after it is defined.

        This forum thread will be open for other community members to share their insights on C# code that could accomplish this.

        Please let me know if I may assist further.​
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          I've found a solution on how make already defined brush modifiable, and change the Opacity property.
          altho I still don't know to get RGB values from a brush which would also be nice to know if anyone on the forums knows how

          Code:
          Brush newBrush = oldBrush.CloneCurrentValue();
          newBrush.Opacity = 0.5;
          newBrush.Freeze();
          PlotBrushes[0][0] = newBrush;
          Last edited by LuxSpuzy; 02-21-2023, 02:47 AM.

          Comment


            #6
            LuxSpuzy See if this helps:
            Code:
            // Assume a Brush called BullColor exists
            double myOpacity = 0.5;   // Must be between 0.0 and 1.0
            SolidColorBrush scb = BullColor as SolidColorBrush;
            SolidColorBrush mynewscb = new SolidColorBrush(Color.FromArgb((byte)(255*myOpacity),scb.Color.R,scb.Color.G,scb.Color.B));​​
            mynewscb.Freeze();​
            Thanks.
            Multi-Dimensional Managed Trading
            jeronymite
            NinjaTrader Ecosystem Vendor - Mizpah Software

            Comment


              #7
              Exactly what I needed. Thank you very much.

              Comment


                #8
                Originally posted by jeronymite View Post
                LuxSpuzy See if this helps:
                Code:
                // Assume a Brush called BullColor exists
                double myOpacity = 0.5; // Must be between 0.0 and 1.0
                SolidColorBrush scb = BullColor as SolidColorBrush;
                SolidColorBrush mynewscb = new SolidColorBrush(Color.FromArgb((byte)(255*myOpacity),scb.Color.R,scb.Color.G,scb.Color.B));​​
                mynewscb.Freeze();​
                Thanks.
                jeronymite, Hi.

                I came across, suposedly, a simpler way to accomplish the same with fewer lines of code to apply opacity using the NT standard default colors. This approach eliminaties the need for "Assume a Brush called BullColor exists." as seen in the attached screenshot. I have six condition sets that each uses 1 different brush. So, it saves a lot of coding to define six diferent sets of brush properties.

                The code generates an error "Statement expected" for the six brushes (I am just showing one of them). However it does compile fine and works on the chart. The results conform with the approach that you outlined above.

                My issue is, while the code compiles and draws correctly, it continues to generate the said error with no impact on compiling or performance. However, I do not see in the code what the missing "expected Statement" is that generates the error, perhaps you could.

                Cheers!​
                Click image for larger version  Name:	SolidColorBrush.png Views:	0 Size:	39.1 KB ID:	1245233
                Last edited by aligator; 04-08-2023, 02:38 PM.

                Comment


                  #9
                  Hi aligator I confess to being a little confused with this. First, the error means it is not compiling, yet you say it compiles and runs. Are you sure it's not just using a prior version that compiled? This version showing is not compiling and therefore cannot be the version you are running. You should be able to prove that easily by including some sort of Print statement at the start of OnBarUpdate. If it prints even though it seems to not compile that would be "most unusual" ... but I suspect it will not print, showing that this really is not compiling and not running.

                  Note also that the error is identified as occurring at line 127, column 23. If you double-click the actual error message the cursor will go to the (supposed) place in the window where it says the error is detected. Looking at your image, that seems to be the end of a blank line, just before the mynewcon1.Freeze(); statement. Interestingly, there is no C# error code in the Code column (normally something like "CS1002", for example, would be expected).

                  Also, it looks like you have edited out some things (yellow highlighted lines), and so it's not really possible to know what may have been there and what effect it may have had.

                  Suggestions:
                  • Try the Print approach to ensure you know what is or is not running
                  • When you have a definitive answer to that, and if there is still an error that you cannot understand, post again with updated information so the community can see an accurate state of play to assess
                  Thanks.
                  Multi-Dimensional Managed Trading
                  jeronymite
                  NinjaTrader Ecosystem Vendor - Mizpah Software

                  Comment


                    #10
                    Hello alligator,

                    Thanks for your notes.

                    jeronymite is correct. Typically, a compile error indicates that there is an error in the script that is preventing it from compiling successfully.

                    Ensure that you are not using a prior version of the script that did compile successfully. The error message indicates that the line with an error is a blank line in the script as jeronymite stated. It seems that you have modified the script but have not compiled it after making changes.

                    Try running a compile after making those changes to see if the error persists.

                    You may add debugging prints to the script to know exactly what is working in your script and where some lines of code are not running as expected. Once you know what exactly is not working correctly, please share your findings so I may accurately assist.

                    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
                    https://ninjatrader.com/support/foru...121#post791121

                    Let me know if I may assist further.​
                    Brandon H.NinjaTrader Customer Service

                    Comment


                      #11
                      I too get that same error so i know what he means
                      when he opens up the script in the editor we see a yellow line saying Statement Expected

                      but when we press compile, it compiles fine and the message is gone, then after I do some edits, and new errors are made because I was writing and didn't finish yet, then this error appears again, and when I finish my code and all the errors are gone, this one stays. but the file can be compiled.

                      Code:
                      public Color from_brush(double myOpacity, Brush myCustomBrush)
                                  {
                                      Color clr;
                                      myOpacity = Math.Min(Math.Max(myOpacity,0),1);
                                      SolidColorBrush scb = myCustomBrush as SolidColorBrush;
                                      clr = Color.FromArgb((byte)(255*myOpacity),scb.Color.R,scb.Color.G,scb.Color.B);
                                      return clr;​​
                                  }​
                      when I click on the statement expected error it takes my cursor to

                      clr = Color.FromArgb((byte)(255*myOpacity),scb.Color.R,s cb.Color.G,scb.Color.B);

                      this line at Colo(I)r.FromArgb

                      with (I) being my cursor.

                      the file compiles normally.
                      since
                      Click image for larger version  Name:	image.png Views:	0 Size:	27.9 KB ID:	1245431
                      since you 2 didn't believe him because he had a compile error mark next to his errors, here's without the mark
                      I can compile
                      and so can he.
                      and the code works normally

                      this is a NinjaEditor bug.
                      Last edited by LuxSpuzy; 04-10-2023, 08:02 AM.

                      Comment


                        #12
                        Thanks LuxSpuzy, NinjaTrader_BrandonH, jeronymite

                        Hard to explain the issue but LuxSpuzy explained it better than I did. That is exactly what happens. The only difference is that I use a slightly different code, only two lines, without brush properties that works fine if NT default colors are used as follows:

                        Code:
                        if(Condition1)
                        {
                        SolidColorBrush mynewcon1 = new SolidColorBrush(Colors.Green) {Opacity = myOpacity};
                        mynewcon1.Freeze();​
                        
                        BarBrush = mycondition1;​
                        {
                        This code placed under OnBarUpdate will work fine and compiles without the need to code brush properties. If I begin to edit anything this "Statement expected" error appears with no error #. I have searched my files and could not find another instance of the code, as suggested by jeronymite​.

                        Cheers!
                        Last edited by aligator; 04-10-2023, 09:09 AM.

                        Comment


                          #13
                          Hello alligator and LuxSpuzy,

                          I have added code to a script similar to the example code shared by alligator and I am not able to replicate the behavior on my end.

                          See this demonstration video: https://brandonh-ninjatrader.tinytak...Nl8yMTI3NjQzOQ

                          Attached you will find the indicator script used to test this. If you import the attached script and test it in the NinjaScript Editor, do you see the same behavior as you are reporting?

                          Please let me know if I may assist further.​
                          Attached Files
                          Brandon H.NinjaTrader Customer Service

                          Comment


                            #14
                            the script you provided doesn't show me the error, but when I add my function to it then it does
                            here is a video and code for the function is above in my post

                            Use Loom to record quick videos of your screen and cam. Explain anything clearly and easily – and skip the meeting. An essential tool for hybrid workplaces.


                            I also forgot to mention that the region and #endregion also get messed up for the file where this bug appears.
                            it can be seen in the video.

                            Comment


                              #15
                              Hello LuxSpuzy,

                              Thanks for your note.

                              I have added the code you used in your video to the SolidColorBrushTest indicator script and I am not able to reproduce the behavior you are reporting still.

                              See this demonstration video: https://brandonh-ninjatrader.tinytak...Ml8yMTI4MTU0MA

                              You will also find the modified version of the indicator used to test this attached to my reply. Are you able to reproduce the behavior when testing the modified script?

                              Please ensure that you are updated to the latest version of NinjaTrader (8.1.1.3). Updates to NinjaTrader will contain the latest security updates and bug fixes, as well as occasional new features and instruments.

                              To update NinjaTrader
                              • Shutdown all programs including NinjaTrader *important
                              • Download NinjaTrader from the link below
                              • Double-click the downloaded file to execute it
                              • Follow the on-screen instructions
                              • Restart NinjaTrader
                              You can read about changes to this release in the release notes linked below.

                              https://ninjatrader.com/support/help...ease_notes.htm
                              Attached Files
                              Brandon H.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by burtoninlondon, Today, 12:38 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post burtoninlondon  
                              Started by AaronKoRn, Yesterday, 09:49 PM
                              0 responses
                              13 views
                              0 likes
                              Last Post AaronKoRn  
                              Started by carnitron, Yesterday, 08:42 PM
                              0 responses
                              11 views
                              0 likes
                              Last Post carnitron  
                              Started by strategist007, Yesterday, 07:51 PM
                              0 responses
                              13 views
                              0 likes
                              Last Post strategist007  
                              Started by StockTrader88, 03-06-2021, 08:58 AM
                              44 responses
                              3,982 views
                              3 likes
                              Last Post jhudas88  
                              Working...
                              X