Broker Operations
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
Broker Operations in the hyperdrive library encompass the interfaces and logic required to interact with financial intermediaries and exchanges. This system provides a unified way to authenticate with services, retrieve historical market data, manage portfolio symbols, and execute trades. The primary implementation currently focuses on Robinhood for portfolio management and Binance for cryptocurrency exchange operations.
The module facilitates the transition from quantitative research to live execution by providing standardized classes like Robinhood and Binance that abstract the underlying API complexities. These operations are critical for maintaining up-to-date symbol lists and executing orders based on signals generated by the library's research components.
Sources: hyperdrive/Broker.py, hyperdrive/Exchange.py, hyperdrive/Algotrader.py
Robinhood Implementation
The Robinhood class provides the core functionality for traditional equity broker operations. It handles Multi-Factor Authentication (MFA) and provides methods to query holdings and historical data.
Authentication and Initialization
Initialization requires a username, password, and MFA code. If not provided directly, the class retrieves these from environment variables (RH_USERNAME, RH_PASSWORD, RH_2FA).
Sources: hyperdrive/Broker.py:16-32
Key Functions
| Function | Description |
|---|---|
get_hists | Retrieves historical data for a list of symbols with specified span and interval. |
get_names | Resolves stock symbols to their respective company names. |
save_symbols | Persists all symbols in the current portfolio to a CSV file. |
get_holdings | Fetches and builds a dictionary of current account holdings. |
Sources: hyperdrive/Broker.py:38-84
Data Flow for Symbol Management
The library uses automated scripts to sync local data with the broker state. The save_symbols method retrieves active holdings and saves them using the PathFinder utility.
Exchange Operations (Binance)
While Broker.py handles portfolio management, Exchange.py focuses on execution logic specifically for the Binance exchange.
Order Execution Logic
The Binance.order method calculates quantities based on a spend_ratio and account balance. It automatically adjusts for trading fees (0.1%) and rounds quantities to meet the exchange's stepSize or asset precision requirements.
Sources: hyperdrive/Exchange.py:25-66
Configuration Options
| Parameter | Type | Default | Description |
|---|---|---|---|
testnet | bool | False | Whether to use the Binance Testnet environment. |
spend_ratio | float | 1 | Percentage of available balance to use (0.0 to 1.0). |
side | str | N/A | Must be 'BUY' or 'SELL'. |
Sources: hyperdrive/Exchange.py:10-25, scripts/execute_order.py:24-28
Integration with Algotrader
The HyperDrive class in Algotrader.py serves as a high-level wrapper that composes these broker operations into a single interface. It initializes a Robinhood instance and provides an option to load the current portfolio upon instantiation.
Sources: hyperdrive/Algotrader.py:4-16
Conclusion
Broker operations in hyperdrive bridge the gap between data analysis and market interaction. By utilizing specialized classes for different providers (Robinhood and Binance), the system maintains a clean separation between portfolio tracking and trade execution. The inclusion of automated scripts for symbol updates and order execution ensures that the local environment remains synchronized with the live brokerage state.
Sources: hyperdrive/Broker.py, hyperdrive/Exchange.py, scripts/execute_order.py