Navigating NinjaTrader: Building Webhook Commands for Automated Trading

Learn the powerful command capabilities of CrossTrade + NinjaTrader. Whether you're new to XT or looking to refine your command skills, this post will guide you through using various commands to manage your trades more efficiently.

Navigating NinjaTrader: Building Webhook Commands for Automated Trading
TradingView Create Alert Button for Webhooks

Welcome to the next installment of our How-To guides on sending orders to NinjaTrader from TradingView. Today, we’re tackling the powerful command capabilities of NinjaTrader. Whether you're new to NinjaTrader or looking to refine your command skills, this post will guide you through using various commands to manage your trades more efficiently.

Quick Setup of TradingView Alerts

If you haven't setup TradingView Alerts before, let's quickly review before diving into Ninja commands.

First, create an Alert inside of TradingView by clicking on the Create Alert button inside the main dashboard on your favorite chart.

TradingView Create Alert button inside dashboard

Next, find your personal CrossTrade webhook URL. Your URL can be found inside the CrossTrade app under the "Webhooks" tab. Click the Copy button.

In your new TV alert, click on Notifications and make sure your CrossTrade Webhook URL is set as the target for the alert. Check the Webhook URL box and paste in your CrossTrade URL.

Now you're ready to add your command into the Settings tab. Click on Settings in your TV alert, give your alert a unique name, add your webhook payload in the Message box, click Create, and you're done!

Command message payload for TradingView webhook alert

Follow along below to learn how to craft webhook payloads to send different commands to NinjaTrader.

đź’ˇ
Don't forget to enable the Automated Trading Interface (ATI) in NinjaTrader 8 and restart the application before sending alerts through CrossTrade.

Introduction to NinjaTrader Commands

NinjaTrader’s command functionality allows traders to execute and manage trades using specific text commands. This can be incredibly powerful for automating trading strategies, executing bulk operations, or quickly modifying orders without navigating through multiple GUI screens.

NinjaTrader's ATI (Automated Trading Interface) command list
NinjaTrader's ATI (Automated Trading Interface) command list

The Essentials of NinjaTrader Commands

  • Cancel: This command is used to cancel a specific order by providing its order ID and an optional strategy ID.
  • Cancel All Orders: Quickly cancel all active orders across all accounts and broker connections, streamlining the process during volatile market conditions.

Strategic Order Management

  • Change: Modify the parameters of an existing order, such as adjusting the price or quantity, offering flexibility as market conditions shift.
  • Close Position: Use this command to immediately close a position for a specified account and instrument, a crucial action in risk management.
  • Close Strategy: This specifically targets and closes any strategy that might be running, ensuring traders can quickly halt any automated strategy executions.

Advanced Position Control

  • Flatten Everything: An essential command for risk management, allowing traders to close all positions and cancel all orders, ensuring a clean slate in fast-moving markets.
  • Place: Initiate new orders, apply strategies, or modify existing ones seamlessly. Example:
key=your-secret-key;
command=PLACE;
account=sim101;
instrument=ES 06-24;
action=BUY;
qty=1;
order_type=MARKET;
tif=DAY;
  • Reverse Position: Quickly reverse an open position to capitalize on market movements, essentially selling if you’re long and buying if you’re short, with one command.
  • Take Profit and Stop Loss: Set your take profit and stop loss levels, either solo or as an OCO bracket, and send it all the NinjaTrader in one alert message.

Utilizing Commands Effectively

To utilize these commands effectively, traders need to understand the parameters such as account, instrument, action (buy or sell), quantity, and order type (market, limit, etc.). Integrating these commands into daily trading can vastly improve response times and execution efficiency.

NinjaTrader ATI required and optional values for each command value
NinjaTrader ATI required (R) and optional (O) values for each command value

Available Parameters and Valid Values

Each command in NinjaTrader can have one or more required or optional parameters (shown above). Following is additional information about how each of those fields must be formatted.

Table of available parameters for CrossTrade commands
Table of available parameters for CrossTrade commands

Command Formatting

The message crafted in your TradingView alert or CrossTrade Sandbox takes the form of parameter=value; for all parameters. For readability, each parameter is generally entered on a new line, but that's not a requirement.

Example PLACE command:

key=your-secret-key;
command=PLACE;
account=sim101;
instrument=ES 06-24;
action=BUY;
qty=1;
order_type=MARKET;
tif=DAY;
flatten_first=true;

In general, commands are one word (e.g., CANCELALLORDERS) and parameters with spaces in them are denoted in CrossTrade messages with an underscore. For example, ORDER TYPE would be ORDER_TYPE in your TradingView alert.

We recently added the FLATTEN_FIRST option, available exclusively to the PLACE command. This option instructs NinjaTrader to pre-flatten any position in the instrument before executing the Buy/Sell order. Read more about "flatten first" and how it works.

đź’ˇ
The letter case of the text in your TV parameters doesn't matter. E.g., ORDER_TYPE and order_type achieve the same effect. But the value of your ACCOUNT or STRATEGY, for instance, must match the value and case in NinjaTrader exactly.

Examples and Best Practices

Scenario 1: If you want to send a request to open a position along with a specific NinjaTrader ATM (Advanced Trade Management) strategy to manage stop losses, for example, you can pass the name of the strategy right inside of the alert message. The name of the strategy inside NinjaTrader must match exactly:

key=your-secret-key;
command=PLACE;
account=sim101;
instrument=ES 06-24;
action=BUY;
qty=1;
order_type=MARKET;
tif=DAY;
strategy=MY_STRATEGY;

Scenario 2: If a trader needs to exit a position quickly due to changing market news, they might use the CLOSEPOSITION command with the specific instrument and account to minimize losses. Example:

key=your-secret-key;
command=CLOSEPOSITION;
account=sim101;
instrument=NQ 06-24;

Scenario 3: For those managing multiple strategies, using the CANCELALLORDERS command at the end of the trading day ensures no open orders are left that could be executed undesirably overnight. Example:

key=your-secret-key;
command=CANCELALLORDERS;

Wrapping Up

Mastering the command inputs in NinjaTrader not only enhances your trading capabilities but also equips you with the tools to react swiftly and effectively to any market situation. As you become more comfortable with these commands, you’ll find that your ability to manage and execute trades will be smoother and more responsive.

Remember, practice is key to mastering any new tool, especially one as powerful as CrossTrade. Keep experimenting with different scenarios in a simulation environment to build your confidence and ensure precision in live markets.


Start your free trial

Try CrossTrade for 7 days.

Sign Up