AmpyFin Trading Bot is a high-performance NASDAQ-100 trading bot that uses a ranked ensemble learning system with 50 trading algorithms. Each algorithm's rank dynamically adjusts based on the profitability of hypothetical buy/sell decisions, optimizing final trading decisions. The bot is configured for paper trading by default, allowing for safe testing and refinement of strategies. Transitioning to live trading is as simple as updating API keys and adjusting configuration settings.
- NASDAQ-100 Ticker Retrieval: Fetches tickers using the Financial Modeling Prep API during early market hours.
- Market Status Monitoring: Checks market status in real-time (open, closed, premarket) with the Polygon API.
- Algorithm Ranking System: Adjusts algorithm rankings based on performance to optimize trading decisions.
- Paper Trading: Safe testing environment, with live trading option available via configuration.
- Data Storage: Utilizes MongoDB for secure storage of ticker data and trading activity logs.
- Customizable: Allows for the extension of trading strategies and configurations.
Each trading algorithm starts with a base score of 0. Rankings are updated dynamically based on the algorithm’s profitability, using a coefficient calculated as:
where (i) is the inverse of the algorithm’s ranking. This creates a system where the highest-ranked algorithms contribute more heavily to the bot’s decisions, adapting to changing market conditions.
- Objective: Orchestrates both trading and ranking clients.
- Features:
- Initiates trading and ranking clients at appropriate times.
- Ensures trading operations during premarket and market hours.
- Includes error handling and logs system performance.
- Objective: Executes trading based on algorithmic decisions.
- Features:
- Executes trading algorithms every 60 seconds.
- Manages a minimum balance of $15,000 and maintains 30% liquidity.
- Logs all trades with timestamps, stock details, price, and reasons.
- Includes checks for sufficient balance, unauthorized selling, and automatic sell-off conditions.
- Objective: Runs the ranking algorithm to evaluate and rank trading strategies.
- Features:
- Downloads and stores NASDAQ-100 tickers in MongoDB.
- Executes strategies on each ticker.
- Updates algorithm scores based on trade success.
- Includes a 30-second interval between ranking updates.
- Objective: Defines multiple trading strategies with a standardized interface.
- Features:
- Includes strategies like mean reversion, momentum, and arbitrage.
- Ensures consistent decision-making across all strategies.
client_helper.py
: Common functions for client operations (MongoDB setup, error handling).ranking_helper.py
: Functions for updating rankings based on performance.
-
Clone the Repository:
git clone https://github.com/yeonholee50/polygon-trading-bot.git cd polygon-trading-bot
-
Install Dependencies:
pip install -r requirements.txt
-
Set Up MongoDB:
- Sign up for a MongoDB cluster (e.g., via MongoDB Atlas).
- Get your MongoDB connection string and create a database for stock data storage.
- Create
config.py
:- Copy
config_template.py
toconfig.py
and enter your API keys and MongoDB credentials.
POLYGON_API_KEY = "your_polygon_api_key" FINANCIAL_PREP_API_KEY = "your_fmp_api_key" MONGO_DB_USER = "your_mongo_user" MONGO_DB_PASS = "your_mongo_password" API_KEY = "your_alpaca_api_key" API_SECRET = "your_alpaca_secret_key" BASE_URL = "https://paper-api.alpaca.markets"
- Copy
- Sign up at Polygon.io and get an API key.
- Add it to
config.py
asPOLYGON_API_KEY
.
- Sign up at Financial Modeling Prep and get an API key.
- Add it to
config.py
asFINANCIAL_PREP_API_KEY
.
- Sign up at Alpaca and get API keys.
- Add them to
config.py
asAPI_KEY
andAPI_SECRET
.
To start the bot, execute on two separate terminals:
python ranking_client.py
python trading_client.py
The bot logs all major events and errors to a system.log
file, including API errors, MongoDB operations, and market status checks. You can access the log file to review the bot's activities and diagnose potential issues. The bot will also log rank events to a separate rank.log
file.
- The bot is limited to 250 API calls per day (as per the Polygon API free tier).
- Future enhancements can include adding custom trading strategies or integrating with a brokerage API for live trading.
Contributions are welcome! Feel free to open a pull request or submit issues for bugs or feature requests. Future improvements may focus on optimizing the ranking system or expanding the bot's capabilities for more advanced trading strategies.
This project is licensed under the MIT License. See the LICENSE file for details.