Building and Validating Simple Stock Trading Algorithms Using Python (2024)

Introduction

Algorithmic trading is a widely adopted trading strategy that has revolutionized the way people trade stocks. More and more people are making money on the side by investing in stocks and automating their trading strategies. This tutorial will teach you how to build stock trading algorithms using primitive technical indicators like MACD, SMA, EMA, etc., and select the best strategies based on their actual performance/returns, completely using Python.

Learning Objectives

  • Get to know what algorithmic trading is.
  • Build simple stock trading algorithms in Python, using technical indicators to generate buy and sell signals.
  • Learn to implement trading strategies and automate them in Python.
  • Learn to compare and select the best trading strategy based on their average returns.

This article was published as a part of the Data Science Blogathon.

Disclaimer – This isnot financial advice, and all work done in this project is for educational purposes only.

Table of Contents

  • What is Algorithmic Trading?
  • How to Use Python Algorithms for Stock Trading Analysis?
    • Selecting the Dataset
    • Installing the Required Libraries
    • Extracting the Historical Stock Prices Using yfinance
    • Building Stock Trading Algorithms Using Technical Indicators
  • Understanding the Performance of the Signals
  • Frequently Asked Questions

What is Algorithmic Trading?

Algorithmic trading is a method of trading financial assets using automated computer programs to execute orders based on predefined rules and strategies. It involves the use of several trading strategies, including statistical arbitrage, trend following, and mean reversion to name a few.

There are many different types of algorithmic trading. One of them is high-frequency trading, which involves executing trades at high speeds with almost no latency to take advantage of small price movements. Another is news-based trading, which involves making trades based on news and other market events.

In this article, we will be using Python to do stock trading based on technical indicators and candlestick pattern detection.

How to Use Python Algorithms for Stock Trading Analysis?

We can analyze the stock market, figure out trends, develop trading strategies, and set up signals to automate stock trading – all using Python! The process of algorithmic trading using Python involves a few steps such as selecting the database, installing certain libraries, and historical data extraction. Let us now delve into each of these steps and learn to build simple stock trading algorithms.

Selecting the Dataset

There are thousands of publicly traded stocks and we can consider any set of stocks for building the algorithm. However, it is always a good option to consider similar kinds of stocks as their fundamentals and technicals will be comparable.

In this article, we will be considering the Nifty 50 stocks. Nifty 50 index consists of 50 of the top companies in India chosen based on various factors such as market capitalization, liquidity, sector representation, and financial performance. The index is also widely used as a benchmark to measure the performance of the Indian stock market and as a result, there is a lesser amount of risk involved while investing in these companies when compared to investing in small-cap or mid-cap companies. I will consider WIPROfor performing the analysis in this article. The analysis approach discussed in this article can be performed on any set of similar stocks by calling the functions for each stock in a for loop.

Installing the Required Libraries

We will use the default libraries like pandas, numpy, matplotlib along with yfinance and pandas_ta. The yfinance library will be used to extract the historical stock prices. The pandas_ta library will be used for implementing the SMA and the MACD indicator and building the trading algorithm. These modules can be directly installed using pip like any other Python library. Let’s import the modules after installing them.

!pip install yfinance!pip install pandas-ta
import yfinance as yfimport pandas as pdimport pandas_ta as taimport numpy as npfrom datetime import datetime as dtimport matplotlib.pyplot as pltfrom datetime import timedelta as deltaimport numpy as npimport osimport seaborn as sb

Now that we have installed and imported all the required libraries, let’s get our hands dirty and start building the strategies.

Extracting the Historical Stock Prices Using yfinance

We will use the “download()” function from the yfinance module to extract the historical price of a stock, which accepts the stock’s picker, start date, and end date. For the sake of simplicity, we will take “2000-01-01” as the start date and the current date as the end date. We will write a simple function that extracts the historical stock prices and returns it as a data frame for processing further while saving it as a CSV file on our disk.

def get_stock_info(stock, save_to_disk=False): start_date = '2000-01-01' end_date = (dt.now() + delta(1)).strftime('%Y-%m-%d') df = yf.download(f"{stock}.NS", period='1d', start=start_date, end=end_date, progress=False) if(save_to_disk == True): path = './csv' try: os.mkdir(path) except OSError as error: pass df.to_csv(f'{path}/{stock}.csv') return dfdf = get_stock_info('WIPRO', save_to_disk = True)

Building Stock Trading Algorithms Using Technical Indicators

There are numerous indicators available for performing stock trading but we will be using one of the two simplest yet extremely popular indicators namely, SMA and MACD. SMA stands for Simple Moving Average while MACD stands for Moving Average Convergence Divergence. In case you are not familiar with these terms, you can learn more about them in this article. In short, we will try to find the SMA crossover and MACD crossover as trade signals and try to find the best combination of the lot for maximized returns.

For the SMA crossover, we will take the 10-day, 30-day, 50-day, and 200-day moving averages into account. For the MACD crossover, we will take the 12-day, 26-day, and 9-day exponential moving averages into account. Let’s calculate these values using the pandas_ta library.

For calculating the SMA, we will use the “sma()” function by passing the adjusted close price of the stock along with the number of days. For calculating the MACD, we will use the “macd()” function by passing the adjusted close price of the stock and setting the fast, slow, and signal parameters as 12, 26, and 9 respectively. The SMA and MACD values don’t make a lot of sense as such. So, let’s encode them to understand if there are any crossovers.

In the case of SMA, we will take 3 conditions:

  • The 10-day SMA should be above the 30-day SMA.
  • The 10-day and 30-day SMA should be above the 50-day SMA.
  • The 10-day, 30-day, and 50-day should be above the 200-day SMA.

In the case of MACD, we will have 2 conditions:

  • The MACD should be above the MACD signal.
  • The MACD should be greater than 0.

The Python code given below creates a function to implement the conditions mentioned above.

def add_signal_indicators(df): df['SMA_10'] = ta.sma(df['Adj Close'],length=10) df['SMA_30'] = ta.sma(df['Adj Close'],length=30) df['SMA_50'] = ta.sma(df['Adj Close'],length=50) df['SMA_200'] = ta.sma(df['Adj Close'],length=200) macd = ta.macd(df['Adj Close'], fast=12, slow=26, signal=9) df['MACD'] = macd['MACD_12_26_9'] df['MACD_signal'] = macd['MACDs_12_26_9'] df['MACD_hist'] = macd['MACDh_12_26_9'] df['10_cross_30'] = np.where(df['SMA_10'] > df['SMA_30'], 1, 0) df['MACD_Signal_MACD'] = np.where(df['MACD_signal'] < df['MACD'], 1, 0) df['MACD_lim'] = np.where(df['MACD']>0, 1, 0) df['abv_50'] = np.where((df['SMA_30']>df['SMA_50']) &(df['SMA_10']>df['SMA_50']), 1, 0) df['abv_200'] = np.where((df['SMA_30']>df['SMA_200']) &(df['SMA_10']>df['SMA_200'])&(df['SMA_50']>df['SMA_200']), 1, 0) return dfdf = add_signal_indicators(df)

Now that we have all the signals added to our data, it is time to calculate the returns. The returns will be the most important aspect for selecting the best trading strategy amongst the lot. We will calculate the 5-day and 10-day returns of the stock. We will also label encode the returns as 0 and 1 with 0 indicating negative returns and 1 indicating positive returns. Let’s go ahead and create the function implementing the same.

def calculate_returns(df): df['5D_returns'] = (df['Adj Close'].shift(-5)-df['Adj Close'])/df['Close']*100 df['10D_returns'] = (df['Adj Close'].shift(-10)-df['Adj Close'])/df['Close']*100 df['5D_positive'] = np.where(df['5D_returns']>0, 1, 0) df['10D_positive'] = np.where(df['10D_returns']>0, 1, 0) return df.dropna()df = calculate_returns(df) 

Understanding the Performance of the Signals

We can take all the conditions mentioned above and perform a simple aggregate to calculate the average and the median returns we can expect while trading based on these signals. We can also extract the minimum and the maximum returns each signal has generated in the past. This will not only give us a rough understanding of how good the signals are but also an idea of how much returns can be expected while trading using these signals. Let’s write a simple code to do the same using Python.

def get_eda_and_deepdive(df): eda = df.dropna().groupby(['10_cross_30', 'MACD_Signal_MACD', 'MACD_lim', 'abv_50', 'abv_200'])[['5D_returns', '10D_returns']]\ .agg(['count', 'mean','median', 'min', 'max']) deepdive = df.dropna().groupby(['10_cross_30', 'MACD_Signal_MACD', 'MACD_lim', 'abv_50', 'abv_200','5D_positive', '10D_positive'])[['5D_returns', '10D_returns']]\ .agg(['count', 'mean','median', 'min', 'max']) return eda, deepdive eda, deepdive = get_eda_and_deepdive(df)
Building and Validating Simple Stock Trading Algorithms Using Python (1)

Let’s visualize the box whiskers plot for the top 10 signal combinations sorted based on the 5-day and the 10-day returns.

x = df.copy()def _fun(x): code = '' for i in x.keys(): code += str(x[i]) return codex['signal'] = x[['10_cross_30', 'MACD_Signal_MACD', 'MACD_lim', 'abv_50', 'abv_200', '5D_positive', '10D_positive']].apply(_fun, axis=1)x = x.dropna()lim = x.groupby(['10_cross_30', 'MACD_Signal_MACD', 'MACD_lim', 'abv_50', 'abv_200', '5D_positive', '10D_positive'])['5D_returns'].agg(['mean']).reset_index()lim = lim.sort_values(by='mean', ascending=False).head(10)x = x.merge(lim, on=['10_cross_30', 'MACD_Signal_MACD', 'MACD_lim', 'abv_50', 'abv_200', '5D_positive', '10D_positive'], how='inner')
ax = sb.boxplot(x='signal', y='5D_returns', data=x)ax.set_xticklabels(ax.get_xticklabels(), rotation=45)plt.show()
Building and Validating Simple Stock Trading Algorithms Using Python (2)
ax = sb.boxplot(x='signal', y='10D_returns', data=x)ax.set_xticklabels(ax.get_xticklabels(), rotation=45)plt.show()
Building and Validating Simple Stock Trading Algorithms Using Python (3)

Taking only the 5-day and 10-day returns for selecting the best signals is not the best approach because we will never know how many times the signal has given positive returns against negative returns. This approach could be taken into account while selecting the best strategies, which could potentially improve the performance of the strategies. I will not take this approach to keep this article simple and beginner-friendly.

Conclusion

The concept of algorithmic trading can be extremely tempting for many as it can be very lucrative but at the same time, it tends to be an extremely complex and tedious task to build stock trading algorithms from scratch. It is very crucial to understand the fact that all algorithms can fail, which could potentially lead to huge financial losses when deployed to a live trading environment. The aim of this article was to explore how simple trading algorithms can be built and validated using Python. To proceed further in this project, you may consider other technical indicators and candlestick patterns, and use them interchangeably to build more complex algorithms and strategies.

Key Takeaways

  • In this article, we learned to extract historical stock prices using yfinance.
  • We learned to calculate MACD and SMA values based on the stock price and build Python algorithms to create signals/strategies using these values.
  • We also learned to calculate and visualize the 5-day and 10-day returns of the strategies on the stock.

Note: This is not financial advice, and all work done in this project is for educational purposes only. That’s it for this article. Hope you enjoyed reading this article and learned something new. Thanks for reading and happy learning!

Frequently Asked Questions

Q1. Will stock trading strategies discussed in the article always give positive returns?

Ans. No, all stock trading strategies are bound to fail and can lead to capital loss. The strategies used in this article are for educational purposes only. Do not use them to make financial investments.

Q2.Can we use ML/DL to forecast stock prices based on the signals discussed in the article?

Ans.Yes, these signals can be considered feature-engineered variables and can be used for performing machine learning or deep learning.

Q3.Why can’t we randomly select a set of stocks and perform the analysis?

Ans.It is important to select similar stocks as their fundamentals and other parameters like alpha/beta will be comparable. The alpha and beta values of a large-cap company and a small-cap company might be in different ranges. Hence, it might not be right to randomly mix them while performing this analysis.

Q4.What are some of the commonly used indicators for generating stock trading signals?

Ans.There are hundreds of indicators available and widely used for trading like RSI, MACD, and SMA. There are numerous technical candlestick indicators available as well like HARAMICROSS, MORNINGSTAR, and HAMMER that can help generate the signals.

Q5.Can this analysis be performed for trading commodities or cryptocurrencies apart from stocks?

Ans.This analysis can be performed for commodities with vast historical data. But in the case of cryptocurrencies, it depends on how much historical data is truly available. The analysis might fail or give a wrong conclusion if the signals occur a very times in the entire historical data.

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

AlgorithmblogathonData ExtractionfinanceGuidehistorical datalibraryMACDprojectpythonSMAstock marketstock tradingtrading strategyyfinance

T

TK Kaushik Jegannathan17 Oct 2023

BeginnerMachine LearningPythonStock Trading

Building and Validating Simple Stock Trading Algorithms Using Python (2024)

FAQs

Is Python enough for algo trading? ›

In general, Python is more commonly used in algo trading due to its versatility and ease of use, as well as its extensive community and library support. However, some traders may prefer R for its advanced statistical analysis capabilities and built-in functions.

How to build a trading algorithm in Python? ›

Creating an Algorithmic Trading Plan in Python: A Trend Trading...
  1. Sharing Knowledge: An Invitation to Learn. ...
  2. Why Moving Averages? ...
  3. Step 1: Import Necessary Libraries.
  4. Step 2: Fetch Historical Data.
  5. Step 3: Calculate Moving Averages.
  6. Step 4: Generate Buy and Sell Signals.
  7. Step 5: Visualization.
Jan 16, 2024

Can Python be used for stock trading? ›

We can analyze the stock market, figure out trends, develop trading strategies, and set up signals to automate stock trading – all using Python! The process of algorithmic trading using Python involves a few steps such as selecting the database, installing certain libraries, and historical data extraction.

What is the best Python framework for algo trading? ›

Algorithmic trading frameworks for Python
  • AlphaPy. ...
  • bt. ...
  • AlphaLens. ...
  • PyFolio. ...
  • PyAlgoTrade. ...
  • LEAN. ...
  • FreqTrade. Freqtrade is a free and open source crypto trading bot written in Python. ...
  • Gekko. Gekko is no longer maintainer.

How long does it take to learn Python for trading? ›

The duration to learn Python for finance ranges from one week to several months, depending on the depth of the course and your prior knowledge of Python programming and data science. Learning Python for finance requires a solid foundation in Python programming basics and an understanding of data science.

Can you beat algo trading? ›

There is no way that algo trading can excel manual trading. There is only one way to beat algo trading that is discipline. you limit yourself to limited number of trading.

Can I create my own trading algorithm? ›

If you choose to create an algorithm be aware of how time, financial and market constraints may affect your strategy, and plan accordingly. Turn a current strategy into a rule-based one, which can be more easily programed, or select a quantitative method that has already been tested and researched.

Which algorithm is best for trading? ›

Top Five Algo Trading Strategies of 2024
  1. Trends and Momentum Following Strategy. This is one of the most common and best algo strategy for intraday trading. ...
  2. Arbitrage Trading Strategy. ...
  3. Mean Reversion Strategy. ...
  4. Weighted Average Price Strategy. ...
  5. Statistical Arbitrage Strategy.
Jan 16, 2024

What is an example of a simple trading algorithm? ›

Examples of Simple Trading Algorithms

Buy 100,000 shares of Apple (AAPL) if the price falls below 200. For every 0.1% increase in price beyond 200, buy 1,000 shares. For every 0.1% decrease in price below 200, sell 1,000 shares.

Which programming language is best for stock trading? ›

Different Languages for Different Trading? Speed is of the essence in sell-side trading, so the programming languages like C++ and Java are the best fit in these cases. However, Python is the preferred language for most quantitative traders because of the availability of packages specifically for data analysis.

Can you day trade with Python? ›

Introduction to Day Trading and Machine Learning

Python, with its robust libraries like Pandas, NumPy, and Scikit-learn, is an ideal choice for developing ML models and processing financial data. In the realm of finance, ML models are often trained on historical data to forecast future market behaviors.

Is it legal to use AI for stocks? ›

Algorithmic trading is now legal; it's just that investment firms and stock market traders are responsible for ensuring that AI is used and following the compliance rules and regulations.

Who is the most successful algo trader? ›

He built mathematical models to beat the market. He is none other than Jim Simons. Even back in the 1980's when computers were not much popular, he was able to develop his own algorithms that can make tremendous returns. From 1988 to till date, not even a single year Renaissance Tech generated negative returns.

How to make algo trading software in Python? ›

5 Steps to Create an Algorithmic Trading App
  1. Step 1: Create Algorithmic Trading Platforms. ...
  2. Step 2: Construct a Trading Algorithm Approach. ...
  3. Step 3: Define the Timeframe and Frequency of Trade. ...
  4. Step 4: Evaluate the Trading Algorithm Using Prior Data. ...
  5. Step 5: Connect the Algorithm to the Demo Trading Account before the Live.
Feb 23, 2024

Is Python good for trading bot? ›

Python is a popular choice for developing trading bots, thanks to its simplicity and extensive libraries like Pandas, NumPy and SciPy. These libraries enable efficient data analysis, making Python a preferred language for data-driven trading strategies.

Is Python too slow for trading? ›

Is Python fast enough for trading? Although slower than other programming languages such as Java, C++, or C#, it is more than fast enough for most trading applications. The fact that most automated trading strategies are nowadays implemented in Python is a testament to its suitability.

Is Python good for algorithms? ›

Yes, Python is a powerful programming language that handles all aspects of algorithms very well. Python is one of the most powerful, yet accessible, programming languages in existence, and it's very good for implementing algorithms.

Is it possible to right a crypto trading algorithm in Python to make money? ›

Luckily, with a bit of Python, you can automate trading decisions for you by implementing a trading strategy. In this Guided Project, you will take a first dive into the world of algorithmic trading by implementing a simple strategy and testing its performance.

Top Articles
Latest Posts
Article information

Author: Rubie Ullrich

Last Updated:

Views: 6234

Rating: 4.1 / 5 (72 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Rubie Ullrich

Birthday: 1998-02-02

Address: 743 Stoltenberg Center, Genovevaville, NJ 59925-3119

Phone: +2202978377583

Job: Administration Engineer

Hobby: Surfing, Sailing, Listening to music, Web surfing, Kitesurfing, Geocaching, Backpacking

Introduction: My name is Rubie Ullrich, I am a enthusiastic, perfect, tender, vivacious, talented, famous, delightful person who loves writing and wants to share my knowledge and understanding with you.