get_kline
Retrieve candlestick data for trading pairs from Aster Finance API, formatted as a Markdown table. Specify symbol, interval, start/end times, and limit to customize results.
Instructions
Fetch Kline/Candlestick data from Aster Finance API and return as Markdown table text.
Parameters:
symbol (str): Trading pair symbol (e.g., 'BTCUSDT', 'ETHUSDT'). Case-insensitive.
interval (str): Kline interval (e.g., '1m' for 1 minute, '1h' for 1 hour, '1d' for 1 day).
startTime (Optional[int]): Start time in milliseconds since Unix epoch. If None, defaults to API behavior.
endTime (Optional[int]): End time in milliseconds since Unix epoch. If None, defaults to API behavior.
limit (Optional[int]): Number of Klines to return (1 to 1500). If None, defaults to 500.
Returns:
str: Markdown table containing open_time, open, high, low, and close.
Raises:
Exception: If the API request fails or data processing encounters an error.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| endTime | No | ||
| interval | Yes | ||
| limit | No | ||
| startTime | No | ||
| symbol | Yes |
Implementation Reference
- main.py:19-93 (handler)The handler function for the 'get_kline' tool. It fetches Kline (candlestick) data from the Aster Finance API using httpx, processes the data into a Pandas DataFrame, formats timestamps and prices, and returns the data as a Markdown table.@mcp.tool() async def get_kline( symbol: str, interval: str, startTime: Optional[int] = None, endTime: Optional[int] = None, limit: Optional[int] = None ) -> str: """ Fetch Kline/Candlestick data from Aster Finance API and return as Markdown table text. Parameters: symbol (str): Trading pair symbol (e.g., 'BTCUSDT', 'ETHUSDT'). Case-insensitive. interval (str): Kline interval (e.g., '1m' for 1 minute, '1h' for 1 hour, '1d' for 1 day). startTime (Optional[int]): Start time in milliseconds since Unix epoch. If None, defaults to API behavior. endTime (Optional[int]): End time in milliseconds since Unix epoch. If None, defaults to API behavior. limit (Optional[int]): Number of Klines to return (1 to 1500). If None, defaults to 500. Returns: str: Markdown table containing open_time, open, high, low, and close. Raises: Exception: If the API request fails or data processing encounters an error. """ endpoint = "/fapi/v1/klines" # Construct query parameters params = { "symbol": symbol.upper(), # Ensure symbol is uppercase (e.g., BTCUSDT) "interval": interval, # e.g., 1m, 1h, 1d } if startTime is not None: params["startTime"] = startTime if endTime is not None: params["endTime"] = endTime if limit is not None: params["limit"] = limit async with httpx.AsyncClient() as client: try: # Make GET request to the API response = await client.get(f"{BASE_URL}{endpoint}", params=params) response.raise_for_status() # Raise exception for 4xx/5xx errors # Parse JSON response kline_data: List[List[Any]] = response.json() # Create pandas DataFrame df = pd.DataFrame(kline_data, columns=[ "open_time", "open", "high", "low", "close", "volume", "close_time", "quote_asset_volume", "number_of_trades", "taker_buy_base_asset_volume", "taker_buy_quote_asset_volume", "ignore" ]) # Convert timestamps to readable format df["open_time"] = pd.to_datetime(df["open_time"], unit="ms") # Select relevant columns and format numbers df = df[["open_time", "open", "high", "low", "close"]] df["open"] = df["open"].astype(float).round(8) df["high"] = df["high"].astype(float).round(8) df["low"] = df["low"].astype(float).round(8) df["close"] = df["close"].astype(float).round(8) # Convert DataFrame to Markdown table markdown_table = df.to_markdown(index=False) return markdown_table except httpx.HTTPStatusError as e: # Handle HTTP errors (e.g., 400, 429) raise Exception(f"API request failed: {e.response.status_code} - {e.response.text}") except Exception as e: # Handle other errors (e.g., network issues, pandas errors) raise Exception(f"Error processing Kline data: {str(e)}")
- main.py:19-19 (registration)The @mcp.tool() decorator registers the get_kline function as an MCP tool.@mcp.tool()
- main.py:20-42 (schema)The function signature and docstring define the input schema (parameters with types and descriptions) and output (Markdown table string) for the get_kline tool.async def get_kline( symbol: str, interval: str, startTime: Optional[int] = None, endTime: Optional[int] = None, limit: Optional[int] = None ) -> str: """ Fetch Kline/Candlestick data from Aster Finance API and return as Markdown table text. Parameters: symbol (str): Trading pair symbol (e.g., 'BTCUSDT', 'ETHUSDT'). Case-insensitive. interval (str): Kline interval (e.g., '1m' for 1 minute, '1h' for 1 hour, '1d' for 1 day). startTime (Optional[int]): Start time in milliseconds since Unix epoch. If None, defaults to API behavior. endTime (Optional[int]): End time in milliseconds since Unix epoch. If None, defaults to API behavior. limit (Optional[int]): Number of Klines to return (1 to 1500). If None, defaults to 500. Returns: str: Markdown table containing open_time, open, high, low, and close. Raises: Exception: If the API request fails or data processing encounters an error. """