Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
how to disable margin/leverage in backtest?

Hi,

I'm seeing some weird things while backtesting. With a test with initial capital of $100K, I see on a few days, $300K or $400K were purchased.
The code has a check:

if context.portfolio.cash > X:  
#buy stock of amount X  

Is this not enough to prevent margin from being used?

Thanks!

1 response

I found in a couple of old threads that context.portfolio.cash isn't updated per order() call, but rather per minute.
So, I just added a

temp_cash = context.portfolio.cash  

at the beginning of scheduled method.
then manually decrement order value for each order call
temp_cash -= X and check if temp_cash > X instead of context.portfolio.cash > X before ordering more stocks