NinjaScript compiler troubleshooting

Fix CS1056 Unexpected Character '$' in NinjaScript

CS1056 identifies an unexpected character. If it points to the dollar prefix of an interpolated string, inspect the literal syntax and the actual compiler language version before changing the code.

SafeStringFormatIndicator.cs
Compatible Indicator Example
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.NinjaScript;

namespace NinjaTrader.NinjaScript.Indicators
{
    public class SafeStringFormatIndicator : Indicator
    {
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Name = "SafeStringFormatIndicator";
                Description = "Demonstrates safe string formatting in NinjaScript without CS1056.";
                Calculate = Calculate.OnBarClose;
                IsOverlay = true;
                Period = 14;
                AddPlot(Brushes.DodgerBlue, "Average");
            }
        }

        protected override void OnBarUpdate()
        {
            if (CurrentBar < Period - 1)
                return;

            Value[0] = SMA(Period)[0];

            // Avoid string interpolation that causes CS1056 in C# 5 compiler contexts:
            // Print($"Bar: {CurrentBar}, Close: {Close[0]}, SMA: {Value[0]}");

            // Solution 1: Use string.Format
            Print(string.Format("Bar: {0}, Close: {1:F2}, SMA: {2:F2}", CurrentBar, Close[0], Value[0]));

            // Solution 2: Use string concatenation
            // Print("Bar: " + CurrentBar + ", Close: " + Close[0].ToString("F2"));
        }

        [NinjaScriptProperty]
        [Range(1, 200)]
        [Display(Name = "Period", GroupName = "Parameters", Order = 0)]
        public int Period { get; set; }

        [Browsable(false)]
        [XmlIgnore]
        public Series<double> Average { get { return Values[0]; } }
    }
}

The example uses string.Format and an explicit property getter to avoid those C# 6 constructs. Compile it in the target NinjaScript Editor; compatibility with every installation is not established.

Direct answer

An interpolated C# string uses $"...", not single quotes. It requires C# 6 or later. In an older compilation context, replace a simple interpolation with string.Format or concatenation while preserving values, format specifiers, and culture. A dollar sign in an invalid location can cause CS1056 for other reasons; the error alone does not identify every NinjaTrader installation as C# 5.

What to know first

  • Read the precise character and surrounding syntax reported by CS1056.
  • String interpolation was introduced in C# 6; check the compiler configuration actually producing the error.
  • The .NET runtime version alone does not identify the C# language version.
  • A formatting replacement removes one syntax dependency, not every possible compilation error.
  • Pineify provides external static diagnostics; native NinjaScript Editor compilation remains a separate check.

Locate the unexpected character

Check quotes, escaping, and the position of the dollar sign before diagnosing a language-version mismatch. A valid interpolated string begins with a dollar sign and double quote. If that syntax is rejected by an older compiler, a formatting replacement can remove the unsupported construct.

Record the NinjaTrader build and the compiler or external editor that produced the diagnostic. A framework version does not by itself prove which C# features the compiler accepts.

Preserve formatting while changing syntax

For a simple debug message, replace an interpolation with string.Format using correctly ordered placeholders. Preserve alignment, numeric formats, escaped braces, null handling, and the intended culture. Do not assume every interpolation can be replaced mechanically with identical evaluation behavior.

  • Interpolation: $"Bar {CurrentBar}: {Close[0]:F2}"
  • Composite formatting: string.Format("Bar {0}: {1:F2}", CurrentBar, Close[0])

Check other unsupported constructs only when relevant

Expression-bodied properties, null-conditional operators, and nameof also arrived with C# 6. If the actual target requires older syntax, review those constructs too. The example uses an explicit getter to remain consistent with this older-syntax demonstration.

Modern compilers can lower interpolated strings in different ways. Do not assume identical allocations or performance from the .NET version alone; avoid unnecessary formatting in a hot path and measure if it matters.

Separate static diagnostics from platform compilation

Pineify can assist source revision and check a single Indicator or Strategy against its pinned static reference profile. That profile is not your installed NinjaTrader environment.

Use F5 in NinjaScript Editor, inspect its compile-error display, and repair remaining diagnostics. The NinjaScript Output window is for runtime Print output; an empty output window is not proof of successful compilation.

Practical checks

Inspect the exact literal

Check the flagged character, quotes, and language version before replacing syntax.

Preserve formatting

Keep placeholder order, units, numeric formats, and culture consistent when changing the message.

Read compiler diagnostics

Compile in NinjaScript Editor and inspect its error display; use the Output window separately for Print observations.

Primary sources

This guide is for educational and software development purposes. It does not constitute investment advice. Test all code in NinjaScript Editor and simulation before using it in live trading.

Frequently asked questions

Fix Your NinjaScript Code with AI

Paste your NinjaScript indicator or strategy, resolve compiler errors like CS1056, and generate clean C# code for NinjaTrader 8.

Open NinjaTrader Coding Agent