### Install pynacl Source: https://docs.robinhood.com/crypto/trading Install the pynacl library using pip to run the Python key generation script. ```bash pip install pynacl ``` -------------------------------- ### Install PyNaCl Library Source: https://docs.robinhood.com/crypto/trading Install the PyNaCl library, which is required for cryptographic operations used in API authentication. ```bash pip install pynacl ``` -------------------------------- ### Response Sample for Get Crypto Trading Accounts Source: https://docs.robinhood.com/crypto/trading This is a sample JSON response for the GET /api/v2/crypto/trading/accounts/ endpoint, illustrating the structure of account data returned. ```json { "next": "string", "previous": "string", "results": [ { "account_number": "string", "status": "active", "buying_power": "string", "buying_power_currency": "string", "account_type": "string", "is_api_tradable": true, "fee_tier_status": { "fee_ratio": 0.1, "thirty_day_volume": 0.1, "next_fee_tier_ratio": 0.1, "next_fee_tier_threshold": 0.1 } } ] } ``` -------------------------------- ### Response Sample for Get Best Price Source: https://docs.robinhood.com/crypto/trading This JSON response sample corresponds to the GET /api/v2/crypto/marketdata/best_bid_ask/ endpoint, showing the structure for best bid and ask prices. ```json { "results": [ { "symbol": "string", "bid": 0.1, "ask": 0.1 } ] } ``` -------------------------------- ### Get All Crypto Orders (v1) Source: https://docs.robinhood.com/crypto/trading Fetch a list of all crypto orders associated with the account. This uses a simple GET request to the orders endpoint. ```python def get_orders(self) -> Any: path = "/api/v1/crypto/trading/orders/" return self.make_api_request("GET", path) ``` -------------------------------- ### Response Sample for Get Estimated Price Source: https://docs.robinhood.com/crypto/trading This JSON response sample illustrates the output for the GET /api/v2/crypto/trading/estimated_price/ endpoint, detailing estimated costs and credits for trades. ```json { "results": [ { "symbol": "string", "side": "bid", "quantity": 0.1, "timestamp": "2019-08-24T14:15:22Z", "bid": 0.1, "ask": 0.1, "fee_ratio": 0.1, "est_fee": 0.1, "est_total_cost": 0.1, "est_total_credit": 0.1 } ] } ``` -------------------------------- ### Main Function Execution Source: https://docs.robinhood.com/crypto/trading The main function demonstrates how to instantiate the client and call various API methods. It includes commented-out examples for different functionalities. ```python def main(): api_trading_client = CryptoAPITradingV2() print(api_trading_client.get_trading_pairs()) # print(api_trading_client.get_trading_pairs("BTC-USD", "ETH-USD")) # print(api_trading_client.get_best_bid_ask("BTC-USD")) #"BTC-USD", "ETH-USD" # print(api_trading_client.get_estimated_price(symbol="BTC-USD", side="both", quantity="0.01")) # accounts = api_trading_client.get_accounts() # print(accounts) # account_number = accounts["results"][0]["account_number"] # print(f"account_number: {account_number}") # print(api_trading_client.get_orders(account_number)) # print(api_trading_client.get_holdings(account_number)) # print(api_trading_client.get_holdings(account_number, "BTC", "ETH")) """ BUILD YOUR TRADING STRATEGY HERE order = api_trading_client.place_order( account_number, str(uuid.uuid4()), "buy", "market", "BTC-USD", {"asset_quantity": "0.000001"} ) """ if __name__ == "__main__": main() ``` -------------------------------- ### Get Accounts Source: https://docs.robinhood.com/crypto/trading Fetches a list of user accounts. This is a GET request to the /api/v2/accounts/ endpoint. ```python # accounts = api_trading_client.get_accounts() # print(accounts) # account_number = accounts["results"][0]["account_number"] # print(f"account_number: {account_number}") ``` -------------------------------- ### Get All Trading Pairs Source: https://docs.robinhood.com/crypto/trading Fetches all available trading pairs. This is a basic GET request to the /api/v2/crypto/trading/pairs/ endpoint. ```python api_trading_client.get_trading_pairs() ``` -------------------------------- ### Get Specific Trading Pairs Source: https://docs.robinhood.com/crypto/trading Fetches specific trading pairs by providing a list of symbols. This is a GET request with query parameters. ```python # print(api_trading_client.get_trading_pairs("BTC-USD", "ETH-USD")) ``` -------------------------------- ### Get Best Bid/Ask Prices Source: https://docs.robinhood.com/crypto/trading Retrieves the best bid and ask prices for specified trading symbols. This is a GET request to the /api/v2/crypto/marketdata/best_bid_ask/ endpoint. ```python # print(api_trading_client.get_best_bid_ask("BTC-USD")) #"BTC-USD", "ETH-USD" ``` -------------------------------- ### Get Order Details Source: https://docs.robinhood.com/crypto/trading Retrieves the details of a specific order for a given account. ```APIDOC ## GET /api/v2/crypto/trading/orders/{order_id}/ ### Description Retrieves the details of a specific order for a given account. ### Method GET ### Endpoint /api/v2/crypto/trading/orders/{order_id}/ ### Parameters #### Path Parameters - **order_id** (string) - Required - The ID of the order to retrieve. #### Query Parameters - **account_number** (string) - Required - The account number associated with the order. ### Response #### Success Response (200) - **order_id** (string) - The ID of the order. - **status** (string) - The current status of the order. - **symbol** (string) - The trading pair. - **side** (string) - The side of the order (buy/sell). - **type** (string) - The type of order. #### Response Example { "order_id": "order-12345", "status": "filled", "symbol": "BTC-USD", "side": "buy", "type": "market" } ``` -------------------------------- ### Get Orders Source: https://docs.robinhood.com/crypto/trading Retrieves a list of orders for a specified account, with an option to filter by creation date. ```APIDOC ## GET /api/v2/crypto/trading/orders/ ### Description Retrieves a list of orders for a specified account, with an option to filter by creation date. ### Method GET ### Endpoint /api/v2/crypto/trading/orders/ ### Parameters #### Query Parameters - **account_number** (string) - Required - The account number for which to retrieve orders. - **created_at_start** (string) - Optional - Filters orders created at or after this ISO 8601 timestamp (e.g., "2023-01-01T20:57:50Z"). ### Response #### Success Response (200) - **results** (list) - A list of orders. - **next** (string) - URL for the next page of results, if available. #### Response Example { "results": [ { "order_id": "order-12345", "status": "filled", "symbol": "BTC-USD", "side": "buy", "type": "market" } ], "next": "/api/v2/crypto/trading/orders?account_number=12345&cursor=next_cursor" } ``` -------------------------------- ### Get a Specific Crypto Order (v1) Source: https://docs.robinhood.com/crypto/trading Retrieve details for a specific crypto order by providing its order ID. Uses a GET request to the order's detail endpoint. ```python def get_order(self, order_id: str) -> Any: path = f"/api/v1/crypto/trading/orders/{order_id}/" return self.make_api_request("GET", path) ``` -------------------------------- ### Get Account Information Source: https://docs.robinhood.com/crypto/trading Retrieves information about the user's Robinhood trading account. ```APIDOC ## GET /api/v1/crypto/trading/accounts/ ### Description Retrieves information about the user's Robinhood trading account. ### Method GET ### Endpoint /api/v1/crypto/trading/accounts/ ### Parameters None ### Request Example None ### Response #### Success Response (200) - **account_info** (object) - Details of the trading account. ``` -------------------------------- ### Get Best Bid/Ask Prices Source: https://docs.robinhood.com/crypto/trading Fetches the best bid and ask prices for specified trading symbols. ```APIDOC ## GET /api/v2/crypto/marketdata/best_bid_ask ### Description Fetches the best bid and ask prices for specified trading symbols. ### Method GET ### Endpoint /api/v2/crypto/marketdata/best_bid_ask ### Parameters #### Query Parameters - **symbol** (list of strings) - Required - A list of trading pairs (e.g., "BTC-USD", "ETH-USD"). ### Response #### Success Response (200) - **symbol** (string) - The trading pair. - **bid_price** (string) - The best bid price. - **ask_price** (string) - The best ask price. #### Response Example { "BTC-USD": { "bid_price": "49900.00", "ask_price": "50100.00" } } ``` -------------------------------- ### Get Trading Pairs (v2) Source: https://docs.robinhood.com/crypto/trading Fetches information about supported crypto trading pairs. Can filter by specific symbols or retrieve all available pairs if no symbols are provided. ```python # The symbols argument must be formatted in trading pairs, e.g "BTC-USD", "ETH-USD". If no symbols are provided, # all supported symbols will be returned def get_trading_pairs(self, *symbols: Optional[str]) -> list: params = {"symbol": list(symbols)} if symbols else {} query_params = self.get_query_params(params) path = f"/api/v2/crypto/trading/trading_pairs/{query_params}" all_results = [] response = self.make_api_request("GET", path) while response: ``` -------------------------------- ### Get Orders Source: https://docs.robinhood.com/crypto/trading Fetches a list of orders for the current user in a specific account. Supports filtering by various criteria. ```APIDOC ## GET /api/v2/crypto/trading/orders/ ### Description Fetches a list of orders for the current user in a specific account. Supports filtering by various criteria. ### Method GET ### Endpoint /api/v2/crypto/trading/orders/ ### Parameters #### Query Parameters - **account_number** (string) - Required - The number of the crypto account to retrieve orders from. - **cursor** (string) - Optional - Cursor for pagination. - **created_at_start** (string) - Optional - Filter orders created on or after this timestamp (ISO 8601 format). - **created_at_end** (string) - Optional - Filter orders created on or before this timestamp (ISO 8601 format). - **updated_at_start** (string) - Optional - Filter orders updated on or after this timestamp (ISO 8601 format). - **updated_at_end** (string) - Optional - Filter orders updated on or before this timestamp (ISO 8601 format). - **symbol** (string) - Optional - Filter orders by trading pair symbol. - **side** (string) - Optional - Enum: "buy" "sell" - Filter orders by side. - **type** (string) - Optional - Enum: "market" "limit" "stop_loss" "stop_limit" - Filter orders by type. - **state** (string) - Optional - Enum: "open" "canceled" "filled" "failed" "pending" - Filter orders by state. ### Response #### Success Response (200) (Response details not provided in the source text, but typically includes a list of order objects.) #### Response Example (Response example not provided in the source text.) ``` -------------------------------- ### Get Authorization Header (v2) Source: https://docs.robinhood.com/crypto/trading Generates the necessary authorization headers for API requests, including API key, timestamp, and a signature generated using the private key. ```python def get_authorization_header( self, method: str, path: str, body: str, timestamp: int ) -> Dict[str, str]: message_to_sign = f"{self.api_key}{timestamp}{path}{method}{body}" signed = self.private_key.sign(message_to_sign.encode("utf-8")) return { "x-api-key": self.api_key, "x-signature": base64.b64encode(signed.signature).decode("utf-8"), "x-timestamp": str(timestamp), } ``` -------------------------------- ### Get Crypto Trading Pairs Source: https://docs.robinhood.com/crypto/trading Fetch a list of available crypto trading pairs. This endpoint provides details such as asset codes, increments, order sizes, and trading status. It can be filtered by symbol. ```bash curl -X "GET" "https://trading.robinhood.com/api/v1/crypto/trading/trading_pairs/?symbol=BTC-USD" \ -H 'x-api-key: ' \ -H 'x-timestamp: ' \ -H 'x-signature: ' \ -H 'Content-Type: application/json; charset=utf-8' ``` -------------------------------- ### Get Crypto Orders Source: https://docs.robinhood.com/crypto/trading Fetch a list of orders for the current user. This endpoint supports filtering by creation and update times, symbol, order ID, side, state, and type. It also allows for pagination using a cursor and limit. ```APIDOC ## GET /api/v1/crypto/trading/orders/ ### Description Fetch a list of orders for the current user. This endpoint supports filtering by creation and update times, symbol, order ID, side, state, and type. It also allows for pagination using a cursor and limit. ### Method GET ### Endpoint https://trading.robinhood.com/api/v1/crypto/trading/orders/ ### Parameters #### Query Parameters - **created_at_start** (string) - Optional - Filter by created at start time range (greater than or equal to) in ISO 8601 format. - **created_at_end** (string) - Optional - Filter by created at end time range (less than or equal to) in ISO 8601 format. - **symbol** (string) - Optional - Currency pair symbol. Ensure that the symbol is provided in all uppercase. - **id** (string) - Optional - Order ID, unique per order. - **side** (string) - Optional - Enum: "buy" "sell" - Buy or sell. - **state** (string) - Optional - Enum: "open" "canceled" "partially_filled" "filled" "failed" - State of the order. - **type** (string) - Optional - Enum: "limit" "market" "stop_limit" "stop_loss" - Type of order. - **updated_at_start** (string) - Optional - Filter by updated at start time range (greater than or equal to) in ISO 8601 format. - **updated_at_end** (string) - Optional - Filter by updated at end time range (less than or equal to) in ISO 8601 format. - **cursor** (string) - Optional - Cursor for pagination. - **limit** (integer) - Optional - Limit the number of results per page size. ### Request Example ``` curl -X "GET" "https://trading.robinhood.com/api/v1/crypto/trading/orders/" -H 'x-api-key: ' -H 'x-timestamp: ' -H 'x-signature: ' -H 'Content-Type: application/json; charset=utf-8' ``` ### Response #### Success Response (200) - **next** (string) - URL for the next page of results. - **previous** (string) - URL for the previous page of results. - **results** (array) - An array of order objects. - **id** (string) - Unique identifier for the order. - **account_number** (string) - The account number associated with the order. - **symbol** (string) - The currency pair symbol. - **client_order_id** (string) - User-defined order ID for idempotency. - **side** (string) - The side of the order (buy or sell). - **executions** (array) - Details of order executions. - **effective_price** (string) - The effective price of the execution. - **quantity** (string) - The quantity executed. - **timestamp** (string) - The timestamp of the execution. - **type** (string) - The type of the order. - **state** (string) - The current state of the order. - **average_price** (number) - The average price of the order. - **filled_asset_quantity** (number) - The quantity of the asset that has been filled. - **created_at** (string) - The timestamp when the order was created. - **updated_at** (string) - The timestamp when the order was last updated. - **market_order_config** (object) - Configuration for market orders. - **limit_order_config** (object) - Configuration for limit orders. - **stop_loss_order_config** (object) - Configuration for stop-loss orders. - **stop_limit_order_config** (object) - Configuration for stop-limit orders. #### Response Example ```json { "next": "https://trading.robinhood.com/api/v1/crypto/trading/orders/?cursor={CURSOR_ID}", "previous": "https://trading.robinhood.com/api/v1/crypto/trading/orders/?cursor={CURSOR_ID}", "results": [ { "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08", "account_number": "string", "symbol": "string", "client_order_id": "11299b2b-61e3-43e7-b9f7-dee77210bb29", "side": "buy", "executions": [ { "effective_price": "string", "quantity": "string", "timestamp": "2019-08-24T14:15:22Z" } ], "type": "limit", "state": "open", "average_price": 0.1, "filled_asset_quantity": 0.1, "created_at": "string", "updated_at": "string", "market_order_config": {}, "limit_order_config": { "quote_amount": 0.1, "asset_quantity": 0.1, "limit_price": 0.1 }, "stop_loss_order_config": { "quote_amount": 0.1, "asset_quantity": 0.1, "stop_price": 0.1, "time_in_force": "gtc" }, "stop_limit_order_config": { "quote_amount": 0.1, "asset_quantity": 0.1, "limit_price": 0.1, "stop_price": 0.1, "time_in_force": "gtc" } } ] } ``` ``` -------------------------------- ### Make API Request (v2) Source: https://docs.robinhood.com/crypto/trading Handles making HTTP requests (GET, POST) to the Robinhood API. Includes error handling for request exceptions and non-success status codes. ```python def make_api_request(self, method: str, path: str, body: str = "") -> Any: timestamp = self._get_current_timestamp() headers = self.get_authorization_header(method, path, body, timestamp) url = self.base_url + path try: response = {} if method == "GET": response = requests.get(url, headers=headers, timeout=10) elif method == "POST": response = requests.post(url, headers=headers, json=json.loads(body) if body else None, timeout=10) # Check for non-success status codes if response.status_code >= 400: print(f"HTTP Error {response.status_code}: {response.reason}") try: return response.json() except json.JSONDecodeError: return {"error": response.text or response.reason, "status_code": response.status_code} return response.json() except requests.RequestException as e: print(f"Error making API request: {e}") return None ``` -------------------------------- ### Get Best Bid/Ask Source: https://docs.robinhood.com/crypto/trading Retrieves the best bid and ask prices for specified trading symbols. If no symbols are provided, it returns data for all supported symbols. ```APIDOC ## GET /api/v1/crypto/marketdata/best_bid_ask/ ### Description Retrieves the best bid and ask prices for specified trading symbols. If no symbols are provided, it returns data for all supported symbols. ### Method GET ### Endpoint /api/v1/crypto/marketdata/best_bid_ask/ ### Parameters #### Query Parameters - **symbol** (string) - Optional - The trading pair symbol (e.g., "BTC-USD", "ETH-USD"). Multiple symbols can be provided. ### Request Example None ### Response #### Success Response (200) - **best_bid_ask** (object) - An object containing the best bid and ask prices for the requested symbols. ``` -------------------------------- ### Get Best Price Source: https://docs.robinhood.com/crypto/trading Fetches the best bid and ask price for specified crypto symbols. This price represents the best available across partner market makers and may not be the final execution price. Requires API Key, Signature, and Timestamp for authorization. ```APIDOC ## Get Best Price Fetch a single bid and ask price per symbol, representing the best available price across our partner market makers. This price does not take into account the order size, and may not be the final execution price. ### Method GET ### Endpoint /api/v1/crypto/marketdata/best_bid_ask/ ### Parameters #### Query Parameters - **symbol** (string) - Required - List of trading pair symbol(s) to retrieve data for. Multiple symbols can be provided by using the "symbol" parameter with different values. Ensure that the symbol(s) are provided in all uppercase. For example, **?symbol=BTC-USD &symbol=ETH-USD** ### Responses #### Success Response (200) - **results** (array) - A list of best bid/ask prices for the requested symbols. - **symbol** (string) - The trading symbol. - **price** (number) - The best price. - **bid_inclusive_of_sell_spread** (number) - The bid price including the sell spread. - **sell_spread** (number) - The sell spread. - **ask_inclusive_of_buy_spread** (number) - The ask price including the buy spread. - **buy_spread** (number) - The buy spread. - **timestamp** (string) - The timestamp of the price data. ### Request Example ```curl curl -X "GET" "https://trading.robinhood.com/api/v1/crypto/marketdata/best_bid_ask/?symbol=BTC-USD" \ -H 'x-api-key: ' \ -H 'x-timestamp: ' \ -H 'x-signature: ' \ -H 'Content-Type: application/json; charset=utf-8' ``` ### Response Example (200) { "results": [ { "symbol": "string", "price": 0.1, "bid_inclusive_of_sell_spread": 0.1, "sell_spread": 0.1, "ask_inclusive_of_buy_spread": 0.1, "buy_spread": 0.1, "timestamp": "string" } ] } ``` -------------------------------- ### Get Estimated Crypto Price Source: https://docs.robinhood.com/crypto/trading Retrieve the estimated total cost or credit for a given symbol, book side, and asset quantity. This is useful for understanding potential execution prices before placing an order. Multiple quantities can be queried in a single request. ```bash curl -X "GET" "https://trading.robinhood.com/marketdata/api/v1/estimated_price/?symbol=BTC-USD&side=ask&quantity=0.1,1,1.999" \ -H 'x-api-key: ' \ -H 'x-timestamp: ' \ -H 'x-signature: ' \ -H 'Content-Type: application/json; charset=utf-8' ``` -------------------------------- ### Create Project Directory and Files Source: https://docs.robinhood.com/crypto/trading This snippet shows how to create a new directory for your project and initialize Python script files for API interaction. ```bash mkdir robinhood-api-trading && cd robinhood-api-trading touch robinhood_api_trading.py touch robinhood_api_trading_v2.py ``` -------------------------------- ### Initialize Crypto API Trading Client (v2) Source: https://docs.robinhood.com/crypto/trading Sets up the v2 crypto trading client, including API key and private key for authentication. Ensure API_KEY and BASE64_PRIVATE_KEY are correctly set. ```python API_KEY = "ADD YOUR API KEY HERE" BASE64_PRIVATE_KEY = "ADD YOUR PRIVATE KEY HERE" class CryptoAPITradingV2: def __init__(self): self.api_key = API_KEY private_key_seed = base64.b64decode(BASE64_PRIVATE_KEY) self.private_key = SigningKey(private_key_seed) self.base_url = "https://trading.robinhood.com" ``` -------------------------------- ### Running the Script Source: https://docs.robinhood.com/crypto/trading Instructions on how to execute the Python script from the command line. ```bash 5. Run your script from the command line ``` -------------------------------- ### Get Orders for an Account Source: https://docs.robinhood.com/crypto/trading Retrieves all orders for a specific account, with an option to filter by creation date. This is a GET request to the /api/v2/crypto/trading/orders/ endpoint. ```python # print(api_trading_client.get_orders(account_number)) ``` -------------------------------- ### Place a Crypto Order (v1) Source: https://docs.robinhood.com/crypto/trading Use this method to place a new crypto order. Ensure you have the necessary order details like side, type, symbol, and order configuration. ```python def place_order(self, order_id: str, side: str, order_type: str, symbol: str, order_config: Dict[str, Any]) -> Any: body = { "side": side, "type": order_type, "symbol": symbol, f"{order_type}_order_config": order_config, } path = "/api/v1/crypto/trading/orders/" return self.make_api_request("POST", path, json.dumps(body)) ``` -------------------------------- ### Get Estimated Price Source: https://docs.robinhood.com/crypto/trading Calculates the estimated price for a given symbol, side (bid/ask), and quantity. This is a GET request to the /api/v2/crypto/trading/estimated_price/ endpoint. ```python # print(api_trading_client.get_estimated_price(symbol="BTC-USD", side="both", quantity="0.01")) ``` -------------------------------- ### Get Current Timestamp (v2) Source: https://docs.robinhood.com/crypto/trading Helper method to get the current UTC timestamp in seconds, used for API request signing. ```python @staticmethod def _get_current_timestamp() -> int: return int(datetime.datetime.now(tz=datetime.timezone.utc).timestamp()) ``` -------------------------------- ### Run Python Script with v1 API Source: https://docs.robinhood.com/crypto/trading Execute the Python script to interact with the v1 API endpoints. ```bash python robinhood_api_trading.py ``` -------------------------------- ### Get Crypto Accounts (v2) Source: https://docs.robinhood.com/crypto/trading Retrieves a list of all crypto trading accounts associated with the API key. Uses a GET request to the accounts endpoint. ```python def get_accounts(self) -> Any: path = "/api/v2/crypto/trading/accounts/" return self.make_api_request("GET", path) ``` -------------------------------- ### Crypto Order Placement Response Sample Source: https://docs.robinhood.com/crypto/trading This JSON represents a successful response after placing a crypto trading order. It includes the order ID, status, and details of any executions. ```json { "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08", "account_number": "string", "symbol": "string", "client_order_id": "11299b2b-61e3-43e7-b9f7-dee77210bb29", "side": "buy", "executions": [ { "effective_price": "string", "quantity": "string", "timestamp": "2019-08-24T14:15:22Z" } ], "type": "limit", "state": "open", "average_price": 0.1, "filled_asset_quantity": 0.1, "created_at": "string", "updated_at": "string", "market_order_config": { "asset_quantity": 0.1 }, "limit_order_config": { "quote_amount": 0.1, "asset_quantity": 0.1, "limit_price": 0.1 }, "stop_loss_order_config": { "quote_amount": 0.1, "asset_quantity": 0.1, "stop_price": 0.1, "time_in_force": "gtc" }, "stop_limit_order_config": { "quote_amount": 0.1, "asset_quantity": 0.1, "limit_price": 0.1, "stop_price": 0.1, "time_in_force": "gtc" } } ``` -------------------------------- ### Response Sample for New Crypto Order Placement Source: https://docs.robinhood.com/crypto/trading This JSON object represents the successful response after placing a new crypto order, including details like order ID and status. ```json { "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08", "account_number": "string", "symbol": "string", "client_order_id": "11299b2b-61e3-43e7-b9f7-dee77210bb29", "side": "buy", "executions": [ { "effective_price": "string", "quantity": "string", "timestamp": "2019-08-24T14:15:22Z" } ], "type": "limit", "state": "open", "average_price": 0.1, "filled_asset_quantity": 0.1, "created_at": "string", "updated_at": "string", "market_order_config": { "asset_quantity": 0.1 }, "limit_order_config": { "quote_amount": 0.1, "asset_quantity": 0.1, "limit_price": 0.1 }, "stop_loss_order_config": { "quote_amount": 0.1, "asset_quantity": 0.1, "stop_price": 0.1, "time_in_force": "gtc" }, "stop_limit_order_config": { "quote_amount": 0.1, "asset_quantity": 0.1, "limit_price": 0.1, "stop_price": 0.1, "time_in_force": "gtc" }, "fee_charged": 0.1, "estimated_fee_remaining": 0.1 } ``` -------------------------------- ### Request Sample for Placing New Crypto Order Source: https://docs.robinhood.com/crypto/trading This is a sample JSON payload for placing a new crypto trading order. Ensure you include the correct order configuration based on the order type. ```json { "symbol": "string", "client_order_id": "11299b2b-61e3-43e7-b9f7-dee77210bb29", "side": "buy", "type": "limit", "market_order_config": { "asset_quantity": 0.1 }, "limit_order_config": { "quote_amount": 0.1, "asset_quantity": 0.1, "limit_price": 0.1, "time_in_force": "gtc" }, "stop_loss_order_config": { "quote_amount": 0.1, "asset_quantity": 0.1, "stop_price": 0.1, "time_in_force": "gtc" }, "stop_limit_order_config": { "quote_amount": 0.1, "asset_quantity": 0.1, "limit_price": 0.1, "stop_price": 0.1, "time_in_force": "gtc" } } ``` -------------------------------- ### Get a Specific Order Source: https://docs.robinhood.com/crypto/trading Retrieves details for a specific order using its account number and order ID. This is a GET request to the /api/v2/crypto/trading/orders/{order_id}/ endpoint. ```python api_trading_client.get_order(account_number, order_id) ``` -------------------------------- ### Response Sample for Crypto Orders Source: https://docs.robinhood.com/crypto/trading This is a sample JSON response for a crypto trading order, detailing its state and execution information. ```json { "next": "string", "previous": "string", "results": [ { "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08", "account_number": "string", "symbol": "string", "client_order_id": "11299b2b-61e3-43e7-b9f7-dee77210bb29", "side": "buy", "executions": [ { "effective_price": "string", "quantity": "string", "timestamp": "2019-08-24T14:15:22Z" } ], "type": "limit", "state": "open", "average_price": 0.1, "filled_asset_quantity": 0.1, "created_at": "string", "updated_at": "string", "market_order_config": { "asset_quantity": 0.1 }, "limit_order_config": { "quote_amount": 0.1, "asset_quantity": 0.1, "limit_price": 0.1 }, "stop_loss_order_config": { "quote_amount": 0.1, "asset_quantity": 0.1, "stop_price": 0.1, "time_in_force": "gtc" }, "stop_limit_order_config": { "quote_amount": 0.1, "asset_quantity": 0.1, "limit_price": 0.1, "stop_price": 0.1, "time_in_force": "gtc" }, "fee_charged": 0.1, "estimated_fee_remaining": 0.1 } ] } ``` -------------------------------- ### Build Query Parameters (v2) Source: https://docs.robinhood.com/crypto/trading Constructs a URL query string from a dictionary of parameters. Handles single values and lists for multiple values of the same key. ```python @staticmethod def get_query_params(params_dict: Dict[str, Any]) -> str: """ Build query parameter string from a dictionary. - Single values: {"key1": "value1", "key2": "value2"} - Multiple values for same key: {"symbol": ["BTC-USD", "ETH-USD"]} """ if not params_dict: return "" params = [] for key, value in params_dict.items(): if value is None: continue if isinstance(value, list): # Handle multiple values for the same key for v in value: params.append(f"{key}={v}") else: params.append(f"{key}={value}") return "?" + "&".join(params) if params else "" ``` -------------------------------- ### Place Order Source: https://docs.robinhood.com/crypto/trading Places a new crypto order on the Robinhood platform. ```APIDOC ## POST /api/v1/crypto/trading/orders/ ### Description Places a new crypto order on the Robinhood platform. ### Method POST ### Endpoint /api/v1/crypto/trading/orders/ ### Parameters #### Request Body - **client_order_id** (string) - Required - A unique identifier for the client's order. - **side** (string) - Required - The side of the order, either "buy" or "sell". - **order_type** (string) - Required - The type of order (e.g., "market", "limit"). - **symbol** (string) - Required - The trading pair symbol (e.g., "BTC-USD"). - **order_config** (object) - Required - Configuration details specific to the order type. - **quantity** (string) - Required - The quantity of the asset to trade. - **time_in_force** (string) - Required - The duration for which the order remains active (e.g., "gtc", "day"). - **limit_price** (string) - Optional - The limit price for limit orders. ### Request Example ```json { "client_order_id": "unique-order-id-123", "side": "buy", "order_type": "market", "symbol": "BTC-USD", "order_config": { "quantity": "0.001", "time_in_force": "gtc" } } ``` ### Response #### Success Response (200) - **order_details** (object) - Details of the placed order. ``` -------------------------------- ### Get Estimated Price Source: https://docs.robinhood.com/crypto/trading Estimates the price for a given symbol, side (bid/ask), and quantity. ```APIDOC ## GET /api/v2/crypto/trading/estimated_price ### Description Estimates the price for a given symbol, side (bid/ask), and quantity. ### Method GET ### Endpoint /api/v2/crypto/trading/estimated_price ### Parameters #### Query Parameters - **symbol** (string) - Required - The trading pair (e.g., "BTC-USD"). - **side** (string) - Required - The side of the trade, either "bid", "ask", or "both". - **quantity** (string) - Required - The quantity for which to estimate the price (e.g., "0.1,1,1.999"). ### Response #### Success Response (200) - **estimated_price** (string) - The estimated price. #### Response Example { "estimated_price": "50050.00" } ``` -------------------------------- ### Run Python Script with v2 API Source: https://docs.robinhood.com/crypto/trading Execute the Python script to interact with the v2 API endpoints. ```bash python robinhood_api_trading_v2.py ``` -------------------------------- ### Place Order Source: https://docs.robinhood.com/crypto/trading Places a new crypto order for a specified account. ```APIDOC ## POST /api/v2/crypto/trading/orders ### Description Places a new crypto order for a specified account. ### Method POST ### Endpoint /api/v2/crypto/trading/orders ### Parameters #### Query Parameters - **account_number** (string) - Required - The account number for which to place the order. #### Request Body - **client_order_id** (string) - Required - A unique identifier for the client's order. - **side** (string) - Required - The side of the order, either "buy" or "sell". - **type** (string) - Required - The type of order (e.g., "market", "limit"). - **symbol** (string) - Required - The trading pair (e.g., "BTC-USD"). - **market_order_config** (object) - Required if type is "market". Configuration for a market order. - **asset_quantity** (string) - The quantity of the asset to trade. - **limit_order_config** (object) - Required if type is "limit". Configuration for a limit order. - **price** (string) - The limit price. - **time_in_force** (string) - The time in force for the limit order (e.g., "gtc", "day"). ### Request Example ```json { "client_order_id": "your-unique-order-id", "side": "buy", "type": "market", "symbol": "BTC-USD", "market_order_config": { "asset_quantity": "0.000001" } } ``` ### Response #### Success Response (200) - **order_id** (string) - The ID of the placed order. - **status** (string) - The status of the order. #### Response Example { "order_id": "order-12345", "status": "open" } ``` -------------------------------- ### Place a Market Order Source: https://docs.robinhood.com/crypto/trading Places a market order for a specified symbol, account, and quantity. Requires a unique client order ID and order configuration. ```python order = api_trading_client.place_order( account_number, str(uuid.uuid4()), "buy", "market", "BTC-USD", {"asset_quantity": "0.000001"} ) ``` -------------------------------- ### Get Estimated Price Source: https://docs.robinhood.com/crypto/trading Calculates the estimated price for a given symbol, side (bid/ask), and quantity. ```APIDOC ## GET /api/v1/crypto/marketdata/estimated_price/ ### Description Calculates the estimated price for a given symbol, side (bid/ask), and quantity. ### Method GET ### Endpoint /api/v1/crypto/marketdata/estimated_price/ ### Parameters #### Query Parameters - **symbol** (string) - Required - The trading pair symbol (e.g., "BTC-USD", "ETH-USD"). - **side** (string) - Required - The side of the trade, either "bid" or "ask". - **quantity** (string) - Required - The quantity of the asset for which to estimate the price (e.g., "0.1", "1", "1.999"). ### Request Example None ### Response #### Success Response (200) - **estimated_price** (object) - An object containing the estimated price details. ``` -------------------------------- ### place_order Source: https://docs.robinhood.com/crypto/trading Places a new crypto order. Requires order details such as side, type, symbol, and configuration. ```APIDOC ## POST /api/v1/crypto/trading/orders/ ### Description Places a new crypto order. ### Method POST ### Endpoint /api/v1/crypto/trading/orders/ ### Parameters #### Request Body - **side** (string) - Required - The side of the order (e.g., 'buy' or 'sell'). - **type** (string) - Required - The type of order (e.g., 'market' or 'limit'). - **symbol** (string) - Required - The trading symbol (e.g., 'BTC-USD'). - **order_config** (object) - Required - Configuration specific to the order type (e.g., `{"asset_quantity": "0.0001"}`). ``` -------------------------------- ### Trading Pairs Response Sample Source: https://docs.robinhood.com/crypto/trading This is a sample JSON response for the trading pairs endpoint. It includes pagination details and a list of tradable crypto pairs with their specifications. ```json { "next": "string", "previous": "string", "results": [ { "symbol": "string", "asset_code": "string", "quote_code": "string", "asset_increment": "string", "quote_increment": "string", "max_order_size": "string", "min_order_amount": "string", "status": "string", "is_api_tradable": true } ] } ``` -------------------------------- ### Get Trading Pairs Source: https://docs.robinhood.com/crypto/trading Retrieves information about supported trading pairs. Optionally filter by specific symbols. ```APIDOC ## GET /api/v1/crypto/trading/trading_pairs/ ### Description Retrieves information about supported trading pairs. Optionally filter by specific symbols. ### Method GET ### Endpoint /api/v1/crypto/trading/trading_pairs/ ### Parameters #### Query Parameters - **symbol** (string) - Optional - The trading pair symbol (e.g., "BTC-USD", "ETH-USD"). Multiple symbols can be provided. ### Request Example None ### Response #### Success Response (200) - **trading_pairs** (array) - A list of supported trading pairs. ```