trading_agent.trading.broker_reader
¶
The one place trading-agent reads from UBI, through tradingmachine.
tradingmachine is synchronous, so every call runs in a worker thread and the desks keep working while UBI answers. A lock lets one UBI call run at a time, as the tradingmachine_mcp server does, because UBI holds a single access token and broker rate limits are shared.
The client is built from tradingmachine's own .env, named in trading_agent.toml. Left to itself, tradingmachine would look for a .env in the working folder and find trading-agent's, which has no UBI settings.
Typical usage example:
reader = BrokerReader(Path('/home/pramod/Projects/tradingmachine/.env')) quote = await reader.call(lambda: reader.instrument(arguments).quote)
BrokerReader
¶
Reads instruments, prices, the portfolio and the order book from UBI, and hands out the instruments the order tools trade.
Remembers where tradingmachine's settings are, without connecting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
environment_file
|
Path
|
tradingmachine's |
required |
Source code in src/trading_agent/trading/broker_reader.py
call
async
¶
Runs one piece of UBI work in a worker thread, one at a time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
work
|
Callable[[], _Result]
|
The work, such as reading a quote. |
required |
Returns:
| Type | Description |
|---|---|
_Result
|
What the work returned. |
Raises:
| Type | Description |
|---|---|
UnifiedBrokerInterfaceError
|
UBI refused or failed the request. |
InstrumentError
|
The instrument does not exist. |
Source code in src/trading_agent/trading/broker_reader.py
get
¶
Sends a GET request to one of UBI's read routes. Call it inside call().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The route, such as /api/portfolio/funds. |
required |
parameters
|
dict[str, Any] | None
|
The query parameters, or None. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
UBI's decoded JSON answer. |
Source code in src/trading_agent/trading/broker_reader.py
client
¶
The shared UBI client, for tradingmachine objects that need one, such as Account. Call it inside call().
Returns:
| Type | Description |
|---|---|
UnifiedBrokerInterface
|
The client. |
Source code in src/trading_agent/trading/broker_reader.py
instrument
¶
Looks an instrument up as a plain Instrument, which has no order methods. Call it inside call().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arguments
|
InstrumentArguments
|
How the model named the instrument. |
required |
Returns:
| Type | Description |
|---|---|
Instrument
|
The instrument. |
Source code in src/trading_agent/trading/broker_reader.py
tradeable_instrument
¶
Looks an instrument up as a TradeableInstrument, which has the order methods. Call it inside call().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arguments
|
InstrumentArguments
|
How the model named the instrument. |
required |
Returns:
| Type | Description |
|---|---|
TradeableInstrument
|
The instrument. |
Raises:
| Type | Description |
|---|---|
TradeableInstrumentError
|
The instrument is an index, which cannot be traded. |