Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Fixing Color Spectrum Glitch For Plot Coloring

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

    Fixing Color Spectrum Glitch For Plot Coloring

    Hi there,

    I have a custom NinjaTrader 8 indicator that uses a color spectrum to color a plot according to specific levels on the indicator panel. The levels and corresponding RGB colors are as follows:
    • 0 - 20: (0, 255, 255)
    • 20 - 50: (0, 255, 0)
    • 50 - 80: (255, 255, 0)
    • 80 - 100: (255, 0, 0)

    I prefer using RGB values instead of the built-in NinjaTrader colors, and I mention this in case using RGB values might cause issues.

    After some time, the color spectrum seems to get stuck on one specific color. To fix this, I need to remove and re-add the indicator to the chart to update the colors.

    In the past, I've noticed that setting the indicator to "OnBarClose" instead of "OnEachTick" can sometimes fix this issue, but not always.

    However, I would prefer to keep the indicator set to "OnEachTick" while ensuring the colors do not get stuck.

    I'm seeking advice on how to fix this problem or a reference script that addresses this issue.

    Any help would be much appreciated.

    Thank you.

    #2
    Hello RoswellTrader,

    Based on the description of it getting stuck that sounds like something in its logic related to the setting of the plots color is having a problem. The best way to see what is happening would be to add prints into that part of the logic to confirm the value is getting stuck in some way.

    Comment


      #3
      Hi Jesse,

      Thank you for your suggestion. I appreciate your insight into the issue. I will add prints into the logic to check if the value is getting stuck and see if that reveals any problems.

      Comment


        #4
        Hi again,

        So I've been trying various methods to try and fix this issue, but haven't come right.

        I'm hoping someone could have a look at my code below and the image attached to see what I'm doing wrong.

        Any help will be much appreciated, I apologize for the inconvenience.

        My code:
        region Using declarations
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.ComponentModel.DataAnnotations;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using System.Windows;
        using System.Windows.Input;
        using System.Windows.Media;
        using System.Xml.Serialization;
        using NinjaTrader.Cbi;
        using NinjaTrader.Gui;
        using NinjaTrader.Gui.Chart;
        using NinjaTrader.Gui.SuperDom;
        using NinjaTrader.Gui.Tools;
        using NinjaTrader.Data;
        using NinjaTrader.NinjaScript;
        using NinjaTrader.Core.FloatingPoint;
        using NinjaTrader.NinjaScript.Strategies;
        using NinjaTrader.NinjaScript.Indicators;
        using NinjaTrader.NinjaScript.DrawingTools;
        #endregion

        namespace NinjaTrader.NinjaScript.Indicators
        {
        public class SpectrumColor : Indicator
        {
        private Series<double> dSeries;

        [NinjaScriptProperty]
        [Display(Name = "Smooth", Order = 1, GroupName = "Parameters")]
        public int Smooth { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "D", Order = 2, GroupName = "Parameters")]
        public int D { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "K", Order = 3, GroupName = "Parameters")]
        public int K { get; set; }

        protected override void OnStateChange()
        {
        if (State == State.SetDefaults)
        {
        Description = @"Stochastic indicator with custom spectrum coloring";
        Name = "SpectrumColor";
        Calculate = Calculate.OnEachTick;
        IsOverlay = false;
        DrawOnPricePanel = false;

        Smooth = 6;
        D = 3;
        K = 8;

        AddPlot(new Stroke(Brushes.Transparent, 2), PlotStyle.Line, "K");
        AddPlot(new Stroke(Brushes.White, 2), PlotStyle.Line, "D");

        AddLine(Brushes.Cyan, 100, "Upper Critical");
        AddLine(Brushes.Cyan, 80, "Upper Critical");
        AddLine(Brushes.DarkViolet, 20, "Lower Critical");
        AddLine(Brushes.DarkViolet, 0, "Lower Critical");
        }
        else if (State == State.Configure)
        {
        dSeries = new Series<double>(this);
        }
        }

        protected override void OnBarUpdate()
        {
        if (CurrentBar < D) return;

        double kValue = Stochastics(1, K, Smooth).K[0];
        double dValue = SMA(Stochastics(1, K, Smooth).K, D)[0];

        Values[0][0] = kValue;
        dSeries[0] = dValue;
        Values[1][0] = dValue;

        PlotBrushes[1][0] = GetSpectrumColor(dValue);
        }

        private Brush GetSpectrumColor(double value)
        {
        if (value <= 25)
        return GetFrozenGradientBrush(value, 0, 25, Color.FromRgb(0, 0, 255), Color.FromRgb(0, 255, 255));
        else if (value <= 50)
        return GetFrozenGradientBrush(value, 25, 50, Color.FromRgb(0, 255, 255), Color.FromRgb(0, 255, 0));
        else if (value <= 75)
        return GetFrozenGradientBrush(value, 50, 75, Color.FromRgb(0, 255, 0), Color.FromRgb(255, 255, 0));
        else
        return GetFrozenGradientBrush(value, 75, 100, Color.FromRgb(255, 255, 0), Color.FromRgb(255, 0, 0));
        }

        private Brush GetFrozenGradientBrush(double value, double minValue, double maxValue, Color startColor, Color endColor)
        {
        double ratio = (value - minValue) / (maxValue - minValue);
        byte r = (byte)((endColor.R - startColor.R) * ratio + startColor.R);
        byte g = (byte)((endColor.G - startColor.G) * ratio + startColor.G);
        byte b = (byte)((endColor.B - startColor.B) * ratio + startColor.B);
        var brush = new SolidColorBrush(Color.FromRgb(r, g, b));
        brush.Freeze();
        return brush;
        }
        }
        }​
        Attached Files

        Comment


          #5
          Hello RoswellTrader,

          from the code you would need to print out the value you are passing to GetSpectrumColor. That method has a default value that is returned if the value is outside of the ranges you specified, that would likely be the best location to start checking the values to see what is happening. You could add a Print before the following line:

          Print(Time[0] + " " + dValue);
          PlotBrushes[1][0] = GetSpectrumColor(dValue);

          That would let you see the value for each bar so you can get a better idea how that condition will evaluate for each bar.

          Comment


            #6
            Thanks so much for the help Jesse, I managed to solve the issue

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            581 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            338 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            103 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            554 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            552 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X