---
description: Use this skill for Futu OpenD / moomoo stock trading operations -- placing orders, checking positions and holdings, querying account info for HK and US equities. Covers FutuOpenD setup, the moomoo Python API, and HK market hours.
---

# Futu OpenD / moomoo Stock Trading API

## Setup

- **FutuOpenD**: Download from Futu official site, install to `~/FutuOpenD/`
- **Start command**: `open ~/FutuOpenD/Futu_OpenD_VERSION/FutuOpenD.app`
- **API port**: `127.0.0.1:11111`
- **Python library**: `moomoo` (NOT `futu`) — `pip3 install moomoo-api`

## Credentials

Check memory for:
- Account phone number
- Login password
- Trading unlock password (6-digit PIN)

## Key Discovery: Use OpenSecTradeContext

```python
# WRONG — returns empty/wrong account for comprehensive accounts
from moomoo import *
trade_ctx = OpenHKTradeContext(host='127.0.0.1', port=11111)

# CORRECT — use this for the comprehensive margin account
trade_ctx = OpenSecTradeContext(filter_trdmarket=TrdMarket.HK, host='127.0.0.1', port=11111)
```

Multiple accounts may exist under one login. Identify the correct one by:
- Querying all accounts: `trade_ctx.get_acc_list()`
- The "保证金综合账户" (comprehensive margin account) is typically the main real account

## Restarting FutuOpenD

```bash
open ~/FutuOpenD/Futu_OpenD_VERSION/FutuOpenD.app
# Session is cached after first login — no SMS code needed on restart
```

## Basic Operations

```python
from moomoo import *

# Connect
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
trade_ctx = OpenSecTradeContext(filter_trdmarket=TrdMarket.HK, host='127.0.0.1', port=11111)

# Unlock trading (required before placing orders) — check memory for PIN
trade_ctx.unlock_trade(password='TRADING_PIN')

# Get account info
ret, data = trade_ctx.accinfo_query(trd_env=TrdEnv.REAL)

# Get holdings
ret, data = trade_ctx.position_list_query(trd_env=TrdEnv.REAL)

# Get orders
ret, data = trade_ctx.order_list_query(trd_env=TrdEnv.REAL)
```

## Placing an Order

```python
# Limit SELL order — check memory for account ID
ret, data = trade_ctx.place_order(
    price=530.0,
    qty=100,
    code='HK.00700',
    trd_side=TrdSide.SELL,
    order_type=OrderType.NORMAL,  # limit order
    trd_env=TrdEnv.REAL,
    acc_id=REAL_ACCOUNT_ID  # from get_acc_list()
)
order_id = data['order_id'][0]
```

## Order Status Values

| Status | Meaning |
|--------|---------|
| `WAITING_SUBMIT` | Pre-market, waiting for market open |
| `SUBMITTING` | Being submitted to exchange |
| `SUBMITTED` | Submitted, waiting for match |
| `FILLED_ALL` | Fully executed |
| `CANCELLED_ALL` | Cancelled |

## HK Market Hours

- Opens: 9:30 AM HKT (UTC+8)
- Lunch break: 12:00-13:00 HKT
- Closes: 16:00 HKT
- Pre-market orders: Submitted during market hours, status = WAITING_SUBMIT outside hours

## Checking Order Execution

```python
ret, data = trade_ctx.order_list_query(trd_env=TrdEnv.REAL)
# Filter by order_id to find specific order
order = data[data['order_id'] == 'ORDER_ID']
print(order[['order_status', 'dealt_qty', 'dealt_avg_price']])
```

## Stock Price Monitoring

A monitoring script can be run via pm2:
- Alerts at configurable price thresholds (buy signal, stop loss, take profit)
- Uses Polygon.io or similar API for real-time data
- Notify owner via Telegram when thresholds are hit

## Earnings Calendar Monitoring

Set Google Calendar reminders for 1 day before earnings:
- Look up earnings dates for relevant stocks
- When calendar reminder fires → notify owner via Telegram
