/// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { double averageValue = 0; if (Smoothed) { averageValue = smoothedAverage[0]; } else { averageValue = average[0]; } double offset = 0; if (Smoothed) { offset = NumStdDev * smoothedVolatility[0]; } else { offset = NumStdDev * volatility[0]; } if (showMidband) { Middle.Set(averageValue); } else { Middle.Reset(); } if (showChannels) { Upper.Set(averageValue + offset); Lower.Set(averageValue - offset); } else { Upper.Reset(); Lower.Reset(); } if (CurrentBar < Math.Max(Period, StdDevPeriod)) { return; } if (FirstTickOfBar) { neutralSlope = threshold * averageTrueRange[1] / 1000; priorBull = bullish; middle = Middle[1] + Middle[2]; upper = Upper[1] + Upper[2]; lower = Lower[1] + Lower[2]; } if (2 * averageValue > middle + 3 * neutralSlope) { PlotColors[1][0] = upColor; if (useCentralSlope) { PlotColors[0][0] = UpColor; PlotColors[2][0] = UpColor; } if (priorBull <= 0) { index = CurrentBar; } if (Opacity != 0) { DrawRegion("bollinger" + index, CurrentBar - index + 1, 0, Upper, Lower, Color.Transparent, UpColor, Opacity); } bullish = 1; } else if (2 * averageValue < middle - 3 * neutralSlope) { PlotColors[1][0] = DownColor; if (useCentralSlope) { PlotColors[0][0] = DownColor; PlotColors[2][0] = DownColor; } if (priorBull >= 0) { index = CurrentBar; } if (Opacity != 0) { DrawRegion("bollinger" + index, CurrentBar - index + 1, 0, Upper, Lower, Color.Transparent, DownColor, Opacity); } bullish = -1; } else { PlotColors[1][0] = NeutralColor; if (useCentralSlope) { PlotColors[0][0] = NeutralColor; PlotColors[2][0] = NeutralColor; } if (priorBull != 0) { index = CurrentBar; } if (Opacity != 0) { DrawRegion("bollinger" + index, CurrentBar - index + 1, 0, Upper, Lower, Color.Transparent, NeutralColor, Opacity); } bullish = 0; } if (!useCentralSlope) { if (2 * averageValue + 2 * offset >= upper + 3 * neutralSlope) { PlotColors[0][0] = UpColor; } else if (2 * averageValue + 2 * offset <= upper - 3 * neutralSlope) { PlotColors[0][0] = DownColor; } else { PlotColors[0][0] = NeutralColor; } if (2 * averageValue - 2 * offset >= lower + 3 * neutralSlope) { PlotColors[2][0] = UpColor; } else if (2 * averageValue - 2 * offset <= lower - 3 * neutralSlope) { PlotColors[2][0] = DownColor; } else { PlotColors[2][0] = NeutralColor; } } }