AI-based Algorithmic Trading
This article overviews two projects I worked on during my Masters in Computer Science and Artificial Intelligence at University of Nottingham, 2024. The first project titled “Quant Portfolio Management System” deals with creation of AI-agents based trading systems. The second, my dissertation research study titled “Explainable Time Series” deals with inducing explainable AI (XAI) analysis to understand these black-box AI Agents.
The Quant Portfolio Management System deploys 12 AI agents - among which 2 agents earn a profit of Rs 88,000 (35% return on Rs 252,000) within 10 days of trading in the Indian Markets.
Quant Portfolio Management System
The story begins when Mr. Harilal (a fictional character) is troubled with his portfolio of equity shares in the Indian Stock Market. The Quant PMS is developed to trade daily on his behalf.
The portfolio held spans across various industries from IT Services, Banking & Finance, FMCG, Chemicals, Energy, Insurance, Textiles, Pharmacy, Automobile, Telecommunications and Airlines. In the world of finance, it is considered good to hold a portfolio which is well-diversified.
Agent-Based Structure
There are three classes of agents involved in this Quant PMS namely, (1) Forecasting agents (2) Portfolio Optimization agents and (3) Trading Agent.
Forecasting Agents | Portfolio Optimization Agents | Trading Agent | |
---|---|---|---|
Decision Making | What is tomorrow’s Closing price? | How much money to invest in each stock? | When to buy/sell? |
Modus Operandi | Analyze historical prices & forecast using time series. | Analyze historical data and forecast to determine % investment using portfolio optimisation methods. | Analyze price movements & determine when to buy/sell. |
Forecasting Agents
The aim here is to predict tomorrow’s closing price. For this task, we rely on Univariate Time Series Forecasting models using four models - Seasonal Naive
, SARIMA
, Support Vector Machine (SVM)
and Elman Neural Network (RNN)
.
The study “Explainable Time Series” extends this analysis to Multivariate Time Series forecasting.
The SNaive & SARIMA forecasters have the lowest overall MAPE, followed by SVM & Elman Network. However, the overall profitability relies on the interaction between forecasters and portfolio optimizer agents.
Portfolio Optimization (PO) Agents
The aim here is to determine how much money should be invested in each stock. For this task, we leverage four optimization strategies - Naive, Minimum Variance, Max Sharpe Ratio & Risk Parity Portfolio.
The Quant PMS uses Monte-Carlo simulation based approach - simulating 50,000 portfolios everyday with random investments. From these simulated portfolios, we pick the ones with minimum variance and maximum Sharpe ratio respectively.
The Efficient Frontier above visualizes these 50,000 portfolios and the ones we are interested are highlighted as well.
Trading Agent
After forecasting & optimizing, we can go ahead and devise a trading strategy. For this project, the trading decisions are implemented in simulation - which closely resembles the real world.
The trading agent operates on a simple stop-loss validation rule : “If the price increases by 2% from the opening price, we sell the share and vice-versa.” Otherwise, there is no trade.
Evaluation
After 10 days of trading in the Indian Markets, we can now evaluate how these 16 agents have performed. The best performing agent earns a profit of Rs 88,000 on an initial investment of Rs 252,000 (a 35% return).
Total Portfolio Value
The overall portfolio value fluctuates for each forecaster family and their respective optimizer. The highest value as at 9th May 2024 stands at Rs 319,000 (at the end of 10 trading days) which accounts for a 26.5% growth.
Unrealized Profit/Loss
The profit/loss on 9th May is unrealized, meaning that Mr. Harilal will incur this profit/loss if he decides to sell all his shares.
Profit/Loss = Portfolio Value today - transaction amount - Portfolio value yesterday
Here transaction amount is the net additional funds spent or released due to buying/selling activity during the day.
The Naive and SARIMA families coupled with Max Sharpe ratio optimizer yield the highest profits of Rs 88,000
. The SVM & Elman families with Minimum Variance optimizer agents are runner ups. While the SARIMA family yields the highest profit with Max Sharpe Ratio optimizer, it also yields the highest loss with Minimum Variance optimizer. This indicates that the SARIMA forecaster does not sit well with the Minimum Variance optimizer. Let’s evaluate the Portfolio optimizer agents below.
Buy/sell tendency
Let’s look at the daily net transactions for each family of agents. The idea here is to spot if there is any bias in the agents to just buy or sell shares.
The Naive optimizer agents do not participate in the market. It invests equal amounts and holds the shares for all 10 days. The Risk parity optimizer’s participation frequency is low as it operates on annualized covariance which varies very less on a daily basis. Otherwise, all agents have a similar buying/selling tendency over 10 days.
Sharpe Ratio Analysis
Sharpe Ratio measures the excess return per units of risk. A Sharpe ratio of 0.05 implies that for every additional unit of risk, our return goes up by 0.05 units.
For Naive optimizer agents, across all forecasting families, the Sharpe ratios are consistently negative indicating that this strategy under-performs the risk free rate. Meaning that we are better off investing in risk free assets than following this strategy.
For Min Var PO Agent, the SR values have a lot of variation indicating that this agent is sensitive to the choice of forecasting agent. Here, the SARIMA forecaster has the best risk-adjusted returns.
For Max SR PO Agent, there is a similar variation. Here, the Elman forecaster performs best, which may be due to its ability to capture underlying non-linear relationships.
For Risk Parity PO Agent, the SR values are high across all forecasting families, indicating that this strategy is effectively balancing the risk across different assets and is less sensitive to forecasting errors or variability. Here, the SVM forecaster performs the best.
If Mr. Harilal is a risk adverse investor (people who don’t like risk) then the Risk Parity PO Agent becomes a suitable strategy.
Sortino Ratio Analysis
Sortino ratio focuses on the downside volatility rather than the total standard deviation of returns, offering a more detailed insight on risk-adjusted performance regarding negative returns.
For Naïve PO Agent, the ratios are consistently negative across all forecasting families. This is clearly not a good strategy.
For Min Var PO Agent, the ratios vary a lot, like the variation in Sharpe ratios indicating sensitivity to forecasting agents. The SARIMA forecaster performs well, indicating its ability to capture and predict market downswings, allowing Min Var PO Agent to adjust weights minimizing downside exposure effectively.
For Max SR PO Agent, we see a significant performance with the Elman forecaster suggesting that it might be capturing beneficial asymmetrical risk-return characteristics, possibly due to Elman’s better handling of non-linear patterns which are pertinent in calculating downside risk.
For Risk Parity PO Agent, we see exceptionally high Sortino ratios across all models, indicating that it manages downside risk robustly.
This also confirms that if Mr. Harilal is risk-averse, we lean more towards agents with Risk parity portfolio optimization. If Mr. Harilal is risk-loving (a person who is more profit focused and less caring about risk) then we lean towards agents with higher profits like “SARIMA + Max SR PO”.
Explainable Time Series
This study is an extension to the “Quant Portfolio Management System” with the idea to inject Explainable AI (XAI) analysis to uncover the black-box nature of these AI agents. Now we are dealing with forecasting stock prices for 5 Indian companies. In doing so, we seek to answer three research questions -
- Stock Price forecasting trade off between Accuracy vs Explainability - To make accurate predictions, we want strong forecasting models. This is where Machine Learning & AI come into play. They can capture more complex patterns than most of the traditional models.
- What is XAI & it’s importance - Now as these models become better, more complex, we lose out on understanding how they operate. This is why we need Explainable AI. It helps us build reliability & trust on these models.
- How to use XAI on forecasts & what insights can we derive for stakeholders?
Workflow
Data Preparation Stage - We start by fetching historical stock prices from Yahoo Finance for the last 2 years. Perform Feature engineering to create about 33 input features capturing trend, volume, volatility, economic and market sentiments. From these, top 10 most important features are selected using a Random forest model.
Time Series Forecasting Stage - We can now go ahead with forecasting tomorrow’s closing price. Forecasting models span across different levels of complexity from Naive, Statistical to Machine and Deep Learning - allowing us to explore the accuracy vs explainability trade-off.
Time Series XAI Stage - After models are trained and forecasts are ready, we move towards XAI Analysis. Global XAI methods help us understand the overall model behavior and Local ones explain a single prediction. These insights are evaluated, both quantitatively & qualitatively.
Forecasting Power vs Interpretability Trade-off
This abstract representation outlines how gaining forecasting power often leads to fall in interpretability. The SNaive and ARIMA models are fairly simple and easy to interpret. As we move towards more complex XGBoost and Neural Network structures, we face difficulties tracking how they work. However, developments of XAI techniques are changing these narratives.
Forecasting Performance
The Elman Neural Network has the best performance over a span of 60 test days for all 5 companies.
Explainable AI Analysis
An R shiny dashboard was developed to show the forecasting and XAI analysis graphs.
On comparing the SHAP Summary plots for XGBoost, Elman, LSTM & GRU we understand that -
XGBoost prioritizes stable and immediate predictors like Lag 1 and Open – making it more robust & stable.
Elman network is more adaptable, the feature importance shifts based on changing market conditions.
LSTM excels at capturing long term dependencies by focusing on RSI & MACD variables.
Finally, the GRU model shows more sensitivity to more recent market shifts.
Feature Importance plots reveal which input features play a vital role for the model to make predictions. These importance scores are generally in line with the SHAP analysis which acts as a qualitative assessment.
The next method, called “Prediction Rule Ensemble” gives us a set of rules summarizing how the models behave. These rules capture the behavior of individual variables and their interactions.
Rule 110: RSI > 56.68, then price +9
Rule 259: COD ≤ - 29 and ATR > 53, then price +10
Rule 49: Nifty50 ≤ 22570 and MACD > -0.4, then price +2.5
On comparing these prediction rules we understand that
XGBoost manages direct interactions and thresholds efficiently.
Elman offers a more balanced and precise understanding of momentum & volatility.
LSTM captures broader temporal patterns.
GRU responds adeptly to immediate market shifts.
LIME & SHAP giving us a break-down at the level of an individual predictions. In this case, I noticed that the LIME model does a poor job – showing that a momentum variable having high values leads to a fall in price – which does not make sense. To overcome this, we rely on the SHAP analysis.
For more complex models like LSTM/GRU we have SHAP Sequence, Saliency & Attention Mechanism. They tells us how inputs are processed by these complex models – which ones play an important roles at what time step.
To assess the reliability of SHAP & Saliency, we introduce noise in the data and recalculate these values. This increase in errors implies that the SHAP & Saliency values are reliable.
Notable Contributions
As the value of Input Features increase, the LIME Model’s Explanation power (R2) decreases.
This study proposes the “LIME Hypothesis” which states that as values of input features increases, the LIME model’s explanation power, measured by R-square, decreases.
To investigate the hypothesis, we analyse how well the LIME model fits (via the scatterplots) and the trend in R-square values across input features. For this company, Reliance, we can see a poor performance over the test data and a downward trend in R-square values. Thus, the hypothesis holds!
However, when we perform the same analysis, for the 2nd company, the hypothesis does not hold! We see a poor fit on the test data but an increasing trend in R-square values.
To investigate further, let’s look at the train-test density distributions. For Reliance, we see that there the test data is very skewed, compared to the training data – for most variables. But in case of Unilever, there is a lot of overlap. As a result of which the hypothesis holds. We perform statistical tests to confirm if these differences are significant or not.
We have developed some XAI-based trading insights which can be used for traders and investors who are the final stakeholders. For example, In case of Reliance, during periods of high volatility, the Elman model heavily relies on DEMA & ATR to make accurate forecasts. So, traders can use these indicators to decide when to buy/sell the shares. In case of IFB Industries, traders should monitor DEMA for short-term price movements. Volume of trade is important. During high trading activity, there are significant price changes as captured by OBV & RVOL technical indicators.
In R, the way Elman Network is implemented, it does not offer compatibility with XAI Methods. Thus custom code development was essential. This code is available on github : https://github.com/ooo-kunaal/TimeSeries_XAI
Summary
This article delves into two innovative projects undertaken during a Master’s program in Computer Science and Artificial Intelligence at the University of Nottingham. The first, titled “Quant Portfolio Management System (PMS),” develops an AI-driven trading framework for a diversified equity portfolio in the Indian Stock Market. It leverages agent-based structures, including forecasting, portfolio optimization, and trading agents, to automate trading decisions. Forecasting accuracy and profitability are achieved using advanced time series models like SARIMA, SVM, and Elman Neural Networks, alongside optimization strategies such as Minimum Variance and Max Sharpe Ratio. The system’s trading strategy, based on stop-loss rules, demonstrates profitability and aligns with risk-adjusted metrics like the Sharpe and Sortino ratios.
The second project, “Explainable Time Series,” extends the Quant PMS by incorporating Explainable AI (XAI) techniques to make AI-driven predictions transparent and trustworthy. Focusing on stock price forecasting for five Indian companies, the study evaluates the trade-offs between model accuracy and interpretability. It applies a range of XAI methods, including SHAP, LIME, Saliency Maps, and Attention Mechanisms, to decode the decision-making processes of advanced models like LSTM and GRU. The integration of these insights into trading strategies underscores the practical applicability of XAI in enhancing stakeholder confidence.
Through these projects, the research explores the intersection of AI and financial trading, offering a pathway to create transparent, accurate, and actionable AI-driven systems. The work emphasizes the importance of balancing forecasting performance with explainability, setting a foundation for future advancements in algorithmic trading and XAI methodologies.
Links
Explainable Time Series dashboard - https://psxkc5.shinyapps.io/TimeSeries_XAI/
XAI Time Series Github - https://github.com/ooo-kunaal/TimeSeries_XAI
Quant Portfolio Management System Youtube - https://youtu.be/gw_2zPcNdtQ