Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Simple moving average model - Liquidity Issues

New to the platform and seem to be having an issue with closing out a position under tight liquidity conditions. I have used the order_target_percent function to both buy and sell a single security based on the 5 vs 20 day moving average, the purchase was completed as expected but the sale occurred during a rapid price drop and the order was filled incrementally but continued to sell (even more shares than were originally owned) leading to a short position which is not feasible. I've tried the order_target and order_value functions as well to no avail as well as the old school approach of getting the current position size each time the if statement before the sale is triggered.

Please let me know if you have any suggestions for dealing with this issue.

Thanks!

1 response

Generally, don't place any orders for a particular security while there are unfilled (or partially filled) orders for that same security still open. You can check for open orders using the get_open_orders method. Ive attached a backtest with logic added to only order if no open orders. There are other better ways to do this but just wanted to show the concept. Check out this post https://www.quantopian.com/posts/new-orders-filled-without-of-my-control .

Below are some links to forum posts that also may help...

order_target_percent will sometimes over order or under order.
https://www.quantopian.com/posts/backtest-transactions

You can over-ride the default filling logic and force the backtester to fill completely in the minute posted
https://www.quantopian.com/posts/how-can-the-submitted-order-be-filled-immediately