/// <summary> /// Called on each bar update event (incoming tick) protected override void OnBarUpdate() { if (CurrentBar == 0) { upTrend.Set(true); StopDot.Set(Close[0]); StopLine.Set(Close[0]); PlotColors[0][0] = neutralColor; PlotColors[1][0] = neutralColor; return; } if (FirstTickOfBar) { priorUpTrend = upTrend[1]; priorStop = StopLine[1]; priorColor = PlotColors[0][1]; medianline = MM[1]; offset = MAE[1]; } if (Close[0] > priorStop) { upTrend.Set(true); newStop = medianline - Multiplier * offset; currentUpTrend = true; if (!priorUpTrend) // trend change up { StopDot.Set(newStop); StopLine.Set(newStop); } else { StopDot.Set(Math.Max(newStop, priorStop)); StopLine.Set(Math.Max(newStop, priorStop)); } } else if (Close[0] < priorStop) { upTrend.Set(false); newStop = medianline + Multiplier * offset; currentUpTrend = false; if (priorUpTrend) // trend change down { StopDot.Set(newStop); StopLine.Set(newStop); } else { StopDot.Set(Math.Min(newStop, priorStop)); StopLine.Set(Math.Min(newStop, priorStop)); } } else { upTrend.Set(priorUpTrend); currentUpTrend = priorUpTrend; StopDot.Set(priorStop); StopLine.Set(priorStop); } if (PaintBars) { if (currentUpTrend) { CandleOutlineColor = upColor; BarColor = upColor; } else { CandleOutlineColor = downColor; BarColor = downColor; } if (Open[0] < Close[0] && candles) { BarColor = Color.Transparent; } } if (ShowArrows) { if (currentUpTrend && !priorUpTrend) { DrawArrowUp("arrow" + CurrentBar, true, 0, newStop - 0.5 * offset, upColor); } else if (!currentUpTrend && priorUpTrend) { DrawArrowDown("arrow" + CurrentBar, true, 0, newStop + 0.5 * offset, downColor); } else { RemoveDrawObject("arrow" + CurrentBar); } } if (ShowStopDots) { if (currentUpTrend) { PlotColors[0][0] = upColor; } else { PlotColors[0][0] = downColor; } } if (ShowStopLine) { if (currentUpTrend && !priorUpTrend) { if (gap) { PlotColors[1][0] = neutralColor; } else { PlotColors[1][0] = upColor; } } else if (currentUpTrend) { PlotColors[1][0] = upColor; } else if (!currentUpTrend && priorUpTrend) { if (gap) { PlotColors[1][0] = neutralColor; } else { PlotColors[1][0] = downColor; } } else { PlotColors[1][0] = downColor; } } }
/// <summary> /// Called on each bar update event (incoming tick) protected override void OnBarUpdate() { if (CurrentBar == 0) { displacement = Math.Max(Displacement, -CurrentBar); priorUpTrend = true; currentUpTrend = true; upTrend.Set(true); StopDot.Set(Close[0]); StopLine.Set(Close[0]); PlotColors[0][-displacement] = Color.Transparent; PlotColors[1][-displacement] = Color.Transparent; return; } if (FirstTickOfBar) { displacement = Math.Max(Displacement, -CurrentBar); movingBase = baseline[1]; offset = Math.Max(TickSize, offsetSeries[1]); trailingAmount = multiplier * offset; margin = volatility[1]; if (currentUpTrend) { currentStopShort = movingBase + trailingAmount; if (priorUpTrend) { currentStopLong = Math.Max(currentStopLong, movingBase - trailingAmount); } else { currentStopLong = movingBase - trailingAmount; } StopDot.Set(currentStopLong); ReverseDot.Set(currentStopShort); PlotColors[0][-displacement] = upColor; if (showStopLine) { StopLine.Set(currentStopLong); if (gap && !priorUpTrend) { PlotColors[1][-displacement] = Color.Transparent; } else { PlotColors[1][-displacement] = upColor; } } else { StopLine.Reset(); } } else { currentStopLong = movingBase - trailingAmount; if (!priorUpTrend) { currentStopShort = Math.Min(currentStopShort, movingBase + trailingAmount); } else { currentStopShort = movingBase + trailingAmount; } StopDot.Set(currentStopShort); ReverseDot.Set(currentStopLong); PlotColors[0][-displacement] = downColor; if (showStopLine) { StopLine.Set(currentStopShort); if (gap && priorUpTrend) { PlotColors[1][-displacement] = Color.Transparent; } else { PlotColors[1][-displacement] = downColor; } } else { StopLine.Reset(); } } if (showPaintBars) { if (currentUpTrend) { trendColor = upColor; } else { trendColor = downColor; } CandleOutlineColorSeries[-displacement] = trendColor; BarColorSeries[-displacement] = trendColor; } if (showArrows) { if (currentUpTrend && !priorUpTrend) { DrawArrowUp("arrow" + CurrentBar, true, -displacement, currentStopLong - 0.5 * margin, upColor); } else if (!currentUpTrend && priorUpTrend) { DrawArrowDown("arrow" + CurrentBar, true, -displacement, currentStopShort + 0.5 * margin, downColor); } } priorUpTrend = currentUpTrend; stoppedOut = false; } if (reverseIntraBar) // only one trend change per bar is permitted { if (!stoppedOut) { if (priorUpTrend && Low[0] < currentStopLong) { currentUpTrend = false; stoppedOut = true; } else if (!priorUpTrend && High[0] > currentStopShort) { currentUpTrend = true; stoppedOut = true; } } } else { if (priorUpTrend && Close[0] < currentStopLong) { currentUpTrend = false; } else if (!priorUpTrend && Close[0] > currentStopShort) { currentUpTrend = true; } else { currentUpTrend = priorUpTrend; } } // this information can be accessed by a strategy if (CalculateOnBarClose) { upTrend.Set(currentUpTrend); } else if (FirstTickOfBar && !reverseIntraBar) { upTrend.Set(priorUpTrend); } else if (reverseIntraBar) { upTrend.Set(currentUpTrend); } if (showPaintBars && candles) { if (Open[0] < Close[0]) { BarColorSeries[-displacement] = Color.FromArgb(alpha, trendColor); } } if (soundAlert && !Historical && IsConnected() && (CalculateOnBarClose || reverseIntraBar)) { if (currentUpTrend && !priorUpTrend) { try { Alert("NewUpTrend", NinjaTrader.Cbi.Priority.Medium, "NewUpTrend", confirmedUpTrend, rearmTime, Color.Black, upColor); } catch {} } else if (!currentUpTrend && priorUpTrend) { try { Alert("NewDownTrend", NinjaTrader.Cbi.Priority.Medium, "NewDownTrend", confirmedDownTrend, rearmTime, Color.Black, downColor); } catch {} } } if (soundAlert && !Historical && IsConnected() && !CalculateOnBarClose && !reverseIntraBar) { if (currentUpTrend && !priorUpTrend) { try { Alert("PotentialUpTrend", NinjaTrader.Cbi.Priority.Medium, "PotentialUpTrend", potentialUpTrend, rearmTime, Color.Black, upColor); } catch {} } else if (!currentUpTrend && priorUpTrend) { try { Alert("PotentialDownTrend", NinjaTrader.Cbi.Priority.Medium, "PotentialDownTrend", potentialDownTrend, rearmTime, Color.Black, downColor); } catch {} } } }
protected override void OnBarUpdate() { if (CurrentBar == 0) { upTrend.Set(true); StopDot.Set(Close[0]); StopLine.Set(Close[0]); PlotColors[0][0] = neutralColor; PlotColors[1][0] = neutralColor; return; } if (FirstTickOfBar) { priorUpTrend = upTrend[1]; priorStop = StopLine[1]; offset = mae[1]; } if (Close[0] > priorStop) { upTrend.Set(true); newStop = mm[0] - (Multiplier * offset); currentUpTrend = true; if (!priorUpTrend) { StopDot.Set(newStop); StopLine.Set(newStop); } else { StopDot.Set(Math.Max(newStop, priorStop)); StopLine.Set(Math.Max(newStop, priorStop)); } } else if (Close[0] < priorStop) { upTrend.Set(false); newStop = mm[0] + (Multiplier * offset); currentUpTrend = false; if (priorUpTrend) { StopDot.Set(newStop); StopLine.Set(newStop); } else { StopDot.Set(Math.Min(newStop, priorStop)); StopLine.Set(Math.Min(newStop, priorStop)); } } else { currentUpTrend = priorUpTrend; upTrend.Set(priorUpTrend); StopDot.Set(priorStop); StopLine.Set(priorStop); } if (PaintBars) { if (currentUpTrend) { CandleOutlineColor = upColor; BarColor = upColor; } else { CandleOutlineColor = downColor; BarColor = downColor; } } if (colorbackground) { BackColor = Color.FromArgb(opacity, currentUpTrend ? backgroundcolorUp : backgroundcolorDn); } if (colorAllBackgrounds) { BackColorAll = Color.FromArgb(opacity, currentUpTrend ? backgroundcolorUp : backgroundcolorDn); } if (ShowArrows) { if (currentUpTrend && !priorUpTrend) { DrawArrowUp("arrow" + CurrentBar, true, 0, newStop - (0.5 * offset), upColor); PlaySound("Alert4.wav"); } else if (!currentUpTrend && priorUpTrend) { DrawArrowDown("arrow" + CurrentBar, true, 0, newStop + (0.5 * offset), downColor); PlaySound("Alert4.wav"); } else { RemoveDrawObject("arrow" + CurrentBar); } } if (ShowStopLine) { PlotColors[1][0] = currentUpTrend && !priorUpTrend ? (gap ? neutralColor : upColor) : (currentUpTrend ? upColor : (!currentUpTrend && priorUpTrend ? (gap ? neutralColor : downColor) : downColor)); } }