Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to shade default brush background

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

    How to shade default brush background

    Hello,
    I have wanted to change background color when closes above or below bollinger band.
    Currently, the color is solid but I want to tone down the color and show as 70% transparent of default color. (if 0% transparent is solid color) (ref, image - ideal)

    Code:
    public class Bollinger2 : Indicator
    {
    private SMA sma;
    private StdDev stdDev;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionBollinger;
    Name = "Bollinger2";
    IsOverlay = true;
    IsSuspendedWhileInactive = true;
    NumStdDev = 2;
    Period = 20;
    
    AddPlot(Brushes.MediumSlateBlue, NinjaTrader.Custom.Resource.BollingerUpperBand);
    AddPlot(Brushes.MediumSlateBlue, NinjaTrader.Custom.Resource.BollingerMiddleBand);
    AddPlot(Brushes.MediumSlateBlue, NinjaTrader.Custom.Resource.BollingerLowerBand);
    }
    else if (State == State.DataLoaded)
    {
    sma = SMA(Period);
    stdDev = StdDev(Period);
    }
    }
    
    protected override void OnBarUpdate()
    {
    double sma0 = sma[0];
    double stdDev0 = stdDev[0];
    
    Upper[0] = sma0 + NumStdDev * stdDev0;
    Middle[0] = sma0;
    Lower[0] = sma0 - NumStdDev * stdDev0;
    
    // Set 1
    if (Close[0] >= Upper[0])
    {
    BackBrushAll = Brushes.Maroon;
    }
    
    // Set 2
    if (Close[0] <= Lower[0])
    {
    BackBrushAll = Brushes.DarkGreen;
    }
    
    }
    ​
    Assuming I need to tweak brushes, how can I customize the color? I have looked up in help guide but could not figure out (e.g. opacity). I am new to ninja script. Therefore, simple is the best.

    BackBrushAll = Brushes.Maroon;
    BackBrushAll = Brushes.DarkGreen;

    Marble
    Attached Files

    #2
    Hello Marble,

    You would need to make custom brushes that have an opacify specified and then freeze those brushes so that they can be used. After doing that you can supply the brush just like you are now to color the background. There is an example of creating a custom brush that uses opacity in the following link:



    Code:
    int opacity = 50;
    Brush brushCopy = Brushes.Maroon.Clone();
    brushCopy.Opacity = opacity / 100;
    brushCopy.Freeze();
    ​​

    Comment


      #3
      Hi
      I have update as below. but does not seems to work. now backcolor all disappeared

      Code:
      protected override void OnBarUpdate()
      {
      double sma0 = sma[0];
      double stdDev0 = stdDev[0];
      
      Upper[0] = sma0 + NumStdDev * stdDev0;
      Middle[0] = sma0;
      Lower[0] = sma0 - NumStdDev * stdDev0;
      
      int opacity = 50;
      Brush brushCopy = Brushes.Maroon.Clone();
      brushCopy.Opacity = opacity / 100;
      brushCopy.Freeze();
      
      Brush brushCopy2 = Brushes.DarkGreen.Clone();
      brushCopy2.Opacity = opacity / 100;
      brushCopy2.Freeze();
      
      
      // Set 1
      if (Close[0] >= Upper[0])
      {
      BackBrushAll = brushCopy;
      }
      
      // Set 2
      if (Close[0] <= Lower[0])
      {
      BackBrushAll = brushCopy2;
      }
      
      }​
      also do I need to create burshCopy each objects that I want to create? then how do I change color thru indicator?
      also, the link provides just returns page not found in help page.
      Last edited by Marble; 08-29-2023, 11:53 AM.

      Comment


        #4
        Hello,

        Please try making sure the opacity is a double like the following:

        Code:
        double opacity = 50d;
        Brush brushCopy = Brushes.Maroon.Clone()
        brushCopy.Opacity = opacity/100d;
        brushCopy.Freeze();
        BackBrush = brushCopy;
        I see this working on my end. ​
        Last edited by NinjaTrader_Jesse; 08-30-2023, 06:48 AM.

        Comment


          #5
          Jesse,
          What is 50d;/100d;? very confusing.,,,,

          It does not work. I have tried with below and it turned all backcolor greenish.
          Code:
          double opacity = 50;
          Brush brushCopy = Brushes.Maroon.Clone();
          brushCopy.Opacity = opacity/100;
          brushCopy.Freeze();
          BackBrush = brushCopy;
          
          Brush brushCopy2 = Brushes.Green.Clone();
          brushCopy2.Opacity = opacity/100;
          brushCopy2.Freeze();
          BackBrush2 = brushCopy2;​
          
                       // Set 1
                      if (Close[0] >= Upper[0])
                      {
                          BackBrushAll = brushCopy;
                      }
          
                       // Set 2
                      if (Close[0] <= Lower[0])
                      {
                          BackBrushAll = brushCopy2;
                      }
                      ​
          I just need to change the color of applicable bars in more transparent color. not the whole background.

          Click image for larger version  Name:	image.png Views:	0 Size:	51.2 KB ID:	1266673
          Last edited by Marble; 08-29-2023, 06:21 PM.

          Comment


            #6
            Hello Marble,

            50d means 50 as a double number divided by 100d which is also a double number, that gives you 0.5 as an opacity.

            Your image appears to have opacity, you can turn down the opacity further if you want by changing the value of the opacity variable.



            Comment


              #7
              Please refer to my "ideal image" in post my first post. This is not at all what I am trying to achieve.
              I just want to highlight the background of bars where closed upper (in transparent Maroon) and lower (transparent Green) no affect in who background.

              Comment


                #8
                Hello Marble,

                The code i provided is just for making a brush, if you want to color specific bars you need to make conditions for that. The code that is in post 4 just shows how to make a brush with opacity, if you copy/paste that into your script and don't have any conditions then it would just color every bar.

                The code you provided has basic conditions which when they become true will color a bar, if you need those conditions to work differently you need to add to them or change them.

                Comment


                  #9
                  What is the problem with this code?
                  Condition is set within If {}

                  Code:
                  double opacity = 50;
                  Brush brushCopy = Brushes.Maroon.Clone();
                  brushCopy.Opacity = opacity/100;
                  brushCopy.Freeze();
                  BackBrush = brushCopy;
                  
                  Brush brushCopy2 = Brushes.Green.Clone();
                  brushCopy2.Opacity = opacity/100;
                  brushCopy2.Freeze();
                  BackBrush2 = brushCopy2;​
                  
                  // Set 1
                  if (Close[0] >= Upper[0])
                  {
                  BackBrushAll = brushCopy;
                  }
                  
                  // Set 2
                  if (Close[0] <= Lower[0])
                  {
                  BackBrushAll = brushCopy2;
                  }
                  ​​

                  Comment


                    #10
                    Hello Marble,

                    There is not a problem with that code, when the conditions become true it will color a bar. As mentioned if you are expecting a different result you need to change your conditions.

                    Comment


                      #11
                      Then why is how screen filled with green where nothing to do with condition?

                      I have // where it seems applying to background but now it shows only solid line.

                      Code:
                      double opacity = 50;
                      Brush brushCopy = Brushes.Maroon.Clone();
                      brushCopy.Opacity = opacity/100;
                      brushCopy.Freeze();
                      //BackBrush = brushCopy;
                      
                      Brush brushCopy2 = Brushes.Green.Clone();
                      brushCopy2.Opacity = opacity/100;
                      brushCopy2.Freeze();
                      //BackBrush = brushCopy2;​
                      
                      // Set 1
                      if (Close[0] >= Upper[0])
                      {
                      BackBrushAll = brushCopy;
                      }
                      
                      // Set 2
                      if (Close[0] <= Lower[0])
                      {
                      BackBrushAll = brushCopy2;
                      }​
                      how to fix to show like this. these are transparent lines.
                      Click image for larger version  Name:	image.png Views:	0 Size:	6.1 KB ID:	1266747
                      Last edited by Marble; 08-30-2023, 08:39 AM.

                      Comment


                        #12
                        Hello Marble,

                        The reason for that is due to your conditions. If the conditions are constantly true that means the chart will be colored like the image in post 5. You can use prints to see when the condition is true so you can get a better idea of the frequency however you already can visually see the frequency that the conditions are true based on nearly every bar being colored.

                        If you want the bars to be colored like the image in post 11 you would have to make conditions that support that. I can't tell from the image what type of condition would be needed to color those bars, you would have to figure out what price conditions you want and then make that type of condition.

                        Your first post was about making a transparent brush which was answered in post 2 and 4, that is how you make a transparent brush. If you want to do other tasks like making something similar to the image in post 11 you would have to determine what price conditions you need and form conditions for that.


                        Comment


                          #13
                          Click image for larger version

Name:	image.png
Views:	287
Size:	30.9 KB
ID:	1266751
                          From original posted code, I believe the condition is clear... If closed above / below. bollinger band. color background (all panel only for applicable bar area)
                          Now the bar is solid even with transparent code.
                          ​​

                          Code:
                          protected override void OnBarUpdate()
                          {
                          double sma0 = sma[0];
                          double stdDev0 = stdDev[0];
                          
                          Upper[0] = sma0 + NumStdDev * stdDev0;
                          Middle[0] = sma0;
                          Lower[0] = sma0 - NumStdDev * stdDev0;
                          
                          double opacity = 50;
                          Brush brushCopy = Brushes.Maroon.Clone();
                          brushCopy.Opacity = opacity/100;
                          brushCopy.Freeze();
                          //BackBrush = brushCopy;
                          
                          Brush brushCopy2 = Brushes.Green.Clone();
                          brushCopy2.Opacity = opacity/100;
                          brushCopy2.Freeze();
                          //BackBrush = brushCopy2;​
                          
                          // Set 1
                          if (Close[0] >= Upper[0])
                          {
                          BackBrushAll = brushCopy;
                          }
                          
                          // Set 2
                          if (Close[0] <= Lower[0])
                          {
                          BackBrushAll = brushCopy2;
                          }
                          
                          }
                          
                          ​

                          Comment


                            #14
                            Hello Marble,

                            If you want to detect crossing you need to use the CrossAbove and CrossBelow methods. Your current conditions check if the price is greater or lesser than the value which would be frequently true.

                            Comment


                              #15
                              i dont see any issue in the logic, all I need is to make colored back transparent because solid color is too vivid.
                              i dont need to check crossing. the bar highlight is working fine.
                              Attached Files
                              Last edited by Marble; 08-30-2023, 09:32 AM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              637 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              366 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              107 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              569 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              571 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X