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

Balance of Power Color Change

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

    Balance of Power Color Change

    I am trying to have the Balance of Power Indicator show green when above zero, and red when below zero. I seem to be stuck and cannot get it to function. Any ideas?

    Code:
    // 
    // Copyright (C) 2016, NinjaTrader LLC <www.ninjatrader.com>.
    // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
    //
    #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.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion
    
    // This namespace holds indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
        /// <summary>
        /// The balance of power indicator measures the strength of the bulls vs. bears by
        ///  assessing the ability of each to push price to an extreme level.
        /// </summary>
        public class BOPDualColor : Indicator
        {
            private Series<double>    bop;
            private SMA                sma;
                
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                    = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDescriptionBOP;
                    Name                        = "BOPDualColor";
                    IsSuspendedWhileInactive    = true;
                    Smooth                         = 14;
                    IsOverlay                    = false;
                if (Value[0] > 0)
                {
                    AddPlot(new Stroke(Brushes.Lime, 2), PlotStyle.Bar, "BOPDualColor");
                }
                else
                {
                    AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.Bar, "BOPDualColor");
                    }
                    
                }
                else if (State == State.DataLoaded)
                {
                    bop = new Series<double>(this);
                    sma    = SMA(bop, Smooth);
                }
            }
            
            protected override void OnBarUpdate()
                
                
    {
                double high0    = High[0];
                double low0        = Low[0];
    
                if ((high0 - low0).ApproxCompare(0) == 0)
                    bop[0] = 0;
                else 
                    bop[0] = (Close[0] - Open[0]) / (high0 - low0);
                
                Value[0] = sma[0];
                
                
            }
    
    
    
            #region Properties
            [Range(1, int.MaxValue), NinjaScriptProperty]
            [Display(ResourceType = typeof(Custom.Resource), Name = "Smooth", GroupName = "NinjaScriptParameters", Order = 0)]
            public int Smooth
            { get; set; }    
            #endregion
        }
    }
    
    #region NinjaScript generated code. Neither change nor remove.
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
        {
            private BOPDualColor[] cacheBOPDualColor;
            public BOPDualColor BOPDualColor(int smooth)
            {
                return BOPDualColor(Input, smooth);
            }
    
            public BOPDualColor BOPDualColor(ISeries<double> input, int smooth)
            {
                if (cacheBOPDualColor != null)
                    for (int idx = 0; idx < cacheBOPDualColor.Length; idx++)
                        if (cacheBOPDualColor[idx] != null && cacheBOPDualColor[idx].Smooth == smooth && cacheBOPDualColor[idx].EqualsInput(input))
                            return cacheBOPDualColor[idx];
                return CacheIndicator<BOPDualColor>(new BOPDualColor(){ Smooth = smooth }, input, ref cacheBOPDualColor);
            }
        }
    }
    
    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
        public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
        {
            public Indicators.BOPDualColor BOPDualColor(int smooth)
            {
                return indicator.BOPDualColor(Input, smooth);
            }
    
            public Indicators.BOPDualColor BOPDualColor(ISeries<double> input , int smooth)
            {
                return indicator.BOPDualColor(input, smooth);
            }
        }
    }
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
        {
            public Indicators.BOPDualColor BOPDualColor(int smooth)
            {
                return indicator.BOPDualColor(Input, smooth);
            }
    
            public Indicators.BOPDualColor BOPDualColor(ISeries<double> input , int smooth)
            {
                return indicator.BOPDualColor(input, smooth);
            }
        }
    }
    
    #endregion

    #2
    Hello lenmat,

    Thanks for your post and welcome to the forums!

    In the State.SetDefaults you would not be able access the Value[0] statement as data has not loaded and this is not the correct way in which to change the color of the plot.

    To change the color of the plot you do not need to add another plot, you only need to change the brush color of the existing plot based on your conditions. This would be done in the OnBarUpdate() method and would be accomplished by setting the brush color through PlotBrushes. Reference: http://ninjatrader.com/support/helpG...lotbrushes.htm

    For example

    Code:
    protected override void OnBarUpdate()
    		{
    			double high0	= High[0];
    			double low0		= Low[0];
    
    			if ((high0 - low0).ApproxCompare(0) == 0)
    				bop[0] = 0;
    			else 
    				bop[0] = (Close[0] - Open[0]) / (high0 - low0);
    			
    			Value[0] = sma[0];
    [B]
                            if (Value[0] > 0)
                                 PlotBrushes[0][0] = Brushes.Green;
                             else
                                 PlotBrushes[0][0] = Brushes.Red;[/B]
    		}
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      What Paul said (our posts crossed in the ether):
      Remove the conditional test in SetDefaults. A single plot will suffice because you can use PlotBrushes to adjust its color bar by bar in OnBarUpdate according to conditions there.
      Last edited by tradesmart; 03-01-2017, 07:04 AM.

      Comment


        #4
        Thank you both, still learning! this worked perfectly.

        Comment


          #5
          Hi guys
          Following from this I would like your help. This BOP color is just what I was trying to do but I would like to go an extra step. I would like to color the price bars as well as the indicator. The thing is my coding skills are pretty basic.

          I know that if I change the:

          PlotBrushes[0][0] = Brushes.Green;

          to

          BarBrush = Brushes.Green;

          I´ll have the price bars colored but not the indicator.
          If I try to put both with a && it doesn´t accept it.
          If I try to put one after the other like this:

          BarBrush = Brushes.Green;
          PlotBrushes[0][0] = Brushes.Green;

          then only the first one works even if I swap them around.

          So how can I have both the indicator and the price bar colored?
          Appreciate your help!

          Comment


            #6
            Hello cooltrooper,

            Thanks for writing in to our Support team.

            If you are attempting to assign the plot and bar color after a conditional if() statement, you will need to encase your assignments within curly braces after the if() statement so that both assignments can occur when the condition is met, like so:

            if(Value[0]>0)
            {
            BarBrush = Brushes.Green;
            PlotBrushes[0][0] = Brushes.Green;
            }

            The way in which you are assigning colors will only assign the first line of code after the if statement. If you are not using curly braces, your if statements will only run the next line of code immediately after the if statement - if you want to run more code then you will need to use the curly braces {}.

            Additionally, you can use BarBrushes to color bars other than the current bar. You can find more information in our help guide here:

            BarBrushes - http://ninjatrader.com/support/helpG...barbrushes.htm

            Please let me know if I may be of any further assistance.
            Alan S.NinjaTrader Customer Service

            Comment


              #7
              Hi Alan

              That´s what I was missing. The curly braces. Now it is working perfectly.

              Thank you very much.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by ZenCortexCLICK, Today, 04:58 AM
              0 responses
              2 views
              0 likes
              Last Post ZenCortexCLICK  
              Started by sidlercom80, 10-28-2023, 08:49 AM
              172 responses
              2,280 views
              0 likes
              Last Post sidlercom80  
              Started by Irukandji, Yesterday, 02:53 AM
              2 responses
              17 views
              0 likes
              Last Post Irukandji  
              Started by adeelshahzad, Today, 03:54 AM
              0 responses
              4 views
              0 likes
              Last Post adeelshahzad  
              Started by Barry Milan, Yesterday, 10:35 PM
              3 responses
              13 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Working...
              X