Why Your Backtests Fail in Margin Trading & Securities Lending (And How to Fix It) | by DolphinDB | Apr, 2026 | MediumSitemapOpen in appSign up<br>Sign in
Medium Logo
Get app<br>Write
Search
Sign up<br>Sign in
Why Your Backtests Fail in Margin Trading & Securities Lending (And How to Fix It)
DolphinDB
22 min read·<br>Apr 29, 2026
Listen
Share
Press enter or click to view image in full size
Margin trading and securities lending are forms of leveraged trading that amplify investment returns, providing investors with greater access to capital and more trading opportunities. In quantitative investing, margin trading and securities lending are widely used in a variety of trading practices and strategies, including leveraged and high-frequency trading, hedging, market-neutral, and arbitrage strategies. At present, many quantitative fund managers often overlook margin trading and securities lending transactions when researching and backtesting their trading strategies, relying on standard strategy backtesting. However, they introduce margin trading and securities lending in the actual transactions. As a result, the backtest results differ significantly from the actual results.<br>Margin trading and securities lending mainly differ from standard trading in the following three aspects:<br>In addition to commissions, backtesting a margin trading and securities lending strategy also involves costs such as margin interest and securities lending fee.<br>The margin trading and securities lending market is highly dynamic. Key attributes of the underlying securities change with market sentiment, such as the collateral discount rate for the day, the margin usage ratio, and whether a security is eligible for margin trading or securities lending.<br>The risk management module for margin trading and securities lending strategy backtesting is more stringent, imposing limits on the account’s maintenance margin ratio, long concentration, and short concentration.<br>The Backtest plugin of DolphinDB now supports margin trading and securities lending strategy backtesting. This backtesting framework comprehensively incorporates key elements to make the backtest results more closely reflect actual outcomes, including transaction costs, the daily eligible securities for margin trading and securities lending, the collateral discount rate, and the risk management module, thereby narrowing the gap between backtesting results and live trading outcomes.<br>Backtesting margin trading and securities lending strategy differs significantly from other types of strategy backtesting in terms of operation, risk management, and capital usage. Therefore, their parameter configuration, market data, and methods used also differ. This tutorial demonstrates how to implement margin trading and securities lending strategy backtesting using the Backtest plugin of DolphinDB, covering an introduction to margin trading and securities lending transactions, DolphinDB-based solution for margin trading and securities lending strategy backtesting, and specific backtesting cases.<br>Note : All scripts in this tutorial are compatible with DolphinDB Server versions 2.00.14.1, 3.00.2.1, and later.<br>1. Introduction<br>Margin trading and securities lending are forms of securities margin trading in which investors borrow funds or securities to obtain leveraged trading opportunities to amplify returns or hedge risk. Margin trading refers to borrowing funds from a broker to purchase securities. In contrast, securities lending involves borrowing securities from a broker, selling them, and intending to buy back the same quantity at a lower price in the future and return them to the broker. Margin trading and securities lending generate profits based on expected price movements. Margin trading and securities lending require margin. It can take the form of cash, stocks, or bonds, and brokers assign a collateral discount rate based on the asset quality. Investors must pay costs such as interest, securities lending fees, and transaction fees. Note that for margin trading and securities lending transactions, only marginable securities can be traded, and only securities eligible as collateral can be purchased as collateral. Because of the leverage effect, this mechanism increases both potential gains and losses, exposing investors to challenges such as forced liquidation, market volatility, and cost accumulation. In addition, only stocks that meet specific criteria are eligible for such transactions.<br>1.1 Margin Requirements<br>In margin trading and securities lending, the margin system is a prerequisite to protecting the exchange’s interests. When engaging in margin trading and securities lending, investors must trade using a margin account. They are required to transfer cash or eligible collateral securities into the account before engaging in margin trading and securities lending transactions.<br>For the SSE, SZSE, and BSE, the margin requirement ratio of securities lending for individual investors shall...