schwab-sdk-unofficial


Nameschwab-sdk-unofficial JSON
Version 0.2.7 PyPI version JSON
download
home_pageNone
SummaryCliente ligero para API de Schwab: OAuth, REST y Streaming WebSocket
upload_time2025-10-07 07:26:02
maintainerNone
docs_urlNone
authorSchwab SDK Contributors
requires_python>=3.9
licenseNone
keywords schwab trading market-data websocket sdk api
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Schwab SDK (Python)

Lightweight client for the Schwab API: OAuth, REST (Trader/Market Data), and WebSocket Streaming.

* Focus: thin wrappers, no heavy validation; robust token handling, refresh, and retries.
* Coverage: Accounts, Orders, Market Data, and Streaming (Level One, Book, Chart, Screener, Account Activity).

## Installation

Unofficial PyPI package (distribution name):

```bash
pip install schwab_sdk_unofficial
```

Import in code (module):

```python
from schwab_sdk import Client, AsyncClient
```

⚠️ Note: The package name on PyPI is `schwab_sdk_unofficial`, but the import remains `schwab_sdk` for a clean API.

## Table of Contents

* [Requirements](#requirements)
* [Configuration](#configuration)
* [Quick Start](#quick-start)
* [Authentication (OAuth)](#authentication-oauth)
* [Request & Error Handling (REST)](#request--error-handling-rest)
* [Accounts (`accounts.py`)](#accounts-accountspy)
* [Orders (`orders.py`)](#orders-orderspy)
* [Market Data (`market.py`)](#market-data-marketpy)
* [WebSocket Streaming (`streaming.py`)](#websocket-streaming-streamingpy)

  * [Key Formats (quick table)](#key-formats-quick-table)
  * [Service-by-Service Examples](#service-by-service-examples)
  * [Utilities](#utilities)
  * [Recommended Fields](#recommended-fields)
  * [Quick Field Guide (IDs → meaning)](#quick-field-guide-ids--meaning)
  * [Frame Structure](#frame-structure)
* [Advanced Troubleshooting](#advanced-troubleshooting)
* [Contributions](#contributions)
* [Disclaimer](#disclaimer)
* [License](#license)

## Requirements

* Python 3.9+
* Install dependencies:

```bash
pip install requests websocket-client flask
```

## Configuration

Create `.env` (or export environment variables):

```env
SCHWAB_CLIENT_ID=your_client_id
SCHWAB_CLIENT_SECRET=your_client_secret
SCHWAB_REDIRECT_URI=https://127.0.0.1:8080/callback
```

### Token storage modes

- `save_token` controla la persistencia de tokens en los clientes (Sync/Async).
- `save_token=True` (default): guarda tokens en archivo JSON (`schwab_tokens.json`) y rota automáticamente (access ~29 min; aviso de re-login cuando el refresh expira ~7 d).
- `save_token=False`: mantiene tokens solo en memoria; puedes inicializar con `token_data` y consultar el estado con `client.token_handler.get_token_payload()`.

## Quick Start

```python
from schwab_sdk import Client
import os

client = Client(
    os.environ['SCHWAB_CLIENT_ID'],
    os.environ['SCHWAB_CLIENT_SECRET'],
    os.environ.get('SCHWAB_REDIRECT_URI','https://127.0.0.1:8080/callback'),
    save_token=True,  # True: guarda en archivo JSON, False: solo en memoria
    # Opcional: inicializar tokens desde un dict (refresh/boot)
    token_data={
        # Minimal example: provide current tokens
        # 'access_token': '...',
        # 'refresh_token': '...',
        # either expires_in (seconds) OR access_token_expires_at (ISO)
        # 'expires_in': 1800,
        # 'access_token_expires_at': '2025-10-01T12:00:00',
        # optional refresh token expiration (ISO)
        # 'refresh_token_expires_at': '2025-10-07T11:00:00'
    }
)

# First use: OAuth login (opens the browser)
result = client.login()
if result["success"]:
    print("Login successful!")
    print("Token data:", result["tokens"])
else:
    print("Login failed")

# REST
quotes = client.market.get_quotes(["AAPL","MSFT"])  # Market Data
accounts = client.account.get_accounts()               # Accounts

# Streaming (Level One equities)
ws = client.streaming
ws.on_data(lambda f: print("DATA", f))
ws.connect(); ws.login()
ws.equities_subscribe(["AAPL"])
```

## Async Support

The SDK also provides async versions of all endpoints using `AsyncClient`:

```python
import asyncio
from schwab_sdk import AsyncClient

async def main():
    # Initialize async client
    async with AsyncClient(
        os.environ['SCHWAB_CLIENT_ID'],
        os.environ['SCHWAB_CLIENT_SECRET'],
        save_token=True  # True: guarda en archivo JSON, False: solo en memoria
    ) as client:
        # Login (async wrapper)
        result = await client.login()
        if result["success"]:
            print("Async login successful!")
            print("Token data:", result["tokens"])
        
        # Async REST calls
        quotes = await client.market.get_quotes(["AAPL", "MSFT"])
        accounts = await client.account.get_accounts()
        orders = await client.orders.get_all_orders()
        
        # Async streaming
        await client.streaming.connect()
        await client.streaming.login()
        await client.streaming.equities_subscribe(["AAPL"])
        
        # Set up callbacks
        client.streaming.on_data(lambda data: print("DATA:", data))
        client.streaming.on_response(lambda resp: print("RESPONSE:", resp))
        
        # Keep running to receive data
        await asyncio.sleep(10)  # Example: run for 10 seconds
        await client.streaming.disconnect()

# Run async function
asyncio.run(main())
```

### Async Benefits

- **Non-blocking**: Multiple API calls can run concurrently
- **Better performance**: Especially for multiple simultaneous requests
- **Context manager**: Automatic session cleanup with `async with`
- **Same API**: All methods have async equivalents with `await`

### Async vs Sync

| Feature | Sync | Async |
|---------|------|-------|
| **Import** | `from schwab_sdk import Client` | `from schwab_sdk import AsyncClient` |
| **Usage** | `client.method()` | `await client.method()` |
| **Context** | `client = Client(...)` | `async with AsyncClient(...)` |
| **Performance** | Sequential | Concurrent |
| **Dependencies** | `requests`, `websocket-client` | `aiohttp`, `websockets` |

### Async Streaming

The async streaming client provides non-blocking WebSocket connections:

```python
import asyncio
from schwab_sdk import AsyncClient

async def streaming_example():
    async with AsyncClient(client_id, client_secret, save_token=False) as client:
        await client.login()
        
        # Connect to streaming
        await client.streaming.connect()
        await client.streaming.login()
        
        # Set up callbacks
        client.streaming.on_data(lambda data: print("Market data:", data))
        client.streaming.on_response(lambda resp: print("Response:", resp))
        client.streaming.on_notify(lambda notify: print("Notification:", notify))
        
        # Subscribe to data streams
        await client.streaming.equities_subscribe(["AAPL", "MSFT"])
        await client.streaming.options_subscribe([
            client.streaming.create_option_symbol("AAPL", "2025-12-19", "C", 200.0)
        ])
        await client.streaming.account_activity_subscribe("your_account_hash")
        
        # Keep running
        await asyncio.sleep(30)  # Run for 30 seconds
        
        # Cleanup
        await client.streaming.disconnect()

asyncio.run(streaming_example())
```

### Streaming Services Available

| Service | Method | Description |
|---------|--------|-------------|
| **Equities** | `equities_subscribe(symbols)` | Stock quotes |
| **Options** | `options_subscribe(option_symbols)` | Option quotes |
| **Futures** | `futures_subscribe(symbols)` | Futures quotes |
| **Forex** | `forex_subscribe(pairs)` | Currency pairs |
| **Account** | `account_activity_subscribe(account_hash)` | Account activity |

## Async Accounts Module

Async version of account and transaction endpoints:

### Methods

* `get_accounts(fields=None)` - Get all accounts
* `get_account_by_id(account_hash, fields=None)` - Get specific account
* `get_transactions(account_hash, from_date=None, to_date=None, symbol=None, types=None)` - Get transactions
* `get_transaction(account_hash, transaction_id)` - Get specific transaction
* `get_preferences(account_hash)` - Get account preferences

### Example

```python
async with AsyncClient(client_id, client_secret) as client:
    _ = await client.login()
    
    # Get all accounts
    accounts = await client.account.get_accounts()
    
    # Get account with positions
    account = await client.account.get_account_by_id("123456789", fields="positions")
    
    # Get transactions for date range
    transactions = await client.account.get_transactions(
        "123456789", 
        from_date="2025-01-01", 
        to_date="2025-01-31"
    )
```

## Async Orders Module

Async version of order management endpoints:

### Methods

* `get_orders(account_hash, max_results=None, from_entered_time=None, to_entered_time=None, status=None)` - Get orders
* `get_all_orders(max_results=None, from_entered_time=None, to_entered_time=None, status=None)` - Get all orders
* `place_order(account_hash, order_data)` - Place new order
* `get_order(account_hash, order_id)` - Get specific order
* `cancel_order(account_hash, order_id)` - Cancel order
* `replace_order(account_hash, order_id, order_data)` - Replace order
* `preview_order(account_hash, order_data)` - Preview order

### Example

```python
async with AsyncClient(client_id, client_secret) as client:
    _ = await client.login()
    
    # Get orders for account (various date formats)
    orders = await client.orders.get_orders("123456789", status="FILLED")
    
    # Get orders with date range (YYYY-MM-DD format)
    orders = await client.orders.get_orders(
        "123456789", 
        from_entered_time="2025-01-01", 
        to_entered_time="2025-01-31"
    )
    
    # Get orders with full ISO format (passed through as-is)
    orders = await client.orders.get_orders(
        "123456789",
        from_entered_time="2025-01-01T09:00:00.000Z",
        to_entered_time="2025-01-01T17:00:00.000Z"
    )
    
    # Mixed formats also work
    orders = await client.orders.get_orders(
        "123456789",
        from_entered_time="2025-01-01",  # YYYY-MM-DD (auto-converted)
        to_entered_time="2025-01-31T23:59:59.000Z"  # Full ISO (passed through)
    )
    
    # Place a market order
    order_data = {
        "orderType": "MARKET",
        "session": "NORMAL",
        "duration": "DAY",
        "orderStrategyType": "SINGLE",
        "orderLegCollection": [{
            "instruction": "BUY",
            "quantity": 100,
            "instrument": {"symbol": "AAPL", "assetType": "EQUITY"}
        }]
    }
    result = await client.orders.place_order("123456789", order_data)
```

## Async Market Module

Async version of market data endpoints:

### Methods

* `get_quote(symbol)` - Get single quote
* `get_quotes(symbols)` - Get multiple quotes
* `get_movers(symbol_id, sort=None, frequency=None, params=None)` - Get market movers
* `get_option_chain(symbol, contract_type=None, ...)` - Get option chain
* `get_expiration_chain(symbol, params=None)` - Get expiration dates
* `get_markets(date=None)` - Get market hours
* `get_market_hours(date=None)` - Get market hours for date

### Example

```python
async with AsyncClient(client_id, client_secret) as client:
    _ = await client.login()
    
    # Get quotes
    quotes = await client.market.get_quotes(["AAPL", "MSFT", "GOOGL"])
    
    # Get option chain
    options = await client.market.get_option_chain(
        "AAPL",
        contract_type="CALL",
        strike_count=5,
        from_date="2025-01-01",
        to_date="2025-12-31"
    )
    
    # Get market movers
    movers = await client.market.get_movers("$SPX.X", sort="PERCENT_CHANGE")
```

## Authentication (OAuth)

* `client.login(timeout=300, auto_open_browser=True)`
* Handy: `client.has_valid_token()`, `client.refresh_token_now()`, `client.logout()`
* Internals: adhoc HTTPS callback server (dev), code-for-token exchange, auto-refresh and notice when refresh expires.

## Request & Error Handling (REST)

All REST calls use `Client._request()` with:

* Automatic Authorization headers
* Refresh retry on 401 (once) and immediate resend
* Retries with backoff for 429/5xx (exponential with factor 0.5)

---

## Accounts (`accounts.py`)

### get_account_numbers() -> List[dict]

* GET `/accounts/accountNumbers`
* Returns `accountNumber` and `hashValue` pairs.
* Example response:

```json
[
  {"accountNumber":"12345678","hashValue":"827C...AC12"}
]
```

### get_accounts(params: dict|None=None) -> dict

* GET `/accounts`
* Query parameters:

  * `fields` (optional): the API currently accepts `positions` to return positions. E.g.: `fields=positions`.

### get_account_by_id(account_hash: str, params: dict|None=None) -> dict

* GET `/accounts/{accountNumber}`
* `account_hash`: encrypted account identifier (`hashValue`).
* Query parameters:

  * `fields` (optional): `positions` to include positions. E.g.: `fields=positions`.

### find_account(last_4_digits: str) -> dict|None

* Helper that uses `get_account_numbers()` and filters by the last 4 digits, then calls `get_account_by_id`.

### get_transactions(account_hash: str, from_date: str|None, to_date: str|None, filters: dict|None=None) -> dict

* GET `/accounts/{accountHash}/transactions`
* **ONE DATE REQUIRED**: you may pass only `from_date` or only `to_date`. If you pass a single date, the SDK fills in the other for the same day:

  * Short format `YYYY-MM-DD`: start → `YYYY-MM-DDT00:00:00.000Z`, end → `YYYY-MM-DDT23:59:59.000Z`
  * Full ISO UTC `YYYY-MM-DDTHH:MM:SS.ffffffZ`: used as-is; if the other date is missing, it is derived with `00:00:00.000Z` or `23:59:59.000Z` of the same day.
* Params:

  * `startDate`: ISO UTC - `YYYY-MM-DDTHH:MM:SS.ffffffZ` (or short `YYYY-MM-DD`)
  * `endDate`: ISO UTC - `YYYY-MM-DDTHH:MM:SS.ffffffZ` (or short `YYYY-MM-DD`)
  * `filters`: optional dict:

    * `types`: string with valid types: `TRADE`, `RECEIVE_AND_DELIVER`, `DIVIDEND_OR_INTEREST`, `ACH_RECEIPT`, `ACH_DISBURSEMENT`, `CASH_RECEIPT`, `CASH_DISBURSEMENT`, `ELECTRONIC_FUND`, `WIRE_OUT`, `WIRE_IN`, `JOURNAL`
    * `symbol`: specific symbol
    * `status`: transaction status

  Note: In some API configurations, `types` may be considered mandatory. The SDK does not require it and treats it as an optional filter.

**Correct example**:

```python
from datetime import datetime, timezone, timedelta

# Get hashValue
hash_value = client.account.get_account_numbers()[0]['hashValue']

# Create UTC dates
start = datetime.now(timezone.utc) - timedelta(days=7)
start = start.replace(hour=0, minute=0, second=0, microsecond=0)
end = datetime.now(timezone.utc).replace(hour=23, minute=59, second=59, microsecond=999999)

# Proper call
transactions = client.account.get_transactions(
    account_hash=hash_value,  # Use hashValue!
    from_date=start.strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
    to_date=end.strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
    filters={"types": "TRADE,DIVIDEND_OR_INTEREST"}  # Optional
)
```

### get_transaction(account_hash: str, transaction_id: str) -> dict

* GET `/accounts/{accountHash}/transactions/{transactionId}`
* Path parameters:

  * `account_hash` (required)
  * `transaction_id` (required): numeric transaction ID
* Returns details of a specific transaction.

### get_user_preferences() -> dict

* GET `/userPreference`
* Returns user preferences and, when applicable, streamer information needed for WebSocket:

  * `streamerSocketUrl`
  * `schwabClientCustomerId`
  * `schwabClientCorrelId`
  * `SchwabClientChannel`
  * `SchwabClientFunctionId`
* Useful to initialize `client.streaming` (LOGIN and subscriptions).

---

## Orders (`orders.py`)

All responses include HTTP metadata and the native data:

```json
{
  "status_code": 200,
  "success": true,
  "headers": {"...": "..."},
  "url": "https://...",
  "elapsed_seconds": 0.42,
  "method": "GET|POST|PUT|DELETE",
  "params": {"...": "..."},
  "data": {},
  "order_id": "..."   
}
```

### get_orders(account_hash, from_entered_time=None, to_entered_time=None, status=None, max_results=None) -> dict

* GET `/accounts/{accountNumber}/orders`
* **Date format**: Accepts both `YYYY-MM-DD` (auto-converted to ISO) or full ISO-8601 format. If omitted, defaults to "last 60 days".
* `status` (case-insensitive): normalized to uppercase. Values accepted by the API:
  `AWAITING_PARENT_ORDER`, `AWAITING_CONDITION`, `AWAITING_STOP_CONDITION`, `AWAITING_MANUAL_REVIEW`, `ACCEPTED`, `AWAITING_UR_OUT`, `PENDING_ACTIVATION`, `QUEUED`, `WORKING`, `REJECTED`, `PENDING_CANCEL`, `CANCELED`, `PENDING_REPLACE`, `REPLACED`, `FILLED`, `EXPIRED`, `NEW`, `AWAITING_RELEASE_TIME`, `PENDING_ACKNOWLEDGEMENT`, `PENDING_RECALL`, `UNKNOWN`.
* `maxResults` (optional): record limit (API default 3000).
* **Date conversion**: `YYYY-MM-DD` → `YYYY-MM-DDT00:00:00.000Z` (start) or `YYYY-MM-DDT23:59:59.000Z` (end)

### get_all_orders(from_entered_time=None, to_entered_time=None, status=None, max_results=None) -> dict

* GET `/orders`
* **Date format**: Accepts both `YYYY-MM-DD` (auto-converted to ISO) or full ISO-8601 format. If omitted, defaults to "last 60 days".
* Filters identical to `get_orders` (including `status` normalization).
* `maxResults` (optional): record limit (API default 3000).
* **Date conversion**: `YYYY-MM-DD` → `YYYY-MM-DDT00:00:00.000Z` (start) or `YYYY-MM-DDT23:59:59.000Z` (end)

### place_order(account_hash: str, order_data: dict) -> dict

* POST `/accounts/{accountNumber}/orders`
* Extracts `order_id` from the `Location` header when present.

### get_order(account_hash: str, order_id: str) -> dict

* GET `/accounts/{accountNumber}/orders/{orderId}`

### cancel_order(account_hash: str, order_id: str) -> dict

* DELETE `/accounts/{accountNumber}/orders/{orderId}`
* Tries to extract `order_id` from `Location` if the server returns it.

### replace_order(account_hash: str, order_id: str, new_order_data: dict) -> dict

* PUT `/accounts/{accountNumber}/orders/{orderId}`
* Returns new `order_id` (from `Location`) when applicable.

### preview_order(account_hash: str, order_data: dict) -> dict

* POST `/accounts/{accountNumber}/previewOrder`
* Tries to extract `order_id` from `Location` if the server returns it.

### Payload helpers

* `build_limit_order(symbol, quantity, price, instruction="BUY")`
* `build_market_order(symbol, quantity, instruction="BUY")`
* `build_bracket_order(symbol, quantity, entry_price, take_profit_price, stop_loss_price)`

Example (preview):

```python
acc = client.account.get_account_numbers()[0]['hashValue']
order = client.orders.build_limit_order("AAPL", 1, 100.00)
preview = client.orders.preview_order(acc, order)
```

---

## Market Data (`market.py`)

### get_quotes(symbols: str|List[str], params: dict|None=None) -> dict

* GET `/quotes?symbols=...`
* Parameters:

  * `symbols` (required): str or list of symbols separated by commas. E.g.: `AAPL,AMZN,$DJI,/ESH23`.
  * `params` (optional):

    * `fields`: subset of data. Values: `quote`, `fundamental`, `extended`, `reference`, `regular`. Default: all.
    * `indicative`: boolean (`true|false`) to include indicative quotes (e.g., ETFs). Example: `indicative=false`.

### get_quote(symbol: str, params: dict|None=None) -> dict

* GET `/{symbol}/quotes`
* Parameters:

  * `symbol` (required): single symbol (e.g., `TSLA`).
  * `params` (optional):

    * `fields`: same as in `get_quotes`.

### get_option_chain(symbol: str, contract_type: str|None=None, strike_count: int|None=None, include_underlying_quote: bool|None=None, strategy: str|None=None, interval: float|None=None, strike: float|None=None, range_type: str|None=None, from_date: str|None=None, to_date: str|None=None, volatility: float|None=None, underlying_price: float|None=None, interest_rate: float|None=None, days_to_expiration: int|None=None, exp_month: str|None=None, option_type: str|None=None, entitlement: str|None=None, params: dict|None=None) -> dict

* GET `/chains`

* Parameters:

  * `symbol` (required): Underlying asset symbol
  * `contract_type` (optional): Contract Type. Available values: `CALL`, `PUT`, `ALL`
  * `strike_count` (optional): The Number of strikes to return above or below the at-the-money price
  * `include_underlying_quote` (optional): Underlying quotes to be included (boolean)
  * `strategy` (optional): OptionChain strategy. Default is SINGLE. Available values: `SINGLE`, `ANALYTICAL`, `COVERED`, `VERTICAL`, `CALENDAR`, `STRANGLE`, `STRADDLE`, `BUTTERFLY`, `CONDOR`, `DIAGONAL`, `COLLAR`, `ROLL`
  * `interval` (optional): Strike interval for spread strategy chains (see strategy param)
  * `strike` (optional): Strike Price
  * `range_type` (optional): Range(ITM/NTM/OTM etc.)
  * `from_date` (optional): From date (pattern: yyyy-MM-dd)
  * `to_date` (optional): To date (pattern: yyyy-MM-dd)
  * `volatility` (optional): Volatility to use in calculations. Applies only to ANALYTICAL strategy chains
  * `underlying_price` (optional): Underlying price to use in calculations. Applies only to ANALYTICAL strategy chains
  * `interest_rate` (optional): Interest rate to use in calculations. Applies only to ANALYTICAL strategy chains
  * `days_to_expiration` (optional): Days to expiration to use in calculations. Applies only to ANALYTICAL strategy chains
  * `exp_month` (optional): Expiration month. Available values: `JAN`, `FEB`, `MAR`, `APR`, `MAY`, `JUN`, `JUL`, `AUG`, `SEP`, `OCT`, `NOV`, `DEC`, `ALL`
  * `option_type` (optional): Option Type
  * `entitlement` (optional): Applicable only if its retail token, entitlement of client PP-PayingPro, NP-NonPro and PN-NonPayingPro. Available values: `PN`, `NP`, `PP`
  * `params` (optional): Additional query parameters

### get_expiration_chain(symbol: str, params: dict|None=None) -> dict

* GET `/expirationchain`

* Parameters:

  * `symbol` (required): Underlying asset symbol
  * `params` (optional): Additional query parameters

* Returns: JSON response with option expiration dates for the symbol

### get_price_history(symbol, periodType="month", period=1, frequencyType="daily", frequency=1, startDate=None, endDate=None, params=None) -> dict

* GET `/pricehistory`
* Parameters:

  * `periodType`: `day|month|year|ytd`
  * `period`: int
  * `frequencyType`: `minute|daily|weekly|monthly`
  * `frequency`: int
  * `startDate`/`endDate` (ms since epoch)
  * Additional optionals (`needExtendedHoursData`, etc., depending on entitlements)

### get_movers(symbol_id: str, sort: str|None=None, frequency: int|None=None, params: dict|None=None) -> dict

* GET `/movers/{symbol_id}` (e.g., `$DJI`, `$SPX`, `NASDAQ`)
* Parameters:

  * `symbol_id` (required): Index Symbol. Available values: `$DJI`, `$COMPX`, `$SPX`, `NYSE`, `NASDAQ`, `OTCBB`, `INDEX_ALL`, `EQUITY_ALL`, `OPTION_ALL`, `OPTION_PUT`, `OPTION_CALL`
  * `sort` (optional): Sort by a particular attribute. Available values: `VOLUME`, `TRADES`, `PERCENT_CHANGE_UP`, `PERCENT_CHANGE_DOWN`
  * `frequency` (optional): To return movers with the specified directions of up or down. Available values: `0,1,5,10,30,60` (min). Default `0`
  * `params` (optional): Additional query parameters

### get_markets(params: dict|None=None) -> dict

* GET `/markets`
* Query parameters:

  * `markets` (required by API): array of `equity`, `option`, `bond`, `future`, `forex` (the SDK accepts `params={"markets": ...}`)
  * `date` (optional): `YYYY-MM-DD` (if you send ISO, the SDK trims to date)

### get_market_hours(market_id: str, params: dict|None=None) -> dict

* GET `/markets/{market_id}` (`equity`, `option`, `bond`, `forex`)
* Query parameters:

  * `date` (optional): `YYYY-MM-DD` (if you send ISO, the SDK trims to date)

### get_instruments(symbols: str|List[str], projection: str, extra_params: dict|None=None) -> dict

* GET `/instruments`
* Parameters:

  * `symbols` (required): single symbol or comma-separated list
  * `projection` (required by API): `symbol-search`, `symbol-regex`, `desc-search`, `desc-regex`, `search`, `fundamental`
    Note: the SDK does not require it in the signature, but it is recommended to provide it.

### get_instrument_by_cusip(cusip_id: str, params: dict|None=None) -> dict

* GET `/instruments/{cusip_id}`

---

## WebSocket Streaming (`streaming.py`)

### Callbacks

* `on_data(fn)`  (data frames)
* `on_response(fn)`  (command confirmations/errors)
* `on_notify(fn)`  (heartbeats/notices)

### Basic flow

```python
ws = client.streaming
ws.on_data(lambda f: print("DATA", f))
ws.on_response(lambda f: print("RESP", f))
ws.on_notify(lambda f: print("NOTIFY", f))
ws.connect(); ws.login()  # Authorization = access token without "Bearer"
ws.equities_subscribe(["AAPL","MSFT"])             # LEVELONE_EQUITIES
ws.options_subscribe(["AAPL  250926C00257500"])      # LEVELONE_OPTIONS
ws.nasdaq_book(["MSFT"])                             # NASDAQ_BOOK
ws.chart_equity(["AAPL"])                            # CHART_EQUITY
ws.screener_equity(["NYSE_VOLUME_5"])                # SCREENER_EQUITY
```

### Key Formats (quick table)

| Type           | Format                               | Example                 | Notes                                      |
| -------------- | ------------------------------------ | ----------------------- | ------------------------------------------ |
| Equities       | Ticker                               | `AAPL`, `MSFT`          | Uppercase                                  |
| Options        | `RRRRRRYYMMDDsWWWWWddd`              | `AAPL  251219C00200000` | 6-char symbol, YYMMDD, C/P, 5+3 strike    |
| Futures        | `/<root><month><yy>`                 | `/ESZ25`                | Root/month/year in uppercase               |
| FuturesOptions | `./<root><month><year><C/P><strike>` | `./OZCZ23C565`          | Depends on the feed                        |
| Forex          | `PAIR`                               | `EUR/USD`, `USD/JPY`    | `/` separator                              |
| Screener       | `PREFIX_SORTFIELD_FREQUENCY`         | `NYSE_VOLUME_5`         | Prefix/criterion/frequency                 |

### Service-by-Service Examples

#### Level One Options

```python
ws.options_subscribe(["AAPL  250926C00257500"])  # standard option string
# Default fields: 0,2,3,4,8,16,17,18,20,28,29,30,31,37,44
```

#### Level One Futures

```python
ws.futures_subscribe(["/ESZ25"])  # E-mini S&P 500 Dec 2025
# Default fields: 0,1,2,3,4,5,8,12,13,18,19,20,24,33
```

#### Level One Forex

```python
ws.forex_subscribe(["EUR/USD","USD/JPY"])  
# Default fields: 0,1,2,3,4,5,6,7,8,9,10,11,15,16,17,20,21,27,28,29
```

#### Book (Level II)

```python
ws.nasdaq_book(["MSFT"])  # Also: ws.nyse_book, ws.options_book
# Default fields: 0 (Symbol), 1 (BookTime), 2 (Bids), 3 (Asks)
```

#### Chart (Series)

```python
ws.chart_equity(["AAPL"])      # 0..7: key, open, high, low, close, volume, sequence, chartTime
ws.chart_futures(["/ESZ25"])   # 0..5
```

#### Screener

```python
ws.screener_equity(["NYSE_VOLUME_5"])   
ws.screener_options(["CBOE_VOLUME_5"])
```

#### Account Activity

```python
ws.account_activity()  # Gets accountHash and subscribes
```

### Utilities

* `subscribe(service, keys, fields=None)`
* `add(service, keys)`
* `unsubscribe(service, keys)` / `unsubscribe_service(service, keys)`
* `view(service, fields)`

### Option Symbol Helper

* `create_option_symbol(symbol, expiration, option_type, strike_price)` - Creates Schwab option symbol from components

**Example:**
```python
# Create option symbol from components
option_symbol = ws.create_option_symbol("AAPL", "2025-12-19", "C", 200.0)
# Returns: "AAPL  251219C00200000"

# Use in subscription
ws.options_subscribe([option_symbol])
```

**Parameters:**
* `symbol`: Underlying symbol (e.g., "AAPL")
* `expiration`: Expiration date in "YYYY-MM-DD" format (e.g., "2025-10-03")
* `option_type`: "C" for Call or "P" for Put
* `strike_price`: Strike price (e.g., 257.5)

### Recommended Fields

* LEVELONE_EQUITIES: `0,1,2,3,4,5,8,10,18,42,33,34,35`
* LEVELONE_OPTIONS: `0,2,3,4,8,16,17,18,20,28,29,30,31,37,44`
* LEVELONE_FUTURES: `0,1,2,3,4,5,8,12,13,18,19,20,24,33`
* CHART_EQUITY: `0,1,2,3,4,5,6,7`

#### Quick fields table (copy/paste)

| Service                  | Fields CSV                                        |
| ------------------------ | ------------------------------------------------- |
| LEVELONE_EQUITIES        | 0,1,2,3,4,5,8,10,18,42,33,34,35                   |
| LEVELONE_OPTIONS         | 0,2,3,4,8,16,17,18,20,28,29,30,31,37,44           |
| LEVELONE_FUTURES         | 0,1,2,3,4,5,8,12,13,18,19,20,24,33                |
| LEVELONE_FUTURES_OPTIONS | 0,1,2,3,4,5,8,12,13,18,19,20,24,33                |
| LEVELONE_FOREX           | 0,1,2,3,4,5,6,7,8,9,10,11,15,16,17,20,21,27,28,29 |
| NASDAQ_BOOK              | 0,1,2,3                                           |
| NYSE_BOOK                | 0,1,2,3                                           |
| OPTIONS_BOOK             | 0,1,2,3                                           |
| CHART_EQUITY             | 0,1,2,3,4,5,6,7                                   |
| CHART_FUTURES            | 0,1,2,3,4,5                                       |
| SCREENER_EQUITY          | 0,1,2,3,4                                         |
| SCREENER_OPTION          | 0,1,2,3,4                                         |
| ACCT_ACTIVITY            | 0,1,2                                             |

### SUBS / ADD / VIEW / UNSUBS examples by service

```python
ws = client.streaming
ws.on_data(lambda f: print("DATA", f))
ws.on_response(lambda f: print("RESP", f))
ws.connect(); ws.login()

# LEVELONE_EQUITIES
ws.equities_subscribe(["AAPL","TSLA"], fields=[0,1,2,3,4,5,8,10,18,42,33,34,35])
ws.equities_add(["MSFT"])                          # adds without replacing
ws.equities_view([0,1,2,3,5,8,18])                  # changes fields
ws.equities_unsubscribe(["TSLA"])                  # removes symbols

# LEVELONE_OPTIONS
ws.options_subscribe(["AAPL  250926C00257500"], fields=[0,2,3,4,8,16,17,18,20,28,29,30,31,37,44])
ws.options_add(["AAPL  250926P00257500"])          
ws.options_view([0,2,3,4,8,16,17,20,28,29,30,31,37,44])
ws.options_unsubscribe(["AAPL  250926C00257500"])  

# LEVELONE_FUTURES
ws.futures_subscribe(["/ESZ25"], fields=[0,1,2,3,4,5,8,12,13,18,19,20,24,33])
ws.futures_add(["/NQZ25"])
ws.futures_view([0,1,2,3,4,5,8,12,13,18,19,20,24,33])
ws.futures_unsubscribe(["/ESZ25"])  

# BOOK (Level II)
ws.nasdaq_book(["MSFT"], fields=[0,1,2,3])
ws.add("NASDAQ_BOOK", ["AAPL"])                    # generic ADD
ws.view("NASDAQ_BOOK", [0,1,2,3])                   # generic VIEW
ws.unsubscribe_service("NASDAQ_BOOK", ["MSFT"])    # generic UNSUBS

# CHART (Series)
ws.chart_equity(["AAPL"], fields=[0,1,2,3,4,5,6,7])
ws.add("CHART_EQUITY", ["MSFT"])                  # generic ADD
ws.view("CHART_EQUITY", [0,1,2,3,4,5,6,7])          # generic VIEW
ws.unsubscribe("CHART_EQUITY", ["AAPL"])           # generic UNSUBS

# SCREENER
ws.screener_equity(["EQUITY_ALL_VOLUME_5"], fields=[0,1,2,3,4])
ws.add("SCREENER_EQUITY", ["NYSE_TRADES_1"])      
ws.view("SCREENER_EQUITY", [0,1,2,3,4])
ws.unsubscribe("SCREENER_EQUITY", ["EQUITY_ALL_VOLUME_5"])

# ACCT_ACTIVITY
ws.account_activity(fields=[0,1,2])                  # subscribe account activity
# For UNSUBS you need the same key used in SUBS (account_hash)
account_hash = getattr(client, "_account_hash", None)
if account_hash:
    ws.unsubscribe_service("ACCT_ACTIVITY", [account_hash])
```

### Quick Field Guide (IDs → meaning)

> Note: exact mappings may vary depending on entitlements/version. Below are practical equivalences observed in frames.

#### LEVELONE_EQUITIES

| ID | Field                      |
| -- | -------------------------- |
| 0  | symbol/key                 |
| 1  | bidPrice                   |
| 2  | askPrice                   |
| 3  | lastPrice                  |
| 4  | bidSize                    |
| 5  | askSize                    |
| 8  | totalVolume                |
| 10 | referencePrice (open/mark) |
| 18 | netChange                  |
| 42 | percentChange              |

#### LEVELONE_OPTIONS

| ID | Field                           |
| -- | ------------------------------- |
| 0  | symbol/key                      |
| 2  | bidPrice                        |
| 3  | askPrice                        |
| 4  | lastPrice                       |
| 8  | totalVolume                     |
| 16 | openInterest                    |
| 17 | daysToExpiration                |
| 20 | strikePrice                     |
| 28 | delta                           |
| 29 | gamma                           |
| 30 | theta                           |
| 31 | vega                            |
| 44 | impliedVolatility (if provided) |

#### LEVELONE_FUTURES

| ID | Field                                      |
| -- | ------------------------------------------ |
| 0  | symbol/key                                 |
| 1  | bidPrice                                   |
| 2  | askPrice                                   |
| 3  | lastPrice                                  |
| 4  | bidSize                                    |
| 5  | askSize                                    |
| 8  | totalVolume                                |
| 12 | openInterest                               |
| 13 | contractDepth/series info (per feed)       |
| 18 | netChange                                  |
| 19 | sessionChange (or days/indicator per feed) |
| 20 | percentChange/ratio (per feed)             |
| 24 | lastSettlement/mark                        |
| 33 | priorSettle                                |

### Frame Structure

* Confirmations (`response`): `{ "response": [ { "service":"ADMIN","command":"LOGIN","content":{"code":0,"msg":"..."}} ] }`
* Data (`data`): `{ "service":"LEVELONE_EQUITIES","timestamp":...,"command":"SUBS","content":[{"key":"AAPL",...}] }`
* Notifications (`notify`): `{ "notify": [ { "heartbeat": "..." } ] }`

### Streamer API Cheat Sheet (parameters and commands)

1. Connection and prerequisites

* **Auth**: use the Access Token from the OAuth flow.
* **Session IDs** (from `GET /userPreference`): `schwabClientCustomerId`, `schwabClientCorrelId`, `SchwabClientChannel`, `SchwabClientFunctionId`.
* **Transport**: JSON WebSocket. One stream per user (if you open more: code 12 CLOSE_CONNECTION).

2. Envelope of each command

* **Common fields**:

  * `service` (req.): `ADMIN`, `LEVELONE_EQUITIES`, `LEVELONE_OPTIONS`, `LEVELONE_FUTURES`, `LEVELONE_FUTURES_OPTIONS`, `LEVELONE_FOREX`, `NYSE_BOOK`, `NASDAQ_BOOK`, `OPTIONS_BOOK`, `CHART_EQUITY`, `CHART_FUTURES`, `SCREENER_EQUITY`, `SCREENER_OPTION`, `ACCT_ACTIVITY`.
  * `command` (req.): `LOGIN`, `SUBS`, `ADD`, `UNSUBS`, `VIEW`, `LOGOUT`.
  * `requestid` (req.): unique request identifier.
  * `SchwabClientCustomerId` and `SchwabClientCorrelId` (recommended): from `userPreference`.
  * `parameters` (optional): depends on service/command.
* **Notes**: `SUBS` overwrites list; `ADD` appends; `UNSUBS` removes; `VIEW` changes `fields`.

3. ADMIN (session)

* `LOGIN` (`service=ADMIN`, `command=LOGIN`)

  * parameters: `Authorization` (token without "Bearer"), `SchwabClientChannel`, `SchwabClientFunctionId`.
* `LOGOUT` (`service=ADMIN`, `command=LOGOUT`)

  * parameters: empty.

4. LEVEL ONE (L1 quotes)

* Common parameters: `keys` (req., CSV list), `fields` (optional, indexes).
* `LEVELONE_EQUITIES`: `keys` uppercase tickers (e.g., `AAPL,TSLA`).
* `LEVELONE_OPTIONS`: `keys` Schwab option format `RRRRRR  YYMMDD[C/P]STRIKE`.
* `LEVELONE_FUTURES`: `keys` `/<root><monthCode><yearCode>` (month codes: F,G,H,J,K,M,N,Q,U,V,X,Z; year two digits), e.g., `/ESZ25`.
* `LEVELONE_FUTURES_OPTIONS`: `keys` `./<root><month><yy><C|P><strike>`, e.g., `./OZCZ23C565`.
* `LEVELONE_FOREX`: `keys` `BASE/QUOTE` pairs CSV (e.g., `EUR/USD,USD/JPY`).

5. BOOK (Level II)

* Services: `NYSE_BOOK`, `NASDAQ_BOOK`, `OPTIONS_BOOK`.
* Parameters: `keys` (req., tickers), `fields` (optional, level indexes).

6. CHART (streaming series)

* `CHART_EQUITY`: `keys` equities; `fields` indexes (OHLCV, time, seq).
* `CHART_FUTURES`: `keys` futures (same format as L1 futures); `fields` indexes.

7. SCREENER (gainers/losers/actives)

* Services: `SCREENER_EQUITY`, `SCREENER_OPTION`.
* `keys` pattern `PREFIX_SORTFIELD_FREQUENCY`, e.g., `EQUITY_ALL_VOLUME_5`.

  * `PREFIX` examples: `$COMPX`, `$DJI`, `$SPX`, `INDEX_ALL`, `NYSE`, `NASDAQ`, `OTCBB`, `EQUITY_ALL`, `OPTION_PUT`, `OPTION_CALL`, `OPTION_ALL`.
  * `SORTFIELD`: `VOLUME`, `TRADES`, `PERCENT_CHANGE_UP`, `PERCENT_CHANGE_DOWN`, `AVERAGE_PERCENT_VOLUME`.
  * `FREQUENCY`: `0,1,5,10,30,60` (min; `0` = full day).
* `fields` (optional): screener field indexes.

8. ACCOUNT (account activity)

* Service: `ACCT_ACTIVITY` (`SUBS`/`UNSUBS`).
* `keys` (req.): arbitrary identifier for your sub; if you send multiple, the first is used.
* `fields` (recommended): `0` (or `0,1,2,3` per example/need).

9. Server responses

* Types: `response` (to your requests), `notify` (heartbeats), `data` (market flow).
* Key codes: `0` SUCCESS, `3` LOGIN_DENIED, `11` SERVICE_NOT_AVAILABLE, `12` CLOSE_CONNECTION, `19` REACHED_SYMBOL_LIMIT, `20` STREAM_CONN_NOT_FOUND, `21` BAD_COMMAND_FORMAT, `26/27/28/29` successes for `SUBS/UNSUBS/ADD/VIEW`.

10. Delivery Types

* `All Sequence`: everything with sequence number.
* `Change`: only changed fields (conflated).
* `Whole`: full messages with throttling.

11. Best practices

* Do `LOGIN` and wait for `code=0` before `SUBS/ADD`.
* To add symbols without losing existing ones, use `ADD` (not `SUBS`).
* Change `fields` with `VIEW` for performance.
* Handle `notify` (heartbeats) and reconnect if they are lost.
* Reuse your `SchwabClientCorrelId` during the session.
* If you see `19` (symbol limit), shard loads by service/session.

---

## Advanced Troubleshooting

* `notify.code=12` (Only one connection): close other active WebSocket sessions.
* `response.content.code=3` (Login denied): invalid/expired token → `client.login()`.
* `response.content.code=21` (Bad command formatting): check `Authorization` format (without `Bearer`) and keys (spacing for options, uppercase).
* Persistent REST 401: delete `schwab_tokens.json` and re-run `client.login()`.
* High latency/lost frames: avoid parallel reconnects; use the SDK's auto-resubscription.

---

## Contributions

Your contributions are welcome! Ideas, issues, and PRs help improve the SDK:

* Open an issue with clear details (environment, steps, expected/actual error).
* Propose endpoint coverage improvements and examples.
* Follow a clear style and add tests or minimal examples when possible.

If you want to hold working sessions or discuss the roadmap, open an issue labeled `discussion`.

## Disclaimer

This project is unofficial and is not affiliated with, sponsored by, or endorsed by Charles Schwab & Co., Inc. “Schwab” and other trademarks are the property of their respective owners. Use of this SDK is subject to the terms and conditions of Schwab APIs and applicable regulations. Use at your own discretion and responsibility.

---

## License

MIT ([LICENSE](LICENSE))

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "schwab-sdk-unofficial",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "schwab, trading, market-data, websocket, sdk, api",
    "author": "Schwab SDK Contributors",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/15/61/1c3a888d28d6e38d8b5ab8122338188b646670e992f6fe2d9f7d445113ca/schwab_sdk_unofficial-0.2.7.tar.gz",
    "platform": null,
    "description": "# Schwab SDK (Python)\r\n\r\nLightweight client for the Schwab API: OAuth, REST (Trader/Market Data), and WebSocket Streaming.\r\n\r\n* Focus: thin wrappers, no heavy validation; robust token handling, refresh, and retries.\r\n* Coverage: Accounts, Orders, Market Data, and Streaming (Level One, Book, Chart, Screener, Account Activity).\r\n\r\n## Installation\r\n\r\nUnofficial PyPI package (distribution name):\r\n\r\n```bash\r\npip install schwab_sdk_unofficial\r\n```\r\n\r\nImport in code (module):\r\n\r\n```python\r\nfrom schwab_sdk import Client, AsyncClient\r\n```\r\n\r\n\u26a0\ufe0f Note: The package name on PyPI is `schwab_sdk_unofficial`, but the import remains `schwab_sdk` for a clean API.\r\n\r\n## Table of Contents\r\n\r\n* [Requirements](#requirements)\r\n* [Configuration](#configuration)\r\n* [Quick Start](#quick-start)\r\n* [Authentication (OAuth)](#authentication-oauth)\r\n* [Request & Error Handling (REST)](#request--error-handling-rest)\r\n* [Accounts (`accounts.py`)](#accounts-accountspy)\r\n* [Orders (`orders.py`)](#orders-orderspy)\r\n* [Market Data (`market.py`)](#market-data-marketpy)\r\n* [WebSocket Streaming (`streaming.py`)](#websocket-streaming-streamingpy)\r\n\r\n  * [Key Formats (quick table)](#key-formats-quick-table)\r\n  * [Service-by-Service Examples](#service-by-service-examples)\r\n  * [Utilities](#utilities)\r\n  * [Recommended Fields](#recommended-fields)\r\n  * [Quick Field Guide (IDs \u2192 meaning)](#quick-field-guide-ids--meaning)\r\n  * [Frame Structure](#frame-structure)\r\n* [Advanced Troubleshooting](#advanced-troubleshooting)\r\n* [Contributions](#contributions)\r\n* [Disclaimer](#disclaimer)\r\n* [License](#license)\r\n\r\n## Requirements\r\n\r\n* Python 3.9+\r\n* Install dependencies:\r\n\r\n```bash\r\npip install requests websocket-client flask\r\n```\r\n\r\n## Configuration\r\n\r\nCreate `.env` (or export environment variables):\r\n\r\n```env\r\nSCHWAB_CLIENT_ID=your_client_id\r\nSCHWAB_CLIENT_SECRET=your_client_secret\r\nSCHWAB_REDIRECT_URI=https://127.0.0.1:8080/callback\r\n```\r\n\r\n### Token storage modes\r\n\r\n- `save_token` controla la persistencia de tokens en los clientes (Sync/Async).\r\n- `save_token=True` (default): guarda tokens en archivo JSON (`schwab_tokens.json`) y rota autom\u00e1ticamente (access ~29 min; aviso de re-login cuando el refresh expira ~7 d).\r\n- `save_token=False`: mantiene tokens solo en memoria; puedes inicializar con `token_data` y consultar el estado con `client.token_handler.get_token_payload()`.\r\n\r\n## Quick Start\r\n\r\n```python\r\nfrom schwab_sdk import Client\r\nimport os\r\n\r\nclient = Client(\r\n    os.environ['SCHWAB_CLIENT_ID'],\r\n    os.environ['SCHWAB_CLIENT_SECRET'],\r\n    os.environ.get('SCHWAB_REDIRECT_URI','https://127.0.0.1:8080/callback'),\r\n    save_token=True,  # True: guarda en archivo JSON, False: solo en memoria\r\n    # Opcional: inicializar tokens desde un dict (refresh/boot)\r\n    token_data={\r\n        # Minimal example: provide current tokens\r\n        # 'access_token': '...',\r\n        # 'refresh_token': '...',\r\n        # either expires_in (seconds) OR access_token_expires_at (ISO)\r\n        # 'expires_in': 1800,\r\n        # 'access_token_expires_at': '2025-10-01T12:00:00',\r\n        # optional refresh token expiration (ISO)\r\n        # 'refresh_token_expires_at': '2025-10-07T11:00:00'\r\n    }\r\n)\r\n\r\n# First use: OAuth login (opens the browser)\r\nresult = client.login()\r\nif result[\"success\"]:\r\n    print(\"Login successful!\")\r\n    print(\"Token data:\", result[\"tokens\"])\r\nelse:\r\n    print(\"Login failed\")\r\n\r\n# REST\r\nquotes = client.market.get_quotes([\"AAPL\",\"MSFT\"])  # Market Data\r\naccounts = client.account.get_accounts()               # Accounts\r\n\r\n# Streaming (Level One equities)\r\nws = client.streaming\r\nws.on_data(lambda f: print(\"DATA\", f))\r\nws.connect(); ws.login()\r\nws.equities_subscribe([\"AAPL\"])\r\n```\r\n\r\n## Async Support\r\n\r\nThe SDK also provides async versions of all endpoints using `AsyncClient`:\r\n\r\n```python\r\nimport asyncio\r\nfrom schwab_sdk import AsyncClient\r\n\r\nasync def main():\r\n    # Initialize async client\r\n    async with AsyncClient(\r\n        os.environ['SCHWAB_CLIENT_ID'],\r\n        os.environ['SCHWAB_CLIENT_SECRET'],\r\n        save_token=True  # True: guarda en archivo JSON, False: solo en memoria\r\n    ) as client:\r\n        # Login (async wrapper)\r\n        result = await client.login()\r\n        if result[\"success\"]:\r\n            print(\"Async login successful!\")\r\n            print(\"Token data:\", result[\"tokens\"])\r\n        \r\n        # Async REST calls\r\n        quotes = await client.market.get_quotes([\"AAPL\", \"MSFT\"])\r\n        accounts = await client.account.get_accounts()\r\n        orders = await client.orders.get_all_orders()\r\n        \r\n        # Async streaming\r\n        await client.streaming.connect()\r\n        await client.streaming.login()\r\n        await client.streaming.equities_subscribe([\"AAPL\"])\r\n        \r\n        # Set up callbacks\r\n        client.streaming.on_data(lambda data: print(\"DATA:\", data))\r\n        client.streaming.on_response(lambda resp: print(\"RESPONSE:\", resp))\r\n        \r\n        # Keep running to receive data\r\n        await asyncio.sleep(10)  # Example: run for 10 seconds\r\n        await client.streaming.disconnect()\r\n\r\n# Run async function\r\nasyncio.run(main())\r\n```\r\n\r\n### Async Benefits\r\n\r\n- **Non-blocking**: Multiple API calls can run concurrently\r\n- **Better performance**: Especially for multiple simultaneous requests\r\n- **Context manager**: Automatic session cleanup with `async with`\r\n- **Same API**: All methods have async equivalents with `await`\r\n\r\n### Async vs Sync\r\n\r\n| Feature | Sync | Async |\r\n|---------|------|-------|\r\n| **Import** | `from schwab_sdk import Client` | `from schwab_sdk import AsyncClient` |\r\n| **Usage** | `client.method()` | `await client.method()` |\r\n| **Context** | `client = Client(...)` | `async with AsyncClient(...)` |\r\n| **Performance** | Sequential | Concurrent |\r\n| **Dependencies** | `requests`, `websocket-client` | `aiohttp`, `websockets` |\r\n\r\n### Async Streaming\r\n\r\nThe async streaming client provides non-blocking WebSocket connections:\r\n\r\n```python\r\nimport asyncio\r\nfrom schwab_sdk import AsyncClient\r\n\r\nasync def streaming_example():\r\n    async with AsyncClient(client_id, client_secret, save_token=False) as client:\r\n        await client.login()\r\n        \r\n        # Connect to streaming\r\n        await client.streaming.connect()\r\n        await client.streaming.login()\r\n        \r\n        # Set up callbacks\r\n        client.streaming.on_data(lambda data: print(\"Market data:\", data))\r\n        client.streaming.on_response(lambda resp: print(\"Response:\", resp))\r\n        client.streaming.on_notify(lambda notify: print(\"Notification:\", notify))\r\n        \r\n        # Subscribe to data streams\r\n        await client.streaming.equities_subscribe([\"AAPL\", \"MSFT\"])\r\n        await client.streaming.options_subscribe([\r\n            client.streaming.create_option_symbol(\"AAPL\", \"2025-12-19\", \"C\", 200.0)\r\n        ])\r\n        await client.streaming.account_activity_subscribe(\"your_account_hash\")\r\n        \r\n        # Keep running\r\n        await asyncio.sleep(30)  # Run for 30 seconds\r\n        \r\n        # Cleanup\r\n        await client.streaming.disconnect()\r\n\r\nasyncio.run(streaming_example())\r\n```\r\n\r\n### Streaming Services Available\r\n\r\n| Service | Method | Description |\r\n|---------|--------|-------------|\r\n| **Equities** | `equities_subscribe(symbols)` | Stock quotes |\r\n| **Options** | `options_subscribe(option_symbols)` | Option quotes |\r\n| **Futures** | `futures_subscribe(symbols)` | Futures quotes |\r\n| **Forex** | `forex_subscribe(pairs)` | Currency pairs |\r\n| **Account** | `account_activity_subscribe(account_hash)` | Account activity |\r\n\r\n## Async Accounts Module\r\n\r\nAsync version of account and transaction endpoints:\r\n\r\n### Methods\r\n\r\n* `get_accounts(fields=None)` - Get all accounts\r\n* `get_account_by_id(account_hash, fields=None)` - Get specific account\r\n* `get_transactions(account_hash, from_date=None, to_date=None, symbol=None, types=None)` - Get transactions\r\n* `get_transaction(account_hash, transaction_id)` - Get specific transaction\r\n* `get_preferences(account_hash)` - Get account preferences\r\n\r\n### Example\r\n\r\n```python\r\nasync with AsyncClient(client_id, client_secret) as client:\r\n    _ = await client.login()\r\n    \r\n    # Get all accounts\r\n    accounts = await client.account.get_accounts()\r\n    \r\n    # Get account with positions\r\n    account = await client.account.get_account_by_id(\"123456789\", fields=\"positions\")\r\n    \r\n    # Get transactions for date range\r\n    transactions = await client.account.get_transactions(\r\n        \"123456789\", \r\n        from_date=\"2025-01-01\", \r\n        to_date=\"2025-01-31\"\r\n    )\r\n```\r\n\r\n## Async Orders Module\r\n\r\nAsync version of order management endpoints:\r\n\r\n### Methods\r\n\r\n* `get_orders(account_hash, max_results=None, from_entered_time=None, to_entered_time=None, status=None)` - Get orders\r\n* `get_all_orders(max_results=None, from_entered_time=None, to_entered_time=None, status=None)` - Get all orders\r\n* `place_order(account_hash, order_data)` - Place new order\r\n* `get_order(account_hash, order_id)` - Get specific order\r\n* `cancel_order(account_hash, order_id)` - Cancel order\r\n* `replace_order(account_hash, order_id, order_data)` - Replace order\r\n* `preview_order(account_hash, order_data)` - Preview order\r\n\r\n### Example\r\n\r\n```python\r\nasync with AsyncClient(client_id, client_secret) as client:\r\n    _ = await client.login()\r\n    \r\n    # Get orders for account (various date formats)\r\n    orders = await client.orders.get_orders(\"123456789\", status=\"FILLED\")\r\n    \r\n    # Get orders with date range (YYYY-MM-DD format)\r\n    orders = await client.orders.get_orders(\r\n        \"123456789\", \r\n        from_entered_time=\"2025-01-01\", \r\n        to_entered_time=\"2025-01-31\"\r\n    )\r\n    \r\n    # Get orders with full ISO format (passed through as-is)\r\n    orders = await client.orders.get_orders(\r\n        \"123456789\",\r\n        from_entered_time=\"2025-01-01T09:00:00.000Z\",\r\n        to_entered_time=\"2025-01-01T17:00:00.000Z\"\r\n    )\r\n    \r\n    # Mixed formats also work\r\n    orders = await client.orders.get_orders(\r\n        \"123456789\",\r\n        from_entered_time=\"2025-01-01\",  # YYYY-MM-DD (auto-converted)\r\n        to_entered_time=\"2025-01-31T23:59:59.000Z\"  # Full ISO (passed through)\r\n    )\r\n    \r\n    # Place a market order\r\n    order_data = {\r\n        \"orderType\": \"MARKET\",\r\n        \"session\": \"NORMAL\",\r\n        \"duration\": \"DAY\",\r\n        \"orderStrategyType\": \"SINGLE\",\r\n        \"orderLegCollection\": [{\r\n            \"instruction\": \"BUY\",\r\n            \"quantity\": 100,\r\n            \"instrument\": {\"symbol\": \"AAPL\", \"assetType\": \"EQUITY\"}\r\n        }]\r\n    }\r\n    result = await client.orders.place_order(\"123456789\", order_data)\r\n```\r\n\r\n## Async Market Module\r\n\r\nAsync version of market data endpoints:\r\n\r\n### Methods\r\n\r\n* `get_quote(symbol)` - Get single quote\r\n* `get_quotes(symbols)` - Get multiple quotes\r\n* `get_movers(symbol_id, sort=None, frequency=None, params=None)` - Get market movers\r\n* `get_option_chain(symbol, contract_type=None, ...)` - Get option chain\r\n* `get_expiration_chain(symbol, params=None)` - Get expiration dates\r\n* `get_markets(date=None)` - Get market hours\r\n* `get_market_hours(date=None)` - Get market hours for date\r\n\r\n### Example\r\n\r\n```python\r\nasync with AsyncClient(client_id, client_secret) as client:\r\n    _ = await client.login()\r\n    \r\n    # Get quotes\r\n    quotes = await client.market.get_quotes([\"AAPL\", \"MSFT\", \"GOOGL\"])\r\n    \r\n    # Get option chain\r\n    options = await client.market.get_option_chain(\r\n        \"AAPL\",\r\n        contract_type=\"CALL\",\r\n        strike_count=5,\r\n        from_date=\"2025-01-01\",\r\n        to_date=\"2025-12-31\"\r\n    )\r\n    \r\n    # Get market movers\r\n    movers = await client.market.get_movers(\"$SPX.X\", sort=\"PERCENT_CHANGE\")\r\n```\r\n\r\n## Authentication (OAuth)\r\n\r\n* `client.login(timeout=300, auto_open_browser=True)`\r\n* Handy: `client.has_valid_token()`, `client.refresh_token_now()`, `client.logout()`\r\n* Internals: adhoc HTTPS callback server (dev), code-for-token exchange, auto-refresh and notice when refresh expires.\r\n\r\n## Request & Error Handling (REST)\r\n\r\nAll REST calls use `Client._request()` with:\r\n\r\n* Automatic Authorization headers\r\n* Refresh retry on 401 (once) and immediate resend\r\n* Retries with backoff for 429/5xx (exponential with factor 0.5)\r\n\r\n---\r\n\r\n## Accounts (`accounts.py`)\r\n\r\n### get_account_numbers() -> List[dict]\r\n\r\n* GET `/accounts/accountNumbers`\r\n* Returns `accountNumber` and `hashValue` pairs.\r\n* Example response:\r\n\r\n```json\r\n[\r\n  {\"accountNumber\":\"12345678\",\"hashValue\":\"827C...AC12\"}\r\n]\r\n```\r\n\r\n### get_accounts(params: dict|None=None) -> dict\r\n\r\n* GET `/accounts`\r\n* Query parameters:\r\n\r\n  * `fields` (optional): the API currently accepts `positions` to return positions. E.g.: `fields=positions`.\r\n\r\n### get_account_by_id(account_hash: str, params: dict|None=None) -> dict\r\n\r\n* GET `/accounts/{accountNumber}`\r\n* `account_hash`: encrypted account identifier (`hashValue`).\r\n* Query parameters:\r\n\r\n  * `fields` (optional): `positions` to include positions. E.g.: `fields=positions`.\r\n\r\n### find_account(last_4_digits: str) -> dict|None\r\n\r\n* Helper that uses `get_account_numbers()` and filters by the last 4 digits, then calls `get_account_by_id`.\r\n\r\n### get_transactions(account_hash: str, from_date: str|None, to_date: str|None, filters: dict|None=None) -> dict\r\n\r\n* GET `/accounts/{accountHash}/transactions`\r\n* **ONE DATE REQUIRED**: you may pass only `from_date` or only `to_date`. If you pass a single date, the SDK fills in the other for the same day:\r\n\r\n  * Short format `YYYY-MM-DD`: start \u2192 `YYYY-MM-DDT00:00:00.000Z`, end \u2192 `YYYY-MM-DDT23:59:59.000Z`\r\n  * Full ISO UTC `YYYY-MM-DDTHH:MM:SS.ffffffZ`: used as-is; if the other date is missing, it is derived with `00:00:00.000Z` or `23:59:59.000Z` of the same day.\r\n* Params:\r\n\r\n  * `startDate`: ISO UTC - `YYYY-MM-DDTHH:MM:SS.ffffffZ` (or short `YYYY-MM-DD`)\r\n  * `endDate`: ISO UTC - `YYYY-MM-DDTHH:MM:SS.ffffffZ` (or short `YYYY-MM-DD`)\r\n  * `filters`: optional dict:\r\n\r\n    * `types`: string with valid types: `TRADE`, `RECEIVE_AND_DELIVER`, `DIVIDEND_OR_INTEREST`, `ACH_RECEIPT`, `ACH_DISBURSEMENT`, `CASH_RECEIPT`, `CASH_DISBURSEMENT`, `ELECTRONIC_FUND`, `WIRE_OUT`, `WIRE_IN`, `JOURNAL`\r\n    * `symbol`: specific symbol\r\n    * `status`: transaction status\r\n\r\n  Note: In some API configurations, `types` may be considered mandatory. The SDK does not require it and treats it as an optional filter.\r\n\r\n**Correct example**:\r\n\r\n```python\r\nfrom datetime import datetime, timezone, timedelta\r\n\r\n# Get hashValue\r\nhash_value = client.account.get_account_numbers()[0]['hashValue']\r\n\r\n# Create UTC dates\r\nstart = datetime.now(timezone.utc) - timedelta(days=7)\r\nstart = start.replace(hour=0, minute=0, second=0, microsecond=0)\r\nend = datetime.now(timezone.utc).replace(hour=23, minute=59, second=59, microsecond=999999)\r\n\r\n# Proper call\r\ntransactions = client.account.get_transactions(\r\n    account_hash=hash_value,  # Use hashValue!\r\n    from_date=start.strftime('%Y-%m-%dT%H:%M:%S.%fZ'),\r\n    to_date=end.strftime('%Y-%m-%dT%H:%M:%S.%fZ'),\r\n    filters={\"types\": \"TRADE,DIVIDEND_OR_INTEREST\"}  # Optional\r\n)\r\n```\r\n\r\n### get_transaction(account_hash: str, transaction_id: str) -> dict\r\n\r\n* GET `/accounts/{accountHash}/transactions/{transactionId}`\r\n* Path parameters:\r\n\r\n  * `account_hash` (required)\r\n  * `transaction_id` (required): numeric transaction ID\r\n* Returns details of a specific transaction.\r\n\r\n### get_user_preferences() -> dict\r\n\r\n* GET `/userPreference`\r\n* Returns user preferences and, when applicable, streamer information needed for WebSocket:\r\n\r\n  * `streamerSocketUrl`\r\n  * `schwabClientCustomerId`\r\n  * `schwabClientCorrelId`\r\n  * `SchwabClientChannel`\r\n  * `SchwabClientFunctionId`\r\n* Useful to initialize `client.streaming` (LOGIN and subscriptions).\r\n\r\n---\r\n\r\n## Orders (`orders.py`)\r\n\r\nAll responses include HTTP metadata and the native data:\r\n\r\n```json\r\n{\r\n  \"status_code\": 200,\r\n  \"success\": true,\r\n  \"headers\": {\"...\": \"...\"},\r\n  \"url\": \"https://...\",\r\n  \"elapsed_seconds\": 0.42,\r\n  \"method\": \"GET|POST|PUT|DELETE\",\r\n  \"params\": {\"...\": \"...\"},\r\n  \"data\": {},\r\n  \"order_id\": \"...\"   \r\n}\r\n```\r\n\r\n### get_orders(account_hash, from_entered_time=None, to_entered_time=None, status=None, max_results=None) -> dict\r\n\r\n* GET `/accounts/{accountNumber}/orders`\r\n* **Date format**: Accepts both `YYYY-MM-DD` (auto-converted to ISO) or full ISO-8601 format. If omitted, defaults to \"last 60 days\".\r\n* `status` (case-insensitive): normalized to uppercase. Values accepted by the API:\r\n  `AWAITING_PARENT_ORDER`, `AWAITING_CONDITION`, `AWAITING_STOP_CONDITION`, `AWAITING_MANUAL_REVIEW`, `ACCEPTED`, `AWAITING_UR_OUT`, `PENDING_ACTIVATION`, `QUEUED`, `WORKING`, `REJECTED`, `PENDING_CANCEL`, `CANCELED`, `PENDING_REPLACE`, `REPLACED`, `FILLED`, `EXPIRED`, `NEW`, `AWAITING_RELEASE_TIME`, `PENDING_ACKNOWLEDGEMENT`, `PENDING_RECALL`, `UNKNOWN`.\r\n* `maxResults` (optional): record limit (API default 3000).\r\n* **Date conversion**: `YYYY-MM-DD` \u2192 `YYYY-MM-DDT00:00:00.000Z` (start) or `YYYY-MM-DDT23:59:59.000Z` (end)\r\n\r\n### get_all_orders(from_entered_time=None, to_entered_time=None, status=None, max_results=None) -> dict\r\n\r\n* GET `/orders`\r\n* **Date format**: Accepts both `YYYY-MM-DD` (auto-converted to ISO) or full ISO-8601 format. If omitted, defaults to \"last 60 days\".\r\n* Filters identical to `get_orders` (including `status` normalization).\r\n* `maxResults` (optional): record limit (API default 3000).\r\n* **Date conversion**: `YYYY-MM-DD` \u2192 `YYYY-MM-DDT00:00:00.000Z` (start) or `YYYY-MM-DDT23:59:59.000Z` (end)\r\n\r\n### place_order(account_hash: str, order_data: dict) -> dict\r\n\r\n* POST `/accounts/{accountNumber}/orders`\r\n* Extracts `order_id` from the `Location` header when present.\r\n\r\n### get_order(account_hash: str, order_id: str) -> dict\r\n\r\n* GET `/accounts/{accountNumber}/orders/{orderId}`\r\n\r\n### cancel_order(account_hash: str, order_id: str) -> dict\r\n\r\n* DELETE `/accounts/{accountNumber}/orders/{orderId}`\r\n* Tries to extract `order_id` from `Location` if the server returns it.\r\n\r\n### replace_order(account_hash: str, order_id: str, new_order_data: dict) -> dict\r\n\r\n* PUT `/accounts/{accountNumber}/orders/{orderId}`\r\n* Returns new `order_id` (from `Location`) when applicable.\r\n\r\n### preview_order(account_hash: str, order_data: dict) -> dict\r\n\r\n* POST `/accounts/{accountNumber}/previewOrder`\r\n* Tries to extract `order_id` from `Location` if the server returns it.\r\n\r\n### Payload helpers\r\n\r\n* `build_limit_order(symbol, quantity, price, instruction=\"BUY\")`\r\n* `build_market_order(symbol, quantity, instruction=\"BUY\")`\r\n* `build_bracket_order(symbol, quantity, entry_price, take_profit_price, stop_loss_price)`\r\n\r\nExample (preview):\r\n\r\n```python\r\nacc = client.account.get_account_numbers()[0]['hashValue']\r\norder = client.orders.build_limit_order(\"AAPL\", 1, 100.00)\r\npreview = client.orders.preview_order(acc, order)\r\n```\r\n\r\n---\r\n\r\n## Market Data (`market.py`)\r\n\r\n### get_quotes(symbols: str|List[str], params: dict|None=None) -> dict\r\n\r\n* GET `/quotes?symbols=...`\r\n* Parameters:\r\n\r\n  * `symbols` (required): str or list of symbols separated by commas. E.g.: `AAPL,AMZN,$DJI,/ESH23`.\r\n  * `params` (optional):\r\n\r\n    * `fields`: subset of data. Values: `quote`, `fundamental`, `extended`, `reference`, `regular`. Default: all.\r\n    * `indicative`: boolean (`true|false`) to include indicative quotes (e.g., ETFs). Example: `indicative=false`.\r\n\r\n### get_quote(symbol: str, params: dict|None=None) -> dict\r\n\r\n* GET `/{symbol}/quotes`\r\n* Parameters:\r\n\r\n  * `symbol` (required): single symbol (e.g., `TSLA`).\r\n  * `params` (optional):\r\n\r\n    * `fields`: same as in `get_quotes`.\r\n\r\n### get_option_chain(symbol: str, contract_type: str|None=None, strike_count: int|None=None, include_underlying_quote: bool|None=None, strategy: str|None=None, interval: float|None=None, strike: float|None=None, range_type: str|None=None, from_date: str|None=None, to_date: str|None=None, volatility: float|None=None, underlying_price: float|None=None, interest_rate: float|None=None, days_to_expiration: int|None=None, exp_month: str|None=None, option_type: str|None=None, entitlement: str|None=None, params: dict|None=None) -> dict\r\n\r\n* GET `/chains`\r\n\r\n* Parameters:\r\n\r\n  * `symbol` (required): Underlying asset symbol\r\n  * `contract_type` (optional): Contract Type. Available values: `CALL`, `PUT`, `ALL`\r\n  * `strike_count` (optional): The Number of strikes to return above or below the at-the-money price\r\n  * `include_underlying_quote` (optional): Underlying quotes to be included (boolean)\r\n  * `strategy` (optional): OptionChain strategy. Default is SINGLE. Available values: `SINGLE`, `ANALYTICAL`, `COVERED`, `VERTICAL`, `CALENDAR`, `STRANGLE`, `STRADDLE`, `BUTTERFLY`, `CONDOR`, `DIAGONAL`, `COLLAR`, `ROLL`\r\n  * `interval` (optional): Strike interval for spread strategy chains (see strategy param)\r\n  * `strike` (optional): Strike Price\r\n  * `range_type` (optional): Range(ITM/NTM/OTM etc.)\r\n  * `from_date` (optional): From date (pattern: yyyy-MM-dd)\r\n  * `to_date` (optional): To date (pattern: yyyy-MM-dd)\r\n  * `volatility` (optional): Volatility to use in calculations. Applies only to ANALYTICAL strategy chains\r\n  * `underlying_price` (optional): Underlying price to use in calculations. Applies only to ANALYTICAL strategy chains\r\n  * `interest_rate` (optional): Interest rate to use in calculations. Applies only to ANALYTICAL strategy chains\r\n  * `days_to_expiration` (optional): Days to expiration to use in calculations. Applies only to ANALYTICAL strategy chains\r\n  * `exp_month` (optional): Expiration month. Available values: `JAN`, `FEB`, `MAR`, `APR`, `MAY`, `JUN`, `JUL`, `AUG`, `SEP`, `OCT`, `NOV`, `DEC`, `ALL`\r\n  * `option_type` (optional): Option Type\r\n  * `entitlement` (optional): Applicable only if its retail token, entitlement of client PP-PayingPro, NP-NonPro and PN-NonPayingPro. Available values: `PN`, `NP`, `PP`\r\n  * `params` (optional): Additional query parameters\r\n\r\n### get_expiration_chain(symbol: str, params: dict|None=None) -> dict\r\n\r\n* GET `/expirationchain`\r\n\r\n* Parameters:\r\n\r\n  * `symbol` (required): Underlying asset symbol\r\n  * `params` (optional): Additional query parameters\r\n\r\n* Returns: JSON response with option expiration dates for the symbol\r\n\r\n### get_price_history(symbol, periodType=\"month\", period=1, frequencyType=\"daily\", frequency=1, startDate=None, endDate=None, params=None) -> dict\r\n\r\n* GET `/pricehistory`\r\n* Parameters:\r\n\r\n  * `periodType`: `day|month|year|ytd`\r\n  * `period`: int\r\n  * `frequencyType`: `minute|daily|weekly|monthly`\r\n  * `frequency`: int\r\n  * `startDate`/`endDate` (ms since epoch)\r\n  * Additional optionals (`needExtendedHoursData`, etc., depending on entitlements)\r\n\r\n### get_movers(symbol_id: str, sort: str|None=None, frequency: int|None=None, params: dict|None=None) -> dict\r\n\r\n* GET `/movers/{symbol_id}` (e.g., `$DJI`, `$SPX`, `NASDAQ`)\r\n* Parameters:\r\n\r\n  * `symbol_id` (required): Index Symbol. Available values: `$DJI`, `$COMPX`, `$SPX`, `NYSE`, `NASDAQ`, `OTCBB`, `INDEX_ALL`, `EQUITY_ALL`, `OPTION_ALL`, `OPTION_PUT`, `OPTION_CALL`\r\n  * `sort` (optional): Sort by a particular attribute. Available values: `VOLUME`, `TRADES`, `PERCENT_CHANGE_UP`, `PERCENT_CHANGE_DOWN`\r\n  * `frequency` (optional): To return movers with the specified directions of up or down. Available values: `0,1,5,10,30,60` (min). Default `0`\r\n  * `params` (optional): Additional query parameters\r\n\r\n### get_markets(params: dict|None=None) -> dict\r\n\r\n* GET `/markets`\r\n* Query parameters:\r\n\r\n  * `markets` (required by API): array of `equity`, `option`, `bond`, `future`, `forex` (the SDK accepts `params={\"markets\": ...}`)\r\n  * `date` (optional): `YYYY-MM-DD` (if you send ISO, the SDK trims to date)\r\n\r\n### get_market_hours(market_id: str, params: dict|None=None) -> dict\r\n\r\n* GET `/markets/{market_id}` (`equity`, `option`, `bond`, `forex`)\r\n* Query parameters:\r\n\r\n  * `date` (optional): `YYYY-MM-DD` (if you send ISO, the SDK trims to date)\r\n\r\n### get_instruments(symbols: str|List[str], projection: str, extra_params: dict|None=None) -> dict\r\n\r\n* GET `/instruments`\r\n* Parameters:\r\n\r\n  * `symbols` (required): single symbol or comma-separated list\r\n  * `projection` (required by API): `symbol-search`, `symbol-regex`, `desc-search`, `desc-regex`, `search`, `fundamental`\r\n    Note: the SDK does not require it in the signature, but it is recommended to provide it.\r\n\r\n### get_instrument_by_cusip(cusip_id: str, params: dict|None=None) -> dict\r\n\r\n* GET `/instruments/{cusip_id}`\r\n\r\n---\r\n\r\n## WebSocket Streaming (`streaming.py`)\r\n\r\n### Callbacks\r\n\r\n* `on_data(fn)`  (data frames)\r\n* `on_response(fn)`  (command confirmations/errors)\r\n* `on_notify(fn)`  (heartbeats/notices)\r\n\r\n### Basic flow\r\n\r\n```python\r\nws = client.streaming\r\nws.on_data(lambda f: print(\"DATA\", f))\r\nws.on_response(lambda f: print(\"RESP\", f))\r\nws.on_notify(lambda f: print(\"NOTIFY\", f))\r\nws.connect(); ws.login()  # Authorization = access token without \"Bearer\"\r\nws.equities_subscribe([\"AAPL\",\"MSFT\"])             # LEVELONE_EQUITIES\r\nws.options_subscribe([\"AAPL  250926C00257500\"])      # LEVELONE_OPTIONS\r\nws.nasdaq_book([\"MSFT\"])                             # NASDAQ_BOOK\r\nws.chart_equity([\"AAPL\"])                            # CHART_EQUITY\r\nws.screener_equity([\"NYSE_VOLUME_5\"])                # SCREENER_EQUITY\r\n```\r\n\r\n### Key Formats (quick table)\r\n\r\n| Type           | Format                               | Example                 | Notes                                      |\r\n| -------------- | ------------------------------------ | ----------------------- | ------------------------------------------ |\r\n| Equities       | Ticker                               | `AAPL`, `MSFT`          | Uppercase                                  |\r\n| Options        | `RRRRRRYYMMDDsWWWWWddd`              | `AAPL  251219C00200000` | 6-char symbol, YYMMDD, C/P, 5+3 strike    |\r\n| Futures        | `/<root><month><yy>`                 | `/ESZ25`                | Root/month/year in uppercase               |\r\n| FuturesOptions | `./<root><month><year><C/P><strike>` | `./OZCZ23C565`          | Depends on the feed                        |\r\n| Forex          | `PAIR`                               | `EUR/USD`, `USD/JPY`    | `/` separator                              |\r\n| Screener       | `PREFIX_SORTFIELD_FREQUENCY`         | `NYSE_VOLUME_5`         | Prefix/criterion/frequency                 |\r\n\r\n### Service-by-Service Examples\r\n\r\n#### Level One Options\r\n\r\n```python\r\nws.options_subscribe([\"AAPL  250926C00257500\"])  # standard option string\r\n# Default fields: 0,2,3,4,8,16,17,18,20,28,29,30,31,37,44\r\n```\r\n\r\n#### Level One Futures\r\n\r\n```python\r\nws.futures_subscribe([\"/ESZ25\"])  # E-mini S&P 500 Dec 2025\r\n# Default fields: 0,1,2,3,4,5,8,12,13,18,19,20,24,33\r\n```\r\n\r\n#### Level One Forex\r\n\r\n```python\r\nws.forex_subscribe([\"EUR/USD\",\"USD/JPY\"])  \r\n# Default fields: 0,1,2,3,4,5,6,7,8,9,10,11,15,16,17,20,21,27,28,29\r\n```\r\n\r\n#### Book (Level II)\r\n\r\n```python\r\nws.nasdaq_book([\"MSFT\"])  # Also: ws.nyse_book, ws.options_book\r\n# Default fields: 0 (Symbol), 1 (BookTime), 2 (Bids), 3 (Asks)\r\n```\r\n\r\n#### Chart (Series)\r\n\r\n```python\r\nws.chart_equity([\"AAPL\"])      # 0..7: key, open, high, low, close, volume, sequence, chartTime\r\nws.chart_futures([\"/ESZ25\"])   # 0..5\r\n```\r\n\r\n#### Screener\r\n\r\n```python\r\nws.screener_equity([\"NYSE_VOLUME_5\"])   \r\nws.screener_options([\"CBOE_VOLUME_5\"])\r\n```\r\n\r\n#### Account Activity\r\n\r\n```python\r\nws.account_activity()  # Gets accountHash and subscribes\r\n```\r\n\r\n### Utilities\r\n\r\n* `subscribe(service, keys, fields=None)`\r\n* `add(service, keys)`\r\n* `unsubscribe(service, keys)` / `unsubscribe_service(service, keys)`\r\n* `view(service, fields)`\r\n\r\n### Option Symbol Helper\r\n\r\n* `create_option_symbol(symbol, expiration, option_type, strike_price)` - Creates Schwab option symbol from components\r\n\r\n**Example:**\r\n```python\r\n# Create option symbol from components\r\noption_symbol = ws.create_option_symbol(\"AAPL\", \"2025-12-19\", \"C\", 200.0)\r\n# Returns: \"AAPL  251219C00200000\"\r\n\r\n# Use in subscription\r\nws.options_subscribe([option_symbol])\r\n```\r\n\r\n**Parameters:**\r\n* `symbol`: Underlying symbol (e.g., \"AAPL\")\r\n* `expiration`: Expiration date in \"YYYY-MM-DD\" format (e.g., \"2025-10-03\")\r\n* `option_type`: \"C\" for Call or \"P\" for Put\r\n* `strike_price`: Strike price (e.g., 257.5)\r\n\r\n### Recommended Fields\r\n\r\n* LEVELONE_EQUITIES: `0,1,2,3,4,5,8,10,18,42,33,34,35`\r\n* LEVELONE_OPTIONS: `0,2,3,4,8,16,17,18,20,28,29,30,31,37,44`\r\n* LEVELONE_FUTURES: `0,1,2,3,4,5,8,12,13,18,19,20,24,33`\r\n* CHART_EQUITY: `0,1,2,3,4,5,6,7`\r\n\r\n#### Quick fields table (copy/paste)\r\n\r\n| Service                  | Fields CSV                                        |\r\n| ------------------------ | ------------------------------------------------- |\r\n| LEVELONE_EQUITIES        | 0,1,2,3,4,5,8,10,18,42,33,34,35                   |\r\n| LEVELONE_OPTIONS         | 0,2,3,4,8,16,17,18,20,28,29,30,31,37,44           |\r\n| LEVELONE_FUTURES         | 0,1,2,3,4,5,8,12,13,18,19,20,24,33                |\r\n| LEVELONE_FUTURES_OPTIONS | 0,1,2,3,4,5,8,12,13,18,19,20,24,33                |\r\n| LEVELONE_FOREX           | 0,1,2,3,4,5,6,7,8,9,10,11,15,16,17,20,21,27,28,29 |\r\n| NASDAQ_BOOK              | 0,1,2,3                                           |\r\n| NYSE_BOOK                | 0,1,2,3                                           |\r\n| OPTIONS_BOOK             | 0,1,2,3                                           |\r\n| CHART_EQUITY             | 0,1,2,3,4,5,6,7                                   |\r\n| CHART_FUTURES            | 0,1,2,3,4,5                                       |\r\n| SCREENER_EQUITY          | 0,1,2,3,4                                         |\r\n| SCREENER_OPTION          | 0,1,2,3,4                                         |\r\n| ACCT_ACTIVITY            | 0,1,2                                             |\r\n\r\n### SUBS / ADD / VIEW / UNSUBS examples by service\r\n\r\n```python\r\nws = client.streaming\r\nws.on_data(lambda f: print(\"DATA\", f))\r\nws.on_response(lambda f: print(\"RESP\", f))\r\nws.connect(); ws.login()\r\n\r\n# LEVELONE_EQUITIES\r\nws.equities_subscribe([\"AAPL\",\"TSLA\"], fields=[0,1,2,3,4,5,8,10,18,42,33,34,35])\r\nws.equities_add([\"MSFT\"])                          # adds without replacing\r\nws.equities_view([0,1,2,3,5,8,18])                  # changes fields\r\nws.equities_unsubscribe([\"TSLA\"])                  # removes symbols\r\n\r\n# LEVELONE_OPTIONS\r\nws.options_subscribe([\"AAPL  250926C00257500\"], fields=[0,2,3,4,8,16,17,18,20,28,29,30,31,37,44])\r\nws.options_add([\"AAPL  250926P00257500\"])          \r\nws.options_view([0,2,3,4,8,16,17,20,28,29,30,31,37,44])\r\nws.options_unsubscribe([\"AAPL  250926C00257500\"])  \r\n\r\n# LEVELONE_FUTURES\r\nws.futures_subscribe([\"/ESZ25\"], fields=[0,1,2,3,4,5,8,12,13,18,19,20,24,33])\r\nws.futures_add([\"/NQZ25\"])\r\nws.futures_view([0,1,2,3,4,5,8,12,13,18,19,20,24,33])\r\nws.futures_unsubscribe([\"/ESZ25\"])  \r\n\r\n# BOOK (Level II)\r\nws.nasdaq_book([\"MSFT\"], fields=[0,1,2,3])\r\nws.add(\"NASDAQ_BOOK\", [\"AAPL\"])                    # generic ADD\r\nws.view(\"NASDAQ_BOOK\", [0,1,2,3])                   # generic VIEW\r\nws.unsubscribe_service(\"NASDAQ_BOOK\", [\"MSFT\"])    # generic UNSUBS\r\n\r\n# CHART (Series)\r\nws.chart_equity([\"AAPL\"], fields=[0,1,2,3,4,5,6,7])\r\nws.add(\"CHART_EQUITY\", [\"MSFT\"])                  # generic ADD\r\nws.view(\"CHART_EQUITY\", [0,1,2,3,4,5,6,7])          # generic VIEW\r\nws.unsubscribe(\"CHART_EQUITY\", [\"AAPL\"])           # generic UNSUBS\r\n\r\n# SCREENER\r\nws.screener_equity([\"EQUITY_ALL_VOLUME_5\"], fields=[0,1,2,3,4])\r\nws.add(\"SCREENER_EQUITY\", [\"NYSE_TRADES_1\"])      \r\nws.view(\"SCREENER_EQUITY\", [0,1,2,3,4])\r\nws.unsubscribe(\"SCREENER_EQUITY\", [\"EQUITY_ALL_VOLUME_5\"])\r\n\r\n# ACCT_ACTIVITY\r\nws.account_activity(fields=[0,1,2])                  # subscribe account activity\r\n# For UNSUBS you need the same key used in SUBS (account_hash)\r\naccount_hash = getattr(client, \"_account_hash\", None)\r\nif account_hash:\r\n    ws.unsubscribe_service(\"ACCT_ACTIVITY\", [account_hash])\r\n```\r\n\r\n### Quick Field Guide (IDs \u2192 meaning)\r\n\r\n> Note: exact mappings may vary depending on entitlements/version. Below are practical equivalences observed in frames.\r\n\r\n#### LEVELONE_EQUITIES\r\n\r\n| ID | Field                      |\r\n| -- | -------------------------- |\r\n| 0  | symbol/key                 |\r\n| 1  | bidPrice                   |\r\n| 2  | askPrice                   |\r\n| 3  | lastPrice                  |\r\n| 4  | bidSize                    |\r\n| 5  | askSize                    |\r\n| 8  | totalVolume                |\r\n| 10 | referencePrice (open/mark) |\r\n| 18 | netChange                  |\r\n| 42 | percentChange              |\r\n\r\n#### LEVELONE_OPTIONS\r\n\r\n| ID | Field                           |\r\n| -- | ------------------------------- |\r\n| 0  | symbol/key                      |\r\n| 2  | bidPrice                        |\r\n| 3  | askPrice                        |\r\n| 4  | lastPrice                       |\r\n| 8  | totalVolume                     |\r\n| 16 | openInterest                    |\r\n| 17 | daysToExpiration                |\r\n| 20 | strikePrice                     |\r\n| 28 | delta                           |\r\n| 29 | gamma                           |\r\n| 30 | theta                           |\r\n| 31 | vega                            |\r\n| 44 | impliedVolatility (if provided) |\r\n\r\n#### LEVELONE_FUTURES\r\n\r\n| ID | Field                                      |\r\n| -- | ------------------------------------------ |\r\n| 0  | symbol/key                                 |\r\n| 1  | bidPrice                                   |\r\n| 2  | askPrice                                   |\r\n| 3  | lastPrice                                  |\r\n| 4  | bidSize                                    |\r\n| 5  | askSize                                    |\r\n| 8  | totalVolume                                |\r\n| 12 | openInterest                               |\r\n| 13 | contractDepth/series info (per feed)       |\r\n| 18 | netChange                                  |\r\n| 19 | sessionChange (or days/indicator per feed) |\r\n| 20 | percentChange/ratio (per feed)             |\r\n| 24 | lastSettlement/mark                        |\r\n| 33 | priorSettle                                |\r\n\r\n### Frame Structure\r\n\r\n* Confirmations (`response`): `{ \"response\": [ { \"service\":\"ADMIN\",\"command\":\"LOGIN\",\"content\":{\"code\":0,\"msg\":\"...\"}} ] }`\r\n* Data (`data`): `{ \"service\":\"LEVELONE_EQUITIES\",\"timestamp\":...,\"command\":\"SUBS\",\"content\":[{\"key\":\"AAPL\",...}] }`\r\n* Notifications (`notify`): `{ \"notify\": [ { \"heartbeat\": \"...\" } ] }`\r\n\r\n### Streamer API Cheat Sheet (parameters and commands)\r\n\r\n1. Connection and prerequisites\r\n\r\n* **Auth**: use the Access Token from the OAuth flow.\r\n* **Session IDs** (from `GET /userPreference`): `schwabClientCustomerId`, `schwabClientCorrelId`, `SchwabClientChannel`, `SchwabClientFunctionId`.\r\n* **Transport**: JSON WebSocket. One stream per user (if you open more: code 12 CLOSE_CONNECTION).\r\n\r\n2. Envelope of each command\r\n\r\n* **Common fields**:\r\n\r\n  * `service` (req.): `ADMIN`, `LEVELONE_EQUITIES`, `LEVELONE_OPTIONS`, `LEVELONE_FUTURES`, `LEVELONE_FUTURES_OPTIONS`, `LEVELONE_FOREX`, `NYSE_BOOK`, `NASDAQ_BOOK`, `OPTIONS_BOOK`, `CHART_EQUITY`, `CHART_FUTURES`, `SCREENER_EQUITY`, `SCREENER_OPTION`, `ACCT_ACTIVITY`.\r\n  * `command` (req.): `LOGIN`, `SUBS`, `ADD`, `UNSUBS`, `VIEW`, `LOGOUT`.\r\n  * `requestid` (req.): unique request identifier.\r\n  * `SchwabClientCustomerId` and `SchwabClientCorrelId` (recommended): from `userPreference`.\r\n  * `parameters` (optional): depends on service/command.\r\n* **Notes**: `SUBS` overwrites list; `ADD` appends; `UNSUBS` removes; `VIEW` changes `fields`.\r\n\r\n3. ADMIN (session)\r\n\r\n* `LOGIN` (`service=ADMIN`, `command=LOGIN`)\r\n\r\n  * parameters: `Authorization` (token without \"Bearer\"), `SchwabClientChannel`, `SchwabClientFunctionId`.\r\n* `LOGOUT` (`service=ADMIN`, `command=LOGOUT`)\r\n\r\n  * parameters: empty.\r\n\r\n4. LEVEL ONE (L1 quotes)\r\n\r\n* Common parameters: `keys` (req., CSV list), `fields` (optional, indexes).\r\n* `LEVELONE_EQUITIES`: `keys` uppercase tickers (e.g., `AAPL,TSLA`).\r\n* `LEVELONE_OPTIONS`: `keys` Schwab option format `RRRRRR  YYMMDD[C/P]STRIKE`.\r\n* `LEVELONE_FUTURES`: `keys` `/<root><monthCode><yearCode>` (month codes: F,G,H,J,K,M,N,Q,U,V,X,Z; year two digits), e.g., `/ESZ25`.\r\n* `LEVELONE_FUTURES_OPTIONS`: `keys` `./<root><month><yy><C|P><strike>`, e.g., `./OZCZ23C565`.\r\n* `LEVELONE_FOREX`: `keys` `BASE/QUOTE` pairs CSV (e.g., `EUR/USD,USD/JPY`).\r\n\r\n5. BOOK (Level II)\r\n\r\n* Services: `NYSE_BOOK`, `NASDAQ_BOOK`, `OPTIONS_BOOK`.\r\n* Parameters: `keys` (req., tickers), `fields` (optional, level indexes).\r\n\r\n6. CHART (streaming series)\r\n\r\n* `CHART_EQUITY`: `keys` equities; `fields` indexes (OHLCV, time, seq).\r\n* `CHART_FUTURES`: `keys` futures (same format as L1 futures); `fields` indexes.\r\n\r\n7. SCREENER (gainers/losers/actives)\r\n\r\n* Services: `SCREENER_EQUITY`, `SCREENER_OPTION`.\r\n* `keys` pattern `PREFIX_SORTFIELD_FREQUENCY`, e.g., `EQUITY_ALL_VOLUME_5`.\r\n\r\n  * `PREFIX` examples: `$COMPX`, `$DJI`, `$SPX`, `INDEX_ALL`, `NYSE`, `NASDAQ`, `OTCBB`, `EQUITY_ALL`, `OPTION_PUT`, `OPTION_CALL`, `OPTION_ALL`.\r\n  * `SORTFIELD`: `VOLUME`, `TRADES`, `PERCENT_CHANGE_UP`, `PERCENT_CHANGE_DOWN`, `AVERAGE_PERCENT_VOLUME`.\r\n  * `FREQUENCY`: `0,1,5,10,30,60` (min; `0` = full day).\r\n* `fields` (optional): screener field indexes.\r\n\r\n8. ACCOUNT (account activity)\r\n\r\n* Service: `ACCT_ACTIVITY` (`SUBS`/`UNSUBS`).\r\n* `keys` (req.): arbitrary identifier for your sub; if you send multiple, the first is used.\r\n* `fields` (recommended): `0` (or `0,1,2,3` per example/need).\r\n\r\n9. Server responses\r\n\r\n* Types: `response` (to your requests), `notify` (heartbeats), `data` (market flow).\r\n* Key codes: `0` SUCCESS, `3` LOGIN_DENIED, `11` SERVICE_NOT_AVAILABLE, `12` CLOSE_CONNECTION, `19` REACHED_SYMBOL_LIMIT, `20` STREAM_CONN_NOT_FOUND, `21` BAD_COMMAND_FORMAT, `26/27/28/29` successes for `SUBS/UNSUBS/ADD/VIEW`.\r\n\r\n10. Delivery Types\r\n\r\n* `All Sequence`: everything with sequence number.\r\n* `Change`: only changed fields (conflated).\r\n* `Whole`: full messages with throttling.\r\n\r\n11. Best practices\r\n\r\n* Do `LOGIN` and wait for `code=0` before `SUBS/ADD`.\r\n* To add symbols without losing existing ones, use `ADD` (not `SUBS`).\r\n* Change `fields` with `VIEW` for performance.\r\n* Handle `notify` (heartbeats) and reconnect if they are lost.\r\n* Reuse your `SchwabClientCorrelId` during the session.\r\n* If you see `19` (symbol limit), shard loads by service/session.\r\n\r\n---\r\n\r\n## Advanced Troubleshooting\r\n\r\n* `notify.code=12` (Only one connection): close other active WebSocket sessions.\r\n* `response.content.code=3` (Login denied): invalid/expired token \u2192 `client.login()`.\r\n* `response.content.code=21` (Bad command formatting): check `Authorization` format (without `Bearer`) and keys (spacing for options, uppercase).\r\n* Persistent REST 401: delete `schwab_tokens.json` and re-run `client.login()`.\r\n* High latency/lost frames: avoid parallel reconnects; use the SDK's auto-resubscription.\r\n\r\n---\r\n\r\n## Contributions\r\n\r\nYour contributions are welcome! Ideas, issues, and PRs help improve the SDK:\r\n\r\n* Open an issue with clear details (environment, steps, expected/actual error).\r\n* Propose endpoint coverage improvements and examples.\r\n* Follow a clear style and add tests or minimal examples when possible.\r\n\r\nIf you want to hold working sessions or discuss the roadmap, open an issue labeled `discussion`.\r\n\r\n## Disclaimer\r\n\r\nThis project is unofficial and is not affiliated with, sponsored by, or endorsed by Charles Schwab & Co., Inc. \u201cSchwab\u201d and other trademarks are the property of their respective owners. Use of this SDK is subject to the terms and conditions of Schwab APIs and applicable regulations. Use at your own discretion and responsibility.\r\n\r\n---\r\n\r\n## License\r\n\r\nMIT ([LICENSE](LICENSE))\r\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Cliente ligero para API de Schwab: OAuth, REST y Streaming WebSocket",
    "version": "0.2.7",
    "project_urls": {
        "Homepage": "https://pypi.org/project/schwab-sdk/",
        "Repository": "https://github.com/your-org/schwab-sdk"
    },
    "split_keywords": [
        "schwab",
        " trading",
        " market-data",
        " websocket",
        " sdk",
        " api"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "266280cf74817c6db3c085f3fc20e1ada296aebab15e45c8dce8070a2742a4c1",
                "md5": "9810d4c575799a15e5ab318a6ad65511",
                "sha256": "de60801683e80b0f57b77b752c9902993dd4509f800331d2954a431adc415acf"
            },
            "downloads": -1,
            "filename": "schwab_sdk_unofficial-0.2.7-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9810d4c575799a15e5ab318a6ad65511",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 65828,
            "upload_time": "2025-10-07T07:26:01",
            "upload_time_iso_8601": "2025-10-07T07:26:01.655333Z",
            "url": "https://files.pythonhosted.org/packages/26/62/80cf74817c6db3c085f3fc20e1ada296aebab15e45c8dce8070a2742a4c1/schwab_sdk_unofficial-0.2.7-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "15611c3a888d28d6e38d8b5ab8122338188b646670e992f6fe2d9f7d445113ca",
                "md5": "5133e28bbddad018885dac84e2dbf717",
                "sha256": "944fbeefa202e4d2b2597aa41e5b3c4d7a6e5abc185ddc80f2719f3fb795c12d"
            },
            "downloads": -1,
            "filename": "schwab_sdk_unofficial-0.2.7.tar.gz",
            "has_sig": false,
            "md5_digest": "5133e28bbddad018885dac84e2dbf717",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 74135,
            "upload_time": "2025-10-07T07:26:02",
            "upload_time_iso_8601": "2025-10-07T07:26:02.566963Z",
            "url": "https://files.pythonhosted.org/packages/15/61/1c3a888d28d6e38d8b5ab8122338188b646670e992f6fe2d9f7d445113ca/schwab_sdk_unofficial-0.2.7.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-07 07:26:02",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "your-org",
    "github_project": "schwab-sdk",
    "github_not_found": true,
    "lcname": "schwab-sdk-unofficial"
}
        
Elapsed time: 1.26543s