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