Skip to content

Ankitkalauni/TradeBot

Repository files navigation

TradeBot

This is a trading bot designed to automate stock trading based on breakout strategies and technical analysis.

Current usecase

python3 /Users/ankitkalauni/Documents/personal/TradeBot/examples/resistance_line_example.py --run-all --lookback-days 90 --max-workers 40 --end-date 2025-06-22

python3 /Users/ankitkalauni/Documents/personal/TradeBot/examples/resistance_line_example.py --symbol AAVAS.NS --lookback-days 90 --max-workers 40 --end-date 2025-06-22

cd /Users/ankitkalauni/Documents/personal/TradeBot && python3 scripts/cup_handle_scanner.py --plot --output-dir output_data/cup_handle_patterns

price_to_handle_ratio is the filter which gives the plots which is near breakout

Quick Start

Running the Breakout Strategy

python3 src/tradebot/run_breakout_strategy.py \
    --symbols datasets/Valid_symbols.csv \
    --mode backtest \
    --start 2024-06-01 \
    --end 2025-06-08 \
    --short-ma 20 \
    --long-ma 90 \
    --base-lookback 25 \
    --min-base-duration 12 \
    --range-threshold 0.08 \
    --volume-factor 1.7 \
    --logic-mode crossover \
    --save-charts \
    --output-dir output_backtest_results

Using SMA-Based Resistance Line Analysis

The system now includes advanced resistance line calculation using SMA-based regression approaches:

# Run resistance analysis on specific stock
python3 src/tradebot/run_resistance_analysis.py --symbol RELIANCE.NS --days 180 --save-charts

# Run analysis with quantile regression (more robust to outliers)
python3 src/tradebot/run_resistance_analysis.py --symbol INFY.NS --use-quantile --quantile 0.95 --save-charts

# Compare multiple regression methods
python3 src/tradebot/run_resistance_analysis.py --symbol TCS.NS --compare-methods --save-charts

# Analyze top 10 stocks by market cap
python3 src/tradebot/run_resistance_analysis.py --market-cap-min 10000 --limit 10 --save-charts

You can also run the example script for a more visual demonstration:

# Run resistance analysis on specific stock
python3 examples/resistance_line_example.py --symbol RELIANCE.NS --days 180

# Run analysis on a set of popular stocks
python3 examples/resistance_line_example.py --run-all

Resistance Line Analysis

The new SMA-based resistance line computation provides several advantages:

  1. Advanced Detection: Uses simple moving average (SMA) to smooth price data and identify significant resistance levels
  2. Multiple Regression Methods: Supports both standard OLS and quantile regression approaches
  3. Time-Weighted Analysis: Option to give higher weight to more recent price action
  4. Visual Analysis Tools: Comprehensive visualization of resistance lines and breakout points

Parameters

  • sma_window: Window size for the SMA calculation (default: 20)
  • use_quantile: Whether to use quantile regression instead of OLS
  • quantile: Quantile value to use (default: 0.95, representing upper resistance)
  • time_weighted: Whether to apply higher weights to more recent data points

Example with BreakoutAlgorithm

from tradebot.strategies.breakout_algorithm import BreakoutAlgorithm

# Initialize with SMA resistance enabled
algo = BreakoutAlgorithm(
    use_sma_resistance=True,
    sma_window=20,
    use_quantile_regression=True,
    quantile=0.95,
    time_weighted_regression=True
)

# Run the strategy
results = algo.run_strategy(
    start_date='2025-01-01',
    end_date='2025-06-21'
)

Project Structure

The project is organized into the following directories:

  • src/: Contains the core source code for the trading bot.
    • tradebot/: Main package for the trading bot.
      • run_breakout_strategy.py: Main script to run the breakout strategy.
      • analysis/: Modules for performing data analysis, performance evaluation, and visualization.
        • performance_evaluation.py: Scripts for evaluating the performance of trading strategies.
        • resistance_finder.py: Scripts to find resistance levels.
        • visualize_breakouts.py: Scripts for visualizing breakout patterns.
      • apis/: Modules for interacting with external APIs.
        • yahoo/: Scripts related to Yahoo Finance API.
          • nse_stock_data_manager.py: Manages NSE stock data from Yahoo Finance.
          • nse_symbol_extractor.py: Extracts NSE symbols.
      • data_processing/: Modules for data collection, cleaning, and preparation.
        • data_preparation.py: Scripts for preparing data for analysis and trading.
        • download_stock_data.py: Scripts for downloading stock data.
        • volume_std.py: Scripts for calculating volume standard deviation.
      • strategies/: Modules implementing different trading strategies.
        • breakout_algorithm.py: Core logic for the breakout trading algorithm.
        • breakout_strategy.py: Implementation of the breakout trading strategy.
      • utils/: Utility modules used across the project.
        • database.py: Database interaction utilities.
        • fix_database.py: Scripts for fixing database issues.
        • health_check.py: Scripts for system health checks.
        • logger.py: Logging utilities.
        • migrate_database.py: Database migration scripts.
        • notifications.py: Notification utilities.
  • config/: Contains configuration files for the project.
    • config.json: JSON configuration file.
    • config.py: Python-based configuration file.
  • dags/: Contains Airflow DAG (Directed Acyclic Graph) definitions for orchestrating workflows.
    • daily_nse_data_crawler_dag.py: DAG for daily crawling of NSE data.
  • docs/: Contains documentation for the project.
    • planning/: Documents related to project planning and design.
      • pattern_reco_algo.txt: Text file describing pattern recognition algorithms.
      • phase3_summary.md: Summary of project phase 3.
  • examples/: Contains example scripts demonstrating how to use parts of the project.
    • example.py: An example script.
  • notebooks/: Contains Jupyter notebooks for experimentation, analysis, and visualization.
    • break_out.ipynb: Notebook for analyzing breakout patterns.
    • resistance_finder.ipynb: Notebook for finding resistance levels.
  • tests/: Contains test scripts for ensuring code quality and correctness.
    • backtest_strategy.py: Script for backtesting trading strategies.
    • test_database.py: Tests for database utilities.
    • test_single_stock.py: Tests for single stock analysis.
  • docker-compose.yaml: Docker Compose file for setting up the development and deployment environment.
  • requirements.txt: Lists the Python dependencies for the project.
  • README.md: This file, providing an overview of the project.

Getting Started

  1. Clone the repository:
    git clone <repository-url>
    cd TradeBot
  2. Install dependencies:
    pip install -r requirements.txt
  3. Configure the bot by editing files in the config/ directory.
  4. Run the main script:
    python src/tradebot/run_breakout_strategy.py

Backtesting with Vectorbt

This project now includes an efficient vectorbt-based backtesting module.

Install Vectorbt

Make sure you have vectorbt installed (added in requirements.txt).

pip install -r requirements.txt

Run the Vectorbt Backtest CLI

python src/tradebot/backtesting/run_vectorbt_backtest.py \
    --symbols config/Valid_symbols.csv \
    --start 2023-01-01 \
    --end 2025-06-07 \
    --init-cash 100000 \
    --fees 0.001 \
    --slippage 0.001

Results will be saved under the performance directory defined in config.py.

Contributing

[Add guidelines for contributing to the project, if applicable.]

License

[Specify the license for the project, if applicable.]

About

Personal tradebot

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors