Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Margins

Hey,
Just getting started with this so I apologize in advance if I'm asking a dumb question.

I've added:
context.min_notional = -50000
context.max_notional = 50000
To my code, to try to limit the amount of money spent, but after running backtests on all my algos, I see that it's using margins and spending hundreds of times more money than cash in the account.

Is there any way to have it only trade with the cash it starts out with?

4 responses

Hi Matt,

We are getting this question quite a bit these days (not a dumb question - and something we are hoping to make a lot easier soon!)

Quantopian does not currently have a margin model built in to protect you from using leverage - setting a max and min notional limit without writing in your own logic to respect them won't achieve this unfortunately.

For a good example of imposing margin limits on an algorithm you can check out these threads:

https://www.quantopian.com/posts/margin-requirements
https://www.quantopian.com/posts/class-for-tracking-margins

Alternatively please feel free to share the algo you are working on either publicly to this this thread, or privately with me (you can invite collaborators to algorithms using the 'Collaborate' button to the left of the 'Run backtest' button -- my email is [email protected]) and I would be happy to help you get this working correctly!

Sorry for the inconvenience & best wishes,
Jess

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Thanks Jess! I'll look into the threads and into incorporating my own logic.

Matt

I wrote this into one of my alogs. Look at the source code.

1) set a variable for "cash = context.portfolio.cash"
2) In the IF statement, instead of using a notional value, use "and cash > 0" or greater than the maximum trade you'll allow it to make like I did. Also when you are selling, there is no good way to stop from shorting. I added in an "and context.portfolio.position_value > whatever the amount of stock you are trying to buy"

Hope that helps!

Thanks Blake! This is great. I've implemented it and it seems to be working.