Charge each short trade its borrow fee, the annual rate times the value borrowed times the days held over 360, and check that the stock could be borrowed at all. Easy-to-borrow large caps cost well under 1% a year; hard-to-borrow names can cost many times that, and those are often the stocks a short-selling rule picks.
What a short actually costs
- Borrow fee. To sell a stock you do not own, your broker borrows it and charges a fee on its value for every day the short is open. US brokers usually accrue it daily on a 360-day year.
- Dividends. A short pays any dividend that goes ex while it is open. On adjusted prices this is already in the returns; on raw prices, charge it.
- Availability. A stock with no shares to lend cannot be shorted, and a lender can recall shares, forcing you to buy back.
- Spread, slippage and commission, as on any trade.
Why borrow cost matters more than it looks
The rate varies by stock and by day. Shorts of small, crowded or heavily shorted stocks, which many short rules select, can carry rates of tens of percent a year. A short rule that earns 15% a year on paper may earn nothing after borrowing hard-to-borrow names. Your broker publishes current rates; historical rates come from securities-lending data vendors.
How to charge it
For each closed short trade:
borrow = qty × entry price × annual rate × days held / 360
This uses the entry value. The fee actually accrues on each day's market value, but entry value is close enough for most holding periods.
How Reamer Research handles it
Reamer Research reports every closed trade with its side, entry price, quantity and timestamps, so borrow is charged after the run, per trade, at any rate you have. This class trades both sides; the code after it charges a flat rate per ticker to the shorts:
from datetime import datetime
import reamer_research as rr
class Reversal:
lookback = 25
def on_bar(self, d):
if d.close[0] == 0 or d.position() != 0:
return None
mean, sd = d.close.mean(), d.close.std()
if d.close[-1] > mean + 2 * sd:
return rr.sell_market(10, take_profit=mean, stop_loss=d.close[-1] + 2 * sd)
if d.close[-1] < mean - 2 * sd:
return rr.buy_market(10, take_profit=mean, stop_loss=d.close[-1] - 2 * sd)
BORROW = {'SPY': 0.0025} # 0.25% a year
result = rr.backtest(Reversal, {'SPY': 'data/SPY_5min.bin'}, spread=0.01)
def days_held(t):
held = datetime.fromisoformat(t['close_timestamp']) - datetime.fromisoformat(t['open_timestamp'])
return held.total_seconds() / 86400
borrow = sum(t['qty'] * t['entry_price'] * BORROW[t['ticker']] * days_held(t) / 360
for t in result.closed_trades if t['side'] == 'sell')
print(f'net {result.net_pnl:.2f}, borrow {borrow:.2f}, after borrow {result.net_pnl - borrow:.2f}')
A short trade has side 'sell'. A dict of rates per ticker, or a table of rates per day loaded with pandas, plugs into the same sum. The class could equally be an on_bar(data) function, or an object configured with its band width.
Limits to know
- The engine's
swap_per_unitsetting suits FX and CFD financing, where longs pay and shorts receive. For stock borrow, which shorts pay, use the calculation above. - Borrow rates and availability are yours to supply. The engine does not hold a stock-loan feed.
- Evaluation Mode runs 30 days or 50,000 bars of one ticker. Enough to check the calculation on your own shorts; universes need a licence.
A $20 month includes the full kit, so you can charge your own borrow rates to your own short strategy and cancel if it does not fit.