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:

  1. Credit Balance += 200% of the stock value sold short (assuming a 100% initial margin requirement)
  2. Cash -= 100% of the stock value (the cash is transferred to the Credit Balance account to collaterize the short sale)
  3. Short Market Value (SMV) -= 100% of stock value (this is the market value of the shorted stock)
  4. Net Account Value += (Credit Balance + Short Market Value + Cash)

When I close out the short position later on, I change the accounts as follows:

  1. Credit Balance -= 100% of the stock value bought back
  2. Short Market Value (SMV) += 100% of stock value bought back
  3. 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.

  • $\begingroup$ Yup, your mismatch between entry and exit is a basic violation of double-entry book-keeping. Imagine you shorted 100 stocks at 100 today; and reversed it by buying 100 back at 100 seconds later. All of your account balances (assuming no costs) would have to be back to ex-ante. However you wish to account for the short, your exit has to be equal and opposite. Lest you could change an unchanged world in seconds :-) $\endgroup$
    – demully
    May 8, 2020 at 23:11
  • $\begingroup$ Point taken. I think the more challenging case arises when some time has passed though, and the market value of the shorted stock has changed. In that case, I am not sure how to reverse the credit balance transaction. Does the remainder of the credit balance, after spending some to buy back the stock, go back to my cash position? In that case, do I need to keep track of how much collateral I originally posted for each stock I short (assuming I am shorting multiple stocks with different trading periods)? $\endgroup$
    – nijshar28
    May 9, 2020 at 0:34
  • $\begingroup$ Keep in mind that you owe shares of stock, not dollars of credit balance. The units are different. $\endgroup$
    – nbbo2
    May 9, 2020 at 7:27
  • $\begingroup$ Sure, my example was deliberately an extreme case. In any case, your exit accounting has to be the opposite of entry. If you're not happy with this, your entry accounting might be suspect... To account for the changing value over time, you'll need a Realised P&L account. $\endgroup$
    – demully
    May 10, 2020 at 22:00
  • $\begingroup$ How you account for the cash and credit balances depends on exactly how you are shorting (ie actual stock borrow vs CFD); but these have to end up unchanged on exit, irrespective of the profitability of the trade. The change in value from the stock in between has to end up booked to a P&L account (that in turn reduces the Net Account Balance). But the mechanics how this works depends on exactly how you are shorting your shorts. $\endgroup$
    – demully
    May 10, 2020 at 22:06


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.