So Ive been creating this with but cant get anything to show on the charts when i select it?
#region Using declarations
using System;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Tools;
using NinjaTrader.NinjaScript;
using NinjaTrader.NinjaScript.Indicators;
using NinjaTrader.NinjaScript.DrawingTools;
using System.Windows.Media;
#endregion
namespace NinjaTrader.NinjaScript.Indicators
{
public class TimeZoneWith50D : Indicator
{
private double sessionHigh;
private double sessionLow;
private bool rangeCaptured = false;
private DateTime sessionDate;
[NinjaScriptProperty]
public int StartTime { get; set; }
[NinjaScriptProperty]
public int EndTime { get; set; }
[NinjaScriptProperty]
public Brush HighLineColor { get; set; }
[NinjaScriptProperty]
public Brush LowLineColor { get; set; }
[NinjaScriptProperty]
public Brush MidLineColor { get; set; }
[NinjaScriptProperty]
public int HighLineThickness { get; set; }
[NinjaScriptProperty]
public int LowLineThickness { get; set; }
[NinjaScriptProperty]
public int MidLineThickness { get; set; }
[NinjaScriptProperty]
public Brush BackgroundColor { get; set; }
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "TimeZoneWith50D";
IsOverlay = true;
Calculate = Calculate.OnBarClose; // Or OnEachTick if you want real-time updates within the bar
StartTime = 930; // e.g., 9:30 AM
EndTime = 1030; // e.g., 10:30 AM
HighLineColor = Brushes.Green;
LowLineColor = Brushes.Red;
MidLineColor = Brushes.Blue;
HighLineThickness = 2;
LowLineThickness = 2;
MidLineThickness = 2;
BackgroundColor = Brushes.LightGray;
}
else if (State == State.Configure)
{
// Ensure historical data is loaded sufficiently
AddDataSeries(BarsPeriodType.Minute, 1); // Or whatever period you are using for your primary series
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < BarsRequiredToPlot)
return;
// Get the time of the current bar in HHMM format
int currentTime = ToTime(Time[0]);
DateTime currentDate = Time[0].Date;
// Check for the start of a new session
if (Bars.IsFirstBarOfSession)
{
sessionHigh = double.MinValue;
sessionLow = double.MaxValue;
rangeCaptured = false; // Reset for a new session
sessionDate = currentDate; // Store the date of the new session
}
// --- Capture Session High/Low within the specified time window ---
if (currentTime >= StartTime && currentTime <= EndTime)
{
sessionHigh = Math.Max(sessionHigh, High[0]);
sessionLow = Math.Min(sessionLow, Low[0]);
}
// --- Draw lines once the range is captured for the day ---
// We want to draw when the current bar's time is *after* the EndTime,
// and the range hasn't been captured for *this* session yet.
if (!rangeCaptured && currentTime > EndTime)
{
// Ensure we are processing the correct session's data
if (currentDate == sessionDate)
{
rangeCaptured = true; // Mark as captured for this session
double mid = (sessionHigh + sessionLow) / 2;
// Determine the start and end bars for drawing.
// The drawing should span from the StartTime of the session
// until the end of the trading session (or current bar if still live).
int drawStartBar = -1;
int drawEndBar = CurrentBar; // Default to current bar for live drawing
// Find the bar corresponding to StartTime for the current session
for (int i = CurrentBar; i >= 0; i--)
{
if (Time[i].Date == sessionDate && ToTime(Time[i]) <= StartTime)
{
drawStartBar = i;
break;
}
}
if (drawStartBar == -1) // If StartTime bar not found in loaded history for this session
return;
// In real-time, the lines should extend to the current bar.
// When backtesting, they should extend to the last bar of the session.
// We can't reliably predict future bars. For historical drawing,
// we can iterate forward. For live drawing, it should extend to CurrentBar.
// For the purpose of making sure lines show up, we'll draw to CurrentBar.
// If you want lines to extend across the whole session on historical data,
// you'd need a more complex `lastBarOfSession` logic, potentially
// iterating Bars.Count once, but that's less suitable for live.
// Clear previous drawings to prevent accumulation if the indicator recalculates
RemoveDrawObject("Zone_" + sessionDate.ToOADate());
RemoveDrawObject("HighLine_" + sessionDate.ToOADate());
RemoveDrawObject("LowLine_" + sessionDate.ToOADate());
RemoveDrawObject("MidLine_" + sessionDate.ToOADate());
// Draw shaded zone
// The rectangle should only span the captured range (StartTime to EndTime)
Draw.Rectangle(this, "Zone_" + sessionDate.ToOADate(), true,
drawStartBar, sessionHigh,
CurrentBar, sessionLow, // Use CurrentBar as the right boundary for the zone
BackgroundColor, BackgroundColor, 50); // Alpha 50 for semi-transparent
// Draw High, Low, Mid lines
// These lines should extend from the start of the time window to the current bar
// (or end of session if historical and you determine that).
// For simplicity and general functionality, let's extend them to the current bar.
Draw.Line(this, "HighLine_" + sessionDate.ToOADate(), false, drawStartBar, sessionHigh, CurrentBar, sessionHigh, HighLineColor, DashStyleHelper.Solid, HighLineThickness);
Draw.Line(this, "LowLine_" + sessionDate.ToOADate(), false, drawStartBar, sessionLow, CurrentBar, sessionLow, LowLineColor, DashStyleHelper.Solid, LowLineThickness);
Draw.Line(this, "MidLine_" + sessionDate.ToOADate(), false, drawStartBar, mid, CurrentBar, mid, MidLineColor, DashStyleHelper.Dash, MidLineThickness);
}
}
// If the range has been captured and we are still in the same session,
// we need to update the lines to extend to the current bar.
// This is important for live data or when bars are still forming after EndTime.
if (rangeCaptured && currentDate == sessionDate)
{
// Update the lines to extend to the current bar's end.
// We need to re-find the drawStartBar for consistent drawing.
int drawStartBar = -1;
for (int i = CurrentBar; i >= 0; i--)
{
if (Time[i].Date == sessionDate && ToTime(Time[i]) <= StartTime)
{
drawStartBar = i;
break;
}
}
if (drawStartBar != -1)
{
double mid = (sessionHigh + sessionLow) / 2;
// Remove existing to redraw extended lines
RemoveDrawObject("HighLine_" + sessionDate.ToOADate());
RemoveDrawObject("LowLine_" + sessionDate.ToOADate());
RemoveDrawObject("MidLine_" + sessionDate.ToOADate());
RemoveDrawObject("Zone_" + sessionDate.ToOADate());
Draw.Rectangle(this, "Zone_" + sessionDate.ToOADate(), true,
drawStartBar, sessionHigh,
CurrentBar, sessionLow,
BackgroundColor, BackgroundColor, 50);
Draw.Line(this, "HighLine_" + sessionDate.ToOADate(), false, drawStartBar, sessionHigh, CurrentBar, sessionHigh, HighLineColor, DashStyleHelper.Solid, HighLineThickness);
Draw.Line(this, "LowLine_" + sessionDate.ToOADate(), false, drawStartBar, sessionLow, CurrentBar, sessionLow, LowLineColor, DashStyleHelper.Solid, LowLineThickness);
Draw.Line(this, "MidLine_" + sessionDate.ToOADate(), false, drawStartBar, mid, CurrentBar, mid, MidLineColor, DashStyleHelper.Dash, MidLineThickness);
}
}
}
}
}
#region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private TimeZoneWith50D cacheTimeZoneWith50D;
public TimeZoneWith50D TimeZoneWith50D(int startTime, int endTime, Brush highLineColor, Brush lowLineColor, Brush midLineColor, int highLineThickness, int lowLineThickness, int midLineThickness, Brush backgroundColor)
{
return TimeZoneWith50D(Input, startTime, endTime, highLineColor, lowLineColor, midLineColor, highLineThickness, lowLineThickness, midLineThickness, backgroundColor);
}
public TimeZoneWith50D TimeZoneWith50D(ISeries<double> input, int startTime, int endTime, Brush highLineColor, Brush lowLineColor, Brush midLineColor, int highLineThickness, int lowLineThickness, int midLineThickness, Brush backgroundColor)
{
if (cacheTimeZoneWith50D != null)
for (int idx = 0; idx < cacheTimeZoneWith50D.Length; idx++)
if (cacheTimeZoneWith50D[idx] != null && cacheTimeZoneWith50D[idx].StartTime == startTime && cacheTimeZoneWith50D[idx].EndTime == endTime && cacheTimeZoneWith50D[idx].HighLineColor == highLineColor && cacheTimeZoneWith50D[idx].LowLineColor == lowLineColor && cacheTimeZoneWith50D[idx].MidLineColor == midLineColor && cacheTimeZoneWith50D[idx].HighLineThickness == highLineThickness && cacheTimeZoneWith50D[idx].LowLineThickness == lowLineThickness && cacheTimeZoneWith50D[idx].MidLineThickness == midLineThickness && cacheTimeZoneWith50D[idx].BackgroundColor == backgroundColor && cacheTimeZoneWith50D[idx].EqualsInput(input))
return cacheTimeZoneWith50D[idx];
return CacheIndicator<TimeZoneWith50D>(new TimeZoneWith50D(){ StartTime = startTime, EndTime = endTime, HighLineColor = highLineColor, LowLineColor = lowLineColor, MidLineColor = midLineColor, HighLineThickness = highLineThickness, LowLineThickness = lowLineThickness, MidLineThickness = midLineThickness, BackgroundColor = backgroundColor }, input, ref cacheTimeZoneWith50D);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.TimeZoneWith50D TimeZoneWith50D(int startTime, int endTime, Brush highLineColor, Brush lowLineColor, Brush midLineColor, int highLineThickness, int lowLineThickness, int midLineThickness, Brush backgroundColor)
{
return indicator.TimeZoneWith50D(Input, startTime, endTime, highLineColor, lowLineColor, midLineColor, highLineThickness, lowLineThickness, midLineThickness, backgroundColor);
}
public Indicators.TimeZoneWith50D TimeZoneWith50D(ISeries<double> input , int startTime, int endTime, Brush highLineColor, Brush lowLineColor, Brush midLineColor, int highLineThickness, int lowLineThickness, int midLineThickness, Brush backgroundColor)
{
return indicator.TimeZoneWith50D(input, startTime, endTime, highLineColor, lowLineColor, midLineColor, highLineThickness, lowLineThickness, midLineThickness, backgroundColor);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.TimeZoneWith50D TimeZoneWith50D(int startTime, int endTime, Brush highLineColor, Brush lowLineColor, Brush midLineColor, int highLineThickness, int lowLineThickness, int midLineThickness, Brush backgroundColor)
{
return indicator.TimeZoneWith50D(Input, startTime, endTime, highLineColor, lowLineColor, midLineColor, highLineThickness, lowLineThickness, midLineThickness, backgroundColor);
}
public Indicators.TimeZoneWith50D TimeZoneWith50D(ISeries<double> input , int startTime, int endTime, Brush highLineColor, Brush lowLineColor, Brush midLineColor, int highLineThickness, int lowLineThickness, int midLineThickness, Brush backgroundColor)
{
return indicator.TimeZoneWith50D(input, startTime, endTime, highLineColor, lowLineColor, midLineColor, highLineThickness, lowLineThickness, midLineThickness, backgroundColor);
}
}
}
#endregion