Use Your Computer to Make Informed Decisions in Stock Trading: Practical Introduction — Part 14: Exploring Tech Indicators For Stocks and Crypto

Ivan Brigida
9 min readApr 6, 2024
Sunny day at sea at the St.Patricks’s weekend

[HINT] You can read my previous articles on Medium or on the website PythonInvest . The complete Python code (Colab notebooks) can be found on Github.

Intro

In this article, we dive into technical indicators, revealing their power to forecast future returns for top stocks and cryptocurrencies. By tapping into data from the YFinance API, we explore how these indicators, spanning momentum and pattern recognition, offer practical insights for predicting market movements.

We pinpoint which indicators strongly correlate with future returns, shaping our predictive models. For instance, the Relative Strength Index (RSI) and Moving Average Convergence Divergence (MACD) often mirror subsequent price shifts, providing actionable insights for traders and investors.

Through decision trees, we uncover the key factors driving future trends, simplifying financial analysis. Historical returns and trading volume emerge as influential factors in forecasting price shifts, highlighting their importance in decision-making.

Executive Summary

This article provides a comprehensive examination of technical indicators’ predictive power in finance, particularly focusing on stocks and cryptocurrencies. By leveraging Python’s TA-Lib library, we demonstrate the straightforward generation of over 100 technical indicators. Through meticulous analysis, we unveil the most influential indicators for predicting market movements in both crypto and stock markets.

Moreover, we offer concrete examples of the most prevalent pattern indicators, such as CDLBelthold and CDLShortLine, highlighting their importance in identifying potential market reversals or continuations. This empirical approach equips traders and investors with actionable insights, enabling them to make informed decisions based on robust data analysis.

Summary of Results

01. Effortless Tech Indicator Generation
Utilizing Python’s TA-Lib library, we seamlessly generated over 100 technical indicators

02. Frequent Pattern Indicators
E.g. for BTC-USD are: CDLBelthold, CDLShortLine, CDLClosingMarubozu

03. Top Correlated With Future Returns Indicators
Commodity Channel Index (CCI), Chande Momentum Oscillator (CMO), and Relative Strength Index (RSI)

04. Top Predictive Indicators for 1hr movement (decision trees)
E.g.: Natr, Fastk

05. Top Predictive Indicators for 24hr movement (decision trees)
E.g.: Ad, obv, trix

Top Crypto and Stocks list

Firstly, we compiled lists of the top 25 most active trading stocks and the top 25 largest cryptocurrencies by market capitalization, followed by accessing hourly data for each ticker over the past two years using the YFinance API.

We extracted lists of stock tickers from the URL: https://finance.yahoo.com/most-active/?offset=0&count=100. While the former is commonly used for screening purposes for humans, we repurposed it to efficiently gather stock data. For cryptocurrencies, we referred to the link provided at https://finance.yahoo.com/crypto/.

Subsequently, leveraging the YFinance API, we retrieved hourly data for each ticker, spanning the preceding two years. It’s worth noting that accessing data beyond this timeframe (2 years for hourly data) may require subscription to paid data sources.

Here are the top 5 (out of 25) stocks and crypto tickers (data for 29th March 2024):

Fig. 1. Top US Stocks (by volume of trade) and Crypto tickers (by market cap)

Single Dataframe with Historical Returns

The process involved gathering hourly OHLCV data from YFinance over the past two years for all 25 top stocks and 25 top cryptocurrencies, which were then merged into a single dataframe.

Stats for each ticker were downloaded and combined into one dataframe for streamlined analysis.

Moreover, additional features such as historical returns for different time intervals (1, 2, 5, 10, and 24 hours back, as well as 3, 7, and 30 days back) were generated to capture potential predictors of future values, particularly in autoregression (AR) models.

Furthermore, future returns for the next 1 hour and binary indicators for positive future returns in 1 hour and 24 hours were calculated to discern the most influential factors in predicting future prices. Importantly, it’s noteworthy that the binary indicator “is_positive_future_growth_1h” (or “.._24h”) is evenly split between two classes, highlighting the importance of balanced consideration when assessing predictive models.
Subsequently, correlations and feature importance in simple decision tree models will be analyzed to identify influential predictors in forecasting future price movements.

Here you can check the resulting dataframe example with some of the new “growth” fields:

Fig. 2. Single OHLCV dataframe for all tickers with additional growth indicators
Fig. 3. Distribution of is_positive_growth_future_1h (across 2years of hourly data for 25 stocks and 25 crypto coins).

TA-Lib Library for Generating Technical Indicators

As the next step we explore the utilization of the TA-Lib library (https://github.com/TA-Lib/ta-lib-python) to generate technical indicators (full list of functions), enhancing our analysis of hourly OHLCV data dumps.

Initially, we utilize hourly OHLCV data as the base stats, laying the groundwork for our analysis. The TA-Lib library is then imported, enabling the precalculation of over 100 additional metrics or technical indicators crucial for strategy development and model building.

These indicators are grouped into four categories:

  1. Momentum (30+): Momentum indicators measure the speed and strength of price movements. Examples include the Relative Strength Index (RSI).
  2. Volume, Volatility, Cycle, Price (15): This category encompasses indicators related to volume, volatility, cyclical patterns, and price movements. Examples include Bollinger Bands.
  3. Pattern Recognition (60): These indicators identify specific patterns in price movements that may signal potential reversals or continuations of trends. Examples include candlestick patterns like Doji and Engulfing.
  4. Other: This category may include various other indicators like moving averages and trendlines.

Pattern Recognition Indicators

Let’s look more in detail into the pattern indicators, as there are so many of them.

Firstly, we apply all indicator functions (60) for one ticker (BTC-USD) and explore the distribution of values, total sum of indicators per hour, and reveal the top frequency pattern indicators such as CDLBelthold, CDLShortLine, CDLClosingMarubozu, CDLLongLine.

Here are the most popular indicators for 2 years of hourly BTC-USD (crypto) data (17k observations):

Fig. 4. Top Pattern Indicators for BTC-USD

As you can see — there is at least one pattern indicator matched in 77% of cases, and the most popular indicators match 10–20% cases. You can read about the Pattern names on this page: Github TA-Lib pattern indicators .

In the next step we show the descriptive statistics for the pattern indicators (10 showed out of 61):

Fig. 5. Descriptive Analysis for Pattern Indicators of BTC-USD

Most of the time, indicator value equals to 0 (“no match”), but sometimes it is +100 or -100 (“indicator was triggered”) — you can see this from the min, 25%, 50%, 75%, max fields.

Finally, we calculate the indicators for all tickers (25 cryptocurrencies and 25 stocks) and combine them into one dataframe. The resulting dataframe comprises approximately 500,000 records, with around 427,000 for cryptocurrencies and 87,000 for stocks. This comprehensive dataset serves as the basis for further analysis and model development.

Now, let’s explore pattern indicators across all records. We’re interested in understanding how many indicators can match for one ticker within a period.

The distribution of pattern indicators ranges from -1000 to +1000 per ticker, per period (hour), with most periods showing 0, 1, or 2 matches. However, some periods may have as many as 8 to 10 matched indicators:

Fig. 6. SUM of patterns matched across one ticker BTC-USD, one period of time (+100/-100 per match, 0 — no match)
Fig. 7. COUNT of patterns matched across one ticker BTC-USD, one match adds 1 to total

Analyzing Future Returns Correlation (next 1 and 24 hours predictions)

Following the generation of new features with indicators, our next step is to assess their correlation with future returns. Even approximate correlations provide valuable clues, guiding us towards relevant indicators to explore among the numerous new fields, facilitating feature return prediction.

As depicted in the table below, individual technical indicators, including but not limited to pattern indicators, exhibit loose correlations with returns, with absolute correlation values typically below 0.1. While not reaching the extremes of -1 or 1, these correlations still offer valuable insights for analysis.

Top Correlated Indicators:

  • For 1-hour future growth, notable negative correlations include Commodity Channel Index (CCI, -0.046), Williams %R (WillR, -0.045), and Stochastic Fast (Fast_k and Fast_d, -0.043), while positive correlations are observed with Aroon Indicator (Aroon_1, 0.02).
  • In the case of 24-hour future growth, leading negative correlations encompass Commodity Channel Index (CCI, -0.045), Chande Momentum Oscillator (CMO, -0.043), Relative Strength Index (RSI, -0.04), Williams %R (WillR, -0.04), and (not indicator) historic growth_24h (-0.04). Conversely, positive correlations stand out with Hilbert Transform — SineWave (HT_SINE_LEADSINE, 0.023) and Aroon Indicator (Aroon_1, 0.034).
Fig. 8. ONE hour future growth: strongest NEGATIVE correlations with tech indicators
Fig. 9. ONE hour future growth: strongest POSITIVE correlations with tech indicators
Fig. 10 TWENTY FOUR hours future growth: strongest NEGATIVE correlations with tech indicators
Fig. 11 TWENTY FOUR hours future growth: strongest POSITIVE correlations with tech indicators

Decision Trees for Explainability

Our next step involves moving beyond mere correlation analysis. We’ve constructed a simple predictive model, specifically a decision tree, to forecast future growth in 1 and 24 hours and ascertain feature importance. Additionally, we offer insights into the tree’s structure by showcasing the features involved in three steps of its path. While these steps may not precisely denote the most crucial features, they provide valuable insights into which features and in what sequence better segment the tree into subtrees.

The importance of features in the updated list surpasses simple correlations, as they are integrated into a statistical model, collectively influencing predictions rather than acting independently as in correlation analysis.

Top Indicators for 1-Hour Prediction:

  • (Normalized) Average True Range (Natr, 0.029)
  • Stochastic Fast (Fast_k and Fast_d, 0.026)
  • Growth_2h (0.026)
  • Growth_1h (0.025)
  • Growth_168h (0.025)

Ivan’s Comments: This aligns with intuition, as previous growth (growth_* indicators) typically holds significant importance. Here, three out of five indicators pertain to previous growth.

Detailed results in the tables below (ALL DATA, ONLY CRYPTO, ONLY STOCKS):

Fig 12. Top strength features to predict ONE hour growth

24-Hour Returns Prediction (Decision Tree):

  • Growth_720h (0.063)
  • The Accumulation/Distribution indicator (AD, 0.054)
  • On-Balance Volume (OBV, 0.05)
  • Growth_168h (0.05)
  • Triple Exponential Average (TRIX, 0.048)

Ivan’s Comments: Similarly, two out of five indicators relate to previous growth, while the remaining three (AD, OBV, TRIX) offer diverse insights.

Detailed results in the tables below (ALL DATA, ONLY CRYPTO, ONLY STOCKS):

Fig 13. Top strength features to predict TWENTY FOUR hours growth

Understanding how decision trees make predictions can seem complex, but breaking it down simplifies the process. Let’s take a peek into the decision-making of a decision tree, focusing on the top 2–3 levels.

Decision trees start at the root, where key features take center stage. These features, with their associated decision rules (like “feature > value”), are like signposts guiding the tree’s path. They’re chosen because they best split the data into distinct groups.

As we traverse down the tree, each level refines the prediction. It’s like a game of “20 Questions” where each question (feature) narrows down the possibilities until a guess is made.

By checking feature values at each level, the decision tree divides the data until it reaches the end, where predictions are made based on the majority in each group.

This simple breakdown helps demystify how decision trees work, offering a clearer picture of their predictive magic.

Here is an image of a tree, that predicts 24hrs returns on all data (STOCKS+CRYPTO):

Fig 14. Example of the top 3 levels of the decision tree (e.g. using rule “cmo≤-0.08” to move left or right)

Conclusion

In conclusion, our analysis has provided a straightforward method for generating a diverse set of technical indicators using the TA-Lib library in Python. By leveraging this powerful tool, traders and investors can effortlessly access over 100 indicators, facilitating comprehensive market analysis.

Additionally, our study has shed light on the significance of various technical indicators across different datasets, highlighting key predictors for market movements in both cryptocurrency and stock markets. From the identification of top predictive indicators such as the Relative Strength Index (RSI) and Moving Average Convergence Divergence (MACD) to the revelation of highly correlated indicators like the Commodity Channel Index (CCI) and Relative Strength Index (RSI), our findings offer actionable insights for informed decision-making.

Moreover, our examination of pattern indicators, particularly in BTC-USD data, has revealed common patterns such as CDLBelthold and CDLShortLine, providing valuable signals for traders seeking to capitalize on market trends.

By equipping traders and investors with a simple yet powerful method for generating technical indicators and providing insights into their predictive power, our study aims to empower market participants to make informed decisions and optimize their trading strategies for success in the dynamic financial landscape.

--

--

Ivan Brigida

Data and Product Analyst at Google. I run a website PythonInvest.com on Python programming, analytics, and stock investing.