Quantifying and Automating with Python

Peaceful Tradevesting using Python

Ankit Rathi
3 min readAug 18, 2024

In my approach, called Tradevesting, I combine the principles of both investing and trading. From an investor’s mindset, I focus on the fundamentals of a company, looking at key metrics such as:

  • Sales and profits
  • PE (Price to Earnings) ratio
  • PB (Price to Book) ratio
  • ROE (Return on Equity)
  • ROCE (Return on Capital Employed)
  • Debt to Equity ratio

These fundamentals help me decide whether a company is worth investing in for the long term. However, from a trader’s mindset, I also pay attention to the technicals, such as:

  • Candlestick patterns (daily and weekly)
  • Moving averages (e.g., 50-day and 200-day)
  • RSI (Relative Strength Index)

The goal is to find companies with strong fundamentals, but also to enter and exit trades at the right time using technical analysis. This blend of both approaches helps maximize returns while managing risk.

To make this process efficient, I use Python to turn these qualitative ideas into numbers, which can then be analyzed automatically.

Quantifying Fundamental Analysis:

  • I use Python to gather financial data like sales, profits, PE ratios, ROE, and more. A library like yfinance can pull this data from sources like Yahoo Finance.
  • Once the data is pulled, I use Pandas to structure it into tables (similar to Excel). This allows me to easily calculate averages, growth rates, or other key ratios. For example, I can compare the PE ratio of a stock against a predefined benchmark, making it easy to quantify if a stock meets my criteria.

Quantifying Technical Analysis:

  • Python helps calculate technical indicators like moving averages, RSI, and candlestick patterns. With the help of the TA-Lib library, I can automatically generate these indicators from stock price data.
  • These indicators are quantified, meaning I can set up rules like “Buy when the 50-day moving average crosses above the 200-day moving average” and have Python detect this automatically for a list of stocks.

After quantifying everything, the next step is to automate it so I can focus more on decision-making and less on manual analysis.

Fundamental Automation:

  • I automate the process of gathering financial data regularly (e.g., daily or weekly) using Python scripts. This means I always have up-to-date data on sales, profits, and ratios without having to manually pull it.
  • Once the data is fetched, Python applies my fundamental rules (like filtering stocks with ROE above a certain percentage). If a stock meets all my criteria, it gets flagged for potential investment.

Technical Automation:

  • I automate the generation of technical indicators using TA-Lib. Python automatically scans my stock list and checks for specific patterns or conditions (e.g., RSI crossing below 30 for a buying signal).
  • To make it more visual, I use Matplotlib to create charts showing stock price trends and indicators. These charts help me see at a glance if a stock is trending in the right direction, based on my trading rules.

End-to-End Automation:

With Python, I’ve built a system that regularly scans stocks, checks both the fundamentals and technicals, and alerts me when a stock is worth looking at. This cuts down on manual effort and helps me make quicker, more informed decisions.

By combining both quantification and automation, my Tradevesting approach becomes more efficient and reliable. Python handles the heavy lifting of data analysis and processing, allowing me to focus on refining strategies and making the final decisions.

You can access my GitHub repository on Tradevesting here, which consists of more than a dozen python notebooks.

GitHub Repo: https://github.com/ankit-rathi/Tradevesting

If you loved this story, please feel free to check my other articles on this topic here: https://ankit-rathi.github.io/tradevesting/

Ankit Rathi is a data techie and weekend tradevestor. His interest lies primarily in building end-to-end data applications/products and making money in stock market using Tradevesting methodology.

--

--