# Semi-Automatic T-Trading Setup ## Architecture ``` ┌─────────────────────────────────────────────┐ │ Cron (every 10 min, market hours only) │ │ ┌─────────────────────────────────────┐ │ │ │ rgti_auto_monitor.py │ │ │ │ 1. Get quote (Python SDK) │ │ │ │ 2. Check position availability │ │ │ │ 3. Check pending orders │ │ │ │ 4. If price in zone + no orders: │ │ │ │ → Auto place limit order │ │ │ │ 5. If price moved away: │ │ │ │ → Auto cancel stale order │ │ │ │ 6. Print message → WeChat delivery │ │ │ └─────────────────────────────────────┘ │ └─────────────────────────────────────────────┘ ``` ## Required SDK Calls ```python import os from longport import openapi # Load env bashrc = open(os.path.expanduser("~/.bashrc")).read() for line in bashrc.splitlines(): if line.startswith("export LONGPORT_") or line.startswith("export LONGBRIDGE_"): parts = line.replace("export ", "").split("=", 1) if len(parts) == 2: os.environ[parts[0]] = parts[1].strip('"').strip("'") os.environ["LONGBRIDGE_TRADE_ENABLED"] = "true" cfg = openapi.Config.from_env() trade_ctx = openapi.TradeContext(config=cfg) quote_ctx = openapi.QuoteContext(config=cfg) # Quote resp = quote_ctx.quote(["SYMBOL.US"]) price = float(resp[0].last_done) # Position (check available_quantity for sellable qty) pos = trade_ctx.stock_positions() for ch in pos.channels: for p in ch.positions: avail = int(p.available_quantity) total = int(p.quantity) # Pending orders orders = trade_ctx.today_orders() for o in orders: status = str(o.status) # "NotReported", "PendingStatus", etc. # Place order (GTC + outside RTH = works pre/regular/post market) resp = trade_ctx.submit_order( symbol="RGTI.US", order_type=openapi.OrderType.LO, side=openapi.OrderSide.Sell, submitted_quantity=15, time_in_force=openapi.TimeInForceType.GoodTilCanceled, submitted_price=21.00, outside_rth=openapi.OutsideRTH.AnyTime, ) # Cancel trade_ctx.cancel_order(order_id) ``` ## State File Pattern Track active orders and cooldowns to prevent spam: ```python STATE_FILE = "~/.hermes/scripts/rgti_t_state.json" def load_state(): try: return json.load(open(STATE_FILE)) except: return {"active_orders": [], "last_action_time": None, "trades_today": 0} # Cooldown: 5 min between actions last_t = state.get("last_action_time") if last_t: diff = (now - datetime.fromisoformat(last_t)).total_seconds() if diff < 300: sys.exit(0) # silent exit ``` ## Cron Job Setup ```python # Via Hermes cronjob tool: cronjob(action="create", name="RGTI半自动做T挂单", no_agent=True, # Script-only, no LLM schedule="*/10 9-15 * * 1-5", # Every 10 min, 9-15 ET, Mon-Fri deliver="weixin", script="rgti_auto_monitor.py") # Relative to ~/.hermes/scripts/ ``` ## Key Design Decisions 1. **No agent (no_agent=True)**: Script runs directly, prints output → delivered as message. No LLM tokens wasted. 2. **Empty stdout = silent**: If nothing to report, print nothing → no message sent. 3. **GTC + AnyTime**: Orders persist across days and work in pre/post market. 4. **5-min cooldown**: Prevents rapid-fire order spam on volatile stocks. 5. **Auto-cancel stale orders**: If price moves >$1.50 from order price, cancel and re-evaluate. 6. **State file for order tracking**: Prevents duplicate orders and tracks today's trade count.