From Code to Trade: Unleashing the Power of Python in Algo Trading!

Adityakale
4 min readJun 16, 2024

--

Project Overview

This project involves the creation of an automated trading bot designed to buy and sell stock options based on specific trading strategies, including price action patterns, Fibonacci retracements, and Simple Moving Averages (SMA) such as the 50-day and 200-day SMAs. The bot utilizes the Kotak Securities API for executing trades and has achieved a 30% return over the last five months.

Objectives

  • Automate Trading: Develop a system that automatically executes buy and sell orders based on pre-defined trading strategies.
  • Increase Efficiency: Enable full-time corporate employees to continue trading without constant monitoring.
  • Leverage Technical Analysis: Use price action patterns, Fibonacci retracements, and SMAs to inform trading decisions.
  • Optimize Returns: Achieve consistent returns, as evidenced by a 30% return over the last five months.

Tools and Technologies

  • Programming Language: Python
  • API: Kotak Securities API
  • Trading Strategies: Price action patterns, Fibonacci retracements, 50-day and 200-day SMAs
  • Libraries: requests, pandas, numpy, ta-lib, etc

Trading Strategies

1. Price Action Patterns

Price action patterns are movements in a stock’s price that can indicate future market behavior. The bot identifies and acts on these patterns to inform buy and sell decisions. The patterns such as a ‘W’ or ‘M’ created at important levels.

2. Fibonacci Retracement

Fibonacci retracements are used to identify potential reversal levels by measuring the peak-to-trough move of a stock. The bot uses these levels to place trades.

3. Simple Moving Averages (SMA)

The 50-day and 200-day SMAs are used to identify trends. A crossover strategy, where the 50-day SMA crosses above or below the 200-day SMA, is employed to signal buy or sell actions.

4. Astro Levels

This are the important levels were the trade will be initiated as will be taken as reference.

This sheet is convert into Pandas Data frame is used as per the statergy progresses.

Code Structure Overview

  1. Input
  2. Logger
  3. Reset
  4. src

1. Input

Purpose: This directory contains all the necessary input files required for the trading bot to function. This includes:

  • Astro sheet.xlsx: This Excel file likely contains important data related to your trading strategy, such as historical data, technical indicators, and possibly other financial metrics.
  • Data related to CE and PE and their tokens: This includes data files for Call Options (CE) and Put Options (PE), along with their associated tokens, which are necessary for executing trades via the Kotak Securities API.

2. Logger

Purpose: The Logger directory is used for maintaining backups of the code’s state and storing ledger statements. This is critical for:

  • Code Backup: Storing snapshots of the code’s execution state, allowing you to restart the bot from any point in time without losing context.
  • Ledger Statements: Keeping a detailed log of all trades executed by the bot, including timestamps, trade details, and outcomes. This ensures transparency and helps in tracking the bot’s performance.

3. Reset

Purpose: The Reset functionality is used to clear the Logger content and reset the system at the start of a new trading day. This ensures:

  • Fresh Start: At the beginning of each trading day, the logs and states are cleared to prevent any carry-over of previous data, which could potentially interfere with new trades.
  • Manual Reset: You can manually trigger a reset if needed, which is useful for testing or debugging purposes.

4. src

Purpose: This directory contains all the source code and relevant libraries that power the automated trading bot. Key components include:

  • Source Code: All the Python scripts that implement the trading logic, API interactions, and other functionalities.
  • Libraries: Any third-party or custom libraries that the bot depends on for tasks such as data processing, technical analysis, and API communication.

Detailed Components within src

  • main.py: The main script that orchestrates the entire process, including fetching data, analyzing it, and placing trades.
  • data_fetcher.py: Handles fetching historical and real-time data from the Kotak Securities API.
  • trade_executor.py: Contains functions for placing buy and sell orders through the API.
  • strategy.py: Implements the trading strategies, including price action patterns, Fibonacci retracements, and SMA crossover logic.
  • logger.py: Manages logging of trades and system states.
  • reset.py: Implements the reset functionality to clear logs and reset the system.
  • config.py: Contains configuration settings such as API keys, token details, and other constants.
/project_directory
/Input
Astro_sheet.xlsx
ce_data.csv
pe_data.csv
tokens.json
/Logger
trade_log.txt
code_backup_20230619.txt
/Reset
reset_log.txt
/src
main.py
data_fetcher.py
trade_executor.py
strategy.py
logger.py
reset.py
config.py
/libs
talib.py
utils.py

Results

Over the last five months, the automated trading bot has achieved a return of 30%, demonstrating the effectiveness of the implemented trading strategies. The bot continuously monitors the market, places trades based on technical indicators, and ensures optimal returns with minimal manual intervention.

Conclusion

This automated trading bot provides a robust solution for full-time corporate employees to engage in trading activities without constant monitoring. By leveraging advanced trading strategies and the Kotak Securities API, the bot achieves significant returns and enhances trading efficiency. This project showcases the potential of automation in the trading world and the benefits it can bring to individual trad

--

--

Adityakale
Adityakale

Written by Adityakale

Software Engineer with expertise in CI/CD, containerization, and infrastructure monitoring.

Responses (1)