How alpacahq/example-hftish works
Main Components
• Trading Algorithm (tick_taker.py): This is the heart of the system where the logic for detecting bid/ask spread moves and executing trades is implemented.
• External Market Data Stream: Represents the incoming live market data from the Polygon streaming API that drives the algorithm.
• Trading API Integration: Represents the connection to Alpaca’s API that the script uses to execute trades. Both paper (testing) and live execution endpoints (or switching based on environment variables) should be highlighted.
• Environment & Configuration Setup: This includes the use of environment variables (APCA_API_KEY_ID, APCA_API_SECRET_KEY, and optionally the APCA_API_BASE_URL) which are part of the configuration process.
Relationships and Interactions
• The Trading Algorithm continuously listens to real-time market data from the Market Data Stream component.
• It processes this data to detect slight movements in the bid/ask spread.
• Once a trading signal is generated, the algorithm sends orders via the Trading API Integration component.
• The environment variables and command-line parameters (such as --symbol, --quantity, etc.) are used to configure the algorithm at startup.
Architectural Patterns and Principles
• The system follows a streaming-based approach for real-time data processing.
• The design leverages external services (Alpaca and Polygon) for data and trade execution, which means handling external dependencies, rate limits, and reliability are part of the overall system responsibilities.
Relevant Technologies
• Python 3.6+: The base language.
• Alpaca Trading API SDK: For order sending and account management.
• Polygon Streaming API: For receiving streaming market data.
• Pipenv: For dependency and environment management.