A powerful application that uses Azure OpenAI to interpret natural language trading strategies, generate executable Python code, and backtest them against real-time cryptocurrency market data from Binance.
- Natural Language Processing: Describe your trading strategy in plain English
- Automated Code Generation: Convert natural language into optimized Python trading code
- Real-Time Market Data: Connect to Binance for real cryptocurrency OHLCV data
- Professional Backtesting: Test strategies with detailed performance metrics
- Interactive Visualization: View equity curves and trade logs
- Azure OpenAI Integration: Leverage the power of Azure OpenAI for strategy interpretation and code generation
- Python 3.7+ (3.13+ recommended)
- Azure OpenAI API key and endpoint
- Binance API key and secret (for live data)
-
Clone the repository:
git clone <repository-url> cd crypto-trading-strategy-generator
-
Create and activate a virtual environment:
python -m venv venv # On Windows venv\Scripts\activate # On macOS/Linux source venv/bin/activate
-
Install dependencies:
pip install -r requirements.txt
-
Configure environment variables: Create a
.envfile in the root directory with the following:AZURE_API_KEY=your_azure_api_key_here AZURE_OPENAI_ENDPOINT=https://your-azure-instance.openai.azure.com AZURE_DEPLOYMENT_NAME=your_deployment_name AZURE_API_VERSION=2025-01-01-preview BINANCE_TESTNET_API_KEY=your_binance_api_key_here BINANCE_TESTNET_API_SECRET=your_binance_api_secret_here
-
Build the Docker image:
docker build -t crypto-strategy-generator . -
Run the container:
docker run -p 8501:8501 \ -e AZURE_API_KEY=your_azure_api_key_here \ -e AZURE_OPENAI_ENDPOINT=your_endpoint \ -e AZURE_DEPLOYMENT_NAME=your_deployment_name \ -e BINANCE_TESTNET_API_KEY=your_binance_key \ -e BINANCE_TESTNET_API_SECRET=your_binance_secret \ crypto-strategy-generator
- Clone or download the repository
- Navigate to the project directory
# Simply run the batch file
run_app.bat# First make the script executable (one-time only)
chmod +x run_app.sh
# Then run it
./run_app.shThe script will automatically:
- Create a virtual environment if needed
- Install all required dependencies
- Test imports to verify everything works
- Start the Streamlit application
If you encounter issues with TA-Lib installation, install it via Homebrew:
brew install ta-libIf you encounter cryptography build errors, install these packages:
sudo apt-get update
sudo apt-get install build-essential libssl-dev-
Start the Streamlit application using the provided scripts:
# On Windows run_app.bat # On macOS/Linux chmod +x run_app.sh # Make the script executable (first time only) ./run_app.sh
Alternatively, you can start it manually:
# Activate virtual environment first # On Windows venv\Scripts\activate # On macOS/Linux source venv/bin/activate # Then run the app streamlit run main.py Or, venv\Scripts\streamlit run main.py
-
Access the app in your browser at
http://localhost:8501 -
Enter your trading strategy in natural language, for example:
Buy 0.5 BTC when the 50-period EMA crosses above the 200-period EMA and RSI(14) is below 40, exit when RSI(14) exceeds 70 or MACD shows a bearish crossover with a 1h time period -
Follow the steps in the UI to:
- Generate strategy parameters
- Visualize historical data
- Generate Python code
- Backtest the strategy
- View performance metrics
Buy BTC when RSI(14) falls below 30 on a 4h timeframe, sell when RSI goes above 70
Buy ETH when the 20-day EMA crosses above the 50-day EMA with volume increasing, sell when price drops 5% from peak
Buy ADA when MACD shows bullish crossover and price is below lower Bollinger Band (20,2), exit when price touches upper Bollinger Band
For production deployment, consider the following:
- API Key Security: Store API keys in a secure location using environment variables or a secrets manager
- Rate Limiting: Implement API rate limiting to prevent excessive API calls
- Monitoring: Add comprehensive logging and monitoring
- Authentication: Add user authentication for public-facing deployments
- Cloud Deployment: Deploy to AWS, Azure, or Google Cloud with proper scaling
# Build and push Docker image to ECR
aws ecr create-repository --repository-name crypto-strategy-generator
docker build -t <your-aws-account-id>.dkr.ecr.<region>.amazonaws.com/crypto-strategy-generator:latest .
aws ecr get-login-password | docker login --username AWS --password-stdin <your-aws-account-id>.dkr.ecr.<region>.amazonaws.com
docker push <your-aws-account-id>.dkr.ecr.<region>.amazonaws.com/crypto-strategy-generator:latest
# Deploy to ECS or Fargate
# Follow AWS documentation for detailed steps-
Missing Modules: If you encounter "No module named X", make sure you've installed all dependencies with
pip install -r requirements.txt- Note about module names: Some packages use different names for pip installation versus Python imports:
python-dotenvpackage →import dotenvrpds-pypackage →import rpds
- Note about module names: Some packages use different names for pip installation versus Python imports:
-
API Connection Issues: Check your API keys and internet connection
-
No Trading Signals: Try relaxing your strategy conditions (e.g., RSI < 40 instead of RSI < 30)
-
Error with rpds-py: If you encounter issues with rpds-py, reinstall with
pip install --force-reinstall rpds-py -
Module import failures: The application includes an
import_test.pyscript to verify all required modules can be imported. Run it to diagnose import issues:python import_test.py
The application consists of the following main components:
- NLP Handler (
app/nlp_handler.py): Interprets natural language strategy descriptions using Azure OpenAI - Strategy Generator (
app/strategy_generator.py): Generates executable Python code from strategy parameters - Data Handler (
app/data_handler.py): Fetches historical cryptocurrency data from Binance - Backtester (
app/backtester.py): Executes and evaluates trading strategies against historical data - UI (
main.py): Streamlit interface for interacting with the system
Run the test suite to ensure all components are working properly:
python tests/run_tests.pyContributions are welcome! Please feel free to submit a Pull Request.
- Streamlit for the interactive web interface
- Azure OpenAI for NLP capabilities
- pandas-ta for technical analysis indicators
- Binance API for market data
