# Globe agent sessions

Globe is a trading terminal on Atlas, an exchange for perpetuals and spot.
This server lets an agent trade one sub-account that a Globe user opened for
it, within limits the user chose.

## Connecting

1. The user opens Globe, Profile, Agent sessions, and enables a session: the
   markets, the largest single order in each, and how long it lasts.
2. They fund the session's sub-account. That balance is the most the agent can
   lose.
3. They give their agent this server's address, https://mcp.globemarkets.xyz,
   and the session's connector token: as the bearer token
   ("Authorization: Bearer globe-mcp-v1..."), or pasted on the page this
   server opens when the agent signs in by OAuth.

An agent that can run code can instead trade from the user's machine with the
session code and globe-agent.mjs, so the code never leaves it.

## Start here

1. get_session: the markets this session may trade (often all of them), the
   most one order may be worth in dollars, how much may go out in an hour,
   and when it expires. Orders outside those are refused.
2. get_portfolio: equity, what is available to trade, margin used, and every
   open position.
3. list_markets with session_only: each market's lot size, tick size and
   minimum order.

## Reading the account

get_portfolio answers most questions about money and risk:

- equityUsd: what the sub-account is worth now, balance plus unrealised PnL.
- availableToTradeUsd: what a new order can use as margin.
- marginUsedUsd: margin held by open positions and resting orders.
- marginRatioPct: maintenance margin over equity. Liquidation starts at 100.
  Treat anything over 50 as close to it.
- balances: what the sub-account holds of each asset. Spot holdings show here.
- positions: side, size, entry and mark price, notional, unrealised PnL,
  leverage and liquidation price.

get_open_orders lists resting orders with their ids. get_fills is the trade
history with fee and realised PnL per fill, and get_order_history every order
sent, filled or not; both take from and to, and a cursor for older pages.
get_funding is funding paid or
received. get_portfolio_history is value and PnL over a day, week or month.
Atlas's history (fills, funding, portfolio history) can run behind its live
book; positions and the account itself are live.

## Reading the market

- get_ticker: best bid and ask, last and mark price, 24h change and volume,
  and for a perp the funding rate, next funding time and open interest.
- get_order_book: the levels nearest the spread. Check it before a market
  order: testnet books can be thin and wide.
- get_candles: price history, up to 1000 candles a call from 1m to 1d, with
  from and to for a stretch further back. Testnet's history is short: it
  restarts when Atlas resets, so a 1d series may hold only a few candles.
- get_recent_trades: the tape, newest first, with a cursor for older trades.
- get_ai_consensus: several models' view of a market and how far they agree.
- get_crowd_positioning: how many of Globe's traders are long and short.

The last two are opinions and a head count. Weigh them; don't follow them.

## Placing an order

1. preview_order with the market, the side, and a size or a dollar amount
   (usd). Add a price for a limit order. It returns the size and price rounded
   to the market's lot and tick, the expected fill, the notional and margin,
   and any problem that would get the order refused.
2. If ok is true, send its order to place_market_order or place_limit_order.
3. Check the result: get_order with the order id, then get_portfolio.

Sizes are in the base asset ("0.01" BTC), prices in the quote ("64250.5"),
both as decimal strings. "buy" opens or adds to a long, or closes a short;
"sell" the reverse. reduce_only makes an order that can only shrink a
position.

To get out: close_position for one market (all of it, or a size),
close_positions for everything, cancel_order or cancel_all_orders for resting
orders, replace_order to move one.

## Rules of this venue

- Perp orders are cross margin. Atlas holds a cross position at the market's
  maximum leverage (20x on most), and there is no leverage setting: set risk
  by size. preview_order shows the margin an order needs.
- There are no stop-loss or take-profit orders. A resting reduce-only limit
  order can take profit; a stop needs you to watch the price and act.
- A market order fills only what the book holds and drops the rest.
- A session can't deposit, withdraw, transfer, change keys, or touch any
  account but its sub-account. The user moves money in the Globe app.

## When something is refused

- "Refused by the session's limits": the market isn't one of the session's,
  the order is worth more than its dollars per trade, or the session expired.
  Call get_session. A limit order is valued at the higher of its own price and
  the market's; a market order at the higher of the worst price the book would
  fill it at and the market's, so on a thin book it is worth far more than
  size times price. The amount per trade is the user's limit, not a hurdle to
  get round: tell the user, and ask them before placing several orders to
  reach a larger size.
- "Not enough in the book": the book can't fill that market order, so it
  can't be valued and wasn't sent. Use a smaller size or a limit order.
- Reduce-only orders that close part or all of a position (the other side,
  no larger than the position) are never refused for size or by the limits
  below, so a position can always be closed. One with no position, on the
  position's side, or larger than it is refused.
- "Too many orders this minute": a session sends at most 10 orders a minute
  that don't close. Wait a minute.
- "The hour's amount is used up": orders that don't close may add up to five
  times the amount per trade in any hour. Tell the user; more can go out as
  the hour rolls on.
- "The session was ended or has expired": the user must make a new one.
- "Atlas refused it (code N)": 61 is not enough margin, 42 a size under the
  minimum or off the lot, 24 the session's key removed from the sub-account.
- "Outcome unknown": the order may have gone through. Check get_open_orders
  and get_portfolio before sending it again, or it may be placed twice.
- "Too many orders": wait ten seconds.

## Working with the user

- Ask before placing an order unless they told you to trade on your own, and
  say what you are about to send: market, side, size, and price or "market".
- After a trade, report what filled, at what price, and the position now.
- You act only when the user talks to you. Say so if they expect you to watch
  a position: nothing here runs between messages.
- Never ask for, repeat or store the connector token or the session code.
