Automated Crypto Trading Bot.
A Python-based crypto trading execution engine using Binance Spot and Futures API.
AlphaTrader/ │ ├── config/ # Configurations (API keys, environment) ├── data_collector/ # Real-time market data fetcher ├── indicators/ # Technical indicator calculations ├── strategies/ # Trading strategies (scalping, trend-following, etc.) ├── regime_detector/ # Market regime detection (trend or range) ├── execution_engine/ # Order execution engine ├── risk_manager/ # Risk controls ├── ui/ # Streamlit dashboard ├── utils/ # Utility functions like logger ├── backtester/ # Historical strategy backtesting (future) ├── venv/ # Virtual environment (excluded from Git) │ ├── main.py # Project entry point ├── requirements.txt # Project dependencies ├── README.md # Project documentation
- Environment Loader:
settings.py
loads.env
variables (keys, secrets, URLs). - Connector Class:
BinanceConnector
inbinance_connector.py
manages:- Signing requests (HMAC SHA256)
- Sending retries with timeout
- Spot and Futures API separation
- Placing, cancelling, querying orders
- Execution Entry:
main.py
callsBinanceConnector.place_order()
etc.
- Load environment variables (API keys) using
dotenv
. - Initialize
BinanceConnector
withaccount_type
(either"spot"
or"futures"
). - Use
place_order()
,cancel_order()
,get_open_orders()
methods to interact with Binance API securely. - Retry failed requests automatically (max 3 retries with 1s delay).
- Handle network issues like timeout, HTTP errors safely.
- Configuration Management: Safely load API keys and settings
- Data Collection: Fetch OHLCV data in real-time
- Indicators: Compute TA indicators using pandas-ta / TA-Lib
- Strategies: Modular strategies (easy to add new ones)
- Execution Engine: Robust, retriable order placement/cancellation
- Risk Manager: Position sizing and risk control logic
- UI Dashboard: Web interface using Streamlit
- Logger: Centralized logging and error handling
- Backtester (coming soon): Historical simulation
ccxt
pandas
pandas-ta
TA-Lib
apscheduler
fastapi
streamlit
requests
dotenv
logging
hmac
,hashlib
,time
,os
,urllib
git clone https://github.com/your-username/AlphaTrader.git
cd AlphaTrader
# Setup virtual environment
python -m venv venv
source venv/bin/activate # Linux/macOS
# or
venv\Scripts\activate # Windows
# Install requirements
pip install -r requirements.txt
## 🚀 Current Progress
| Task | Status |
|---------------------------------- |---------|
| Basic project setup | ✅ Done |
| .env loading via `dotenv` | ✅ Done |
| Spot and Futures separation | ✅ Done |
| Signing and sending secured API requests | ✅ Done |
| Retry mechanism for failed requests | ✅ Done |
| Order placement / cancellation | ✅ Done |
| Proper error handling (HTTPError, RequestException) | ✅ Done |
| Extensive code comments | ✅ Done |
| Project structure documentation | 🚧 In Progress |
| Testing different API endpoints | 🚧 In Progress |
| Logging system for API calls | ❌ Not Started |
| Add unit tests (pytest) | ❌ Not Started |
---
## 🔥 Setup Instructions
1. Clone the repository:
```bash
git clone https://github.com/your-username/AlphaTrader.git
cd AlphaTrader
How BinnanceConnector flow works?
BinanceConnector
|
|--> place_order()
|--> cancel_order()
|--> get_open_orders()
|
--> _send_request()
|
--> _sign_payload() (if needed)
Project Updated Structure
AlphaTrader/
backtester/
test_scalping.py
config/
settings.py
data_collector/
fetch_data.py
execution_engine/
executor.py
mock_executor.py
test_mock_executor.py
indicators/
technicals.py
risk_manager/
risk_control.py
strategies/
scalping.py
ui/
dashboard.py
utils/
logger.py
main.py
README.md
requirements.txt
🛠️ Upcoming Features
Modular Strategy Integration
Real-Time Risk Management
Live Dashboard Analytics
Backtesting Engine
Strategy Optimization Framework
Deployment on Cloud VPS (AWS, GCP)
Todo | Doing | Done
Add Advanced Stop Loss Strategy | 🔥 Unit Test Mock Executor | Scalping + Risk Manager Done
Integrate real Binance API | 🔥 Expand Risk Control | Mock Execution Engine Done
Backtesting framework | |
New format
# AlphaTrade
## **Overview**
AlphaTrader is a Python-based modular crypto trading bot framework designed for rapid development and testing of algorithmic trading strategies. It currently supports backtesting (using mock historical data and a **`MockExecutor`**) and implements a basic Simple Moving Average (SMA)–based scalping strategy. The system is structured in distinct layers – data collection, strategy logic, risk management, and execution – which aligns with common algorithmic trading architectures[dev.to](https://dev.to/nashetking/algorithmic-trading-architecture-and-quants-a-deep-dive-with-case-studies-on-blackrock-and-tower-research-55ao#:~:text=2)[dev.to](https://dev.to/nashetking/algorithmic-trading-architecture-and-quants-a-deep-dive-with-case-studies-on-blackrock-and-tower-research-55ao#:~:text=4). A preliminary Streamlit UI (**`ui/dashboard.py`**) provides a live dashboard skeleton, facilitating future real-time monitoring as the project evolves.
## **Architecture**
AlphaTrader follows a layered architecture typical of trading systems[dev.to](https://dev.to/nashetking/algorithmic-trading-architecture-and-quants-a-deep-dive-with-case-studies-on-blackrock-and-tower-research-55ao#:~:text=2)[dev.to](https://dev.to/nashetking/algorithmic-trading-architecture-and-quants-a-deep-dive-with-case-studies-on-blackrock-and-tower-research-55ao#:~:text=4). **Data Collection:** The **`data_collector/fetch_data.py`** module ingests market data (via exchange APIs or stored feeds) and normalizes it. **Strategy Engine:** Strategies (e.g. the SMA scalping strategy in **`strategies/scalping.py`**) process incoming data and generate trade signals. This “core” strategy engine makes decisions based on indicators[dev.to](https://dev.to/nashetking/algorithmic-trading-architecture-and-quants-a-deep-dive-with-case-studies-on-blackrock-and-tower-research-55ao#:~:text=2). **Risk Manager:** The **`risk_manager/risk_control.py`** module monitors trades and enforces limits (such as max trades per day or daily P/L caps) to curb losses. Effective risk management involves predicting potential losses and setting safeguards[cryptohopper.com](https://www.cryptohopper.com/academy/guides/risk-management-in-trading#:~:text=Risk%20management%20is%20the%20process,Instead%2C%20diversify%20your). **Execution Engine:** Orders are managed by the execution layer (**`execution_engine/`**). In backtest mode, **`mock_executor.py`** simulates fills for generated orders; in future live mode, **`executor.py`** will interface with real exchanges. This resembles an Order Management System (OMS) that ensures orders are sent, filled, or modified correctly[dev.to](https://dev.to/nashetking/algorithmic-trading-architecture-and-quants-a-deep-dive-with-case-studies-on-blackrock-and-tower-research-55ao#:~:text=3). An Execution Management System (EMS) could later optimize trade execution (e.g. order sizing, routing)[dev.to](https://dev.to/nashetking/algorithmic-trading-architecture-and-quants-a-deep-dive-with-case-studies-on-blackrock-and-tower-research-55ao#:~:text=5). **Backtesting:** The **`backtester/test_scalping.py`** script runs historical simulations, rigorously testing strategies before any live deployment[dev.to](https://dev.to/nashetking/algorithmic-trading-architecture-and-quants-a-deep-dive-with-case-studies-on-blackrock-and-tower-research-55ao#:~:text=6). Backtesting (using past data) is crucial for understanding strategy performance and pitfalls[dev.to](https://dev.to/nashetking/algorithmic-trading-architecture-and-quants-a-deep-dive-with-case-studies-on-blackrock-and-tower-research-55ao#:~:text=6).
## **Modules**
- **`config/settings.py`** – Configuration parameters (API keys, trading pairs, mode flags, risk limits). Adjust these settings to switch between paper/live trading or to set P/L thresholds.
- **`data_collector/fetch_data.py`** – Fetches and preprocesses market data (e.g. price candles). This could be expanded to support real-time data streams (via websockets) for live trading.
- **`indicators/technicals.py`** – Calculates technical indicators. Currently includes functions like Simple Moving Average (SMA), which are used by strategies to generate signals.
- **`strategies/scalping.py`** – Implements a simple SMA crossover scalping strategy. Scalpers make rapid, small-profit trades in volatile markets[cryptohopper.com](https://www.cryptohopper.com/blog/crypto-scalp-trading-learn-the-basics-8259#:~:text=Scalp%20trading%20in%20cryptocurrency%20is,seconds%20or%20minutes%20before%20selling). This module generates buy/sell signals based on SMA conditions.
- **`risk_manager/risk_control.py`** – Monitors all open trades and cumulative profits. It enforces risk rules (e.g. maximum daily loss, maximum open positions). If limits are breached, it signals the strategy or executor to halt trading[cryptohopper.com](https://www.cryptohopper.com/academy/guides/risk-management-in-trading#:~:text=Risk%20management%20is%20the%20process,Instead%2C%20diversify%20your).
- **`execution_engine/executor.py`** – [Future] Contains code to place real trades via exchange APIs (Binance Spot/Futures). It will handle order placement, cancellation, and error responses in live mode.
- **`execution_engine/mock_executor.py`** – Simulates order execution by “filling” orders at mocked prices. Used in backtest/paper mode to verify strategy logic without risking real funds.
- **`execution_engine/test_mock_executor.py`** – Unit tests for the mock executor, ensuring simulated trades behave as expected.
- **`backtester/test_scalping.py`** – Backtesting harness: runs the scalping strategy on historical or mock data and logs trade outcomes. This helps evaluate profitability and refine strategy parameters.
- **`ui/dashboard.py`** – Basic Streamlit dashboard for real-time monitoring. Currently a skeleton: future work will display metrics (PnL, positions, indicators) and allow manual controls.
- **`utils/logger.py`** – Centralized logging setup (timestamps, log levels). All modules use this for consistent formatted output.
- **`main.py`** – Entry point script. Parses command-line arguments or settings and launches the bot in the chosen mode (backtest vs live).
- **`requirements.txt`** – Lists Python dependencies. Install them with pip to set up the environment.
## **Sample Output**
In backtest mode, a successful trade might produce logs such as:
2023-04-01 10:23:45 - INFO - SIGNAL: BUY BTCUSDT at 50000.00 (SMA crossover) 2023-04-01 10:23:45 - INFO - ORDER: Executing BUY 0.01 BTC 2023-04-01 11:15:32 - INFO - EXECUTION: Filled BUY 0.01 BTC at 50375.00, Profit: +0.75% 2023-04-01 11:15:32 - INFO - RISK: Day P/L = +0.75% (within limit)
This shows a buy signal (from the SMA strategy), order placement, filled execution, and the resulting profit (with risk manager updating daily P/L). In a real scenario the live mode would similarly log orders and fills via the executor.
## **Setup and Installation**
1. **Clone the repository:**
```bash
git clone https://github.com/yourusername/AlphaTrader.git
cd AlphaTrader
```
2. **Environment:** Ensure Python 3.8 or higher is installed. (VS Code is recommended for development.)
3. **Virtual Environment (optional):**
```bash
python3 -m venv venv
source venv/bin/activate# On Windows: venv\Scripts\activate
```
4. **Install Dependencies:**
```bash
pip install -r requirements.txt
```
This installs libraries (e.g. **`pandas`**, **`streamlit`**, etc.) listed in **`requirements.txt`**.
5. **Configure Settings:** Edit **`config/settings.py`** to set your trading pair, capital, risk limits, and (eventually) exchange API keys. For now, ensure settings designate paper trading/backtest mode.
## **Running in Backtest vs Live Modes**
By default, running **`python main.py`** executes in **backtest mode** using historical/mock data and the **`MockExecutor`**. To enable live (paper/live) trading mode, change the mode flag in **`config/settings.py`** or pass an argument (e.g. **`--live`**) if implemented. In live mode, the system would use real-time data and **`execution_engine/executor.py`** to place orders via the exchange’s API. For example, connecting to Binance’s Spot testnet allows paper trading with minimal risk. **Note:** Always backtest thoroughly before enabling real trading. Ensure live API keys are set in **`config/settings.py`** and switch to real data feeds.
## **Running Unit Tests**
The project includes basic unit tests to catch logic errors early. Run the test suite with:
```bash
pytest
This will execute tests in backtester/test_scalping.py
and execution_engine/test_mock_executor.py
, verifying the strategy logic and the mock executor behavior. It’s recommended to write additional tests as you add features. Using linters and testing frameworks helps identify bugs and maintain code qualitycode.visualstudio.comcode.visualstudio.com.
- Enhance the Streamlit UI: Complete the dashboard by adding real-time charts and metrics (equity curve, cumulative PnL, indicator values). A visual interface for trading strategies is essential for monitoring system performance and understanding decisionsalphavest.inalphavest.in. Streamlit widgets (charts, tables, buttons) should display strategy status, recent signals, and trade history dynamically.
- Implement Additional Strategies: Beyond the current SMA scalping, add diverse strategies (e.g. mean-reversion, momentum, breakout) to broaden market coverage. Mean-reversion is a common quantitative strategy (prices revert to their mean)dev.to. Having multiple strategies allows comparing performance and reduces risk of a single approach failing.
- Ingest Real-Time Market Data: Integrate live price feeds using exchange WebSocket APIs (e.g. Binance WebSocket) in
data_collector
. Real-time data handling (market data feed handlers) is the foundation of any algorithmic systemdev.to. This enables the bot to respond immediately to market movements rather than only in backtest simulations. - Develop Live Execution Engine: Build out
executor.py
to connect to Binance (Spot first, then Futures) for order placement. Libraries like python-binance provide REST and WebSocket interfaces supporting both Spot and Futuresgithub.com. Ensure safe order placement logic (e.g. using testnet modes initially) and implement any required order filters or trade size calculations. - Persistent Logging & Reporting: Set up robust logging (to files or a database) so that all trades, PnL updates, and errors are recorded long-term. Automate periodic reports or alerts (e.g. daily PnL summary) for review. Persistent logs help with debugging, auditing, and refining strategy performance.
- Backtesting Framework Improvements: Enhance the backtester by adding performance metrics (e.g. Sharpe ratio, drawdown) and faster iteration. Consider parameter sweeping or optimization tools. A strong backtesting framework accelerates development by quickly validating new ideasdev.to.
- Complete the UI Dashboard: Finalize the Streamlit interface with real-time charts (e.g. price and SMA lines, equity curve) and KPIs (PnL, win rate). Reasoning: Visibility into live bot activity and performance metrics is crucial for understanding and debugging strategies in real timealphavest.in.
- Implement Real-Time Data Feeds: Connect to exchange data streams (via REST/WebSocket) so the strategy can operate on live prices. Reasoning: Without live data, the bot cannot make actionable decisions in real markets; this lays the groundwork for any live mode.
- Integrate Live Trading (Spot): Use a library like python-binance to execute real orders on Binance Spot market (starting in testnet/paper mode). Reasoning: This is needed to transition from simulation to real trading. Python-binance supports Spot/Futures and testnetsgithub.com, making it easier to validate live execution.
- Expand Strategy Library: Code and backtest additional strategies (e.g. mean-reversion, RSI/momentum) beyond scalping. Reasoning: Multiple strategies diversify opportunities and allow comparative analysis; some strategies may perform better in different market conditions.
- Enhance Risk Management: Refine
risk_control.py
to include features like position sizing (e.g. 1% rule) and dynamic stop-losses. Reasoning: As trading moves to real money, robust risk rules prevent catastrophic lossescryptohopper.com. - Implement Robust Logging and Monitoring: Configure rolling log files, error alerts, and periodic PnL reporting. Reasoning: Persistent logs and reports make it easier to trace issues and evaluate strategy performance over time.
- Improve Backtesting & Optimization: Add features to the backtester such as batch testing of parameter sets, integration with libraries like
backtrader
orzipline
if needed. Reasoning: Faster and more comprehensive backtests speed up development and help fine-tune strategies. - Code Quality and Testing: Add more unit/integration tests (e.g. for risk rules, new strategies) and set up continuous integration (CI) in VS Code or GitHub. Reasoning: A rigorous testing framework and CI pipeline will catch errors early and ensure reliability before deploying any changes.
- Python Extension + Pylance: Microsoft’s Python extension (with Pylance) adds IntelliSense, code navigation, and environment management. It turns VS Code into a full-featured Python IDE, supporting code completion and easy switching between virtual environmentscode.visualstudio.com.
- Linters (e.g. flake8 or pylint): Enable linting in VS Code to catch syntax issues, undefined names, and style problems as you code. Linting highlights semantic or stylistic errors that could lead to bugscode.visualstudio.com. Configure VS Code to run a linter on save or via the Problems panel.
- Formatter (Black): Use an auto-formatter like Black to maintain consistent code style. This avoids manual formatting debates and reduces diff noise. (VS Code can format on save.)
- Debugging Tools: Utilize the built-in Python Debugger in VS Code for breakpoints and step-through debugging. The Python extension supports setting breakpoints, inspecting variables, and stepping through codecode.visualstudio.com. This allows you to trace logic in the strategy or execution code interactively.
- Pytest Extension: Install a pytest runner extension (or use the testing UI) to easily run and debug tests. Good test integration speeds up running
pytest
cases. - GitLens (or Git Graph): For version control, extensions like GitLens provide insights into code commits, blame info, and history right in the editor.
- Docker/WSL Support (optional): If containerizing or using Linux tools, VS Code’s Remote - Containers or WSL extensions can help replicate deployment environments.
- Binance API Documentation: The official Binance API docs (https://binance-docs.github.io/apidocs/) cover Spot and Futures endpoints and examples. They are essential for understanding how to place orders and fetch data programmatically.
- python-binance (GitHub): An unofficial but popular Python wrapper for Binance’s API. Its repository (sammchardy/python-binance) includes code examples and documentation of all Spot/Futures endpointsgithub.com. It supports testnet and WebSocket streams, making implementation easier.
- CCXT Library: A unified crypto trading library that supports Binance and many other exchanges. CCXT’s documentation (https://ccxt.trade/) provides examples of fetching data and placing orders, useful for multi-exchange support.
- Algorithmic Trading Courses and Tutorials: FreeCodeCamp’s YouTube “Algorithmic Trading Using Python” course and similar online tutorials provide practical introductions to coding strategies. (See freeCodeCamp.org News blog and YouTube for their algorithmic trading series.)
- Quantitative Trading Blogs: Educational sites like QuantStart, Investopedia, or Crypto-specific blogs (e.g. Binance Academy, Coin Bureau) have articles on trading strategies and risk management. For example, CryptoHopper’s guide on crypto scalping and risk managementcryptohopper.comcryptohopper.com offers foundational principles.
- Community Code Examples: Explore open-source GitHub repos (e.g. “python-trading-bot”, “CCXT code snippets”) and forums. Studying others’ implementations (backtesting frameworks, trading dashboards) can provide practical insights.
- Streamlit Documentation: Streamlit’s official docs and tutorials (https://docs.streamlit.io/) guide how to build dashboards and integrate visualizations, which will be useful for enhancing
ui/dashboard.py
.
These resources—official docs, libraries, community tutorials and repos—will help deepen understanding of algorithmic trading concepts and practical integration with Binance or other exchanges.
Sources: The above recommendations and architecture overview draw on established algorithmic trading patterns and reputable guidesdev.tocryptohopper.comalphavest.indev.togithub.com, ensuring the design follows best practices in trading system development.
Citations
Algorithmic Trading Architecture and Quants: A Deep Dive with Case Studies on BlackRock and Tower Research - DEV Communityhttps://dev.to/nashetking/algorithmic-trading-architecture-and-quants-a-deep-dive-with-case-studies-on-blackrock-and-tower-research-55ao Algorithmic Trading Architecture and Quants: A Deep Dive with Case Studies on BlackRock and Tower Research - DEV Communityhttps://dev.to/nashetking/algorithmic-trading-architecture-and-quants-a-deep-dive-with-case-studies-on-blackrock-and-tower-research-55ao Risk management in tradinghttps://www.cryptohopper.com/academy/guides/risk-management-in-trading Algorithmic Trading Architecture and Quants: A Deep Dive with Case Studies on BlackRock and Tower Research - DEV Communityhttps://dev.to/nashetking/algorithmic-trading-architecture-and-quants-a-deep-dive-with-case-studies-on-blackrock-and-tower-research-55ao Algorithmic Trading Architecture and Quants: A Deep Dive with Case Studies on BlackRock and Tower Research - DEV Communityhttps://dev.to/nashetking/algorithmic-trading-architecture-and-quants-a-deep-dive-with-case-studies-on-blackrock-and-tower-research-55ao Algorithmic Trading Architecture and Quants: A Deep Dive with Case Studies on BlackRock and Tower Research - DEV Communityhttps://dev.to/nashetking/algorithmic-trading-architecture-and-quants-a-deep-dive-with-case-studies-on-blackrock-and-tower-research-55ao Crypto Scalp Trading: Learn the Basicshttps://www.cryptohopper.com/blog/crypto-scalp-trading-learn-the-basics-8259 Linting Python in Visual Studio Codehttps://code.visualstudio.com/docs/python/linting Python in Visual Studio Codehttps://code.visualstudio.com/docs/languages/python Build a Strategy Tracker Dashboard for Quant Tradinghttps://alphavest.in/build-a-strategy-tracker-dashboard/ Build a Strategy Tracker Dashboard for Quant Tradinghttps://alphavest.in/build-a-strategy-tracker-dashboard/ Algorithmic Trading Architecture and Quants: A Deep Dive with Case Studies on BlackRock and Tower Research - DEV Communityhttps://dev.to/nashetking/algorithmic-trading-architecture-and-quants-a-deep-dive-with-case-studies-on-blackrock-and-tower-research-55ao Algorithmic Trading Architecture and Quants: A Deep Dive with Case Studies on BlackRock and Tower Research - DEV Communityhttps://dev.to/nashetking/algorithmic-trading-architecture-and-quants-a-deep-dive-with-case-studies-on-blackrock-and-tower-research-55ao GitHub - sammchardy/python-binance: Binance Exchange API python implementation for automated tradinghttps://github.com/sammchardy/python-binance Python debugging in VS Codehttps://code.visualstudio.com/docs/python/debugging GitHub - sammchardy/python-binance: Binance Exchange API python implementation for automated tradinghttps://github.com/sammchardy/python-binance
All Sources
devcryptohoppercode.visualstudioalphavestgithub
bash
CopyEdit
git checkout -b realtime-integration
- Create a new module:
data_collector/price_watcher.py
- Use WebSockets (preferred) or REST polling every N seconds:
- Spot:
wss://testnet.binance.vision/ws/btcusdt@kline_1m
- Futures:
wss://stream.binancefuture.com/ws/btcusdt@kline_1m
- Spot:
- Include fallback to REST polling via
/api/v3/klines
or/fapi/v1/klines
- Add retry/backoff mechanism in case of dropped WebSocket connection
- When new kline data arrives:
- Extract closing price
- Pass to your strategy (e.g.,
strategies/scalping.py
) - Collect signal (BUY, SELL, HOLD)
- Abstract executor behind an interface (mock vs live)
- Use
settings.USE_TESTNET
to route between:execution_engine.mock_executor.MockExecutor
execution_engine.executor.Executor
(new live class usingBinanceConnector
)
- Start small (0.001 BTCUSDT on testnet)
- Test:
- Market orders
- Cancel orders
- Error handling (invalid quantity, insufficient balance, etc.)
- Create
integration_tests/test_binance_connector.py
- Test:
- Successful
place_order
- Failure when keys are invalid
- Timeout retry logic
- Successful
- Use
utils/logger.py
to:- Log request payloads and responses
- Highlight retries and failed attempts
- Color-code trade success/failure
- Implement a kill switch (e.g., max daily trades)
- Log and cap risk per trade using
risk_manager/risk_control.py
- Add timestamped logs for auditing
websocket-client
orwebsockets
for Binance streamstenacity
for retries with exponential backoffpytest
for test scaffoldingrich
for better console debugging and formatting
-
Binance API Docs:
-
Testnet Account & Streams:
-
Real-time crypto bots:
Check GitHub:
freqtrade
,ccxt
,jesse
,ta-lib
for strategy inspiration -
Logging/Debugging:
Use Rich logging or Loguru