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

Using Linear Gradient Brush for Bars

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

    Using Linear Gradient Brush for Bars

    Dear Support,

    I am trying to find a code snippet that would color the bars in linear gradient. For example , the equivalent of the BarBrush = Brushes.Green in the following example assuming the gradient is from Green to White :

    Code:
     If (CCI(14)[0] > 0)
    BarBrush = Brushes.Green;
    A simple question yet confusing in NT8 with the new rendering methods.

    Appreciate any samples or conversion to achieve the above condition for Linear Gradient (Green to White in Horizontal direction - bar thickness).

    Many Thanks.

    #2
    Hello,

    Thank you for the post.

    You could use the following as a guideline for creating gradients, although you would likely want to make the brush into a variable that is defined once instead of on every bar:


    Code:
    protected override void OnBarUpdate()
    {
    	LinearGradientBrush gradient = new LinearGradientBrush();
    	gradient.StartPoint = new Point( 0.5, 0.5 );
    	gradient.EndPoint = new Point( 1.0, 0.5 );
    
    	gradient.GradientStops.Add(new GradientStop(System.Windows.Media.Colors.Green, 0));
    	gradient.GradientStops.Add(new GradientStop(System.Windows.Media.Colors.White, 1));
    	gradient.Freeze();
    	BarBrush = gradient;
    }
    To better understand how to shift the gradient, I would suggest looking at Microsofts documentation on LinearGradientBrush.


    I would also suggest reading the comments on brushes here: https://ninjatrader.com/support/help...th_brushes.htm


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

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello,

      Thank you for the post.

      You could use the following as a guideline for creating gradients, although you would likely want to make the brush into a variable that is defined once instead of on every bar:


      Code:
      protected override void OnBarUpdate()
      {
      	LinearGradientBrush gradient = new LinearGradientBrush();
      	gradient.StartPoint = new Point( 0.5, 0.5 );
      	gradient.EndPoint = new Point( 1.0, 0.5 );
      
      	gradient.GradientStops.Add(new GradientStop(System.Windows.Media.Colors.Green, 0));
      	gradient.GradientStops.Add(new GradientStop(System.Windows.Media.Colors.White, 1));
      	gradient.Freeze();
      	BarBrush = gradient;
      }
      To better understand how to shift the gradient, I would suggest looking at Microsofts documentation on LinearGradientBrush.


      I would also suggest reading the comments on brushes here: https://ninjatrader.com/support/help...th_brushes.htm


      I look forward to being of further assistance.
      Hi Jesse,

      Thank you so much, your script worked perfectly and draws gradient bars.

      The question I have now is: does the LinearGradient work with custom colors defined as RGB or only system colors are to be used? For example, if I have a custom brush defined as:

      Code:
      Brush myBrush = new SolidColorBrush(Color.FromRgb(Convert.ToByte(25), Convert.ToByte(120), Convert.ToByte(0)));
      Can it be used in your sample script, how? If I try to substitute "myBrush" for the "System.Windows.Media.Colors.Green" for stops it will cause errors.

      Many thanks.

      Comment


        #4
        Hello,

        You should be able to use the System.Windows.Media.Color namespace and its methods for RGB in place of the default colors as shown in the last example. Here is an example from ARGB:

        Code:
        gradient.GradientStops.Add(new GradientStop(System.Windows.Media.Color.FromArgb(255, 255, 0, 0), 0));
        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Jesse View Post
          Hello,

          You should be able to use the System.Windows.Media.Color namespace and its methods for RGB in place of the default colors as shown in the last example. Here is an example from ARGB:

          Code:
          gradient.GradientStops.Add(new GradientStop(System.Windows.Media.Color.FromArgb(255, 255, 0, 0), 0));
          I look forward to being of further assistance.
          Hi Jesse,

          Perfect. Thank you!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by algospoke, Yesterday, 06:40 PM
          2 responses
          23 views
          0 likes
          Last Post algospoke  
          Started by ghoul, Today, 06:02 PM
          3 responses
          14 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by jeronymite, 04-12-2024, 04:26 PM
          3 responses
          45 views
          0 likes
          Last Post jeronymite  
          Started by Barry Milan, Yesterday, 10:35 PM
          7 responses
          22 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by AttiM, 02-14-2024, 05:20 PM
          10 responses
          181 views
          0 likes
          Last Post jeronymite  
          Working...
          X