Indicators

How to Convert a Pine Script Indicator to NinjaTrader 8 (NinjaScript)

· 9 min read

You have an indicator that works on TradingView and you want it in NinjaTrader 8. There is no reliable automatic converter: you have to rewrite it in NinjaScript (C#). The good news is that the logic translates almost line by line once you know the equivalents.

What changes between Pine Script and NinjaScript

Equivalence table

Pine ScriptNinjaScript (NinjaTrader 8)
close, close[1]Close[0], Close[1]
open, high, low, volumeOpen[0], High[0], Low[0], Volume[0]
bar_indexCurrentBar
ta.sma(close, 20)SMA(Close, 20)[0]
ta.ema(close, 20)EMA(Close, 20)[0]
ta.atr(14)ATR(14)[0]
ta.highest(high, 10) / ta.lowest(low, 10)MAX(High, 10)[0] / MIN(Low, 10)[0]
ta.crossover(a, b)CrossAbove(a, b, 1)
input.int(20, "Length")A property with [NinjaScriptProperty] and its default set in State.SetDefaults
plot(x)AddPlot(...) in SetDefaults and Values[0][0] = x; in OnBarUpdate
var float level = naA class field: private double level = double.NaN;
request.security(..., "60", close)AddDataSeries(BarsPeriodType.Minute, 60) in State.Configure and Closes[1][0]
bgcolor() / barcolor()BackBrush / BarBrush
line.new(), label.new()Draw.Line(...), Draw.Text(...)
alertcondition()Alert(...)
strategy.entry()A Strategy class with EnterLong() / EnterShort()
Some NinjaTrader indicators don't calculate exactly like TradingView's. For example, NinjaTrader's RSI has an extra smoothing parameter. Always compare a few values on both platforms.

Example: a 9/21 EMA crossover

The indicator in Pine Script:

//@version=6
indicator("EMA Cross", overlay = true)
fast = input.int(9, "Fast")
slow = input.int(21, "Slow")
f = ta.ema(close, fast)
s = ta.ema(close, slow)
plot(f, color = color.orange)
plot(s, color = color.blue)
plotshape(ta.crossover(f, s), style = shape.triangleup, location = location.belowbar)

The same indicator in NinjaScript. Create it from New → NinjaScript Editor → Indicators → New Indicator, which already generates the using statements, and replace the class body:

public class EmaCross : Indicator
{
    protected override void OnStateChange()
    {
        if (State == State.SetDefaults)
        {
            Name      = "EmaCross";
            IsOverlay = true;
            Calculate = Calculate.OnBarClose;
            Fast      = 9;
            Slow      = 21;
            AddPlot(Brushes.Orange, "Fast");
            AddPlot(Brushes.DodgerBlue, "Slow");
        }
    }

    protected override void OnBarUpdate()
    {
        if (CurrentBar < Slow) return;
        Values[0][0] = EMA(Fast)[0];
        Values[1][0] = EMA(Slow)[0];
        if (CrossAbove(EMA(Fast), EMA(Slow), 1))
            Draw.TriangleUp(this, "x" + CurrentBar, false, 0, Low[0] - 4 * TickSize, Brushes.LimeGreen);
    }

    [NinjaScriptProperty, Range(1, int.MaxValue)]
    public int Fast { get; set; }

    [NinjaScriptProperty, Range(1, int.MaxValue)]
    public int Slow { get; set; }
}

Compile with F5 in the editor and add it to a chart from the indicator list.

Common conversion errors

When you don't need to convert anything

If all you want is to see your Pine indicator on your prop firm data, you don't need to rewrite it. TQ Charts reads the data from your own NinjaTrader and lets you paste your Pine code as is (Pro and Elite plans), with its inputs and styles. We explain it in this guide. Converting to NinjaScript is worth it when you need the indicator inside NinjaTrader, for example for an automated strategy.

Keep reading