Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Defining a Custom Color

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

    Defining a Custom Color

    The following code example results in the compile error 'System.Windows.Media.Brushes' does not contain a definition for "UptrendColor". What is the correct way to do this?

    Code located in OnStateChange(), and tried both inside If (State == State.SetDefaults) and outside:
    /// Initiate new solid color backbrush with custom color: Uptrend Background Color
    Brush UptrendColor = new SolidColorBrush(Color.FromRgb(50, 50, 50));
    UptrendColor.Freeze();

    Code located in OnBarUpdate():
    /// Uptrend - Background Color
    if (MA1[0] > MA2[0])
    {
    BackBrush = Brushes.UptrendColor;
    }

    #2
    Hello,

    Thank you for the question.

    The "Brushes" collection is the set of Default brushes in System.Windows.Media namespace. As you have defined a custom Brush variable you would just need to use the variable name instead.


    Code:
    if (MA1[0] > MA2[0])
    {
    BackBrush = UptrendColor; 
    }

    I look forward to being of further assistance.

    Comment


      #3
      Thanks! That fixed the original compile error, but introduced a new one for the line containing BackBrush = UptrendColor. Error: "The name 'UptrendColor" does not exist in the current context"

      Comment


        #4
        Hello,

        Thank you for the reply.

        I noted you said you had tried this in the State.SetDefaults and outside, do you currently have this in State.SetDefaults still or have you moved this to a private variable? If you have not defined a private variable and are defining the variable in State.SetDefaults that would be the reason for the error.

        Try using a private variable like the following:

        Code:
        private Brush UptrendColor;
        
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                UptrendColor = new SolidColorBrush(Color.FromRgb(50, 50, 50));
                UptrendColor.Freeze();
            }
        }
        
        protected override void OnBarUpdate()
        {
            if (MA1[0] > MA2[0])
            {
                BackBrush = UptrendColor; 
            }
        }

        Comment


          #5
          I left the custom color initialization in OnStateChange(), within State.SetDefaults. Adding the private variable "private Brush UptrendColor;" prior to OnStateChange() corrected the compile error.

          Having now once defined the custom color UptrendColor, can that color name be used in other scripts without again inserting the initialization code and private variable code each time, or does the initialization and private variable code have to be repeated within each script when that custom color is used?

          Comment


            #6
            Hello,

            Thank you for the reply.

            The private variable would only be able to be used inside of this class or this script.

            In this situation, this is a C# concept or the Scope of your variable. NinajTrader uses C# for NinjaScript so all of the standard C# concepts surrounding structure and variable use apply.

            Once you have defined a value for the variable in State.SetDefaults, the variable can be used from other locations in this script such as OnBarUpdate or the other overrides in this script. Other scripts would need to implement this same syntax to have this variable also.

            For further information on this topic, I would suggest searching online for some C# variable tutorials or object-oriented C# examples. Here is one link I had found using google: http://www.informit.com/articles/art...09145&seqNum=4

            I look forward to being of further assistance.

            Comment


              #7
              Thanks. Now that there are no compile errors, I can see the result on the chart, and the custom background color is not displaying. In the If statement, if I remark out the custom color "BackBrush = UptrendColor;" line and replace with a default brush color "BackBrush = Brushes.Green;", then the background color displays correctly as Green, so the logic is not an issue. Any ideas as to why the custom color is not displaying?

              To summarize all the custom color code:

              Code located prior to OnStateChange(), within public class CustomColorTest : Indicator:
              private Brush UptrendColor;

              Code located in OnStateChange(), and inside If (State == State.SetDefaults):
              /// Initiate new solid color backbrush with custom color: Uptrend Background Color
              Brush UptrendColor = new SolidColorBrush(Color.FromRgb(50, 50, 50));
              UptrendColor.Freeze();

              Code located in OnBarUpdate():
              /// Uptrend - Background Color
              if (MA1[0] > MA2[0])
              {
              BackBrush = UptrendColor;
              }

              Comment


                #8
                Hello,

                Can you provide a more complete sample of your current syntax? I can see what has been discussed so far working on my end.

                Here is an image of the chart when using the syntax: https://www.screencast.com/t/HelV0K5SUXB

                I colored every bar in the top image versus coloring no bars in the bottom image. I can see a color change using the SolidColorBrush. Are you not seeing this occur?

                I look forward to being of further assistance.

                Comment


                  #9
                  The custom color code syntax is exactly as I copied it above, and it is located in the script as described. There isn't any additional code regarding the custom color. The code compiles with no errors, but the custom color background does not display. Making no change other than remarking out the custom color "BackBrush = UptrendColor;" line and replacing it with a default brush color "BackBrush = Brushes.Green;" line, the background color displays correctly as Green. Tried various RGB number combinations for the custom color, but still nothing.

                  Custom color coding seems way more complicated than it should be. For a custom color, the following syntax would be much simpler, not requiring any custom color initialization, freezing, private variable definition, etc., but of course this does not work:

                  BackBrush = Brushes.ColorFromRgb(50, 50, 50);

                  Edit:
                  Tried doing the initialization and freeze in OnStateChange() within State == State.DataLoaded, but got the same result (no custom color on chart).
                  Last edited by Lancer; 02-26-2018, 08:48 PM.

                  Comment


                    #10
                    OK, the problem was the word Brush before UptrendColor in the initialization code. Brush is not needed there because UptrendColor is already defined using private Brush UptrendColor.

                    For anyone else using custom colors, here is a code example that works:

                    Code located prior to OnStateChange(), within public class IndicatorName : Indicator:
                    private Brush UptrendColor;

                    Code located in OnStateChange(), and inside (State == State.SetDefaults) or (State == State.DataLoaded):
                    /// Initiate new solid color backbrush with custom color: Uptrend Background Color
                    UptrendColor = new SolidColorBrush(Color.FromRgb(50, 50, 50));
                    UptrendColor.Freeze();

                    Logic code located in OnBarUpdate():
                    /// Uptrend - Background Color
                    if (MovingAverage1[0] > MovingAverage2[0])
                    {
                    BackBrush = UptrendColor;
                    }

                    Comment


                      #11
                      HI Lancer,

                      Thank you for sharing!

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by argusthome, 03-08-2026, 10:06 AM
                      0 responses
                      85 views
                      0 likes
                      Last Post argusthome  
                      Started by NabilKhattabi, 03-06-2026, 11:18 AM
                      0 responses
                      47 views
                      0 likes
                      Last Post NabilKhattabi  
                      Started by Deep42, 03-06-2026, 12:28 AM
                      0 responses
                      29 views
                      0 likes
                      Last Post Deep42
                      by Deep42
                       
                      Started by TheRealMorford, 03-05-2026, 06:15 PM
                      0 responses
                      32 views
                      0 likes
                      Last Post TheRealMorford  
                      Started by Mindset, 02-28-2026, 06:16 AM
                      0 responses
                      67 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Working...
                      X