2023-02-10 16:39:29 +00:00
|
|
|
#region Using declarations
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.ComponentModel;
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Windows;
|
|
|
|
using System.Windows.Input;
|
|
|
|
using System.Windows.Media;
|
|
|
|
using System.Xml.Serialization;
|
|
|
|
using NinjaTrader.Cbi;
|
|
|
|
using NinjaTrader.Gui;
|
|
|
|
using NinjaTrader.Gui.Chart;
|
|
|
|
using NinjaTrader.Gui.SuperDom;
|
|
|
|
using NinjaTrader.Gui.Tools;
|
|
|
|
using NinjaTrader.Data;
|
|
|
|
using NinjaTrader.NinjaScript;
|
|
|
|
using NinjaTrader.Core.FloatingPoint;
|
|
|
|
using NinjaTrader.NinjaScript.DrawingTools;
|
|
|
|
#endregion
|
|
|
|
|
2023-02-14 22:26:37 +00:00
|
|
|
public enum TimeFrame
|
|
|
|
{
|
|
|
|
Seconds,
|
|
|
|
Minutes,
|
|
|
|
Hours
|
|
|
|
}
|
|
|
|
|
2023-02-10 16:39:29 +00:00
|
|
|
//This namespace holds Indicators in this folder and is required. Do not change it.
|
|
|
|
namespace NinjaTrader.NinjaScript.Indicators
|
|
|
|
{
|
2023-02-10 16:42:09 +00:00
|
|
|
public class OpeningRange : Indicator
|
|
|
|
{
|
2023-03-01 16:19:02 +00:00
|
|
|
private static int MINUTES_IN_HOUR = 60;
|
|
|
|
private static string REGULAR_TRADING_HOURS = "US Equities RTH";
|
|
|
|
|
2023-02-14 22:26:37 +00:00
|
|
|
private double OpeningRangeHigh;
|
|
|
|
private double OpeningRangeLow;
|
2023-03-01 16:19:02 +00:00
|
|
|
private double OpeningRangeMid;
|
|
|
|
|
|
|
|
private DateTime OpeningRangeDate;
|
|
|
|
private DateTime OpeningRangeStartTime;
|
|
|
|
|
|
|
|
private SessionIterator Session;
|
2023-02-10 16:42:09 +00:00
|
|
|
|
|
|
|
protected override void OnStateChange()
|
|
|
|
{
|
|
|
|
if (State == State.SetDefaults)
|
|
|
|
{
|
2023-02-10 16:44:14 +00:00
|
|
|
Description = @"Plots the user-defined opening range";
|
|
|
|
Name = "Opening Range";
|
2023-02-14 22:26:37 +00:00
|
|
|
Calculate = Calculate.OnPriceChange;
|
2023-02-10 16:44:14 +00:00
|
|
|
IsOverlay = true;
|
|
|
|
DisplayInDataBox = true;
|
|
|
|
DrawOnPricePanel = true;
|
|
|
|
DrawHorizontalGridLines = true;
|
|
|
|
DrawVerticalGridLines = true;
|
|
|
|
PaintPriceMarkers = true;
|
|
|
|
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
|
2023-02-10 16:42:09 +00:00
|
|
|
//Disable this property if your indicator requires custom values that cumulate with each new market data event.
|
|
|
|
//See Help Guide for additional information.
|
2023-02-10 16:44:14 +00:00
|
|
|
IsSuspendedWhileInactive = true;
|
2023-03-01 16:19:02 +00:00
|
|
|
ArePlotsConfigurable = false;
|
2023-02-14 22:26:37 +00:00
|
|
|
OpeningRangeLength = 30;
|
|
|
|
OpeningRangeTimeFrame = TimeFrame.Minutes;
|
2023-02-10 16:42:09 +00:00
|
|
|
OpeningRangeHighStroke = new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3);
|
|
|
|
OpeningRangeLowStroke = new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3);
|
2023-03-01 16:19:02 +00:00
|
|
|
OpeningRangeMidStroke = new Stroke(Brushes.Gray, DashStyleHelper.Dash, 3);
|
2023-02-14 22:26:37 +00:00
|
|
|
AddPlot(Brushes.Transparent, "ORH");
|
|
|
|
AddPlot(Brushes.Transparent, "ORL");
|
2023-03-01 16:19:02 +00:00
|
|
|
AddPlot(Brushes.Transparent, "ORM");
|
2023-02-14 22:26:37 +00:00
|
|
|
}
|
|
|
|
else if (State == State.Configure)
|
|
|
|
{
|
|
|
|
OpeningRangeHigh = 0.0;
|
|
|
|
OpeningRangeLow = 0.0;
|
2023-03-01 16:19:02 +00:00
|
|
|
OpeningRangeMid = 0.0;
|
|
|
|
OpeningRangeDate = DateTime.MinValue;
|
|
|
|
OpeningRangeStartTime = DateTime.MinValue;
|
|
|
|
|
2023-02-14 22:26:37 +00:00
|
|
|
if (OpeningRangeTimeFrame == TimeFrame.Seconds)
|
|
|
|
{
|
2023-03-01 16:19:02 +00:00
|
|
|
AddDataSeries(Instrument.FullName, new BarsPeriod {
|
|
|
|
BarsPeriodType = BarsPeriodType.Second, Value = OpeningRangeLength }, REGULAR_TRADING_HOURS);
|
2023-02-14 22:26:37 +00:00
|
|
|
}
|
|
|
|
else if (OpeningRangeTimeFrame == TimeFrame.Hours)
|
|
|
|
{
|
2023-03-01 16:19:02 +00:00
|
|
|
AddDataSeries(Instrument.FullName, new BarsPeriod {
|
|
|
|
BarsPeriodType = BarsPeriodType.Minute, Value = OpeningRangeLength * MINUTES_IN_HOUR }, REGULAR_TRADING_HOURS);
|
2023-02-14 22:26:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-01 16:19:02 +00:00
|
|
|
AddDataSeries(Instrument.FullName, new BarsPeriod {
|
|
|
|
BarsPeriodType = BarsPeriodType.Minute, Value = OpeningRangeLength }, REGULAR_TRADING_HOURS);
|
2023-02-14 22:26:37 +00:00
|
|
|
}
|
2023-02-10 16:42:09 +00:00
|
|
|
}
|
2023-03-01 16:19:02 +00:00
|
|
|
else if (State == State.DataLoaded)
|
|
|
|
{
|
|
|
|
Session = new SessionIterator(BarsArray[1]);
|
|
|
|
}
|
2023-02-10 16:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnBarUpdate()
|
|
|
|
{
|
2023-03-01 16:19:02 +00:00
|
|
|
DateTime now = Times[BarsInProgress][0];
|
|
|
|
DateTime marketOpen = now.Date + Session.GetTradingDayBeginLocal(Session.ActualTradingDayExchange).TimeOfDay;
|
|
|
|
DateTime marketClose = now.Date + Session.GetTradingDayEndLocal(Session.ActualTradingDayExchange).TimeOfDay;
|
2023-02-14 22:26:37 +00:00
|
|
|
|
|
|
|
if (BarsInProgress == 0)
|
|
|
|
{
|
|
|
|
ORH[0] = OpeningRangeHigh;
|
|
|
|
ORL[0] = OpeningRangeLow;
|
2023-03-01 16:19:02 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-03-01 23:15:54 +00:00
|
|
|
if (now >= marketClose)
|
|
|
|
{
|
|
|
|
OpeningRangeHigh = 0.0;
|
|
|
|
OpeningRangeLow = 0.0;
|
|
|
|
OpeningRangeMid = 0.0;
|
|
|
|
}
|
|
|
|
|
2023-02-14 22:26:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-02-10 16:39:29 +00:00
|
|
|
|
2023-03-01 16:19:02 +00:00
|
|
|
if (Bars.IsFirstBarOfSession)
|
2023-02-14 22:26:37 +00:00
|
|
|
{
|
2023-03-01 16:19:02 +00:00
|
|
|
if (Highs[BarsInProgress][0] > OpeningRangeHigh || OpeningRangeHigh == 0.0)
|
2023-02-10 16:39:29 +00:00
|
|
|
{
|
2023-02-10 16:42:09 +00:00
|
|
|
RemoveDrawObject("ORH@" + OpeningRangeHigh);
|
2023-03-01 16:19:02 +00:00
|
|
|
RemoveDrawObject("ORM@" + OpeningRangeMid);
|
|
|
|
OpeningRangeHigh = Highs[BarsInProgress][0];
|
2023-02-10 16:42:09 +00:00
|
|
|
}
|
2023-02-10 16:39:29 +00:00
|
|
|
|
2023-03-01 16:19:02 +00:00
|
|
|
if (Lows[BarsInProgress][0] < OpeningRangeLow || OpeningRangeLow == 0.0)
|
2023-02-10 16:39:29 +00:00
|
|
|
{
|
2023-02-10 16:42:09 +00:00
|
|
|
RemoveDrawObject("ORL@" + OpeningRangeLow);
|
2023-03-01 16:19:02 +00:00
|
|
|
RemoveDrawObject("ORM@" + OpeningRangeMid);
|
|
|
|
OpeningRangeLow = Lows[BarsInProgress][0];
|
2023-02-10 16:42:09 +00:00
|
|
|
}
|
2023-02-10 16:39:29 +00:00
|
|
|
}
|
2023-02-10 16:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
2023-02-14 22:26:37 +00:00
|
|
|
[Range(1, int.MaxValue)]
|
2023-03-01 16:19:02 +00:00
|
|
|
[Display(Name = "Length", Description = "Length of opening range", Order = 1, GroupName = "Parameters")]
|
2023-02-14 22:26:37 +00:00
|
|
|
public int OpeningRangeLength
|
2023-02-10 16:42:09 +00:00
|
|
|
{ get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
2023-03-01 16:19:02 +00:00
|
|
|
[Display(Name = "Time Frame", Description = "Time frame on which opening range is being calculated", Order = 2, GroupName = "Parameters")]
|
2023-02-14 22:26:37 +00:00
|
|
|
public TimeFrame OpeningRangeTimeFrame
|
2023-02-10 16:42:09 +00:00
|
|
|
{ get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
2023-02-14 22:26:37 +00:00
|
|
|
[Display(Name = "Opening Range High", Description = "Opening range high line drawn on chart", Order = 3, GroupName = "Parameters")]
|
2023-02-10 16:42:09 +00:00
|
|
|
public Stroke OpeningRangeHighStroke
|
|
|
|
{ get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
2023-02-14 22:26:37 +00:00
|
|
|
[Display(Name = "Opening Range Low", Description = "Opening range low line drawn on chart", Order = 4, GroupName = "Parameters")]
|
2023-02-10 16:42:09 +00:00
|
|
|
public Stroke OpeningRangeLowStroke
|
|
|
|
{ get; set; }
|
2023-02-14 22:26:37 +00:00
|
|
|
|
2023-03-01 16:19:02 +00:00
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Display(Name = "Opening Range Mid", Description = "Opening range mid line drawn on chart", Order = 5, GroupName = "Parameters")]
|
|
|
|
public Stroke OpeningRangeMidStroke
|
|
|
|
{ get; set; }
|
|
|
|
|
2023-02-14 22:26:37 +00:00
|
|
|
[Browsable(false)]
|
|
|
|
[XmlIgnore]
|
|
|
|
public Series<double> ORH
|
|
|
|
{
|
|
|
|
get { return Values[0]; }
|
|
|
|
}
|
|
|
|
|
|
|
|
[Browsable(false)]
|
|
|
|
[XmlIgnore]
|
|
|
|
public Series<double> ORL
|
|
|
|
{
|
|
|
|
get { return Values[1]; }
|
|
|
|
}
|
2023-03-01 16:19:02 +00:00
|
|
|
|
|
|
|
[Browsable(false)]
|
|
|
|
[XmlIgnore]
|
|
|
|
public Series<double> ORM
|
|
|
|
{
|
|
|
|
get { return Values[2]; }
|
|
|
|
}
|
2023-02-10 16:42:09 +00:00
|
|
|
}
|
2023-02-10 16:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#region NinjaScript generated code. Neither change nor remove.
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.Indicators
|
|
|
|
{
|
2023-02-10 16:42:09 +00:00
|
|
|
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
|
|
|
|
{
|
|
|
|
private OpeningRange[] cacheOpeningRange;
|
2023-03-01 16:19:02 +00:00
|
|
|
public OpeningRange OpeningRange(int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke, Stroke openingRangeMidStroke)
|
2023-02-10 16:42:09 +00:00
|
|
|
{
|
2023-03-01 16:19:02 +00:00
|
|
|
return OpeningRange(Input, openingRangeLength, openingRangeTimeFrame, openingRangeHighStroke, openingRangeLowStroke, openingRangeMidStroke);
|
2023-02-10 16:42:09 +00:00
|
|
|
}
|
|
|
|
|
2023-03-01 16:19:02 +00:00
|
|
|
public OpeningRange OpeningRange(ISeries<double> input, int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke, Stroke openingRangeMidStroke)
|
2023-02-10 16:42:09 +00:00
|
|
|
{
|
|
|
|
if (cacheOpeningRange != null)
|
|
|
|
for (int idx = 0; idx < cacheOpeningRange.Length; idx++)
|
2023-03-01 16:19:02 +00:00
|
|
|
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))
|
2023-02-10 16:42:09 +00:00
|
|
|
return cacheOpeningRange[idx];
|
2023-03-01 16:19:02 +00:00
|
|
|
return CacheIndicator<OpeningRange>(new OpeningRange(){ OpeningRangeLength = openingRangeLength, OpeningRangeTimeFrame = openingRangeTimeFrame, OpeningRangeHighStroke = openingRangeHighStroke, OpeningRangeLowStroke = openingRangeLowStroke, OpeningRangeMidStroke = openingRangeMidStroke }, input, ref cacheOpeningRange);
|
2023-02-10 16:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-10 16:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
|
|
|
|
{
|
2023-02-10 16:42:09 +00:00
|
|
|
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
|
|
|
|
{
|
2023-03-01 16:19:02 +00:00
|
|
|
public Indicators.OpeningRange OpeningRange(int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke, Stroke openingRangeMidStroke)
|
2023-02-10 16:42:09 +00:00
|
|
|
{
|
2023-03-01 16:19:02 +00:00
|
|
|
return indicator.OpeningRange(Input, openingRangeLength, openingRangeTimeFrame, openingRangeHighStroke, openingRangeLowStroke, openingRangeMidStroke);
|
2023-02-10 16:42:09 +00:00
|
|
|
}
|
|
|
|
|
2023-03-01 16:19:02 +00:00
|
|
|
public Indicators.OpeningRange OpeningRange(ISeries<double> input , int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke, Stroke openingRangeMidStroke)
|
2023-02-10 16:42:09 +00:00
|
|
|
{
|
2023-03-01 16:19:02 +00:00
|
|
|
return indicator.OpeningRange(input, openingRangeLength, openingRangeTimeFrame, openingRangeHighStroke, openingRangeLowStroke, openingRangeMidStroke);
|
2023-02-10 16:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-10 16:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.Strategies
|
|
|
|
{
|
2023-02-10 16:42:09 +00:00
|
|
|
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
|
|
|
|
{
|
2023-03-01 16:19:02 +00:00
|
|
|
public Indicators.OpeningRange OpeningRange(int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke, Stroke openingRangeMidStroke)
|
2023-02-10 16:42:09 +00:00
|
|
|
{
|
2023-03-01 16:19:02 +00:00
|
|
|
return indicator.OpeningRange(Input, openingRangeLength, openingRangeTimeFrame, openingRangeHighStroke, openingRangeLowStroke, openingRangeMidStroke);
|
2023-02-10 16:42:09 +00:00
|
|
|
}
|
|
|
|
|
2023-03-01 16:19:02 +00:00
|
|
|
public Indicators.OpeningRange OpeningRange(ISeries<double> input , int openingRangeLength, TimeFrame openingRangeTimeFrame, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke, Stroke openingRangeMidStroke)
|
2023-02-10 16:42:09 +00:00
|
|
|
{
|
2023-03-01 16:19:02 +00:00
|
|
|
return indicator.OpeningRange(input, openingRangeLength, openingRangeTimeFrame, openingRangeHighStroke, openingRangeLowStroke, openingRangeMidStroke);
|
2023-02-10 16:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-10 16:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|