Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

NT8 newbie - changing plot color per bar

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

    NT8 newbie - changing plot color per bar

    I have successfully changed a few indicators to plot green / red based on direction. I am trying to modify the 'included' MoneyFlowOscillator indicator and can't get it to change color. I have used the same technique I used in my other indicator changes, but nothing seems to work. Any assistance would be appreciated.


    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
    /// <summary>
    /// The Money Flow Oscillator measures the amount of money flow volume over a specific period. A move into positive territory indicates buying pressure while a move into negative territory indicates selling pressure.
    /// </summary>
    public class MoneyFlowOscillator : Indicator
    {
    private Series<double> mfv;
    private double dvs, mltp;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionMoneyFlowOscillator;
    Name = NinjaTrader.Custom.Resource.NinjaScriptIndicatorNa meMoneyFlowOscillator;
    IsOverlay = false;
    DrawOnPricePanel = false;
    IsSuspendedWhileInactive = true;
    Period = 20;

    AddPlot(Brushes.DodgerBlue, NinjaTrader.Custom.Resource.NinjaScriptIndicatorMo neyFlowLine);
    AddLine(Brushes.DarkGray, 0, NinjaTrader.Custom.Resource.NinjaScriptIndicatorZe roLine);
    }
    else if (State == State.DataLoaded)
    {
    mfv = new Series<double>(this);
    }
    }

    protected override void OnBarUpdate()
    {
    if(CurrentBar > 0)
    {
    dvs = (High[0] - Low[1]) + (High[1] - Low[0]) == 0 ? 0.00001 : (High[0] - Low[1]) + (High[1] - Low[0]);
    mltp = Math.Round(High[0] < Low[1] ? -1: Low[0] > High[1] ? 1 : ((High[0] - Low[1]) - (High[1] - Low[0])) / dvs, 2);
    mfv[0] = mltp * (Instrument.MasterInstrument.InstrumentType == Cbi.InstrumentType.CryptoCurrency ? Core.Globals.ToCryptocurrencyVolume((long)Volume[0]) : Volume[0]);

    if (CurrentBar >= Period)
    {
    double sumVolume = SUM(Volume, Period)[0];
    if (Instrument.MasterInstrument.InstrumentType == Cbi.InstrumentType.CryptoCurrency)
    sumVolume = Core.Globals.ToCryptocurrencyVolume((long)sumVolum e);
    Values[0][0] = Math.Round(SUM(mfv, Period)[0] / sumVolume, 3);
    }

    // Below are my 2 trys to get the color to change.
    /*
    if( mfv[0] > 0.0 )
    PlotBrushes[0][0] = Brushes.Green;
    if( mfv[0] < 0.0 )
    PlotBrushes[0][0] = Brushes.Red;
    if( mfv[0] = 0.0 )
    PlotBrushes[0][0] = Brushes.Yellow;
    */
    if( Values[0][0] > 0.0 )
    PlotBrushes[0][0] = Brushes.Green;
    if( Values[0][0] < 0.0 )
    PlotBrushes[0][0] = Brushes.Red;
    if( Values[0][0] = 0.0 )
    PlotBrushes[0][0] = Brushes.Yellow;


    }
    else
    mfv[0] = 0;
    }

    #2
    Hello joromero,

    Thanks for your post.

    Using the Values[0][0] should be what you want to base the condition evaluation on.

    This line: if( Values[0][0] = 0.0 ) has a typo and should be if( Values[0][0] == 0.0 ).

    Also, I would place the brush coding below the line mfv[0] = 0;

    So:

    Code:
                else
                    mfv[0] = 0;
    
                if( Values[0][0] > 0.0 )
                    PlotBrushes[0][0] = Brushes.Green;
                if( Values[0][0] < 0.0 )
                    PlotBrushes[0][0] = Brushes.Red;
                if( Values[0][0] == 0.0 )
                    PlotBrushes[0][0] = Brushes.Yellow;
            }

    Comment


      #3
      Paul,

      Thanks for the quick reply. I did make the one change you caught. Thanks. But, it still does not change the plot color? Any other thoughts?

      Comment


        #4
        Hello joromero,

        Thanks for your reply.

        Did you move the code as I showed in the code block?

        Works here:

        Click image for larger version

Name:	Joromero-1.PNG
Views:	358
Size:	108.6 KB
ID:	1062825

        Comment


          #5
          Paul, Again, thanks for your time. I did do as you suggested. I have also removed the indicator, pressed ok, and then once again selected 'Indicator' from the menu to add it back. I also tried to 'reload' ninja script. It looks like you created a copy of the indicator to make the changes. I did it to the original. Once I did a Save As, then compiled and loaded my named copy, it worked here as well. Not sure why, since when I made the changed to the original, it compiled with no issues.

          Any way, it is working now, so who knows ! ! !

          Again, thanks for your time

          John in NC

          Comment


            #6
            Hello John,

            Thanks for your reply.

            Glad you have it working.

            When working with scripts that draw or plot, it is indeed a good idea to remove the indicator from the chart and then reapply after you have changed your script and recompiled. This ensures that you are working with the just changed instance. This is a common mistake (and I make it myself as well).



            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by CarlTrading, 03-31-2026, 09:41 PM
            1 response
            81 views
            1 like
            Last Post NinjaTrader_ChelseaB  
            Started by CarlTrading, 04-01-2026, 02:41 AM
            0 responses
            42 views
            0 likes
            Last Post CarlTrading  
            Started by CaptainJack, 03-31-2026, 11:44 PM
            0 responses
            64 views
            2 likes
            Last Post CaptainJack  
            Started by CarlTrading, 03-30-2026, 11:51 AM
            0 responses
            66 views
            0 likes
            Last Post CarlTrading  
            Started by CarlTrading, 03-30-2026, 11:48 AM
            0 responses
            54 views
            0 likes
            Last Post CarlTrading  
            Working...
            X