I've been trying to draw the weekly OHLC levels. I am currently trying just to get the weekly Open to plot a horizontal line from the Open and if the price breaks/touches that level during the week it will stop plotting into the future. Every time I change something to fix the error it loops back to another mistake.
If anyone can help with a possible idea to rectify this I would be greatly appreciative. Thank you for your time.
James
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 PreviousWeekOpenTouch : Indicator
{
private double previousWeekOpen;
private List<double> touchedPrices = new List<double>();
private bool historicalLevelsShown = false;
private List<DrawnLine> horizontalLines = new List<DrawnLine>();
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = "Plot horizontal line at previous week open and stop when touched.";
Name = "Previous Week Open Touch";
Calculate = Calculate.OnBarClose;
IsOverlay = true;
DisplayInDataBox = true;
DrawOnPricePanel = true;
}
else if (State == State.Configure)
{
// Set the secondary data series to the weekly bar interval
AddDataSeries(Data.BarsPeriodType.Week, 1);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress == 1)
{
// Calculate the weekly open value on the secondary data series (weekly bars)
if (CurrentBar > 0 && Times[1][0].Date.DayOfWeek < Times[1][1].Date.DayOfWeek)
previousWeekOpen = Open[1];
}
else if (BarsInProgress == 0)
{
// Check if it's the start of a new week on the primary chart
if (CurrentBar > 0 && Times[0][0].Date.DayOfWeek < Times[0][1].Date.DayOfWeek)
{
// Add touched level to the historical list (if any)
if (!double.IsNaN(previousWeekOpen) && Close[1] <= previousWeekOpen)
{
touchedPrices.Add(previousWeekOpen);
}
}
// Draw the line at the previous week's open
DrawHorizontalLine("PreviousWeekOpen", previousWeekOpen, Brushes.Blue);
// Check if the line has been touched by the close of the next week's bar
if (CurrentBar > 1 && Close[1] <= previousWeekOpen)
{
if (!historicalLevelsShown)
{
// Add touched level to the historical list (if any)
touchedPrices.Add(previousWeekOpen);
}
historicalLevelsShown = true;
}
else
{
historicalLevelsShown = false;
}
// Plot historical touched levels
for (int i = 0; i < touchedPrices.Count; i++)
{
DrawHorizontalLine("TouchedPriceLine" + i, touchedPrices[i], Brushes.Red, DashStyles.Dot);
}
}
}
private void DrawHorizontalLine(string tag, double y, System.Windows.Media.Brush brush, System.Windows.Media.DashStyle dashStyle = null)
{
string tagUnique = tag + CurrentBar;
// Remove previous line
DrawnLine previousLine = horizontalLines.Find(line => line.Tag == tagUnique);
if (previousLine != null)
{
Draw.RemoveObject(this, previousLine.Tag);
horizontalLines.Remove(previousLine);
}
Draw.Line(this, tagUnique, false, 0, y, 1, y, brush, dashStyle ?? DashStyles.Solid);
horizontalLines.Add(new DrawnLine(tagUnique));
}
private class DrawnLine
{
public string Tag { get; }
public DrawnLine(string tag)
{
Tag = tag;
}
}
}
}
region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private PreviousWeekOpenTouch[] cachePreviousWeekOpenTouch;
public PreviousWeekOpenTouch PreviousWeekOpenTouch()
{
return PreviousWeekOpenTouch(Input);
}
public PreviousWeekOpenTouch PreviousWeekOpenTouch(ISeries<double> input)
{
if (cachePreviousWeekOpenTouch != null)
for (int idx = 0; idx < cachePreviousWeekOpenTouch.Length; idx++)
if (cachePreviousWeekOpenTouch[idx] != null && cachePreviousWeekOpenTouch[idx].EqualsInput(input))
return cachePreviousWeekOpenTouch[idx];
return CacheIndicator<PreviousWeekOpenTouch>(new PreviousWeekOpenTouch(), input, ref cachePreviousWeekOpenTouch);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.PreviousWeekOpenTouch PreviousWeekOpenTouch()
{
return indicator.PreviousWeekOpenTouch(Input);
}
public Indicators.PreviousWeekOpenTouch PreviousWeekOpenTouch(ISeries<double> input )
{
return indicator.PreviousWeekOpenTouch(input);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.PreviousWeekOpenTouch PreviousWeekOpenTouch()
{
return indicator.PreviousWeekOpenTouch(Input);
}
public Indicators.PreviousWeekOpenTouch PreviousWeekOpenTouch(ISeries<double> input )
{
return indicator.PreviousWeekOpenTouch(input);
}
}
}
#endregion
Comment