Writing Passive Relative Orders in NinjaTrader Strategies ( PASSV REL )
INTERACTIVE BROKERS PASS REL ORDERS
I was chatting with a friend that was trying to write passive relative orders in Ninja Trader that he had used in Interactive Brokers TWS.
Since you know I have no love for interactive brokers, I thought I would give him a hand.Besides, I have not written a coding post for the blog in a while.
If you want to find out how I feel about Interactive Brokers, just use the search bar over there.
Regardless, Interactive Brokers ( From now on, I will just type IB instead of Interactive Brokers because it is getting way to tiring to write the whole thing ) has a pretty good selection of orders, one of them being a Passive relative order (PASSV REL )
WHAT IS A PASSIVE RELATIVE ORDER ?
Lets start by looking at the IB website.
Passive Relative orders provide a means for traders to seek a less aggressive price than the National Best Bid and Offer (NBBO) while keeping the order pegged to the best bid (for a buy) or ask (for a sell). The order price is automatically adjusted as the markets move to keep the order less aggressive
What is is in reality is an attempt to get a better price. Putting in an order away from the market hoping the market will come back and get you into the trade at a better price.
Stocks, futures and options ( and other things , but you get the idea) move up and down all the time. A passive relative order is an order that sits relative to the bid / ask and waits for the price to come back and get filled.
The difference between this and a limit order is that limit price will move relative to the bid/ask price. So here is an example of this type of order.
I won’t go into detail here, but there is a really good explanation on the IB website.
THE CODE
This PSEUDO code below is for Ninjatrader strategies. I think the closest you have in the ATM strategies is the ATM strategy. The code is a little advanced so you will have to bear with me on this one. If you are just a beginner at Ninja script, I would leave this type of programming alone.
I went with PSEUDO code because if you are advanced enough write this, you probably would not have a problem writing the C# code yourself. I was going to release code, but I thought that would be to confusing. If there is enough interest, I will release it. So leave a comment and if enough people want it I will release the NinjaTrader code.
The first thing you will need is the order object and the bid / ask prices.
double offset_price ; double max_price ; bool entry_direction ; double current_limit_price ; ...
We need to keep the entry direction since passive relative orders act with the direction.
The next part is in OnOrderUpdate(), remember to check for the IOrder.Token.
OnOrderUpdate()
{
// wait until order comes back confirmed,and store the value.
if ( order completed ) current_limit_price = order price
}
OnMarketData {
...
double mid_point = ( GetCurrentBid() + GetCurrentAsk() ) / 2 ;
// if the price is at max price do not move
if ( current_limit_price gt max_price ) return
// now if price is to far away, move the limit order
if ( difference (mid_price , current_limit_price ) > offset_price )
cancel order
enter_new_order(mid_price , current_limit_price)
... }
Most , well all , of the heavy lifting will be done in the OnMarketData and OnOrderUpdate. You cannot do this using on bar update.
I have just touched on the subject, if you would like me to write more , leave a comment so I know other people are interested.
A LITTLE WARNING
Programming this stuff is considered advanced programming and is for experienced programmers. Don’t blame me if something goes wrong.
Using this type of order can and will generate a lot of cancel order messages as the limit order is moved.
For futures there are message limits that can lead to a fine and in options there are usually cancel fees from the exchange. Of course your broker may add something on also, so you mileage will vary.
And finally, using a lot of cancel and replace orders , you may run into ‘in-flight’ order problems. Which are usually not nice.
Photo by szeke
PIMPING MY NEWSLETTER
I don’t post often, but I do have a mailing list you can subscribe to in the meantime. I usually write about Ninjatrader programming, but other trading topics as well. Subscribe to TraderWerks Blog by Email
Good to see you! If you are new here, you can subscribe to the RSS feed for updates

