Skip to content

yashsm01/TreadBot

Repository files navigation

πŸš€ Time-Based Straddling Strategy

Advanced Cryptocurrency Trading Bot with Time-Based Straddling Strategy

YouTube Video Python FastAPI React TypeScript

πŸ“– Overview

The Time-Based Straddling Strategy is a sophisticated cryptocurrency trading system that employs advanced algorithmic trading techniques to capture market volatility through strategic position placement. The system utilizes multiple timeframes and dynamic volatility analysis to optimize entry and exit points.

🎯 Key Features

  • πŸ”„ Automated Straddle Trading: Executes both long and short positions simultaneously
  • ⏰ Multi-Timeframe Analysis: Short (5m), Medium (1h), and Long (4h) timeframe strategies
  • πŸ“Š Dynamic Volatility Calculation: Adaptive entry levels based on real-time market conditions
  • πŸŽ›οΈ Smart Position Management: Intelligent TP/SL calculation with 1:3 buy/sell ratios
  • πŸ“± Telegram Integration: Real-time notifications and trading updates
  • πŸ”„ Auto-Swap Functionality: Automatic conversion between crypto and stablecoins
  • πŸ“ˆ Portfolio Management: Comprehensive portfolio tracking and analysis
  • πŸ–₯️ Modern Web Interface: Real-time dashboard with live market data
  • πŸ” Paper Trading Mode: Safe testing environment before live trading

πŸ—οΈ Architecture

Time-Based Straddling Strategy/
β”œβ”€β”€ backend/                    # FastAPI Backend
β”‚   β”œβ”€β”€ app/                   # Main application
β”‚   β”‚   β”œβ”€β”€ api/v1/           # API routes
β”‚   β”‚   β”œβ”€β”€ models/           # Database models
β”‚   β”‚   β”œβ”€β”€ services/         # Business logic
β”‚   β”‚   β”œβ”€β”€ crud/             # Database operations
β”‚   β”‚   └── core/             # Configuration & database
β”‚   β”œβ”€β”€ alembic/              # Database migrations
β”‚   └── scripts/              # Utility scripts
β”œβ”€β”€ frontent/ [sic]           # React/TypeScript Frontend
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ components/       # React components
β”‚   β”‚   β”œβ”€β”€ services/         # API services
β”‚   β”‚   β”œβ”€β”€ hooks/            # Custom React hooks
β”‚   β”‚   └── types/            # TypeScript definitions
β”‚   └── ...
└── crypto_scheduler/         # Background job scheduler

πŸš€ Quick Start

Prerequisites

  • Python 3.8+
  • Node.js 16+
  • PostgreSQL 12+
  • Git

1. Clone the Repository

git clone <repository-url>
cd "Time-Based Straddling Strategy"

2. Backend Setup

# Navigate to backend
cd backend

# Create virtual environment
python -m venv venv

# Activate virtual environment
# Windows:
venv\Scripts\activate
# Linux/Mac:
source venv/bin/activate

# Install dependencies
pip install -r requirements.txt

# Set up environment variables
cp .env.example .env
# Edit .env with your configuration

3. Database Setup

# Create PostgreSQL database
createdb crypto_trading

# Run migrations
alembic upgrade head

# Optional: Sync crypto data
python scripts/sync_crypto.py

4. Frontend Setup

# Navigate to frontend
cd frontent

# Install dependencies
npm install

# Set up environment variables
cp .env.example .env
# Edit .env with your configuration

5. Start the Application

Backend:

cd backend
python run.py

Frontend:

cd frontent
npm run dev

Scheduler (Optional):

cd backend/crypto_scheduler
python start.py

βš™οΈ Configuration

Backend Configuration (backend/.env)

# Database
POSTGRES_USER=postgres
POSTGRES_PASSWORD=your_password
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DB=crypto_trading

# Trading Settings
PAPER_TRADING=true
STRATEGY=SHORT  # SHORT, MEDIUM, LONG
DEFAULT_TRADING_PAIR=BTC/USDT

# Exchange API Keys
BINANCE_API_KEY=your_binance_api_key
BINANCE_SECRET_KEY=your_binance_secret_key

# Telegram (Optional)
TELEGRAM_BOT_TOKEN=your_bot_token
TELEGRAM_CHAT_ID=your_chat_id

# 1inch API (For Swaps)
ONEINCH_API_KEY=your_oneinch_api_key
WALLET_ADDRESS=your_wallet_address
PRIVATE_KEY=your_private_key

Frontend Configuration (frontent/.env)

VITE_API_URL=http://localhost:8000/api/v1
VITE_DEV_MODE=true
VITE_AUTO_REFRESH_INTERVAL=30000
VITE_ENABLE_MOCK_FALLBACK=true

🎯 Trading Strategy

Straddle Strategy Logic

The Time-Based Straddling Strategy implements a sophisticated approach to cryptocurrency trading:

  1. Volatility Analysis: Calculates volatility across multiple timeframes
  2. Dynamic Entry Levels: Adjusts entry points based on market conditions
  3. Risk Management: Implements 1:3 buy/sell ratios with intelligent stop-loss
  4. Auto-Rebalancing: Automatically closes and reopens positions based on profit targets

Strategy Parameters

Parameter Short Strategy Medium Strategy Long Strategy
Timeframe 5 minutes 1 hour 4 hours
Take Profit 0.8% 3.0% 6.0%
Stop Loss 0.5% 2.0% 4.0%
Buy:Sell Ratio 1:3 1:3 1:3

πŸ“Š API Endpoints

Live Trading Data

  • GET /api/v1/live/health - System health check
  • GET /api/v1/live/tokens - Real-time token prices
  • GET /api/v1/live/signals - Trading signals
  • GET /api/v1/live/market-overview - Market summary

Trading Operations

  • POST /api/v1/straddle/create - Create straddle position
  • GET /api/v1/straddle/status/{symbol} - Get position status
  • POST /api/v1/straddle/close/{symbol} - Close position

Portfolio Management

  • GET /api/v1/portfolio/summary - Portfolio overview
  • GET /api/v1/portfolio/history - Trading history
  • GET /api/v1/portfolio/performance - Performance metrics

πŸ”§ Advanced Features

Auto-Swap Functionality

The system can automatically swap between cryptocurrencies and stablecoins based on:

  • Market trend analysis
  • Profit thresholds
  • Risk management rules
  • Intraday pattern recognition

Multi-Exchange Support

  • Binance: Primary exchange for trading
  • 1inch: DEX aggregator for optimal swap rates
  • Extensible: Easy to add new exchange integrations

Telegram Bot Integration

Get real-time notifications for:

  • Trade executions
  • Profit/Loss updates
  • System alerts
  • Market opportunities

πŸ“ˆ Performance Monitoring

Built-in Analytics

  • Real-time P&L tracking
  • Win/loss ratios
  • Drawdown analysis
  • Performance attribution
  • Risk metrics

Logging and Monitoring

  • Comprehensive logging system
  • Health check endpoints
  • Performance metrics collection
  • Error tracking and alerting

πŸ§ͺ Testing

Run Backend Tests

cd backend
pytest app/tests/ -v

Test API Connectivity

# Test environment setup
node frontent/test-env.js

# Test API service
node frontent/test-api-service.js

πŸ”’ Security

Best Practices Implemented

  • API Key Management: Secure storage of exchange credentials
  • Paper Trading Mode: Safe testing environment
  • Input Validation: Comprehensive parameter validation
  • Rate Limiting: Protection against API abuse
  • Secure Communications: HTTPS and encrypted connections

Wallet Security

  • Private keys are encrypted and never logged
  • Multi-signature support ready
  • Hardware wallet integration possible

πŸ“š Documentation

Additional Resources

API Documentation

Once the backend is running, access the interactive API documentation at:

  • Swagger UI: http://localhost:8000/docs
  • ReDoc: http://localhost:8000/redoc

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

⚠️ Disclaimer

This software is for educational and research purposes only. Cryptocurrency trading involves substantial risk of loss and is not suitable for every investor. Past performance does not guarantee future results. Always conduct your own research and consider consulting with a financial advisor before making investment decisions.

πŸ†˜ Support

  • πŸ“Ί Video Tutorial: YouTube
  • πŸ“§ Issues: Create an issue on GitHub
  • πŸ’¬ Discussions: Use GitHub Discussions for questions

πŸ”— Links


⭐ If you find this project helpful, please consider giving it a star!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors