// // 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
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!
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
NinjaTrader
Balance of Power Color Change
Collapse
X
-
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:Tags: None
-
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
-
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
-
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
-
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
-
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 Graci117, Today, 09:02 PM
|
1 response
4 views
0 likes
|
Last Post Today, 09:09 PM | ||
Started by ETFVoyageur, Today, 07:55 PM
|
0 responses
7 views
0 likes
|
Last Post
by ETFVoyageur
Today, 07:55 PM
|
||
Started by janio973, Today, 07:24 PM
|
1 response
7 views
0 likes
|
Last Post Today, 07:35 PM | ||
Started by aligator, 01-06-2022, 12:14 PM
|
4 responses
245 views
0 likes
|
Last Post
by john_44573
Today, 06:11 PM
|
||
Started by reynoldsn, Today, 05:56 PM
|
0 responses
14 views
0 likes
|
Last Post
by reynoldsn
Today, 05:56 PM
|
Comment