Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
LimitOrder : why does using LO get me into negative cash

I'm re-using a simple code I've shared previously.
When I use market orders everything is fine (it allocates 0.9 times my available cash, thus leverage =0.9 as expected from my context parameter)

But when I use LimitOrder, with a price +/- 0.5%, I'm always getting into margin, thus leveraging. Am I using LimitOrder Incorrectly

I've tried this in minute and daily mode, and same issue.

3 responses

I have cleaned up the code, and address some shameful copy/pasta errors.
In daily mode, the problem is still there as demonstrated in this backtest.

limit_order is activated as per its contextual_parameter ... but check the next post for the minute mode results (that is as expected)

and there is the minute mode, identical version with limit_order

So, until further comment by Q team, I believe the daily backtesting does not handle properly limit_orders. Maybe I'll check for data frequency in my code prior throwing in limit_orders during development.

Overlapping orders, orders both cancelled and created within the same period, may incur doubled up executions. That is, due to the delay of an entire day, and the backtest market model working all outstanding stops and limits prior to handle_data (or schedule functions) you can get crossed in mid-stream. This is most likely what is occurring. In true real time trading, one would always wait for confirmation of executions (news, cancels, amends) before the next action is taken. One would never work a market in an unknown state. "Is my limit still outstanding? Ah well, send in a market anyway." (Not that your strat is doing this.) Minutely processing would avoid the 389 minute lag of orders crossing during the wait for the next bar handle event. It could still happen though, just not as often. That's my bet.