Complete Position Management: Open or Flatten & Reverse Trades Instantly with One TradingView Alert Message

Introducing the new "flatten first" option for NinjaTrader orders sent from TradingView. When enabled, any existing position in the same instrument will be flattened before the buy/sell order is created.

Complete Position Management: Open or Flatten & Reverse Trades Instantly with One TradingView Alert Message

Motivation for adding this functionality

This feature was inspired by you, the traders. Many of you reached out to us and said, "If only we could open or reverse without having to use multiple alerts..." And we wholeheartedly agree!

Opening a new long or short position in NinjaTrader from TradingView is easy. Simply send a PLACE order message to Buy or Sell through a TradingView alert to your personal CrossTrade webhook URL.

But if you already have a long position open and your alert triggers to sell short, for example, by default NinjaTrader will subtract the number of contracts you have open from the total. If you're long 2 contracts and you place a sell order to go short 2 contracts, you'll end up flat instead of ending up short -2 contracts.

If the goal is to reverse the position from long to short or vice versa, that's simple. Just REVERSEPOSITION from TradingView and send it to Ninja. The problem with this approach is it requires you to have an initial position open prior to the reversal. This makes layering different types of alerts in TradingView difficult, to say the least.

How we solved the problem

To solve this problem, we wanted to cut down on the number of different alert types a trader needs to use to be successful. In trading it's always better to have fewer things to think about and manage, not more.

Using TV alerts to trigger actions in NT8 is somewhat non-traditional. It's a one-way system.

Since it's impossible to communicate position status to TradingView, we added a special flag called FLATTEN_FIRST available to the PLACE command message. When FLATTEN_FIRST is true, NinjaTrader is instructed to close any position in the instrument type prior to executing the buy/sell order.

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

Example alert message using the new flatten_first option

Learn more about how to control NinjaTrader from TradingView.

The benefits of this are two-fold: we don't need a reverse command, and we don't have to worry about open position status in NinjaTrader. We can now conduct an entire trading strategy using PLACE and CLOSEPOSITION to get us out of the trade completely, if desired.

FAQS

How do I use it?

Tack on the FLATTEN_FIRST=true; flag to the end of your PLACE order message. Check out the example above.

What are valid values for the flag?

This flag is specifically looking for the value "true" in any letter case, e.g., upper, lower, or a combination of upper and lowercase letters. True, true, TRUE, TrUe, etc. are all valid. The same is true of the FLATTEN_FIRST parameter, though it must include the underscore.

What happens inside of NinjaTrader when this executes?

When the FLATTEN_FIRST flag is set, the message sent to NT8 becomes an ordered set of instructions: CLOSEPOSITION and then PLACE order. The position close preempts the order; instructions are executed sequentially.

What if I don't have any position open, what will happen if I use the FLATTEN_FIRST=true; option?

Nothing! NinjaTrader will make a note in the log there was no position that could be closed, and it will proceed with your Buy/Sell order.

Is this the same thing as a reversal?

Technically, no. If the quantities match an already open position, e.g., already short -2 contracts and then buy +2 contracts, it's not technically a reversal. It's a flattening and then opening of a new position. So if you're short -2 contracts in NT8 and use FLATTEN_FIRST with an action=BUY order of QTY=1 you'll end up long +1 contracts.

Is this flag compatible with the other PLACE options or does my alert have to look like the example above?

It's absolutely compatible with all other optional fields. For example if you want to open a position but also execute an ATM (Automated Trade Management) strategy called "crosstrade", which is not a required field, you can add that as well:

key=my-secret-key;
command=PLACE;
account=sim101;
instrument=NQ 06-24;
action=BUY;
qty=1;
order_type=MARKET;
tif=DAY;
flatten_first=true;
strategy=crosstrade;

Example alert message using FLATTEN_FIRST with an ATM strategy


We hope this new functionality both helps everyone and simplifies your strategy development. Go forth and conquer!

Start your free trial

Try CrossTrade for 7 days.

Sign Up