NinjaTrader 8 · NinjaScript / C#

    Create a NinjaTrader strategy from trading rules

    Turn explicit trading rules into NinjaScript for NinjaTrader 8. Use TradeScriptAI to draft a strategy or indicator, review validation results, and revise the source before testing it in NinjaTrader.

    Before you begin

    Use a supported NinjaTrader 8 desktop installation with a simulation account and suitable historical data. TradeScriptAI generates source code; it does not connect or operate your brokerage account.

    An example prompt

    Create a NinjaTrader 8 strategy named EmaCrossExample. Calculate on bar close. After at least 21 bars, enter long with one contract only when the 9-period EMA crosses above the 21-period EMA and the position is flat. Exit on the opposite cross. Use a 20-tick stop and a 40-tick target attached to the entry. No short entries or pyramiding. Make the EMA lengths and risk distances configurable.

    This is an educational specification, not a tested trading system or a performance claim.

    Step-by-step workflow

    1. 1. Write complete entry and exit rules

      Specify the instrument, bar interval, session, entry condition, position size, and exits. State whether calculations happen on bar close or intrabar. This example uses a long-only EMA crossover with fixed tick distances; the numbers are illustrative, not a trading recommendation.

    2. 2. Generate a NinjaTrader 8 strategy

      Choose NinjaScript in TradeScriptAI and use the prompt below. Review the class name, bar-count guard, flat-position check, and attached stop and target. Keep tick units distinct from price points. Read the validation results before exporting.

    3. 3. Compile the complete source in NinjaTrader

      Open the NinjaScript Editor in NinjaTrader 8. Create a strategy named EmaCrossExample, open its code, and replace the template with the complete generated source. Compile. A raw C# file is not an importable NinjaScript ZIP package.

    4. 4. Inspect a historical test

      Open Strategy Analyzer and select the strategy, instrument, interval, date range, and session template. Include commissions and appropriate slippage assumptions. Check individual trades against the chart, including entry timing, exit reasons, and stop distances.

    5. 5. Run in simulation before considering live use

      Use a simulation account and confirm order quantities, order cancellation, reconnect behavior, and session boundaries. Compare observed behavior with the written rules. Revise one issue at a time and rerun the same test.

    Check the result against your rules

    • The strategy waits until its indicators have enough bars.
    • An existing position prevents duplicate entries.
    • Stops and targets use ticks, as specified in the prompt.
    • Opposite crosses close the long position without opening a short.

    When something does not work

    NinjaTrader compiles all included custom scripts, so an error can originate in a different file. Read the filename and line number before changing this strategy. For unexpected orders, include the timestamp, instrument, session template, and strategy settings in your repair request.

    What validation tells you

    The NinjaScript workflow includes C# compilation and a bar-replay harness. Those checks are not a substitute for compiling against your installed NinjaTrader version, dependencies, and data. Replay is a code-behavior check, not a broker execution guarantee.

    NinjaTrader 8 official help guide

    Build with TradeScriptAI

    Basic costs US$19/month and includes 50 code generations per month on one selected platform. Starter costs US$49/month and includes unlimited code generation across all three supported platforms. There is no free generation tier.

    Choose your trading platform