ninjatrader/indicators/OpeningRange.cs

186 lines
9.0 KiB
C#

#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
{
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)
{
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;
//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);
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)
{
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;
}
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)
{
OpeningRangeHigh = 0.0;
OpeningRangeLow = 0.0;
OpeningRangeStartAdjusted = DateTime.MinValue;
}
}
[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; }
}
}
#region NinjaScript generated code. Neither change nor remove.
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)
{
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);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
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);
}
}
}
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)
{
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);
}
}
}
#endregion