Backtesting Quantitative Trading Strategies using Python and Pandas

Original Source Here


Now that the quantitative research process is well defined along with the trading signal value we can start the analysis process.

First, let’s plot the return data.

Next, let’s regress the LSM Factor on future open-close returns.

Standard errors are heteroscedasticity and autocorrelation robust

The regression is compelling, but it’s entirely possible that the coefficient of determination is being generated coincidentally. If the signal broken down into returns by quantile doesn’t show a monotonic increase, we can’t say much for the tractability of this signal. I’ve built a function to aid in this visualization.


When this signal is broken down by quartile the chart exhibits the desired monotonic increase. This leads us to believe that a majority of the time if we go short when the LSM Factor is low and long when the LSM Factor is high we will turn a profit.


It’s helpful prior to backtesting to establish strict rules for trade entry/exit. Without this ruleset changing hyperparameters and rerunning backtests may result in bias or overfitting to historic data.

Trade Entry/Exit Rules

  • Positions are opened and closed daily, no position is to be held overnight
  • Only signals in the first and fourth quartile are to be traded
  • First quartile signals imply a short position is to be taken at market open
  • Fourth quartile signals imply a long position is to be taken at market open
  • Daily long-only open-close TSLA returns are used as a benchmark
  • 100% of $10,000 is to be traded for the duration of the backtest
  • Sharpe and Sortino Ratios will be used as a measure of risk-return

First, let’s create our baseline return…

Long-Only Open-Close TSLA Backtest

Mean Daily Return: -0.75%Daily Return Std: 3.76%Trades: 33Minimum Portfolio Value: $7301.72Maximum Portfolio Value: $10386.09Ending Portfolio Value: $7626.26Max Peak-Trough Drawdown: -29.70%

LSM Factor Open-Close TSLA Backtest

Mean Daily Return: 1.82%Daily Return Std: 2.89%Trades: 17Minimum Portfolio Value: $9740.60Maximum Portfolio Value: $14149.87Ending Portfolio Value: $13484.67Sharpe Ratio: 0.89Sortino Ratio: 1.20Max Peak-Trough Drawdown: -4.70%


The backtest results are far from optimal, the Sharpe and Sortino ratio need to double for any chance of implementation in a live trading environment. As discussed in my paper there are several routes to improve this signal including the aforementioned latent semantic analysis of documents to optimize the prescribed set of words in the highly flexible scoring of the Lexicon Sensitivity Model. Even so, this article has walked through deriving a trading signal from research question and model construction to backtesting analysis.

Should you be interested in similar content or techniques used to construct trading signals I will leave some resources below along with a link to my GitHub…


Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: