Welcome to AmpyFin, an advanced AI-powered trading bot designed for the NASDAQ-100. Imagine having expert traders working for you 24/7βAmpyFin makes this a reality.
- Financial Modeling Prep API: Retrieves NASDAQ-100 tickers to gain crucial market insights.
- Polygon API: Monitors real-time market conditions, ensuring that the bot acts based on the most current data.
All data and trading logs are securely stored in MongoDB, allowing fast access to historical trading information and supporting in-depth analysis.
At the core of AmpyFin are diverse algorithms optimized for different market conditions. Rather than relying on a single strategy or multiple strategies, AmpyFin relies on a ranked ensemble learning system that dynamically ranks each strategy and gives more influence in the final decision to strategies with better performance.
Some of the strategies AmpyFin employs include:
- π Mean Reversion: Predicts asset prices will return to their historical average.
- π Momentum: Capitalizes on prevailing market trends.
- π± Arbitrage: Identifies and exploits price discrepancies between related assets.
- π§ AI-Driven Custom Strategies: Continuously refined through machine learning for enhanced performance.
These strategies work collaboratively, ensuring AmpyFin is always prepared for changing market dynamics.
Managing multiple algorithms is simplified with AmpyFinβs dynamic ranking system, which ranks each algorithm based on performance.
Each strategy starts with a base score of 0 and a mock balance of $50,000. The system evaluates their performance and assigns a weight based on the following function:
Where (i) is the strategy's rank. Please keep in mind that the strategy's rank is inverse of its performance. So a strategy ranked 132 is actually performing the best while strategy ranked 1 is performing the worst currently.
This ensures that strategies with better recent performance have a greater influence on decision-making while maintaining balance by also accounting for old performance as well.
- π Quickly adapts to changing market conditions.
- π Prioritizes high-performing algorithms.
- βοΈ Balances risk while maximizing potential returns.
Objective: Executes trades based on algorithmic decisions.
Features:
- Executes trades every 60 seconds by default (adjustable based on user).
- Ensures a minimum spending balance of $15,000 (adjustable based on user) and maintains 30% liquidity (adjustable based on user).
- Logs trades with details like timestamp, stock, and reasoning.
Objective: Runs the ranking system to evaluate trading strategies.
Features:
- Downloads NASDAQ-100 tickers and stores them in MongoDB.
- Updates algorithm scores and rankings every 120 seconds (adjustable based on user).
Objective: Defines various trading strategies. Houses strategies like mean reversion, momentum, and arbitrage.
Features:
- trading_strategies_v1.py: Archived first iteration of AmpyFin used 5 strategies. This file is not supported anymore but is a great reference material
- trading_strategies_v2.py: Archived second gen older strategies being used in the ranking system. Contains 50 strategies with a lot leaning towards momentum.
- trading_strategies_v2_1.py: Archived second gen older strategies that complements the older strategies in trading_strategies_v2.py. Houses 10 more strategies. This is where newer strategies will be implemented until it caps at 50 strategies as well.
- talib_indicators.py: Contains all the technical indicators used in the strategies. To visit the documentation for each technical indicator, please visit the following link: Link to TA. These indicators were not developed by me, but I have modified their use to fit the needs of AmpyFin. Each indicator is fine tuned with a specific period and historical data is either retrieved from MongoDB cache system or from yfinance.
Objective: Helper Files to help with both trading client and ranking client. Houses functions for retrieving a Mongo Client, getting latest prices, current strategies implemented etc.
Features:
- client_helper.py: Contains common functions for client operations in both ranking and trading.
Objective: Contains utility functions for data processing and analysis as well as other miscellaneous functions. These functions are not necessarily being used currently in trading or ranking but stored for development purposes. Features:
- check_strategy_scores.py: Checks the scores of the strategies and prints them out.
- sell_all.py: Sells all the stocks in the portfolio.
- sync_alpaca.py: Syncs the Alpaca account with the MongoDB account.
git clone https://github.com/yeonholee50/AmpyFin.git
cd AmpyFin
- Run the following command to install the required Python packages:
pip install -r requirements.txt
- We have recently migrated to using Ta-Lib for trading. Please follow the installation instructions here:
π Ta-Lib Python Easy Installation
- 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" mongo_url = "your mongo connection string"
- Copy
- Polygon API
- Sign up at Polygon.io and get an API key.
- Add it to
config.py
asPOLYGON_API_KEY
.
- Financial Modeling Prep API
- Sign up at Financial Modeling Prep and get an API key.
- Add it to
config.py
asFINANCIAL_PREP_API_KEY
.
- Alpaca API
- Sign up at Alpaca and get API keys.
- Add them to
config.py
asAPI_KEY
andAPI_SECRET
.
- Sign up for a MongoDB cluster (e.g., via MongoDB Atlas).
- Create a database for stock data storage and replace the
mongo_url
in 'config.py' with your connection string. Make sure to give yourself Network Access. - Run the setup script
setup.py
: - After running the mongo setup script, the MongoDB setup for the rest will be completed on the first minute in trading for both ranking and trading.
To run the bot, execute on two separate terminals:
python ranking_client.py
python trading_client.py
For people looking to do live trading, I suggest training via running ranking_client.py for at least two weeks before running the trading bot altogether. This way, you're running with a client that has been trained to a certain extent (with strategies ranked) and is ready to go. Otherwise, you will most likely be buying random stocks.
- system.log: Tracks major events like API errors and MongoDB operations.
- rank_system.log: Logs all ranking-related events and updates.
Contributions are welcome! π Feel free to submit pull requests or report issues. All contributions should be made on the test branch. Please avoid committing directly to the main branch.
This project is licensed under the MIT License. See the LICENSE file for details.