Here's a handy UserStrategyBase extension method you can use to plot an indicator with colors that change according to the indicator's value changes. Of course, you can always easily convert this from an extension method to a regular method.
For additional info, see the comments for details...
For additional info, see the comments for details...
CODE:
/// <summary> /// Plot an indicator with up and down colors. When the indicator value is greater than the previous value, the upColor /// is used. When the indicator value is less than the previous value, the downColor is used. When the indicator value /// is equal to the previous value, the equalColor is used. /// </summary> /// <param name="userStrategyBase">The strategy for the plot</param> /// <param name="indicator">The indicator to plot</param> /// <param name="upColor"> /// The indicator's color when its value goes up versus the previous bar. Defaults to the chart /// theme's up bar color. /// </param> /// <param name="downColor"> /// The indicator's color when its value goes down versus the previous bar. Defaults to the chart /// theme's down bar color. /// </param> /// <param name="equalColor"> /// The indicator's color when its value is equal to the previous bar's value. Defaults to /// WLColor.Gray /// </param> /// <param name="plotStyle">The indicator's plot style</param> /// <param name="suppressLabels"> /// Set to true to disable the indicator pane labels and value markers on the chart's /// right margin /// </param> /// <param name="paneTag">The pane's tag where the indicator will be shown</param> public static void PlotIndicatorWithUpDownColors(this UserStrategyBase userStrategyBase, IndicatorBase indicator, WLColor upColor = null, WLColor downColor = null, WLColor equalColor = null, PlotStyle plotStyle = PlotStyle.Line, bool suppressLabels = false, string paneTag = null) { var cp = WealthLab.ChartWPF.ChartThemeFactory.Instance.SelectedTheme; downColor ??= cp.ColorDownBar; upColor ??= cp.ColorUpBar; equalColor ??= WLColor.Gray; userStrategyBase.PlotIndicator(indicator, upColor, plotStyle, suppressLabels, paneTag); for (var i = 1; i < indicator.Count; i++) { userStrategyBase.SetSeriesBarColor(indicator, i, indicator[i] > indicator[i - 1] ? upColor : indicator[i] < indicator[i - 1] ? downColor : equalColor); } }
Rename
Thanks for posting the code! I'm always grateful for examples to help with my own coding/learning journey. I was actually thinking of possibly doing something like this and adapting it to Volume bars and highlighting in a different color when it exceeded a certain RVol (relative volume) threshold, but didn't even get to the start line of trying to figure out how to do it.
Thanks again.
Thanks again.
Here is a (minor) fix to allow the indicator's plot style to be easily utilized. The plotStyle type is changed to PlotStyle? and its default value is changed to null...
CODE:
/// <summary> /// Plot an indicator with up and down colors. When the indicator value is greater than the previous value, the upColor /// is used. When the indicator value is less than the previous value, the downColor is used. When the indicator value /// is equal to the previous value, the equalColor is used. /// </summary> /// <param name="userStrategyBase">The strategy for the plot</param> /// <param name="indicator">The indicator to plot</param> /// <param name="upColor"> /// The indicator's color when its value goes up versus the previous bar. Defaults to the chart /// theme's up bar color. /// </param> /// <param name="downColor"> /// The indicator's color when its value goes down versus the previous bar. Defaults to the chart /// theme's down bar color. /// </param> /// <param name="equalColor"> /// The indicator's color when its value is equal to the previous bar's value. Defaults to /// WLColor.Gray /// </param> /// <param name="plotStyle">The indicator's plot style</param> /// <param name="suppressLabels"> /// Set to true to disable the indicator pane labels and value markers on the chart's /// right margin /// </param> /// <param name="paneTag">The pane's tag where the indicator will be shown</param> public static void PlotIndicatorWithUpDownColors(this UserStrategyBase userStrategyBase, IndicatorBase indicator, WLColor upColor = null, WLColor downColor = null, WLColor equalColor = null, PlotStyle? plotStyle = null, bool suppressLabels = false, string paneTag = null) { var cp = WealthLab.ChartWPF.ChartThemeFactory.Instance.SelectedTheme; downColor ??= cp.ColorDownBar; upColor ??= cp.ColorUpBar; equalColor ??= WLColor.Gray; userStrategyBase.PlotIndicator(indicator, upColor, plotStyle, suppressLabels, paneTag); for (var i = 1; i < indicator.Count; i++) { userStrategyBase.SetSeriesBarColor(indicator, i, indicator[i] > indicator[i - 1] ? upColor : indicator[i] < indicator[i - 1] ? downColor : equalColor); } }
Very nice, thanks!
Your Response
Post
Edit Post
Login is required