Edit: I'm trying to create an order matching engine for options trading.

I'd like to ask my question by creating a situation:

Amy has a short position in a single (1) call option on AAPL. Let's assume she shorted the call at a price of \$20.

The price of the option is now \$30.

Amy now wants to cover this short. She does so by placing a limit buy order at a price of \$27. The limit order system sees she has an existing short and removes this position that she can further trade (basically locks it down so only a market order or cancel order can mutate it).

Now Amy(who's getting paranoid about her losses) puts in another limit buy order at \$28. But the limit order system does not see any more free options in her account and assumes this is an outright purchase and freezes \$28 from her account.

Along comes a market order that sells Amy a call option at the price of \$28. But Amy doesn't lose the frozen \$28. She instead covers her short. She now has a net 0 position in the aforementioned call. And \$28 are (still)frozen from her cash account.

Along comes another market order which triggers the limit buy at \$27. This time the exchange realizes that the order is an outright buy and transfers a call option in her positions and removes \$27 from her \$28 of frozen cash.

Now Amy is net long 1 call option but still \$1 frozen.

Amy should actually have 0 dollars frozen but my analogy isn't seeing how. I know exchanges currently handle this easily (I think). But how? I'm trying to implement correct accounting but can't seem to be able to it figure out.

Any help would be great! Thanks a lot. Please let me know if you have questions in the comments. I can edit this post to be more clear.

  • 2
    The problem with these kinds of hypothetical questions is you're assuming a system failure to get to a problem that's never going to occur. Are you having an actual problem? – quid Jun 26 '19 at 21:04
  • Yes. I'm having an actual problem. I tested this exact situation out. I know that market makers would require the system to account all frozen cash precisely. Also, my dad is a trader and said my system is flawed. – Akshay Sakariya Jun 26 '19 at 21:07
  • 3
    "I'm trying to implement correct accounting" What does this mean? Are you trying to replicate the exchange's accounting? Why not just review the remaining orders after the first trade, see that a $27 limit order remains, and freeze $27 instead of $28? What does it matter how the exchange does it if you're trying to do it yourself? – D Stanley Jun 26 '19 at 21:30
  • I'm sorry. I didn't say initially. I'm trying to create an exchange matching engine myself. Thanks for that. I will added an edit that clarifies. – Akshay Sakariya Jun 26 '19 at 21:46
  • 1
    What will your matching engine be used for? – Chris W. Rea Jun 26 '19 at 21:49

I'm not sure what a freeze is (buying power tied up?) or what locking a limit order down means or what mutating an orders is. I also have no idea where you're trading (no tag) so I'm going to describe what happens in the US in such a situation (commissions ignored).

Amy sells an AAPL short call at $20. She then places a BTC order for one contract at $27 and then another buy order for one contract at $28 which is BTO. That restricts $55 of her buying power which is now dedicated to these two potential purchases.

The option exchanges don't know and don't care what's going on with Amy. All that occurs there is that there are two buy orders that have been forwarded to them by Amy's broker and they are now on the order book(s).

What's relevant is what's happening in Amy's brokerage account. When the first fill occurs, $28 is removed from her account along with the short contract that she has now covered. What now remains is a BTO order at $27 and $27 of buying power is tied up. When that order is filled, $27 is removed from her account and she is now long 1 contract with the same terms as the one that was originally sold.

It's that simple.

  • Okay. I gave this some thought and tried out examples. I figured out I wasn't adding to the buying power when someone shorted the options. This put me on the right path. I can now do all accounting correctly. And also without having to 'lock' positions. I would have to change a lot in my code to now reflect buying power and withdrawable cash. No more 'frozen cash'. Thank you very much! – Akshay Sakariya Jun 26 '19 at 23:43

This is not the situation in the US

Although AAPL is cited as the security (a ticker symbol for a US security):

  1. Options Exchanges in the US do not consider the available margin in this way. All options are cleared through the Options Clearing Corporation who have their own rules for calculating margin requirements using a risk based model. It is up to the clearing members to ensure that their customers' accounts stay within the rules. Most of these types of checks are performed by the broker handling the customer orders and not the exchange.

  2. All stock options in the US must be traded on an Exchange (registered with the Securities and Exchange commission). This is something to do with the fact that listing a new series is like issuing a new security - something that only exchanges can do.

This is an allocation problem

A similar scenario is buying and selling stock. If you have a small (long) position and send in a bunch of sell orders for more than your position, some will have to be marked as long and some marked as short. If you modify the price of the orders, moving them around in the order book, there will potentially be situations when you will be selling short although you have a long position and vice versa.

The way to handle this is to apply a consistent approach. One way is simply to allocate (as you describe) in a first-in, first out approach. You take from a pool of stock (long or short) when you send in the order, and replenish that pool when the order is modified or cancelled. If you send in an order (even if you have a long position), but that long position is already allocated to other orders, you should mark it as short.

You could make your allocation model more sophisticated, such as re-arranging the allocations from the pool based on likelihood of execution (e.g. price level in the order book). This creates more complexity, but could result in a better outcome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.