quantopian/zipline — pull request #460 by jlowin (2014-12-31 16:49:43+00:00)
Title: Add LIQUIDATION events

Securities sometimes stop trading due to delisting, acquisition, maturity, etc. Currently, if users hold positions in those securities, the positions just persist after the security disappears, maintaining their market values. There is no way to close these positions because orders are only executed when trades occur (which, once the security stops trading, is never!). This creates a strong divergence from reality because users can't make use of cash which would be returned to them when the security is liquidated and must use leverage instead.

Proposed solution: a new `DATASOURE_TYPE` called `LIQUIDATION` which looks just like a `TRADE` but differs in two ways:
1. When `AlgorithmSimulator` encounters a `LIQUIDATION` event, it (a) cancels all open orders for that `sid` on the blotter (because you can't trade it anymore!) and (b) places one more order that closes any open positions.
2. When `Slippage` encounters a `LIQUIDATION` event, it generates a transaction without regard for commission or volume. (maybe the commission constraint should go?)

Otherwise, liquidation-generated orders/transactions are handled exactly like "normal" ones, to maintain compatibility. The only difference is that when liquidation events appear, all trading is halted and all positions are forcibly closed out.

Usage is simple. Whenever a `DataSource` gets to the last trade of a security, it emits a `LIQUIDATION` event instead of -- or in addition to -- the normal `TRADE` event. Because this is handled on the data source end, users don't need to worry about leaking future knowledge; whenever the security liquidates, the system will process it automatically. Since a LIQUIDATION event can have its own price, a security could be liquidated at 0, par, or some acquisition price regardless of the last observed trade.