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

NT8 color to Brush conversion with Opacity

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

    #16
    Hi turbofib,

    BackBrush lets you change the color of the background behind a specific bar (not the entire chart).

    Even in State.DataLoaded, OnBarUpdate has not started processing bars yet. There needs to be at least one bar on the chart to be able to change the background behind it.

    From my understanding, this code will not work in OnStateChange().
    Chelsea B.NinjaTrader Customer Service

    Comment


      #17
      excuse me...but i 've problem to code it..

      i want to do this :


      if (Open[0] < Close[0])
      {
      BarBrushes[-displacement] = trendColor;
      BarBrushes[-displacement].Opacity = alpha;
      }
      but i can't to change Opacity because this property is only read..

      how i do it?..

      thanks..

      Comment


        #18
        Hello turbofib,

        As a heads up, using negative indexes are not officially supported.

        To set opacity on a brush, you will need to make a new brush that isn't frozen, set the color and opacity and then set the property you are wanting to set to that new brush object.

        This is what you were doing in post #14 though this being done at the wrong time. Cloning the brush, setting the new brushes opacity, freezing, is correct. Doing this in OnStateChange is not correct.
        Code:
        Brush newBrush = BackBrush.Clone();
        newBrush.Opacity = 1;
        newBrush.Freeze();
        BackBrush = newBrush;
        Last edited by NinjaTrader_ChelseaB; 03-28-2017, 08:45 AM.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #19
          Excuse me... but i want to change body candle color...not background....
          Backbrush class is not correct

          Comment


            #20
            Hello turbofib,

            To change the color of a bar use BarBrush.
            Last edited by NinjaTrader_ChelseaB; 03-28-2017, 02:31 PM.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #21
              Originally in nt7

              BarColorSeries[-displacement] = Color.FromArgb(alpha, trendColor);
              ok..finally i've code in following mode


              BarBrushes[displacement] = trendColor;

              Brush newBrush = BarBrushes[displacement].Clone();
              newBrush.Opacity = opacity;// alpha;
              newBrush.Freeze();

              BarBrushes[displacement] = newBrush;

              i ask you 2 question :

              1)My above code is too expensive or is ok?

              2)another question...

              i can't to set directly BarBrus with BarBrush.=Opacity because is property only read

              but in guide of .net i read that :



              public double Opacity { get; set; }

              So why this?
              Last edited by turbofib; 03-29-2017, 05:02 AM.

              Comment


                #22
                Hello turbofib,

                I do not understand your meaning when you ask if the code is "expensive".
                If you are asking if the code will cause high CPU I cannot answer this question because I don't know when or how often this code is called.
                However, to set the brush of a bar with opacity this is required. If you want opacity you must use this.

                A get and set does not mean that an object is not read-only.

                A set is required to set an object that is not read-only.Any property without a set is basically read-only simply because it doesn't have a setter.
                A property that is read-only cannot be set even if it has a get and set.

                Use the readonly keyword. Readonly prevents changes to fields after initialization.

                These examples illustrate using properties in C#. See how the get and set accessors implement read and write access and find out about uses for properties.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #23
                  Originally posted by NinjaTrader_ChelseaB View Post
                  Try adding the following to OnBarUpdate()

                  BackBrush = Brushes.Green;
                  Brush newBrush = BackBrush.Clone();
                  newBrush.Opacity = 0.25;
                  newBrush.Freeze();
                  BackBrush = newBrush;
                  Hi guys,

                  how could I rewrite the above code from Chelsea to change the opacity of a plot? The best would be to create a color with applied opacity during the on State.DataLoaded.

                  I tried:
                  Code:
                  Brush color = originalColor;
                  color.Opacity = colorOpacity / 100;
                  color.Freeze();
                  which doesn't work

                  This one here does work but only if I use Color.xxxxxx. When I use originalColor, it throws an error that I can't convert a Media.Brush to a Media.Color.
                  Code:
                  color1 = new SolidColorBrush(originalColor) { Opacity = colorOpacity / 100 };
                  color1 .Freeze();
                  How could I apply opacity to a color used for a plot?


                  Greetings

                  Comment


                    #24
                    Hello seykool,

                    Thank you for your reply.

                    I'm attaching an example that demonstrates how to set the opacity of a plot.

                    Please let us know if we may be of further assistance to you.
                    Attached Files
                    Kate W.NinjaTrader Customer Service

                    Comment


                      #25
                      Here's the code snippets that work well:

                      public variables in if (State == State.SetDefaults):

                      Code:
                      BackgroundOpacity = 15; // 15% as default value
                      BackgroundUpColor = Brushes.DarkGreen;
                      BackgroundDownColor = Brushes.DarkRed;

                      else if (State == State.Configure):

                      Code:
                      Brush temp = BackgroundUpColor.Clone();
                      temp.Opacity = BackgroundOpacity / 100.0;
                      temp.Freeze();
                      BackgroundUpColor = temp;
                      
                      Brush temp1 = BackgroundDownColor.Clone();
                      temp1.Opacity = BackgroundOpacity / 100.0;
                      temp1.Freeze();
                      BackgroundDownColor = temp1;
                      You add the properties and function to update the background color.

                      Happy Holidays!

                      Comment


                        #26
                        Hi,

                        you both wrote in the same moment.

                        nothingbutprofits, this only works for BackBrushes. On Plots it appears to not work.
                        But therefore Kate, your script works well.

                        Thanks to you both!
                        Happy new year!

                        Comment


                          #27
                          Originally posted by nothingbutprofits View Post
                          Here's the code snippets that work well:

                          public variables in if (State == State.SetDefaults):

                          Code:
                          BackgroundOpacity = 15; // 15% as default value
                          BackgroundUpColor = Brushes.DarkGreen;
                          BackgroundDownColor = Brushes.DarkRed;

                          else if (State == State.Configure):

                          Code:
                          Brush temp = BackgroundUpColor.Clone();
                          temp.Opacity = BackgroundOpacity / 100.0;
                          temp.Freeze();
                          BackgroundUpColor = temp;
                          
                          Brush temp1 = BackgroundDownColor.Clone();
                          temp1.Opacity = BackgroundOpacity / 100.0;
                          temp1.Freeze();
                          BackgroundDownColor = temp1;
                          You add the properties and function to update the background color.

                          Happy Holidays!
                          Many thanks for this:

                          I put the above code in the respective sections.
                          The chart background does colour as it's supposed to and the opacity also works.

                          The problem is when I restart/re open NT8 the indicator I added the above code to is gone from the chart and I have to add it from the indicator list each time. In other words the indicator I added the above code to doesn't save to the chart.

                          Generally speaking:
                          All I'm trying to do is have background colour with opacity function.
                          I can add background colour pretty easily to most of my indicators however things get in a mess when trying to have the opacity function.

                          As stated, doing the above does give me opacity function but the indicator isn't saving to the chart when I next open/start NT8.


                          Any help appreciated.

                          Many thanks
                          Last edited by dj22522; 03-14-2022, 07:06 AM.

                          Comment


                            #28
                            Hello dj22522,

                            Thank you for your reply.

                            Did you create inputs for the brush colors? If so, you'll need to make certain those inputs are properly serialized, or the indicator will not be able to be restored when a workspace is saved, closed and reopened.

                            I recommend reviewing this page of our help guide on serializing brushes:



                            Please let us know if we may be of further assistance to you.
                            Kate W.NinjaTrader Customer Service

                            Comment


                              #29
                              Originally posted by NinjaTrader_Kate View Post
                              Hello dj22522,

                              Thank you for your reply.

                              Did you create inputs for the brush colors? If so, you'll need to make certain those inputs are properly serialized, or the indicator will not be able to be restored when a workspace is saved, closed and reopened.

                              I recommend reviewing this page of our help guide on serializing brushes:

                              https://ninjatrader.com/support/help...lor_inputs.htm

                              Please let us know if we may be of further assistance to you.

                              Thank you for the reply Kate

                              Yes I have created inputs.

                              Assuming I've done this correctly I have the following :

                              protected override void OnStateChange()
                              {
                              if (State == State.SetDefaults) :

                              Code:
                               protected override void OnStateChange()
                              {
                              if (State == State.SetDefaults)
                              
                               BackColourUp = Brushes.Blue;
                              BackColourDn = Brushes.Red;
                              BackgroundOpacity = 15; // 15% as default value
                              
                              {


                              I then have the following in
                              else if (State == State.Configure)


                              Code:
                              else if (State == State.Configure)
                              {
                              
                              Brush temp = BackColourUp.Clone();
                              temp.Opacity = BackgroundOpacity / 100.0;
                              temp.Freeze();
                              BackColourUp = temp;
                              
                              Brush temp1 = BackColourDn.Clone();
                              temp1.Opacity = BackgroundOpacity / 100.0;
                              temp1.Freeze();
                              BackColourDn = temp1;
                              
                              
                              }


                              I have the following in Properties:

                              Code:
                              [NinjaScriptProperty]
                              [Display(Name="Back colour Up", Description="", Order=3, GroupName="Back Colour")]
                              public Brush BackColourUp
                              { get; set; }
                              
                              [Browsable(false)]
                              public string BackColourUpSerializable
                              {
                              get { return Serialize.BrushToString(BackColourUp); }
                              set { BackColourUp = Serialize.StringToBrush(value); }
                              }
                              
                              [NinjaScriptProperty]
                              [Display(Name="Back colour Down", Description="", Order=4, GroupName="Back Colour")]
                              public Brush BackColourDn
                              { get; set; }
                              
                              [Browsable(false)]
                              public string BackColourDnSerializable
                              {
                              get { return Serialize.BrushToString(BackColourDn); }
                              set { BackColourDn = Serialize.StringToBrush(value); }
                              }
                              
                              
                              //Opacity
                              [Range(0, 100)]
                              [Display(Name="Back colour Opacity", Description="", Order=5, GroupName="Back Colour")]
                              public int BackgroundOpacity
                              { get; set; }

                              What am I doing wrong ?

                              Many thanks

                              Comment


                                #30
                                Hello dj22522,

                                Thank you for your reply.

                                This all looks correct - can you supply an export of the script so we may test on our end? You can export it from Tools > Export > NinjaScript Addon. Do not check the box to export as a compiled assembly or we will not be able to review the logic. Please attach the resulting .Zip file to your reply.

                                Thanks in advance; I look forward to assisting you further.
                                Kate W.NinjaTrader Customer Service

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by stafe, 04-15-2024, 08:34 PM
                                7 responses
                                31 views
                                0 likes
                                Last Post NinjaTrader_ChelseaB  
                                Started by adeelshahzad, Today, 03:54 AM
                                4 responses
                                29 views
                                0 likes
                                Last Post adeelshahzad  
                                Started by merzo, 06-25-2023, 02:19 AM
                                10 responses
                                823 views
                                1 like
                                Last Post NinjaTrader_ChristopherJ  
                                Started by frankthearm, Today, 09:08 AM
                                5 responses
                                17 views
                                0 likes
                                Last Post NinjaTrader_Clayton  
                                Started by jeronymite, 04-12-2024, 04:26 PM
                                3 responses
                                43 views
                                0 likes
                                Last Post jeronymite  
                                Working...
                                X