Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,14 +576,19 @@ def submit_market_order_intent(ib, order_intent, **kwargs):


def probe_market_order_write_access(ib):
managed_symbols = resolve_reporting_managed_symbols()
if len(ACCOUNT_IDS) != 1 or not managed_symbols:
"""Probe order-write access with a low-notional safe-haven what-if buy.

Do not use the first managed growth symbol (e.g. SOXL): one share can exceed
small-account initial margin and emit Error 201 even though write access is OK.
"""
if len(ACCOUNT_IDS) != 1:
raise RuntimeError(
"IBKR what-if permission probe requires one account_id and one managed symbol"
"IBKR what-if permission probe requires exactly one account_id"
)
probe_symbol = str(SAFE_HAVEN or "BIL").strip().upper() or "BIL"
return probe_order_write_access(
ib,
symbol=managed_symbols[0],
symbol=probe_symbol,
account_id=ACCOUNT_IDS[0],
stock_exchange=MARKET_EXCHANGE,
stock_currency=MARKET_CURRENCY,
Expand Down
24 changes: 24 additions & 0 deletions tests/test_request_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,30 @@ def fake_build_broker_adapters(*, dry_run_only_override=None):
assert observed["dry_run_only_override"] is True


def test_probe_market_order_write_access_uses_safe_haven_not_growth_symbol(
strategy_module, monkeypatch
):
observed = {}

def fake_probe(_ib, *, symbol, account_id, **_kwargs):
observed["symbol"] = symbol
observed["account_id"] = account_id
return types.SimpleNamespace(warningText="")

monkeypatch.setattr(strategy_module, "probe_order_write_access", fake_probe)
monkeypatch.setattr(strategy_module, "SAFE_HAVEN", "BIL")
monkeypatch.setattr(strategy_module, "ACCOUNT_IDS", ["U1234567"])
monkeypatch.setattr(
strategy_module,
"resolve_reporting_managed_symbols",
lambda: ("SOXL", "SOXX", "BIL"),
)

strategy_module.probe_market_order_write_access(object())

assert observed == {"symbol": "BIL", "account_id": "U1234567"}


def test_handle_dry_run_ignores_paper_liquidate_only(strategy_module, monkeypatch):
observed = {"called": False}

Expand Down