Connect to a broker¶
This page starts ArenaX, the simulated broker, and connects to it from Python to read the account balance. Use two terminals: one for ArenaX, one for your code.
Step 1: start ArenaX¶
In the first terminal, from the CJTrade folder:
uv run arenaxd
This starts ArenaX with the default demo backend, which:
- picks a trading day 11 to 20 days in the past,
- downloads that period's 1-minute bars from Yahoo Finance (no account needed) into the price cache,
- opens a fresh simulated account with NT$100,000,
- listens on
127.0.0.1:8801(local connections only).
Expected output after a few seconds (shortened; your dates will differ):
[INFO][...] ArenaX.server: market data: yfinance
[INFO][...] ArenaX.backend.demo: market time: ... mock_current_time=2026-11-13 09:00:00 ... playback_speed=120.0 ...
[INFO][...] ArenaX.server: starting on 127.0.0.1:8801, backend=demo
[INFO][...] ArenaX.backend.state: fresh account
[INFO][...] ArenaX.backend: sim env started
mock_current_time is ArenaX's simulated now; playback_speed=120.0 means
one real second is two market minutes. Keep this terminal running.
I see [ERROR] yfinance: ... possibly delisted; no price data found
Yahoo has no bars for some of the requested days, usually a weekend or
a holiday. ArenaX continues with the days it received. The error is
harmless once sim env started appears.
Step 2: connect from Python¶
In the second terminal, save this as hello.py and run
uv run python hello.py:
from cjtrade.pkgs.brokers.account_client import AccountClient, BrokerType
client = AccountClient(BrokerType.ARENAX, api_key="testkey123")
client.connect()
print("broker:", client.get_broker_name())
print("balance:", client.get_balance())
print("positions:", client.get_positions())
print("market time:", client.get_market_time()["mock_current_time"])
client.disconnect()
BrokerType.ARENAXselects the broker; the rest of the code is the same for every broker.testkey123is the API key every ArenaX server accepts by default.- Pair every
connect()withdisconnect(). get_balance()returns cash;get_positions()returns holdings.get_market_time()returns ArenaX's simulated market time.
Expected output:
broker: arenax (8801)
balance: 100000.0
positions: []
market time: 2026-11-13 09:24:43.668600
A fresh account has NT$100,000 and no positions. The simulated clock runs as long as ArenaX is up, so a second run a few seconds later shows a later market time.
The account lives as long as the server
Disconnecting and connecting again keeps the same balance and positions.
Restarting arenaxd opens a fresh account on a new random day.
connect() fails with Connection refused
ArenaX is not running, or not ready yet. Wait until the first terminal
shows sim env started, then retry. See
Troubleshooting for other errors.
Sinopac (a real broker)¶
Switching to Sinopac changes only the client constructor. It requires the
sinopac extra and the API key, secret and certificate from
Sinopac's API portal:
client = AccountClient(
BrokerType.SINOPAC,
api_key="...", secret_key="...",
ca_path="/path/to/Sinopac.pfx", ca_passwd="...",
simulation=True, # Sinopac's sandbox: orders are not real
)
client.connect()
simulation=False is your real account
With simulation=False every order is a real order with real money.
Keep simulation=True until your code has run correctly on ArenaX and
on the sandbox.
Keep secrets out of code by putting them in a settings file; see Configuration and paths.
Next: Account and orders.