Products
Platform
Research
Market
Learn
Partner
Support
IPO
Logo_light
Module 4
Coding and Deploying Algos
Course Index

Chapter 4 | 3 min read

A Sample Algo Trading Strategy in Python

Let’s build a simple intraday strategy:
Buy when RSI < 30 and Close > 20 EMA
Sell when RSI > 70 and Close < 20 EMA

We’ll use:

  • yfinance for data
  • ta for indicators
  • Python logic for strategy

This is for learning/demo only. Not for live trading — yet!

1. Install necessary libraries (only once): You can run this in Google Colab or your local Python setup.

!pip install yfinance ta

Note:

This command installs two Python libraries needed for market data analysis and technical indicator calculations:

  • yfinance – used to fetch historical and real-time market data directly from Yahoo Finance into Python.

  • ta – a Technical Analysis library that provides ready-to-use functions for computing indicators like RSI, EMA, MACD, Bollinger Bands, etc.

The ! at the start allows this to be run as a shell command inside Jupyter Notebook or Google Colab, so the packages are installed into your current environment before running the rest of your algo trading code.

2. Import the libraries

import yfinance as yf
import ta
import pandas as pd

Note:

This code imports three essential Python libraries for algo trading:

  • yfinance (yf) to fetch historical and real-time market data from Yahoo Finance.

  • ta to calculate technical indicators such as RSI, EMA, MACD, Bollinger Bands, and others.

  • pandas (pd) for storing, processing, and analyzing time-series market data in DataFrames.

Together, they allow you to download price data, calculate technical indicators, and manipulate the resulting dataset for strategy building and backtesting.

3. Get intraday price data

data = yf.download("RELIANCE.NS", period="5d", interval="15m")
data.dropna(inplace=True)

This gives you 15-minute candles of Reliance for the last 5 days. The securities quoted are exemplary and are not recommendatory

4. Calculate RSI and 20 EMA

data['rsi'] = ta.momentum.RSIIndicator(close=data['Close']).rsi()
data['ema20'] = ta.trend.EMAIndicator(close=data['Close'], 
window=20).ema_indicator()

Note:

  1. ta.momentum.RSIIndicator: Creates a Relative Strength Index (RSI) indicator object from the ta (technical analysis) library.

  2. close=data['Close']: Uses the 'Close' price column from your data DataFrame to calculate RSI.

  3. .rsi(): Computes the RSI values for each row (default period is usually 14 unless specified) and returns a Pandas Series.

  4. data['rsi'] = ...: Stores the resulting RSI values in a new column called 'rsi' in your DataFrame.

  5. ta.trend.EMAIndicator: Creates an Exponential Moving Average (EMA) indicator object.

  6. close=data['Close']: Again, uses the 'Close' price as the data source for calculation.

  7. window=20: Sets the EMA lookback period to 20 bars (e.g., 20 days for daily data).

  8. .ema_indicator(): Calculates the EMA values for the given period.

  9. data['ema20'] = ...: Saves the computed EMA values into a new column 'ema20' in your DataFrame.

Boom — indicators added to your DataFrame.

5. Define the Buy/Sell logic

def generate_signal(row):
    if row['rsi'] < 30 and row['Close'] > row['ema20']:
        return "BUY"
    elif row['rsi'] > 70 and row['Close'] < row['ema20']:
        return "SELL"
    else:
        return "HOLD"

data['signal'] = data.apply(generate_signal, axis=1)

This function checks every row (i.e., every 15-min candle) and assigns a signal

6. View the result

print(data[['Close', 'rsi', 'ema20', 'signal']].tail(10))

Note:

This line prints the last 10 rows of selected columns from the data DataFrame:

  • 'Close' – the closing price of the asset for each period.

  • 'rsi' – the Relative Strength Index values calculated earlier.

  • 'ema20' – the 20-period Exponential Moving Average values.

  • 'signal' – a trading signal column (likely generated by your strategy logic).

The .tail(10) function limits the output to the most recent 10 rows, making it easier to review the latest market data, indicators, and generated trade signals without printing the entire dataset.

You’ll see output like:

This helps you backtest if the logic makes sense visually.

  • Pulled real data
  • Calculated indicators
  • Checked for buy/sell signals
  • Gave you a ready “signal” column

You can now add order execution, logging, and alerts to this strategy step by step.

Want to see how many buy/sell signals came up?

print(data['signal'].value_counts())
Is this chapter helpful?
Previous
Tools for Coded Algo Trading
Next
Deploying Your Algo for Live Trading

Discover our extensive knowledge center

Explore our comprehensive video library that blends expert market insights with Kotak's innovative financial solutions to support your goals.