Add OR mid line and resolve issues around non-time based charts and daylight savings time

This commit is contained in:
moshferatu 2023-03-01 08:19:02 -08:00
parent e2f25cbbaf
commit 8dacfe1606

View File

@ -33,9 +33,17 @@ namespace NinjaTrader.NinjaScript.Indicators
{
public class OpeningRange : Indicator
{
private static int MINUTES_IN_HOUR = 60;
private static string REGULAR_TRADING_HOURS = "US Equities RTH";
private double OpeningRangeHigh;
private double OpeningRangeLow;
private int OpeningRangeInSeconds;
private double OpeningRangeMid;
private DateTime OpeningRangeDate;
private DateTime OpeningRangeStartTime;
private SessionIterator Session;
protected override void OnStateChange()
{
@ -54,85 +62,112 @@ namespace NinjaTrader.NinjaScript.Indicators
//Disable this property if your indicator requires custom values that cumulate with each new market data event.
//See Help Guide for additional information.
IsSuspendedWhileInactive = true;
ArePlotsConfigurable = false;
OpeningRangeLength = 30;
OpeningRangeTimeFrame = TimeFrame.Minutes;
OpeningRangeHighStroke = new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3);
OpeningRangeLowStroke = new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3);
OpeningRangeMidStroke = new Stroke(Brushes.Gray, DashStyleHelper.Dash, 3);
AddPlot(Brushes.Transparent, "ORH");
AddPlot(Brushes.Transparent, "ORL");
AddPlot(Brushes.Transparent, "ORM");
}
else if (State == State.Configure)
{
OpeningRangeHigh = 0.0;
OpeningRangeLow = 0.0;
OpeningRangeMid = 0.0;
OpeningRangeDate = DateTime.MinValue;
OpeningRangeStartTime = DateTime.MinValue;
if (OpeningRangeTimeFrame == TimeFrame.Seconds)
{
AddDataSeries(Data.BarsPeriodType.Second, 1);
OpeningRangeInSeconds = OpeningRangeLength;
AddDataSeries(Instrument.FullName, new BarsPeriod {
BarsPeriodType = BarsPeriodType.Second, Value = OpeningRangeLength }, REGULAR_TRADING_HOURS);
}
else if (OpeningRangeTimeFrame == TimeFrame.Hours)
{
AddDataSeries(Data.BarsPeriodType.Minute, 30);
OpeningRangeInSeconds = OpeningRangeLength * 60 * 60;
AddDataSeries(Instrument.FullName, new BarsPeriod {
BarsPeriodType = BarsPeriodType.Minute, Value = OpeningRangeLength * MINUTES_IN_HOUR }, REGULAR_TRADING_HOURS);
}
else
{
AddDataSeries(Data.BarsPeriodType.Minute, 1);
OpeningRangeInSeconds = OpeningRangeLength * 60;
AddDataSeries(Instrument.FullName, new BarsPeriod {
BarsPeriodType = BarsPeriodType.Minute, Value = OpeningRangeLength }, REGULAR_TRADING_HOURS);
}
}
else if (State == State.DataLoaded)
{
Session = new SessionIterator(BarsArray[1]);
}
}
protected override void OnBarUpdate()
{
// Converting time to UTC in order to simplify opening range calculation.
DateTime now = Times[BarsInProgress][0].ToUniversalTime();
DateTime marketOpen = now.Date.AddHours(14).AddMinutes(30);
DateTime marketClose = now.Date.AddHours(21);
DateTime now = Times[BarsInProgress][0];
DateTime marketOpen = now.Date + Session.GetTradingDayBeginLocal(Session.ActualTradingDayExchange).TimeOfDay;
DateTime marketClose = now.Date + Session.GetTradingDayEndLocal(Session.ActualTradingDayExchange).TimeOfDay;
if (now > marketClose)
if (Bars.IsFirstBarOfSession && IsFirstTickOfBar)
{
OpeningRangeHigh = 0.0;
OpeningRangeLow = 0.0;
OpeningRangeMid = 0.0;
}
if (BarsInProgress == 0)
{
ORH[0] = OpeningRangeHigh;
ORL[0] = OpeningRangeLow;
OpeningRangeMid = (OpeningRangeLow + OpeningRangeHigh) / 2.0;
ORM[0] = OpeningRangeMid;
if (now > marketOpen && now.Date != OpeningRangeDate)
{
OpeningRangeStartTime = now;
OpeningRangeDate = now.Date;
}
if (OpeningRangeHigh > 0.0 && OpeningRangeLow > 0.0)
{
Draw.Line(this, "ORH@" + OpeningRangeHigh, true, OpeningRangeStartTime, OpeningRangeHigh, marketClose, OpeningRangeHigh,
OpeningRangeHighStroke.Brush, OpeningRangeHighStroke.DashStyleHelper, (int)OpeningRangeHighStroke.Width);
Draw.Line(this, "ORL@" + OpeningRangeLow, true, OpeningRangeStartTime, OpeningRangeLow, marketClose, OpeningRangeLow,
OpeningRangeLowStroke.Brush, OpeningRangeLowStroke.DashStyleHelper, (int)OpeningRangeLowStroke.Width);
Draw.Line(this, "ORM@" + OpeningRangeMid, true, OpeningRangeStartTime, OpeningRangeMid, marketClose, OpeningRangeMid,
OpeningRangeMidStroke.Brush, OpeningRangeMidStroke.DashStyleHelper, (int)OpeningRangeMidStroke.Width);
}
return;
}
if (now > marketOpen && now <= marketOpen.AddSeconds(OpeningRangeInSeconds))
if (Bars.IsFirstBarOfSession)
{
// TODO: The OR would ideally start on the first candle of the market session.
// As it is, the OR begins plotting on the candle just prior to open.
if (Highs[1][0] > OpeningRangeHigh || OpeningRangeHigh == 0.0)
if (Highs[BarsInProgress][0] > OpeningRangeHigh || OpeningRangeHigh == 0.0)
{
RemoveDrawObject("ORH@" + OpeningRangeHigh);
OpeningRangeHigh = Highs[1][0];
Draw.Line(this, "ORH@" + OpeningRangeHigh, true, marketOpen.ToLocalTime(), OpeningRangeHigh, marketClose.ToLocalTime(), OpeningRangeHigh,
OpeningRangeHighStroke.Brush, OpeningRangeHighStroke.DashStyleHelper, (int)OpeningRangeHighStroke.Width);
RemoveDrawObject("ORM@" + OpeningRangeMid);
OpeningRangeHigh = Highs[BarsInProgress][0];
}
if (Lows[1][0] < OpeningRangeLow || OpeningRangeLow == 0.0)
if (Lows[BarsInProgress][0] < OpeningRangeLow || OpeningRangeLow == 0.0)
{
RemoveDrawObject("ORL@" + OpeningRangeLow);
OpeningRangeLow = Lows[1][0];
Draw.Line(this, "ORL@" + OpeningRangeLow, true, marketOpen.ToLocalTime(), OpeningRangeLow, marketClose.ToLocalTime(), OpeningRangeLow,
OpeningRangeLowStroke.Brush, OpeningRangeLowStroke.DashStyleHelper, (int)OpeningRangeLowStroke.Width);
RemoveDrawObject("ORM@" + OpeningRangeMid);
OpeningRangeLow = Lows[BarsInProgress][0];
}
}
}
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Length", Description="Length of opening range", Order=1, GroupName="Parameters")]
[Display(Name = "Length", Description = "Length of opening range", Order = 1, GroupName = "Parameters")]
public int OpeningRangeLength
{ get; set; }
[NinjaScriptProperty]
[Display(Name="Time Frame", Description="Time frame on which opening range is being calculated", Order=2, GroupName="Parameters")]
[Display(Name = "Time Frame", Description = "Time frame on which opening range is being calculated", Order = 2, GroupName = "Parameters")]
public TimeFrame OpeningRangeTimeFrame
{ get; set; }
@ -146,6 +181,11 @@ namespace NinjaTrader.NinjaScript.Indicators
public Stroke OpeningRangeLowStroke
{ get; set; }
[NinjaScriptProperty]
[Display(Name = "Opening Range Mid", Description = "Opening range mid line drawn on chart", Order = 5, GroupName = "Parameters")]
public Stroke OpeningRangeMidStroke
{ get; set; }
[Browsable(false)]
[XmlIgnore]
public Series<double> ORH
@ -159,6 +199,13 @@ namespace NinjaTrader.NinjaScript.Indicators
{
get { return Values[1]; }
}
[Browsable(false)]
[XmlIgnore]
public Series<double> ORM
{
get { return Values[2]; }
}
}
}
@ -169,18 +216,18 @@ namespace NinjaTrader.NinjaScript.Indicators
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private OpeningRange[] cacheOpeningRange;
public OpeningRange OpeningRange(int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
public OpeningRange OpeningRange(int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke, Stroke openingRangeMidStroke)
{
return OpeningRange(Input, openingRangeLength, openingRangeTimeFrame, openingRangeHighStroke, openingRangeLowStroke);
return OpeningRange(Input, openingRangeLength, openingRangeTimeFrame, openingRangeHighStroke, openingRangeLowStroke, openingRangeMidStroke);
}
public OpeningRange OpeningRange(ISeries<double> input, int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
public OpeningRange OpeningRange(ISeries<double> input, int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke, Stroke openingRangeMidStroke)
{
if (cacheOpeningRange != null)
for (int idx = 0; idx < cacheOpeningRange.Length; idx++)
if (cacheOpeningRange[idx] != null && cacheOpeningRange[idx].OpeningRangeLength == openingRangeLength && cacheOpeningRange[idx].OpeningRangeTimeFrame == openingRangeTimeFrame && cacheOpeningRange[idx].OpeningRangeHighStroke == openingRangeHighStroke && cacheOpeningRange[idx].OpeningRangeLowStroke == openingRangeLowStroke && cacheOpeningRange[idx].EqualsInput(input))
if (cacheOpeningRange[idx] != null && cacheOpeningRange[idx].OpeningRangeLength == openingRangeLength && cacheOpeningRange[idx].OpeningRangeTimeFrame == openingRangeTimeFrame && cacheOpeningRange[idx].OpeningRangeHighStroke == openingRangeHighStroke && cacheOpeningRange[idx].OpeningRangeLowStroke == openingRangeLowStroke && cacheOpeningRange[idx].OpeningRangeMidStroke == openingRangeMidStroke && cacheOpeningRange[idx].EqualsInput(input))
return cacheOpeningRange[idx];
return CacheIndicator<OpeningRange>(new OpeningRange(){ OpeningRangeLength = openingRangeLength, OpeningRangeTimeFrame = openingRangeTimeFrame, OpeningRangeHighStroke = openingRangeHighStroke, OpeningRangeLowStroke = openingRangeLowStroke }, input, ref cacheOpeningRange);
return CacheIndicator<OpeningRange>(new OpeningRange(){ OpeningRangeLength = openingRangeLength, OpeningRangeTimeFrame = openingRangeTimeFrame, OpeningRangeHighStroke = openingRangeHighStroke, OpeningRangeLowStroke = openingRangeLowStroke, OpeningRangeMidStroke = openingRangeMidStroke }, input, ref cacheOpeningRange);
}
}
}
@ -189,14 +236,14 @@ namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.OpeningRange OpeningRange(int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
public Indicators.OpeningRange OpeningRange(int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke, Stroke openingRangeMidStroke)
{
return indicator.OpeningRange(Input, openingRangeLength, openingRangeTimeFrame, openingRangeHighStroke, openingRangeLowStroke);
return indicator.OpeningRange(Input, openingRangeLength, openingRangeTimeFrame, openingRangeHighStroke, openingRangeLowStroke, openingRangeMidStroke);
}
public Indicators.OpeningRange OpeningRange(ISeries<double> input , int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
public Indicators.OpeningRange OpeningRange(ISeries<double> input , int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke, Stroke openingRangeMidStroke)
{
return indicator.OpeningRange(input, openingRangeLength, openingRangeTimeFrame, openingRangeHighStroke, openingRangeLowStroke);
return indicator.OpeningRange(input, openingRangeLength, openingRangeTimeFrame, openingRangeHighStroke, openingRangeLowStroke, openingRangeMidStroke);
}
}
}
@ -205,14 +252,14 @@ namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.OpeningRange OpeningRange(int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
public Indicators.OpeningRange OpeningRange(int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke, Stroke openingRangeMidStroke)
{
return indicator.OpeningRange(Input, openingRangeLength, openingRangeTimeFrame, openingRangeHighStroke, openingRangeLowStroke);
return indicator.OpeningRange(Input, openingRangeLength, openingRangeTimeFrame, openingRangeHighStroke, openingRangeLowStroke, openingRangeMidStroke);
}
public Indicators.OpeningRange OpeningRange(ISeries<double> input , int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
public Indicators.OpeningRange OpeningRange(ISeries<double> input , int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke, Stroke openingRangeMidStroke)
{
return indicator.OpeningRange(input, openingRangeLength, openingRangeTimeFrame, openingRangeHighStroke, openingRangeLowStroke);
return indicator.OpeningRange(input, openingRangeLength, openingRangeTimeFrame, openingRangeHighStroke, openingRangeLowStroke, openingRangeMidStroke);
}
}
}