I am having trouble modeling short-selling mechanics in my backtesting system. This is not a real live accounting question, but it is about accounting at its core. So, I think it is appropriate to ask here. When I sell stock short, I make the following changed to account variables in my backtesting program: 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, I change my 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.