ninjatrader/indicators/DailyOpenLevels.cs

144 lines
5.5 KiB
C#
Raw Normal View History

2023-02-15 17:52:31 +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
{
public class DailyOpenLevels : Indicator
{
private static int DAILY_BARS = 1;
private HashSet<double> DailyOpens = new HashSet<double>();
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Plots the open of daily candles.";
Name = "Daily COL";
Calculate = Calculate.OnPriceChange;
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;
LookbackPeriod = 3;
DailyOpenStroke = new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3);
}
else if (State == State.Configure)
{
AddDataSeries(Instrument.FullName, new BarsPeriod { BarsPeriodType = BarsPeriodType.Day, Value = 1 }, LookbackPeriod, Bars.TradingHours.Name, Bars.IsResetOnNewTradingDay);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress == DAILY_BARS)
{
DailyOpens.Add(Opens[DAILY_BARS][0]);
}
else
{
foreach (double dailyOpen in DailyOpens)
{
if (DrawObjects["Open@" + dailyOpen] == null)
Draw.HorizontalLine(this, "Open@" + dailyOpen, dailyOpen, DailyOpenStroke.Brush, DailyOpenStroke.DashStyleHelper, (int)DailyOpenStroke.Width);
}
}
}
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Lookback Period", Description = "Number of daily opens to consider", Order = 1, GroupName = "Parameters")]
public int LookbackPeriod
{ get; set; }
[NinjaScriptProperty]
[Display(Name = "Daily Open", Description = "Daily open level drawn on the chart", Order = 2, GroupName = "Parameters")]
public Stroke DailyOpenStroke
{ get; set; }
}
}
#region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private DailyOpenLevels[] cacheDailyOpenLevels;
public DailyOpenLevels DailyOpenLevels(int lookbackPeriod, Stroke dailyOpenStroke)
{
return DailyOpenLevels(Input, lookbackPeriod, dailyOpenStroke);
}
public DailyOpenLevels DailyOpenLevels(ISeries<double> input, int lookbackPeriod, Stroke dailyOpenStroke)
{
if (cacheDailyOpenLevels != null)
for (int idx = 0; idx < cacheDailyOpenLevels.Length; idx++)
if (cacheDailyOpenLevels[idx] != null && cacheDailyOpenLevels[idx].LookbackPeriod == lookbackPeriod && cacheDailyOpenLevels[idx].DailyOpenStroke == dailyOpenStroke && cacheDailyOpenLevels[idx].EqualsInput(input))
return cacheDailyOpenLevels[idx];
return CacheIndicator<DailyOpenLevels>(new DailyOpenLevels(){ LookbackPeriod = lookbackPeriod, DailyOpenStroke = dailyOpenStroke }, input, ref cacheDailyOpenLevels);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.DailyOpenLevels DailyOpenLevels(int lookbackPeriod, Stroke dailyOpenStroke)
{
return indicator.DailyOpenLevels(Input, lookbackPeriod, dailyOpenStroke);
}
public Indicators.DailyOpenLevels DailyOpenLevels(ISeries<double> input , int lookbackPeriod, Stroke dailyOpenStroke)
{
return indicator.DailyOpenLevels(input, lookbackPeriod, dailyOpenStroke);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.DailyOpenLevels DailyOpenLevels(int lookbackPeriod, Stroke dailyOpenStroke)
{
return indicator.DailyOpenLevels(Input, lookbackPeriod, dailyOpenStroke);
}
public Indicators.DailyOpenLevels DailyOpenLevels(ISeries<double> input , int lookbackPeriod, Stroke dailyOpenStroke)
{
return indicator.DailyOpenLevels(input, lookbackPeriod, dailyOpenStroke);
}
}
}
#endregion