Add support for ORs defined in minues and hours in addition to other quality of life improvements

This commit is contained in:
moshferatu 2023-02-14 14:26:37 -08:00
parent 40db152a5b
commit bbd3bbcadf

View File

@ -21,15 +21,21 @@ using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
public enum TimeFrame
{
Seconds,
Minutes,
Hours
}
//This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
public class OpeningRange : Indicator
{
private double OpeningRangeHigh = 0.0;
private double OpeningRangeLow = 0.0;
private DateTime OpeningRangeStartAdjusted = DateTime.MinValue;
private double OpeningRangeHigh;
private double OpeningRangeLow;
private int OpeningRangeInSeconds;
protected override void OnStateChange()
{
@ -37,7 +43,7 @@ namespace NinjaTrader.NinjaScript.Indicators
{
Description = @"Plots the user-defined opening range";
Name = "Opening Range";
Calculate = Calculate.OnBarClose;
Calculate = Calculate.OnPriceChange;
IsOverlay = true;
DisplayInDataBox = true;
DrawOnPricePanel = true;
@ -48,82 +54,111 @@ 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;
OpeningRangeStart = DateTime.Parse("06:30", System.Globalization.CultureInfo.InvariantCulture);
OpeningRangeEnd = DateTime.Parse("07:00", System.Globalization.CultureInfo.InvariantCulture);
TradingEnd = DateTime.Parse("13:00", System.Globalization.CultureInfo.InvariantCulture);
OpeningRangeLength = 30;
OpeningRangeTimeFrame = TimeFrame.Minutes;
OpeningRangeHighStroke = new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3);
OpeningRangeLowStroke = new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3);
AddPlot(Brushes.Transparent, "ORH");
AddPlot(Brushes.Transparent, "ORL");
}
else if (State == State.Configure)
{
OpeningRangeHigh = 0.0;
OpeningRangeLow = 0.0;
if (OpeningRangeTimeFrame == TimeFrame.Seconds)
{
AddDataSeries(Data.BarsPeriodType.Second, 1);
OpeningRangeInSeconds = OpeningRangeLength;
}
else if (OpeningRangeTimeFrame == TimeFrame.Hours)
{
AddDataSeries(Data.BarsPeriodType.Minute, 30);
OpeningRangeInSeconds = OpeningRangeLength * 60 * 60;
}
else
{
AddDataSeries(Data.BarsPeriodType.Minute, 1);
OpeningRangeInSeconds = OpeningRangeLength * 60;
}
}
}
protected override void OnBarUpdate()
{
DateTime now = Time[0];
if (now.TimeOfDay > OpeningRangeStart.TimeOfDay && now.TimeOfDay <= OpeningRangeEnd.TimeOfDay)
{
if (OpeningRangeStartAdjusted == DateTime.MinValue)
{
// The time associated with the bar is the close of the bar.
// So the opening range start time is actually the close of the candle just prior to the open.
// This adjustment aims to correct this.
OpeningRangeStartAdjusted = now;
}
// 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 openingRangeStartTime = now.Date + OpeningRangeStartAdjusted.TimeOfDay;
DateTime openingRangeEndTime = now.Date + TradingEnd.TimeOfDay;
if (High[0] > OpeningRangeHigh || OpeningRangeHigh == 0.0)
{
RemoveDrawObject("ORH@" + OpeningRangeHigh);
OpeningRangeHigh = High[0];
Draw.Line(this, "ORH@" + OpeningRangeHigh, true, openingRangeStartTime, OpeningRangeHigh, openingRangeEndTime, OpeningRangeHigh,
OpeningRangeHighStroke.Brush, OpeningRangeHighStroke.DashStyleHelper, (int)OpeningRangeHighStroke.Width);
}
if (Low[0] < OpeningRangeLow || OpeningRangeLow == 0.0)
{
RemoveDrawObject("ORL@" + OpeningRangeLow);
OpeningRangeLow = Low[0];
Draw.Line(this, "ORL@" + OpeningRangeLow, true, openingRangeStartTime, OpeningRangeLow, openingRangeEndTime, OpeningRangeLow,
OpeningRangeLowStroke.Brush, OpeningRangeLowStroke.DashStyleHelper, (int)OpeningRangeLowStroke.Width);
}
}
if (now.TimeOfDay > TradingEnd.TimeOfDay)
if (now > marketClose)
{
OpeningRangeHigh = 0.0;
OpeningRangeLow = 0.0;
OpeningRangeStartAdjusted = DateTime.MinValue;
}
if (BarsInProgress == 0)
{
ORH[0] = OpeningRangeHigh;
ORL[0] = OpeningRangeLow;
return;
}
if (now > marketOpen && now <= marketOpen.AddSeconds(OpeningRangeInSeconds))
{
// 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)
{
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);
}
if (Lows[1][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);
}
}
}
[NinjaScriptProperty]
[PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKey")]
[Display(Name="Opening Range Start", Description="Opening range start in local time", Order=1, GroupName="Parameters")]
public DateTime OpeningRangeStart
[Range(1, int.MaxValue)]
[Display(Name="Length", Description="Length of opening range", Order=1, GroupName="Parameters")]
public int OpeningRangeLength
{ get; set; }
[NinjaScriptProperty]
[PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKey")]
[Display(Name="Opening Range End", Description="Opening range end in local time", Order=2, GroupName="Parameters")]
public DateTime OpeningRangeEnd
[Display(Name="Time Frame", Description="Time frame on which opening range is being calculated", Order=2, GroupName="Parameters")]
public TimeFrame OpeningRangeTimeFrame
{ get; set; }
[NinjaScriptProperty]
[PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKey")]
[Display(Name="Trading Session End", Description="Trading session end in local time", Order=3, GroupName="Parameters")]
public DateTime TradingEnd
{ get; set; }
[NinjaScriptProperty]
[Display(Name = "Opening Range High", Description = "Opening range high line drawn on chart", Order = 4, GroupName = "Parameters")]
[Display(Name = "Opening Range High", Description = "Opening range high line drawn on chart", Order = 3, GroupName = "Parameters")]
public Stroke OpeningRangeHighStroke
{ get; set; }
[NinjaScriptProperty]
[Display(Name = "Opening Range Low", Description = "Opening range low line drawn on chart", Order = 5, GroupName = "Parameters")]
[Display(Name = "Opening Range Low", Description = "Opening range low line drawn on chart", Order = 4, GroupName = "Parameters")]
public Stroke OpeningRangeLowStroke
{ get; set; }
[Browsable(false)]
[XmlIgnore]
public Series<double> ORH
{
get { return Values[0]; }
}
[Browsable(false)]
[XmlIgnore]
public Series<double> ORL
{
get { return Values[1]; }
}
}
}
@ -134,18 +169,18 @@ namespace NinjaTrader.NinjaScript.Indicators
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private OpeningRange[] cacheOpeningRange;
public OpeningRange OpeningRange(DateTime openingRangeStart, DateTime openingRangeEnd, DateTime tradingEnd, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
public OpeningRange OpeningRange(int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
{
return OpeningRange(Input, openingRangeStart, openingRangeEnd, tradingEnd, openingRangeHighStroke, openingRangeLowStroke);
return OpeningRange(Input, openingRangeLength, openingRangeTimeFrame, openingRangeHighStroke, openingRangeLowStroke);
}
public OpeningRange OpeningRange(ISeries<double> input, DateTime openingRangeStart, DateTime openingRangeEnd, DateTime tradingEnd, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
public OpeningRange OpeningRange(ISeries<double> input, int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
{
if (cacheOpeningRange != null)
for (int idx = 0; idx < cacheOpeningRange.Length; idx++)
if (cacheOpeningRange[idx] != null && cacheOpeningRange[idx].OpeningRangeStart == openingRangeStart && cacheOpeningRange[idx].OpeningRangeEnd == openingRangeEnd && cacheOpeningRange[idx].TradingEnd == tradingEnd && 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].EqualsInput(input))
return cacheOpeningRange[idx];
return CacheIndicator<OpeningRange>(new OpeningRange(){ OpeningRangeStart = openingRangeStart, OpeningRangeEnd = openingRangeEnd, TradingEnd = tradingEnd, OpeningRangeHighStroke = openingRangeHighStroke, OpeningRangeLowStroke = openingRangeLowStroke }, input, ref cacheOpeningRange);
return CacheIndicator<OpeningRange>(new OpeningRange(){ OpeningRangeLength = openingRangeLength, OpeningRangeTimeFrame = openingRangeTimeFrame, OpeningRangeHighStroke = openingRangeHighStroke, OpeningRangeLowStroke = openingRangeLowStroke }, input, ref cacheOpeningRange);
}
}
}
@ -154,14 +189,14 @@ namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.OpeningRange OpeningRange(DateTime openingRangeStart, DateTime openingRangeEnd, DateTime tradingEnd, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
public Indicators.OpeningRange OpeningRange(int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
{
return indicator.OpeningRange(Input, openingRangeStart, openingRangeEnd, tradingEnd, openingRangeHighStroke, openingRangeLowStroke);
return indicator.OpeningRange(Input, openingRangeLength, openingRangeTimeFrame, openingRangeHighStroke, openingRangeLowStroke);
}
public Indicators.OpeningRange OpeningRange(ISeries<double> input , DateTime openingRangeStart, DateTime openingRangeEnd, DateTime tradingEnd, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
public Indicators.OpeningRange OpeningRange(ISeries<double> input , int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
{
return indicator.OpeningRange(input, openingRangeStart, openingRangeEnd, tradingEnd, openingRangeHighStroke, openingRangeLowStroke);
return indicator.OpeningRange(input, openingRangeLength, openingRangeTimeFrame, openingRangeHighStroke, openingRangeLowStroke);
}
}
}
@ -170,14 +205,14 @@ namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.OpeningRange OpeningRange(DateTime openingRangeStart, DateTime openingRangeEnd, DateTime tradingEnd, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
public Indicators.OpeningRange OpeningRange(int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
{
return indicator.OpeningRange(Input, openingRangeStart, openingRangeEnd, tradingEnd, openingRangeHighStroke, openingRangeLowStroke);
return indicator.OpeningRange(Input, openingRangeLength, openingRangeTimeFrame, openingRangeHighStroke, openingRangeLowStroke);
}
public Indicators.OpeningRange OpeningRange(ISeries<double> input , DateTime openingRangeStart, DateTime openingRangeEnd, DateTime tradingEnd, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
public Indicators.OpeningRange OpeningRange(ISeries<double> input , int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
{
return indicator.OpeningRange(input, openingRangeStart, openingRangeEnd, tradingEnd, openingRangeHighStroke, openingRangeLowStroke);
return indicator.OpeningRange(input, openingRangeLength, openingRangeTimeFrame, openingRangeHighStroke, openingRangeLowStroke);
}
}
}