Examples & Use Cases
Example Alerts & Order Behavior
Now that you're familiar with NinjaTrader's various command functions, let's roll through some example alerts and how they behave on NinjaTrader.
Buy or Sell with Market Order
This payload will trigger a simple market order for BUY or SELL actions on the continuous futures contract instrument ES1! that will remain in-force for the rest of the trading day.
key=your-secret-key; command=PLACE; account=sim101; instrument=ES1!; action=BUY; qty=1; order_type=MARKET; TIF=DAY;
Be aware that if you send a BUY order and then a SELL order using the same quantity, the sell order will offset the buy position. This scenario does not create a net short position. If you want to close an existing position before opening a new position, include "flatten_first=true;" inside your message. More Info on Flatten_First.
Buy or Sell with ATM Strategy
This payload will trigger a simple market order for BUY or SELL actions that will use an ATM strategy template and remain in-force for the rest of the trading day.
key=your-secret-key; command=PLACE; account=sim101; instrument=ES 03-26; action=BUY; qty=1; order_type=MARKET; TIF=DAY; flatten_first=true; strategy=crosstrade;
Similar to the offsetting scenario when sending a Sell after a Buy, when using an ATM strategy, this will create orphaned Stops and Target orders as those are not cancelled by default. We recommend always using flatten_first=true; with your ATM payload to ensure that any existing bracket is closed completely. The quantity you set inside of your CrossTrade alert will supersede your default ATM quantity.
Buy or Sell Trade Brackets
Adding take profit and stop loss levels or TP/SL OCO bracket to an alert is very simple and is only available as part of the PLACE or FLATPLACE order commands. Both fields accept numeric values only that represent price level triggers for the order in the underlying (relative prices to the underlying for TP/SL can also be used, such as % and tick values).
Numerical example:
key=your-secret-key; command=PLACE; account=sim101; instrument=ES 03-26; action=BUY; qty=1; order_type=MARKET; TIF=DAY; take_profit=(price above); stop_loss=(price below);
Tick based example:
key=your-secret-key; command=PLACE; account=sim101; instrument=ES 03-26; action=BUY; qty=1; order_type=MARKET; TIF=DAY; take_profit=100ticks; stop_loss=100ticks;
Percentage based example:
key=your-secret-key; command=PLACE; account=sim101; instrument=ES 03-26; action=BUY; qty=1; order_type=MARKET; TIF=DAY; take_profit=10%; stop_loss=10%;
Dollar based example:
key=your-secret-key; command=PLACE; account=sim101; instrument=ES 03-26; action=BUY; qty=1; order_type=MARKET; TIF=DAY; take_profit=$1000; stop_loss=$1000;
Buy or Sell Limit Orders
This payload will trigger a simple limit order for BUY or SELL actions that will use the specified limit price.
key=your-secret-key; command=PLACE; account=sim101; instrument=ES 03-26; action=BUY; qty=1; order_type=LIMIT; limit_price=X; TIF=DAY;
If you place a Buy limit order above the current price, or a Sell limit order below, it will be executed immediately as you're crossing the spread.
Stop Market and Stop Limit Orders
This payload will trigger a Stop Market or Stop limit order for BUY or SELL actions that will use the specified stop price.
key=your-secret-key; command=PLACE; account=sim101; instrument=ES 03-26; action=BUY; qty=1; order_type=STOPMARKET; stop_price=X; TIF=DAY;
You must use the stop_price command and not the limit_price command when submitting Stop Market or Stop Limit order types.
Close then Open (Flatplace)
You have
key=your-secret-key; command=FLATPLACE; account=sim101; instrument=ES 03-26; action=BUY; qty=1; order_type=MARKET; TIF=DAY;
Close Position
This command will close a position and requires an account name value and an instrument name value. The instrument name value is the name of the NinjaTrader instrument including the exchange name. For equities, the symbol is sufficient. This command will cancel any working orders and flatten the position.
key=your-secret-key; command=CLOSEPOSITION; account=sim101; instrument=ES 03-26;
Close Partial Position
Lets assume you have 4 contracts open, this command would close 2 of 4. Additionally you could use quantity=2; to close 2 of 4:
key=your-secret-key; command=CLOSEPOSITION; account=sim101; instrument=ES 03-26; quantity=50%;
Close Strategy
This command will close an ATM Strategy and requires a strategy ID value. The strategy ID value must match a strategy ID given to a strategy in the PLACE command. This command will close the specified strategy.
key=your-secret-key; command=CLOSESTRATEGY; strategy_id=atm-strategy-id;
Reverse
Unlike Reverse Position, which equates to a close position + entry of a specific new order, Reverse is a true inversion of an existing position.
key=your-secret-key; command=REVERSE; account=sim101; instrument=ES 03-26; action=BUY; qty=1; order_type=MARKET; TIF=DAY; strategy=crosstrade;
Reverse Position
This command will close the current position and place an order in the opposite direction. The field requirements are identical to the PLACE command.
key=your-secret-key; command=REVERSEPOSITION; account=sim101; instrument=ES 03-26; action=BUY; qty=1; order_type=MARKET; TIF=DAY; strategy=crosstrade;
When reversing a position with an ATM strategy, you'll want to ensure that a position already exists in the underlying instrument and the action is also the opposite of the existing position you intend to reverse.
Flatten Everything
This command will cancel all active orders and flatten all positions across all accounts and broker connections.
key=your-secret-key; command=FLATTENEVERYTHING;
Flatten Positions
The FLATTEN positions command is very powerful. It provides the option to flatten all positions and pending orders across all accounts, a specific position for a particular instrument in a certain direction in one account, or anything in between.
key=your-secret-key; command=FLATTEN; account=sim101; instrument=ES 03-26;
Flatten all positions in the sim101 account:
key=your-secret-key; command=FLATTEN; account=sim101;
Flatten all short (or long) positions in Sim101:
key=your-secret-key; command=FLATTEN; account=sim101; market_position=short;
Flatten all long positions across all NT8 accounts:
key=your-secret-key; command=FLATTEN; market_position=long;
Flatten all MNQ 03-26 positions across all NT8 accounts:
key=your-secret-key; command=FLATTEN; instrument=MNQ 03-26;
Flatten all MNQ 03-26 positions across all NT8 accounts:
key=your-secret-key; command=FLATTEN;
As you can see the FLATTEN command can be customized for a variety of situations.
Cancel Single Order
This command will cancel an order and requires an order ID value and an optional strategy ID value.
key=your-secret-key; command=CANCEL; order_id=your-order-ID; strategy_id=your-strategy-id; (optional)
Cancel Multiple Orders
Similar to the FLATTEN positions command, CANCELORDERS provides the option to either cancel all orders in an account, or optionally cancel all orders in an account for a specific instrument.
The following alert will cancel all orders in the sim101 account:
key=your-secret-key; command=CANCELORDERS; account=sim101;
The following alert will cancel all orders in the sim101 account for instrument ES 03-26:
key=your-secret-key; command=CANCELORDERS; account=sim101; instrument=ES 03-26;
Cancel All Orders
This command will cancel all active orders across all accounts and broker connections.
key=your-secret-key; command=CANCELORDERS;
Cancel and Replace
The CANCELREPLACE command is effectively a CANCEL order + PLACE order command combination and accepts similar parameters. The caveat is that the PLACE command will only proceed if and only if the order exists and was successfully canceled.
The following alert message will cancel the order with ID "abc123" and execute the remaining fields as a PLACE order command:
key=your-secret-key; command=CANCELREPLACE; account=sim101; instrument=ES 03-26; action=BUY; qty=1; order_type=MARKET; tif=DAY; order_id=abc123;
Change Order
This command will change the parameters of a pending, unfilled order and requires an order_id value, optional price and quantity value, and an optional strategy_id value.
The order_id value must match either the order ID value given to an order placed through the PLACE command.
key=your-secret-key; command=CHANGE; qty=1; (optional) limit_price=XXX; (optional) stop_price=XXX; (optional) order_id=your-order-id; strategy_id=your-strategy-id; (optional)
Last updated