"I am trying to create an indicator that connects the highest and lowest prices for each candle with lines, either 20 or 50 of them. It's called Price Channel in Thinkorswim. I don't have much coding experience, and I tried to create it, but I keep encountering errors. Is there anyone who can help me?"
-----------------------
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.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 PriceChannel : Indicator
{
private double[] priorHigh20;
private double[] priorLow20;
private double[] priorHigh50;
private double[] priorLow50;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = "Price Channel based on each bar's High and Low (20, 50)";
Name = "PriceChannel";
IsOverlay = true;
IsSuspendedWhileInactive = true;
AddPlot(Brushes.Orange, "Upper20");
AddPlot(Brushes.Orange, "Lower20");
AddPlot(Brushes.Blue, "Upper50");
AddPlot(Brushes.Blue, "Lower50");
}
else if (State == State.DataLoaded)
{
priorHigh20 = new double[20];
priorLow20 = new double[20];
priorHigh50 = new double[50];
priorLow50 = new double[50];
}
}
protected override void OnBarUpdate()
{
UpdatePriorValues(High, Low, ref priorHigh20, ref priorLow20, 20);
UpdatePriorValues(High, Low, ref priorHigh50, ref priorLow50, 50);
Upper20[0] = priorHigh20[0];
Lower20[0] = priorLow20[0];
Upper50[0] = priorHigh50[0];
Lower50[0] = priorLow50[0];
}
private void UpdatePriorValues(Series<double> highs, Series<double> lows, ref double[] priorHigh, ref double[] priorLow, int period)
{
double currentHigh = highs[0];
double currentLow = lows[0];
for (int i = 0; i < period - 1; i++)
{
priorHigh[i] = priorHigh[i + 1];
priorLow[i] = priorLow[i + 1];
}
priorHigh[period - 1] = currentHigh;
priorLow[period - 1] = currentLow;
}
region Properties
[Browsable(false)]
[XmlIgnore()]
public Series<double> Upper20
{
get { return Values[0]; }
}
[Browsable(false)]
[XmlIgnore()]
public Series<double> Lower20
{
get { return Values[1]; }
}
[Browsable(false)]
[XmlIgnore()]
public Series<double> Upper50
{
get { return Values[2]; }
}
[Browsable(false)]
[XmlIgnore()]
public Series<double> Lower50
{
get { return Values[3]; }
}
#endregion
}
}
Comment