### Example Invalid Request URL Source: https://twelvedata.com/docs/llms/introduction This is an example of an invalid request URL due to an incorrect 'interval' parameter. It demonstrates a common scenario leading to a 400 Bad Request error. ```http https://api.twelvedata.com/time_series?symbol=AAPL&interval=0.99min&apikey=your_api_key ``` -------------------------------- ### Get Latest Stock Price with Twelve Data Python SDK Source: https://twelvedata.com/docs/llms/introduction Initialize the Twelve Data client with your API key and fetch the latest price for a stock symbol using the Python SDK. The result is returned as JSON. ```python from twelvedata import TDClient # Initialize client with your API key td = TDClient(apikey="your_api_key") # Get latest price for Apple price = td.price(symbol="AAPL").as_json() print(price) ``` -------------------------------- ### Get Latest Stock Price with Twelve Data Node.js SDK Source: https://twelvedata.com/docs/llms/introduction Initialize the Twelve Data client with your API key and fetch the latest price for a stock symbol using the Node.js SDK. The response data is logged to the console. ```javascript import { MarketDataApi, CreateConfig } from "@twelvedata/twelvedata-node"; const config = CreateConfig('your_api_key'); const api = new MarketDataApi(config); async function main() { const response = await api.getPrice({ symbol: "AAPL", }); console.log(response.data); } main().catch(console.error); ``` -------------------------------- ### Example Error Response (400 Bad Request) Source: https://twelvedata.com/docs/llms/introduction This JSON object represents an error response from the Twelve Data API. It indicates a 'Bad Request' due to an invalid 'interval' value and provides a detailed message for resolution. ```json { "code": 400, "message": "Invalid **interval** provided: 0.99min. Supported intervals: 1min, 5min, 15min, 30min, 45min, 1h, 2h, 4h, 8h, 1day, 1week, 1month", "status": "error" } ``` -------------------------------- ### Success Subscription Response Source: https://twelvedata.com/docs/llms/websocket/ws-real-time-price This is a sample response indicating a successful subscription to multiple symbols, detailing the status and success/failure of each subscription. ```json { "event": "subscribe-status", "status": "ok", "success": [ { "symbol":"AAPL","exchange":"NASDAQ", "country":"United States", "type":"Common Stock" }, { "symbol":"RY","exchange":"NYSE", "country":"United States", "type":"Common Stock" }, { "symbol":"RY","exchange":"TSX", "country":"Canada", "type":"Common Stock" }, { "symbol":"EUR/USD","exchange":"FOREX", "country":"", "type":"Physical Currency" }, { "symbol":"BTC/USD","exchange":"FOREX", "country":"", "type":"Physical Currency" } ], "fails": [] } ``` -------------------------------- ### Connect to WebSocket API with API Key in Header Source: https://twelvedata.com/docs/llms/websocket/ws-overview Alternatively, pass your API key in the X-TD-APIKEY header when connecting to the WebSocket API. ```shell wss://ws.twelvedata.com/v1/{$route} X-TD-APIKEY: your_api_key ``` -------------------------------- ### Connect to WebSocket API with API Key Source: https://twelvedata.com/docs/llms/websocket/ws-overview Use this format to pass your API key directly as a connection parameter when establishing a WebSocket connection. ```shell wss://ws.twelvedata.com/v1/{$route}?apikey=your_api_key ``` -------------------------------- ### API Authentication via Query Parameter Source: https://twelvedata.com/docs/llms/introduction Demonstrates how to authenticate API requests by including your API key as a query parameter in the URL. This method is less secure than the HTTP header method. ```http GET https://api.twelvedata.com/endpoint?symbol=AAPL&apikey=your_api_key ``` -------------------------------- ### Fetch Latest Stock Price with cURL Source: https://twelvedata.com/docs/llms/introduction Use cURL to make a simple API call to fetch the latest price for a given stock symbol. Replace 'your_api_key' with your actual API key. ```bash curl "https://api.twelvedata.com/price?symbol=AAPL&apikey=your_api_key" ``` -------------------------------- ### Price Event Data Response Source: https://twelvedata.com/docs/llms/websocket/ws-real-time-price A sample response for a price event, providing real-time tick data for a specific symbol, including its price and day's volume. ```json { "event": "price", "symbol": "AAPL", "currency": "USD", "exchange": "NASDAQ", "type": "Common Stock", "timestamp": 1592249566, "price": 342.0157, "day_volume": 27631112 } ``` -------------------------------- ### API Authentication via HTTP Header Source: https://twelvedata.com/docs/llms/introduction Recommended method for authenticating API requests. Include your API key in the 'Authorization' header with the 'apikey' prefix. ```http Authorization: apikey your_api_key ``` -------------------------------- ### Calculate Correlation Between Stock Prices with Python Source: https://twelvedata.com/docs/llms/introduction Fetch historical daily closing prices for two stock symbols using the Twelve Data Python SDK and calculate the correlation between them using pandas. Ensure data is aligned by datetime index. ```python from twelvedata import TDClient import pandas as pd # Initialize client with your API key td = TDClient(apikey="your_api_key") # Fetch historical price data for Tesla tsla_ts = td.time_series( symbol="TSLA", interval="1day", outputsize=100 ).as_pandas() # Fetch historical price data for Microsoft msft_ts = td.time_series( symbol="MSFT", interval="1day", outputsize=100 ).as_pandas() # Align data on datetime index combined = pd.concat( [tsla_ts['close'].astype(float), msft_ts['close'].astype(float)], axis=1, keys=["TSLA", "MSFT"] ).dropna() # Calculate correlation correlation = combined["TSLA"].corr(combined["MSFT"]) print(f"Correlation of closing prices between TSLA and MSFT: {correlation:.2f}") ``` -------------------------------- ### Bid/Ask Data Response Source: https://twelvedata.com/docs/llms/websocket/ws-real-time-price This response provides real-time price data along with bid and ask prices for instruments where this information is available. ```json { "event": "price", "symbol": "XAU/USD", "currency": "USD", "currency_base": "Gold Spot", "currency_quote": "US Dollar", "type": "Physical Currency", "timestamp": 1647950462, "price": 1925.18, "bid": 1925.05, "ask": 1925.32 } ``` -------------------------------- ### Subscribe to Symbols with Extended Format Source: https://twelvedata.com/docs/llms/websocket/ws-real-time-price Use the extended format to subscribe to symbols when ambiguity exists, by specifying exchange, mic_code, or type. ```json { "action": "subscribe", "params": { "symbols": [{ "symbol": "AAPL", "exchange": "NASDAQ" }, { "symbol": "RY", "mic_code": "XNYS" }, { "symbol": "EUR/USD", "type": "Forex" } ]}} ``` -------------------------------- ### Subscribe to Multiple Symbols Source: https://twelvedata.com/docs/llms/websocket/ws-real-time-price Subscribe to multiple stock and forex symbols using a comma-separated string. You can optionally specify the exchange after a colon. ```json { "action": "subscribe", "params": { "symbols": "AAPL,RY,RY:TSX,EUR/USD,BTC/USD" } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.