I am having trouble modeling short selling mechanics in my backtesting system.
When I sell stock short, I make the following changed to account variables:
- Credit Balance += 200% of the stock value sold short (assuming a 100% initial margin requirement)
- Cash -= 100% of the stock value (the cash is transferred to the Credit Balance account to collaterize the short sale)
- Short Market Value (SMV) -= 100% of stock value (this is the market value of the shorted stock)
- Net Account Value += (Credit Balance + Short Market Value + Cash)
When I close out the short position later on, I change the accounts as follows:
- Credit Balance -= 100% of the stock value bought back
- Short Market Value (SMV) += 100% of stock value bought back
- Net Account Value += (Credit Balance + Short Market Value + Cash)
I am pretty sure this second part (closing out the short position) is wrong. At the very least, my cash position should also be affected. Any suggestions on how to fix it? Thank you.