I have created this indicator in Thinkorswim so now I am trying translate it to Ninjatrader but i cant seem to get it to compile in Ninjascript editor.
#region Using declarations
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Windows.Media;
using NinjaTrader.Data;
using NinjaTrader.Gui.Tools;
using NinjaTrader.NinjaScript;
using NinjaTrader.NinjaScript.Indicators; #endregion
namespace NinjaTrader.NinjaScript.Indicators
{
public class ATRBasedPivots : Indicator
{
private ATR atr;
private Series sessionOpen;
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(Name = "ATR Period", Order = 0, GroupName = "Parameters")]
public int ATRPeriod { get; set; }
[Range(0, int.MaxValue), NinjaScriptProperty]
[Display(Name = "ATR Bars Ago (0 = current, 1 = prior)", Order = 1, GroupName = "Parameters")]
public int ATRBarsAgo { get; set; }
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "ATRBasedPivots";
Description = "Plots ATR-based pivot levels centered on the session open";
IsOverlay = true;
IsChartOnly = true;
DisplayInDataBox = false;
IsSuspendedWhileInactive = true;
ATRPeriod = 14;
ATRBarsAgo = 1;
AddPlot(new Stroke(Brushes.Red, DashStyleHelper.Solid, 1), PlotStyle.HLine, "S2");
AddPlot(new Stroke(Brushes.Orange, DashStyleHelper.Solid, 1), PlotStyle.HLine, "S1");
AddPlot(new Stroke(Brushes.Green, DashStyleHelper.Solid, 1), PlotStyle.HLine, "R1");
AddPlot(new Stroke(Brushes.DarkGreen, DashStyleHelper.Solid, 1), PlotStyle.HLine, "R2");
}
else if (State == State.Configure)
{
AddDataSeries(Bars.Instrument.FullName, BarsPeriodType.Day, 1); // Daily series for ATR
}
else if (State == State.DataLoaded)
{
atr = ATR(BarsArray[1], ATRPeriod);
sessionOpen = new Series<double>(this);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (BarsArray[1].Count <= ATRBarsAgo + ATRPeriod || CurrentBar < 1)
return;
if (Bars.IsFirstBarOfSession)
sessionOpen[0] = Open[0];
double open = sessionOpen[0];
double atrValue = atr[ATRBarsAgo];
// Calculate pivot lines
double s2 = open - atrValue;
double s1 = open - atrValue / 2.0;
double r1 = open + atrValue / 2.0;
double r2 = open + atrValue;
// Set values for plotting
Values[0][0] = s2;
Values[1][0] = s1;
Values[2][0] = r1;
Values[3][0] = r2;
}
}
You’re missing the following at the top. This should resolve those compile errors. I didn’t read the rest of your code to see if there might be other issues with it. I’ll leave that to you and your AI friend
Haha. Thanks man. Ive been trying to figure it out all day but my AI friend gets it working but not to the exact way i want and the more I become specific the more errors it encounters. Hahah. I gave up. I will be manually creating this indicator with the FIbonnaci tool for now.
Generic LLMs fail at ThinkScript to NinjaScript conversions because they try to translate it line-by-line instead of respecting the NT8 event-driven architecture, the AI tried to force a heavy Series<double> object just to hold your session open price which is completely unnecessary and leads to state management errors. You just need a simple primitive variable cached at the start of the session. I cleaned up the data series synchronization and removed the bloated syntax so it actually runs efficiently without lagging your chart, overwrite your entire class with this block and hit compile
#region Using declarations
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Windows.Media;
using NinjaTrader.Data;
using NinjaTrader.Gui;
using NinjaTrader.NinjaScript;
using NinjaTrader.NinjaScript.Indicators; #endregion
namespace NinjaTrader.NinjaScript.Indicators
{
public class ATRBasedPivots : Indicator
{
private ATR atr;
private double currentSessionOpen;
Don’t know if you guys are already using it, but I’ve been using the “Ninjatrader coder” GPT on ChatGpt. I’ve had it write over a hundred strategies and indicators over the last 6 months. I did have quite a few errors at compile in the beginning, but the newer “extended thinking” does better, I think, I still get errors from time to time, but not as many. When I get errors at compile, I export the errors to a csv and upload it to the gpt, it seems to figure it the solution better. With strategies, I’ll them through a backtest, then export the Summary, Analysis, Trades, and Executions data to csvs and upload all of them to the same gpt that wrote the strategy. It runs the data through python and suggest changes to the script based on its review of the data. It always suggests improvements or additional functionality.
I personally found that AI is almost perfect at C-Sharp, so as long as you familiarise yourself with NinjaScript specific element, it very easy to work with AI because all it’s failures will be related to those.