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
|
|
|
|
|
|
|
|
//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
|
|
|
|
{
|
|
|
|
private double OpeningRangeHigh = 0.0;
|
|
|
|
private double OpeningRangeLow = 0.0;
|
|
|
|
|
|
|
|
private DateTime OpeningRangeStartAdjusted = DateTime.MinValue;
|
|
|
|
|
|
|
|
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";
|
|
|
|
Calculate = Calculate.OnBarClose;
|
|
|
|
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-02-10 16:42:09 +00:00
|
|
|
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);
|
|
|
|
OpeningRangeHighStroke = new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3);
|
|
|
|
OpeningRangeLowStroke = new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnBarUpdate()
|
|
|
|
{
|
|
|
|
DateTime now = Time[0];
|
|
|
|
if (now.TimeOfDay > OpeningRangeStart.TimeOfDay && now.TimeOfDay <= OpeningRangeEnd.TimeOfDay)
|
2023-02-10 16:39:29 +00:00
|
|
|
{
|
2023-02-10 16:42:09 +00:00
|
|
|
if (OpeningRangeStartAdjusted == DateTime.MinValue)
|
2023-02-10 16:39:29 +00:00
|
|
|
{
|
2023-02-10 16:42:09 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2023-02-10 16:39:29 +00:00
|
|
|
|
2023-02-10 16:42:09 +00:00
|
|
|
DateTime openingRangeStartTime = now.Date + OpeningRangeStartAdjusted.TimeOfDay;
|
|
|
|
DateTime openingRangeEndTime = now.Date + TradingEnd.TimeOfDay;
|
2023-02-10 16:39:29 +00:00
|
|
|
|
2023-02-10 16:42:09 +00:00
|
|
|
if (High[0] > OpeningRangeHigh || OpeningRangeHigh == 0.0)
|
2023-02-10 16:39:29 +00:00
|
|
|
{
|
2023-02-10 16:42:09 +00:00
|
|
|
RemoveDrawObject("ORH@" + OpeningRangeHigh);
|
|
|
|
OpeningRangeHigh = High[0];
|
|
|
|
Draw.Line(this, "ORH@" + OpeningRangeHigh, true, openingRangeStartTime, OpeningRangeHigh, openingRangeEndTime, OpeningRangeHigh,
|
|
|
|
OpeningRangeHighStroke.Brush, OpeningRangeHighStroke.DashStyleHelper, (int)OpeningRangeHighStroke.Width);
|
|
|
|
}
|
2023-02-10 16:39:29 +00:00
|
|
|
|
2023-02-10 16:42:09 +00:00
|
|
|
if (Low[0] < OpeningRangeLow || OpeningRangeLow == 0.0)
|
2023-02-10 16:39:29 +00:00
|
|
|
{
|
2023-02-10 16:42:09 +00:00
|
|
|
RemoveDrawObject("ORL@" + OpeningRangeLow);
|
|
|
|
OpeningRangeLow = Low[0];
|
|
|
|
Draw.Line(this, "ORL@" + OpeningRangeLow, true, openingRangeStartTime, OpeningRangeLow, openingRangeEndTime, OpeningRangeLow,
|
|
|
|
OpeningRangeLowStroke.Brush, OpeningRangeLowStroke.DashStyleHelper, (int)OpeningRangeLowStroke.Width);
|
|
|
|
}
|
2023-02-10 16:39:29 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 16:42:09 +00:00
|
|
|
if (now.TimeOfDay > TradingEnd.TimeOfDay)
|
2023-02-10 16:39:29 +00:00
|
|
|
{
|
2023-02-10 16:42:09 +00:00
|
|
|
OpeningRangeHigh = 0.0;
|
|
|
|
OpeningRangeLow = 0.0;
|
|
|
|
OpeningRangeStartAdjusted = DateTime.MinValue;
|
2023-02-10 16:39:29 +00:00
|
|
|
}
|
2023-02-10 16:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[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
|
|
|
|
{ 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
|
|
|
|
{ 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")]
|
|
|
|
public Stroke OpeningRangeHighStroke
|
|
|
|
{ get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Display(Name = "Opening Range Low", Description = "Opening range low line drawn on chart", Order = 5, GroupName = "Parameters")]
|
|
|
|
public Stroke OpeningRangeLowStroke
|
|
|
|
{ get; set; }
|
|
|
|
}
|
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;
|
|
|
|
public OpeningRange OpeningRange(DateTime openingRangeStart, DateTime openingRangeEnd, DateTime tradingEnd, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
|
|
|
|
{
|
|
|
|
return OpeningRange(Input, openingRangeStart, openingRangeEnd, tradingEnd, openingRangeHighStroke, openingRangeLowStroke);
|
|
|
|
}
|
|
|
|
|
|
|
|
public OpeningRange OpeningRange(ISeries<double> input, DateTime openingRangeStart, DateTime openingRangeEnd, DateTime tradingEnd, 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))
|
|
|
|
return cacheOpeningRange[idx];
|
|
|
|
return CacheIndicator<OpeningRange>(new OpeningRange(){ OpeningRangeStart = openingRangeStart, OpeningRangeEnd = openingRangeEnd, TradingEnd = tradingEnd, OpeningRangeHighStroke = openingRangeHighStroke, OpeningRangeLowStroke = openingRangeLowStroke }, input, ref cacheOpeningRange);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
public Indicators.OpeningRange OpeningRange(DateTime openingRangeStart, DateTime openingRangeEnd, DateTime tradingEnd, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
|
|
|
|
{
|
|
|
|
return indicator.OpeningRange(Input, openingRangeStart, openingRangeEnd, tradingEnd, openingRangeHighStroke, openingRangeLowStroke);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Indicators.OpeningRange OpeningRange(ISeries<double> input , DateTime openingRangeStart, DateTime openingRangeEnd, DateTime tradingEnd, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
|
|
|
|
{
|
|
|
|
return indicator.OpeningRange(input, openingRangeStart, openingRangeEnd, tradingEnd, openingRangeHighStroke, openingRangeLowStroke);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
public Indicators.OpeningRange OpeningRange(DateTime openingRangeStart, DateTime openingRangeEnd, DateTime tradingEnd, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
|
|
|
|
{
|
|
|
|
return indicator.OpeningRange(Input, openingRangeStart, openingRangeEnd, tradingEnd, openingRangeHighStroke, openingRangeLowStroke);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Indicators.OpeningRange OpeningRange(ISeries<double> input , DateTime openingRangeStart, DateTime openingRangeEnd, DateTime tradingEnd, Stroke openingRangeHighStroke, Stroke openingRangeLowStroke)
|
|
|
|
{
|
|
|
|
return indicator.OpeningRange(input, openingRangeStart, openingRangeEnd, tradingEnd, openingRangeHighStroke, openingRangeLowStroke);
|
|
|
|
}
|
|
|
|
}
|
2023-02-10 16:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|