Skip to the page
INTEGRATIONS/OPENAI AGENTS SDK

OpenAI Agents SDK

tradefloor.integrations.openai_agents wraps an OpenAI Agents SDK Agent. The adapter runs your agent inside a simulated market on your machine. tradefloor sends it a JSON observation each decision, checks the decision it returns, places its orders in the simulation and scores the run. LLM adapters covers what every adapter shares: the observation payload and decisions and model calls.

Install

pip install "tradefloor[openai-agents]"

A first run

The block runs with no API key, because it hands the adapter ScriptedModel, an offline model the SDK ships for testing.

ScriptedModel from agents.testing answers each call with a function of the input. payload_of reads back the observation the adapter sent.

import json

import tradefloor as tf
from agents import Agent
from agents.testing import ModelStep, ScriptedModel, assistant_message
from tradefloor.integrations.openai_agents import OpenAIAgentsAdapter, payload_of

def answer(call):
    # reads the same payload a real model is sent
    first = payload_of(call)["assets"][0]
    order = {"symbol": first["symbol"], "side": "BUY", "quantity": 200}
    return [assistant_message(json.dumps({"actions": [order],
                                          "rationale": "add"}))]

offline = ScriptedModel([ModelStep.respond(answer)] * 5)
pm = Agent(name="Portfolio Manager", instructions="You manage a small portfolio.")
adapter = OpenAIAgentsAdapter(pm, mode="live", model=offline)

market = tf.Universe.random(12, seed=4242)
card = tf.evaluate({"pm": adapter}, seed=4242, universe=market, days=5)["pm"]
print(card)
print(len(adapter.record), "decisions")
Scorecard('pm', pnl=2,055, return=+0.21%, trades=5, impact=+0.18bps, sharpe=n/a (short run), vol=0.7%, in_market=100%, exposure=0.03x)
5 decisions

OpenAIAgentsAdapter defaults to mode="replay", so a live run names mode="live", and openai_agent(agent) is the same adapter with live as its default. For a live run, give the Agent a model your account can use, set OPENAI_API_KEY and leave out model=. The adapter turns the SDK's tracing off for each run it starts unless you pass tracing=True, because tracing is on by default in the SDK and sends traces to OpenAI.

Tested with

openai-agents 0.23.1 and tradefloor 0.9.1.