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

The definitions and requirements for margin accounts are laid out in the FINRA rules docs. I would like to make functions or a class to track requirements and compliance according to these rules but I am not 100% if I'm interpreting them correctly. I would like to add something like this to zipline but need to be sure I'm doing it right.

Here's an initial maintenance requirement function translated from page 9 of the FINRA rules, I wanted to get input before continuing. I'm unsure about the 'current market value.' Is the 'last sale price' the correct field to use or should it be the price?

def margin_req(portfolio):  
    req = 0  
    for stock in portfolio.positions:  
        amount = portfolio.positions[stock].amount  
        last_price = portfolio.positions[stock].last_sale_price  
        if amount > 0:  
            req += .25 * amount * last_price  
        elif amount < 0:  
            if last_price < 5:  
                req += max(2.5 * amount, abs(amount * last_price))  
            else:  
                req += max(5 * amount, abs(0.3 * amount * last_price))  
    return req  
5 responses

David,

We would love to see contributions for margin requirements, so I am thrilled you are taking a look.
The last_sale_price property for the position is equal to the most recent price for the stock, so I think it is the best price to use for calculating current market value.

thanks,
fawce

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.

As another user, thank you for posting this. I would love to see more general make sure I'm not doing anything bad functions.

i would be cool if it was possible to include the borrowing cost of the margin in the return calculations as well

David,

The definition of 'current market value' is on the first page of the FINRA link:

#  The term “current market value” means the total cost or net proceeds of a security on  
#  the day it was purchased or sold or at any other time the preceding business day’s closing price  
#  as shown by any regularly published reporting or quotation service, except for security futures  
#  contracts (see paragraph (f)(10)(C)(ii)).  
#  
#  If there is no closing price, a member may use a reasonable estimate of the market value of the  
#  security as of the close of business on the preceding business day.  
#  

In plain English, I would interpret that to say the current market value is the net price if transacted that day, otherwise it is the prior day's closing price.

Thanks Colin, according to Fawce, last_sale_price works for the current market value so the function above should be a pretty accurate representation of margin requirement. However, not all brokerages calculate the requirement the same way so it should be treated as the minimum possible requirement.

I made a class to keep track of each positions margin req. I put it in a new post