Specifically, to dynamically change the color (brush) of the current bar for an indicator's first (zeroeth) plot to blue, one is supposed to evaluate the following statement:
PlotBrushes[0][0] = Brushes.Blue;
However, doing so raises the following exception:
The calling thread cannot access this object because a different thread owns it.
at System.Windows.Threading.DispatcherObject.VerifyAc cess()
at System.Windows.Freezable.Freeze(Boolean isChecking)
at NinjaTrader.NinjaScript.BrushSeries.Set(Int32 index, Brush value)
at NinjaTrader.NinjaScript.BrushSeries.set_Item(Int32 barsAgo, Brush value)
at NinjaTrader.NinjaScript.Indicators.N1MovAvgOmniMA. colorizePlot(Int32 plotIndex, Double nextValue)
at NinjaTrader.NinjaScript.Indicators.N1LightweightIn dicator.updatePlotAt(Int32 plotIndex)
Here's the relevant activation frames in my code, showing the path of execution from OnBarUpdate():
if (double.IsNaN(nextValue)) return;
var prevValue = prevPlotValue(plotIndex);
if (double.IsNaN(prevValue)) return;
switch (Math.Sign(nextValue - prevValue)) {
case 1:
PlotBrushes[plotIndex][0] = RisingBrush;
break;
default:
PlotBrushes[plotIndex][0] = NeutralDirectionBrush;
break;
case -1:
PlotBrushes[plotIndex][0] = FallingBrush;
break;
}
}
protected virtual void updatePlotAt(int plotIndex) {
var nextValue = computePlotValue(plotIndex);
try {
if (double.IsNaN(nextValue)) {
Values[plotIndex].Reset();
} else {
setPlotValue(plotIndex, nextValue);
if (colorizePlots) colorizePlot(plotIndex, nextValue);
}
} catch (Exception ex) {
logMessage(ex.Message);
logMessage(ex.StackTrace);
}
}
protected override void updatePlots() {
if (BarsInProgress != 0) return;
for (int plotIndex = 0; plotIndex < Values.Length; plotIndex++) {
updatePlotAt(plotIndex);
}
}
protected override void OnBarUpdate() {
preUpdatePlots();
updatePlots();
postUpdatePlots();
}
Comment