# Mechanics of Margin

Folks I am testing a frequently rebalance portfolio where part/all of the cash is margin and I was hoping to understand some of the specifics of how it works.

Does margin work like a line of credit, i.e. I can go to -200% of equity value in trades. If I make a sale does it automatically pay off what I paid.

Lets get an example where X stock has an entry point of \$100.
I put deposit 10,000 dollars and invest all of it in X. | Account: 100 Shares of X Cash \$0
X then goes down 3%, and I want to buy another 150 of X. | Account 101.54 Shares of X Cash -150
Overnight interest applied (on 150) | Account 101.54 Shares of X, Cash -150.015
Next day, X is back to the \$99 a share, I sell 1.02 shares | Account 100.52 X, Cash -\$48.455
Overnight: interest, loan automatically closes partially | Account 100.52 X, Cash -\$48.46
In this example I am up a little bit and there are more complicated scenarios to consider but I want to make sure I get the mechanics of margin correct in my models.

@mechanicalfinance You pretty much have margin nailed. I’ll go through your specific questions, but before I do, it may be good to introduce the concept of “Buying Power”. That’s basically how much stock you can buy at any given time. The calculation is simple

(2 x current_equity) - (sum_current_holdings)

It’s really the max you can spend (ie 2x equity) minus what you already have. That is the what you have left to spend on new purchases.

“Does margin work like a line of credit, i.e. I can go to -200% of equity value in trades.”
Yes. Margin is basically a line of credit. Alpaca will ‘lend’ you up to 100% of your account equity or, another way of looking at it, you can purchase stocks worth 200% (ie 2x) of your initial cash.

“If I make a sale does it automatically pay off what I paid.”
Yes. One’s margin balance is always automatically paid down with any positive cash.

An example

Initially deposit \$10,000
buy 100 shares of stock XYZ at \$100 for \$10,000
Account: 100 shares of XYZ @ 100.00 or \$10,000 / Cash: \$0* / equity: \$10,000
Buying Power: (2 x 10,000) - 10,000 = \$10,000

Correct so far

XYZ then goes down 3% to \$97
Account: 100 shares of XYZ @ 97.00 or \$9,700 / Cash: \$0 / equity: \$9,700
Buying Power: (2 x 9700) - 9700 = \$9,700

Notice our buying power went down because the equity went down

buy 1.54 shares of XYZ at \$97 for \$150
Account: 101.54 shares of XYZ @ 97.00 or \$9849.38 / Cash: -\$150/ equity: \$9,699.38
Buying Power: (2 x 9699.38) - 9849.38 = \$9549.38

Correct so far

Overnight interest is applied on the amount borrowed which was \$150.00
Interest is 3.75% annual / .0375/360 daily / .000104 / \$0.0156
Account: 101.54 shares of XYZ @ 97.00 or \$9849.38 / Cash: -\$150.0156 / equity: \$9,699.364

Conceptually this is correct, but margin interest is debited at the end of each month and not daily. At this point the cash would still be -\$150.00.

sell 1.02 shares of XYZ at \$99 for \$100.98
Account: 100.52 of XYZ @ 99.00 or 9,951.48 / Cash -\$150.00 + 100.98 or -49.02 / equity: \$9902.46
Buying Power: (2 x 9902.46) - 9,951.48 = \$9,853.44

Correct

Overnight: interest is applied to the amount borrowed which is \$49.02

Does that all make sense.

Yes, Thank you so much