This is a follow-up to my recent post. It trades SPY & SH, using a dollar-volume z-score indicator.
This is a follow-up to my recent post. It trades SPY & SH, using a dollar-volume z-score indicator.
Quantopian is using a model of risk based on hedge positions. It will allow you to go long and short simultaneously. These positions balance each other and leave your cash metric unchanged. As a result you can borrow many multiples of your starting cash inadvertently. All built-in portfolio metrics (cash, capital_used) seem to operate this way. I wish there was a 'capital_basis' metric so I didn't have to calculate it myself. Or better yet a 'margin_level' metric.
For retail investing you have to measure the amount of capital you have invested. Your margin by default considers your equity (starting cash + pnl) as collateral for a loan. You can borrow until your equity is only 50% of your capital position. E.g. you can invest twice as much as you have in equity.
I modified your backtest to plot a custom variable for 'capital invested'. It shows that you were borrowing 3 - 4 times your starting cash. I think this is because you are going long/short with 2 stocks and are trying to use 50% margin. If you had 10 stocks it would further magnify the problem.
I honestly don't know if Interactive Brokers will be using a 'capital basis' margin calculation or a more advanced hedge risk model.
I modified your backtest to change the way order sizes are calculated:
1) it checks your existing position and adjusts it accordingly
2) it now uses 50% margin
Thanks Dennis,
I appreciate your comments to my earlier post and to this one, and the tweaks to the code. It'll take me a bit to digest it all, since (if not obvious) I'm new to this field.
Grant
You are welcome Grant. I should mention that I'm also new to this field so take my "wisdom" with a grain of salt (or three).
Cheers,
Dennis
Hello Dennis,
Without borrowing (margin), the constraint is that cash should not be negative for a long-only strategy, right? Presumably, in the context of the Quantopian backtester, negative cash means that I'm on the hook to replace it (undoubtedly with interest), so effectively it is a loan (collateralized by the shares owned by me).
Now, suppose that I go both long and short. Without borrowing cash, it would seem that at any point in time, I just need to be able to cover the short (by buying shares to replace the ones I borrowed and sold). Presumably, brokers allow the short to be covered by combining cash on hand with cash generated by selling securities that I own, right? Or if I short a stock, do I need to hold all of the cash to cover the short?
Also, it seems that shorting and margin kinda go hand-in-hand, since effectively, as soon as the short starts to go sour, the broker effectively is making up for my lack of collateral, right? So, I'm guessing that shorting requires some form of a margin account, right?
Grant
Grant, I'm not an experienced trader so my understanding may be a little shaky. But I will explain how I believe it works.
For a non-margin account the cost basis of long positions must not exceed cash. And cash can only increase after you sell off a position. That is to say the value of your position has no effect on your buying power. Additionally you cannot enter into a short position since every short requires borrowing to sell shares before you buy them.
Non-margin example:
1) Starting cash is $10,000 and you go long $10k of SPY.
2) You now have $0 cash. You cannot buy anything.
3) You sell your position of SPY for $11k.
4) You now have $11,000 of buying power.
If you have a margin account then you can borrow. Let's assume a 50% margin level which means you can borrow until your equity drops to 50% of your position value. Turning that around it means you can enter into a position that is double your cash. As a result the value of your position directly affects your margin level. And that means the value of your position affects your buying power. If the value drops your margin level could be less than 50%. You don't have to pay that back immediately but your buying power will be zero. Typically the "maintenance margin level" will be 25%. If your position value drops and your margin level dips below 25% then you would have a "margin call" and you would have to transfer in more cash or close a position. Your broker has the ability to close positions without consulting you.
Margin example (long SPY, long SHY):
1) Starting cash is $10,000. You have $10k of "margin equity". Buying power is $20k (e.g. $10k / 50%)
2) You go long $10k of SPY (tying up $5k of margin equity). Buying power is $10k (e.g. $5k / 50%)
3) The position in SPY rises in value to $11k. That $1k in profit increases your unused margin equity to $6k.
4) Buying power is $12,000 (e.g. $6k / 50%) and you haven't even sold your position in SPY yet.
5) You buy $12k of SHY bond ETF (tying up $6k of margin equity). Buying power is $0. Margin level is exactly 50%.
6) Position value of SPY drops to $9k. That negative PnL drops your overall position value to $21k ($9k SPY + $12k SHY).
7) Margin equity = $9k (starting cash + PnL). Margin level = 43% ($9k margin equity / $21k position value).
You can treat short and long positions the same in the margin example. Just remember that the positive cost basis ties up margin equity. And the position value affects the margin level percentage.
If your algorithm recalculates all positions at the same time then you just start with margin equity (starting cash + PnL) to get your overall buying power.
If your algorithm waits for buying signals and may have multiple independent positions at any time then you need to calculate your remaining buying power. If you have a really active algorithm you might need to take open orders into account as well.
I have a helper function that calculates my remaining buying power based on position amounts. I haven't needed to track open orders so far.
As far as I can tell the only difference between this function and context.portfolio.cash is the line that makes short positions count the same as long positions. Basically I'm not allowing short positions to balance the risk of long positions.
def buying_power(context, data):
margin_pct = .50 # 50% margin available for leverage (smaller % equals bigger leverage)
starting_cash = context.portfolio.starting_cash # initial deposit (never changes)
pnl = context.portfolio.pnl # profit & loss (updated at the start of handle_data)
market_value = starting_cash + pnl # current market value (cash +/- profit & loss)
# get a sum total of capital spent or borrowed for all current positions
spent = 0.0 # initialize to zero
# check every stock in current positions (also works with set_universe)
for stock in context.portfolio.positions:
# get positive amount of shares in current position for this stock
amount = context.portfolio.positions[stock].amount
amount = max(amount, -amount)
# get the cost basis of the shares (how much we spent on average per share)
cost_basis = context.portfolio.positions[stock].cost_basis
# add dollar amount to the 'spent' total
spent += amount * cost_basis
# portion of 'spent' that we paid directly
paid = spent * margin_pct
# cash remaining (market value minus money paid directly)
cash_on_hand = market_value - paid
# calculate buying power (how much can we spend if we borrow on margin)
buying_power = cash_on_hand / margin_pct
# return amount of buying power for use in trade decisions
return buying_power
Thanks Dennis,
I'll need to read your words more thoroughly, and look over the code, but I think that I get the gist. In effect, the broker is willing to loan based on the collateral of the value of unsold stock that I own (kinda like a bank will provide a home equity loan without my needing to sell my house first). For stock that I've borrowed (and subsequently sold), i.e. a short sale, I'm guessing that if the short has value (i.e. I could buy the shares and return them for a profit), the broker would count it, too, toward the collateral for a loan. So, the amount of money the broker is willing to loan can vary with time, based on the details of my portfolio...makes sense.
Grant
No problem.
It sounds like you have the gist of it. Admittedly it took a lot of effort for me to understand it as well.
Here is an example on the SEC website: http://www.sec.gov/investor/pubs/margin.htm
Hey Dennis,
I don't have time to fiddle with it, but I got curious if your patched up version of my original algorithm would work without margin, and ran it with:
# shares = cash/data[stock].price/len(context.stocks)/.5 # desired position (using margin)
shares = cash/data[stock].price/len(context.stocks) # desired position (no margin)
It seems to do o.k. I'm still a little murky on whether it actually is "no margin" since it does short selling, which would require a margin account, correct?
Grant
You are correct. Short selling requires a margin account. But that doesn't force you to invest more than your actual cash.
I guess I should also mention settlement period. A non-margin account forces you to wait 3 days for each trade to settle. Otherwise you will be tagged as a pattern day trader. You need an account balance of $25k to qualify for a margin account and avoid the settlement period requirement.