TickAtlas
Tutorial 14 min read · March 28, 2026

OpenAI Function Calling + Market Data: Complete Tutorial

Build an AI assistant that can fetch live market data on demand using OpenAI function calling and the TickAtlas API. Full Python implementation included.

CG
By the TickAtlas team

What Is Function Calling?

OpenAI function calling lets GPT models request specific data by generating structured function calls instead of plain text. Instead of the model hallucinating market data, it says "I need the RSI for EURUSD on H4" and your code fetches it from the TickAtlas API. The model then synthesizes the real data into its response.

This combines the analytical reasoning of LLMs with the accuracy of real-time data APIs.

Architecture

User Question
  |
  v
GPT-4o decides which functions to call
  |
  v
Your code executes the calls against TickAtlas API
  |
  v
GPT-4o receives real data and generates final analysis

Step 1: Define the Tool Schema

bash
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_indicator",
            "description": "Fetch a technical indicator for a symbol",
            "parameters": {
                "type": "object",
                "properties": {
                    "symbol": {"type": "string"},
                    "indicator": {"type": "string"},
                    "timeframe": {
                        "type": "string",
                        "enum": ["M5", "M15", "H1", "H4", "D1"],
                    },
                },
                "required": ["symbol", "indicator", "timeframe"],
            },
        },
    },
    {
        "type": "function",
        "function": {
            "name": "get_ohlcv",
            "description": "Fetch OHLCV candle data for a symbol",
            "parameters": {
                "type": "object",
                "properties": {
                    "symbol": {"type": "string"},
                    "timeframe": {"type": "string"},
                    "limit": {"type": "integer", "default": 10},
                },
                "required": ["symbol", "timeframe"],
            },
        },
    },
    {
        "type": "function",
        "function": {
            "name": "get_spread",
            "description": "Get current bid/ask spread",
            "parameters": {
                "type": "object",
                "properties": {
                    "symbol": {"type": "string"},
                },
                "required": ["symbol"],
            },
        },
    },
]

Step 2: Implement the API Calls

python
import requests, json

CLAW_KEY = "YOUR_TICKATLAS_API_KEY"
BASE = "https://tickatlas.com/v1"
HEADERS = {"X-API-Key": CLAW_KEY}

def get_indicator(symbol: str, indicator: str, timeframe: str) -> str:
    r = requests.get(f"{BASE}/indicator", headers=HEADERS,
                     params={"symbol": symbol, "indicator": indicator,
                             "timeframe": timeframe})
    return json.dumps(r.json())

def get_ohlcv(symbol: str, timeframe: str, limit: int = 10) -> str:
    r = requests.get(f"{BASE}/ohlcv", headers=HEADERS,
                     params={"symbol": symbol, "timeframe": timeframe,
                             "limit": limit})
    return json.dumps(r.json())

def get_spread(symbol: str) -> str:
    r = requests.get(f"{BASE}/spread", headers=HEADERS,
                     params={"symbol": symbol})
    return json.dumps(r.json())

FUNCTIONS = {
    "get_indicator": get_indicator,
    "get_ohlcv": get_ohlcv,
    "get_spread": get_spread,
}

Step 3: The Conversation Loop

python
from openai import OpenAI
client = OpenAI()

SYSTEM = (
    "You are a trading assistant with real-time market data access. "
    "Always call functions for current data. Never guess values."
)

def chat(user_msg: str, history: list = None) -> str:
    if history is None:
        history = [{"role": "system", "content": SYSTEM}]
    history.append({"role": "user", "content": user_msg})

    resp = client.chat.completions.create(
        model="gpt-4o", messages=history,
        tools=tools, tool_choice="auto",
    )
    msg = resp.choices[0].message
    history.append(msg)

    while msg.tool_calls:
        for tc in msg.tool_calls:
            fn = FUNCTIONS[tc.function.name]
            args = json.loads(tc.function.arguments)
            result = fn(**args)
            history.append({
                "role": "tool",
                "tool_call_id": tc.id,
                "content": result,
            })
        resp = client.chat.completions.create(
            model="gpt-4o", messages=history,
            tools=tools, tool_choice="auto",
        )
        msg = resp.choices[0].message
        history.append(msg)

    return msg.content

Example Conversation

python
>>> chat("Is EURUSD overbought on the H4?")

# Behind the scenes:
# 1. GPT calls get_indicator("EURUSD", "RSI_14", "H4")
# 2. API returns: {"success": true, "data": {"values": {"rsi": 68.4}, ...}}
# 3. GPT responds:

"EURUSD RSI on H4 is currently at 68.4, which is elevated but
not yet in overbought territory (above 80). The pair is showing
bullish momentum but still has room to run before reaching
extreme levels. Watch the 72-75 zone as an early warning."

Production Considerations

Rate limit your API calls

GPT might call get_indicator 5 times in a single response. Make sure your TickAtlas plan supports the volume. Use the /indicators multi-endpoint to batch when possible.

Add error handling to functions

If the API returns an error, pass the error message back to GPT. It can handle errors gracefully and inform the user rather than crashing.

Cache repeated calls

If the user asks about EURUSD multiple times in one conversation, cache the indicator values for a few seconds to avoid redundant API calls.

Never auto-execute trades

Use AI for analysis and signal generation, but always require human confirmation before placing actual trades through a broker API.

Related Reading

Try this with live data

Every account gets $2.50 in free PAYG credits. No card required — paste your API key and run the code above against live broker data.