Skip to content

Machine learning trading bot that trades while also training

License

Notifications You must be signed in to change notification settings

LorenzoZC/AmpyFin

 
 

Repository files navigation

AmpyFin Trading Bot

AmpyFin Logo

Introduction

Welcome to AmpyFin, an advanced AI-powered trading bot designed for the NASDAQ-100. Imagine having several expert traders working for you 24/7—AmpyFin makes this a reality.

Built with cutting-edge technology, AmpyFin constantly monitors market conditions, executes trades, and refines its strategies to ensure optimal performance. Whether you're an experienced trader or new to algorithmic trading, AmpyFin offers a robust, highly adaptable trading system that will elevate your trading strategies.

AmpyFin’s Data Collection Power

AmpyFin begins its operation by tapping into the Financial Modeling Prep API, collecting NASDAQ-100 ticker data to gain crucial market insights. This data is used to help the bot make informed decisions and set up trades.

To stay ahead in the fast-moving world of trading, AmpyFin uses the Polygon API to monitor real-time market status and feed the bot with the most current market conditions. This allows AmpyFin to execute trades based on up-to-the-minute data, ensuring swift, informed decision-making.

All collected data and trading logs are securely stored in MongoDB, enabling quick access to trading information and providing a secure backend for historical data and analysis.

Algorithms at Work

At the core of AmpyFin are its diverse algorithms, each designed to tackle different market conditions. The bot does not rely on just one strategy—AmpyFin simultaneously employs a variety of trading algorithms, each optimized for different market scenarios.

These algorithms range from fundamental strategies like mean reversion to more sophisticated, AI-driven approaches. The strategies are continually tested and refined in real-time market conditions, giving AmpyFin a competitive edge over traditional traders.

Some of the strategies AmpyFin employs include:

  • Mean Reversion: Predicts that asset prices will eventually return to their historical average.
  • Momentum: Capitalizes on prevailing market trends.
  • Arbitrage: Identifies and exploits price discrepancies between related assets.

Additionally, AmpyFin leverages its own AI-driven strategies to further enhance trading performance. These algorithms work collaboratively, with each one contributing its strength to the overall system.

How Dynamic Ranking Works

Managing multiple trading algorithms is no easy task. AmpyFin simplifies this by using a dynamic ranking system that ranks algorithms based on their performance—both real and simulated.

Each algorithm starts with an initial base score of 50,000 and is ranked based on profitability. The bot evaluates each algorithm's performance and assigns it a weight based on its rank. The ranking system uses a function to calculate how much influence each algorithm should have over the final trading decision. The function looks like this:

$$ \left( \frac{e^e}{e^2 - 1} \right)^{2i} $$

Where (i) is the inverse of the algorithm's ranking. The dynamic nature of the ranking system ensures that the highest-performing algorithms have more influence, while underperforming algorithms lose their weight in the decision-making process. This allows AmpyFin to adjust in real-time to changing market conditions.

The system is designed with a time delta coefficient, which ensures that recent trades are given greater weight in decision-making, though it is balanced to prevent extreme bias towards any single trade.

The dynamic ranking system allows AmpyFin to:

  • Adapt to ever-changing market conditions.
  • Prioritize high-performing algorithms.
  • Balance risk while maximizing potential returns.

API Endpoints

Features

  • NASDAQ-100 Ticker Retrieval: AmpyFin retrieves tickers using the Financial Modeling Prep API during early market hours.
  • Real-Time Market Monitoring: Polygon API is used to track market status (open, closed, premarket) and feed the bot with up-to-date market conditions.
  • Dynamic Algorithm Ranking System: AmpyFin adjusts its algorithm rankings based on real-time performance to prioritize the most profitable strategies.
  • Simulated Trading (Paper Trading): Provides a risk-free environment for testing strategies with an option to switch to live trading.
  • Data Storage: MongoDB is used to securely store market data, trading logs, and algorithm performance.
  • Customizable Strategies: Easily extendable to incorporate new trading strategies or adjust configurations based on market analysis.

File Structure and Objectives

client.py

  • Objective: Manages both trading and ranking clients.
  • Features:
    • Initiates and orchestrates trading and ranking operations.

trading_client.py

  • Objective: Executes trading based on algorithmic decisions.
  • Features:
    • Executes trading algorithms every 60 seconds.
    • Ensures a minimum balance of $15,000 and maintains 30% liquidity.
    • Logs trades with timestamps, stock details, and reasons.
    • Validates balance and prevents unauthorized selling.

ranking_client.py

  • 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 performance.
    • Refreshes rankings every 30 seconds.

ranking_setup.py

  • Objective: Initializes and populates MongoDB collections with strategy ranks and their corresponding coefficients.
  • Features:
    • MongoDB Connection: Connects to MongoDB using the provided connection string.
    • Initialize Rankings:
      • Sets up initial documents for each trading strategy in the algorithm_holdings collection.
      • Initializes the points_tally collection for tracking points for each strategy.
    • Insert Rank to Coefficient Mappings:
      • Clears existing entries in the rank_to_coefficient collection.
      • Calculates and inserts coefficient values for ranks from 1 up to a specified maximum rank.
    • Execution Flow:
      • Runs initialize_rank to set up necessary collections and documents.
      • Executes insert_rank_to_coefficient to populate rank-coefficient mappings.
      • Prints confirmation messages upon successful completion of each operation.

trading_strategies.py

  • Objective: Defines various trading strategies.
  • Features:
    • Includes strategies like mean reversion, momentum, and arbitrage.
    • Ensures consistency across strategy decision-making.

Helper Files

  • client_helper.py: Common functions for client operations (MongoDB setup, error handling).
  • ranking_helper.py: Functions for updating rankings based on performance.

Table of Contents

Installation

  1. Clone the Repository:

    git clone https://github.com/yeonholee50/AmpyFin.git
    cd AmpyFin
  2. Install Dependencies:

    pip install -r requirements.txt
  3. 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.
    • Database Structure:
      • market_data
        • market_structure
      • trading_simulator
        • algorithm_holdings
        • points_tally
        • rank
        • time_delta (make sure you also initialize a time_delta variable with a value of 0.01)
  4. Run ranking_setup.py

  • This initializes some tables in the db that we'll need later
  1. **See 'Usage' ***

Configuration

  1. Create config.py:
    • Copy config_template.py to config.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"
    MONGO_DB_STRING = "your_mongo_string"
    API_KEY = "your_alpaca_api_key"
    API_SECRET = "your_alpaca_secret_key"
    BASE_URL = "https://paper-api.alpaca.markets/v2"

API Setup

Polygon API

  1. Sign up at Polygon.io and get an API key.
  2. Add it to config.py as POLYGON_API_KEY.

Financial Modeling Prep API

  1. Sign up at Financial Modeling Prep and get an API key.
  2. Add it to config.py as FINANCIAL_PREP_API_KEY.

Alpaca API

  1. Sign up at Alpaca and get API keys.
  2. Add them to config.py as API_KEY and API_SECRET.

Usage

To start the bot, execute on two separate terminals:

python ranking_client.py
python trading_client.py

Make sure you have collected a few days worth of data by running ranking_client before running both simultaneously

Logging

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.

Notes

  • 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.

Contributing

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.

License

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

About

Machine learning trading bot that trades while also training

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 90.6%
  • JavaScript 5.7%
  • CSS 2.8%
  • HTML 0.9%