Skip to content

oguztiras/backtesting-engine

Repository files navigation

Crypto Trader

A Django-based cryptocurrency algorithmic trading application for backtesting technical analysis strategies.

Python Django

Overview

Crypto Trader is a comprehensive trading platform built with Django that enables cryptocurrency traders to fetch market data, implement technical analysis indicators, create trading strategies, and backtest them against historical data. The application focuses on providing a robust foundation for strategy development and backtesting, with a clean and intuitive interface.

Features

  • Data Management: Fetch and store cryptocurrency price data from Binance
  • Technical Analysis: Calculate and visualize indicators like SMA, RSI, etc.
  • Strategy Development: Implement and test trading strategies
  • Backtesting: Evaluate strategy performance against historical data
  • Performance Metrics: Track key metrics like win rate, profit/loss, etc.
  • Web Interface: Visualize data, strategies, and backtest results

Tech Stack

  • Backend: Django, Python
  • Data Processing: Pandas, NumPy
  • Charting: Matplotlib
  • API Integration: CCXT (CryptoCurrency eXchange Trading Library)
  • Database: SQLite (default), extensible to PostgreSQL

Installation

# Clone the repository
git clone https://github.com/github_username/crypto-trader.git
cd crypto-trader

# Create and activate virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install required packages
pip install -r requirements.txt

# Run migrations
python manage.py migrate

# Create a superuser
python manage.py createsuperuser

# Start the development server
python manage.py runserver

Usage

Fetching Data

Currently, data fetching is limited to command-line operations:

# Fetch 1000 hourly candles for BTC/USDT
python manage.py fetch_crypto_data BTC/USDT --timeframe 1h --limit 1000

Limitations:

  • Only one fetching option is available using the limit parameter
  • Historical data fetching by date range will be added in future versions
  • All data-fetching operations will eventually be available through the GUI

Calculating Indicators

# Calculate SMA and RSI for BTCUSDT
python manage.py calculate_indicators BTCUSDT --timeframe 1h

Running Backtests

# Run a backtest for a specific date range
python manage.py run_backtest BTCUSDT --timeframe 1h --start-date 2025-04-01 --end-date 2025-04-25

Strategy Framework

Current Implementation

The app currently has one strategy implemented: SMA Crossover, which buys when a short-term SMA crosses above a long-term SMA and sells when it crosses below.

Scalability

The application is designed to be scalable:

  • New indicators can be created under the analysis app
  • New strategies can be added under the strategies app using the base strategy class for consistency
  • Each backtest is initiated via a dedicated script under the backtesting app, with each run having a unique name for traceability

Exit Conditions

While buy/sell signals are defined within each strategy, the exit conditions are handled in backtesting/backtest_engine.py. This exit logic can be extended to accommodate various exit conditions for different strategies.

Trading Modes

Current Capabilities

  • Only spot market buy positions are currently implemented
  • Paper trading mode (simulated trading with no real money)

Future Enhancements

  • Futures trading support will be added
  • Real-time trading implementation with user authentication
  • Support for more advanced order types (limit, stop-loss, etc.)

Data Model

Key Models

  • Trade Model: Currently unused, reserved for future real-time trading implementation with user logins
  • Strategy Model: Includes an is_active field (currently unused) that will help manage which strategies are active during backtesting
  • BacktestRun Model: Stores the results of backtest runs for comparison and analysis

Database Schema Diagram

The following diagram illustrates the database schema and relationships between models in the Crypto Trader application:

Database Schema Diagram

Extending the Application

Adding New Strategies

When adding new strategies:

  1. Create a new strategy class in the strategies app that inherits from BaseStrategy
  2. Implement the required generate_signals method
  3. Update views in backtesting/views.py to import and integrate the new strategy

Modifying Existing Functionality

  • Trading exit conditions can be modified in backtesting/backtest_engine.py
  • Indicators can be added or modified in analysis/indicators.py
  • New data sources can be integrated by extending the data service in data/services.py

Roadmap

  • Historical data fetching by date range
  • GUI for data fetching operations
  • Additional technical indicators (MACD, Bollinger Bands, etc.)
  • More trading strategies
  • Strategy combination capabilities
  • Position sizing options
  • Real-time trading implementation
  • User authentication and profiles
  • Futures trading support
  • Email/mobile notifications for trading signals
  • Performance optimization for large datasets
  • Strategy optimization using machine learning

Acknowledgements

  • CCXT for cryptocurrency exchange API integration
  • Django for the web framework
  • Pandas for data analysis
  • Matplotlib for data visualization

This project is for educational and portfolio purposes. Trading cryptocurrencies involves significant risk. Use this software at your own risk.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors