Based on Moomoo's official API documentation, here's a real, working Python sample program for interacting with their API. This example demonstrates fetching a market snapshot (quote) for a stock (e.g., Tencent Holdings, HK.00700) and placing a simulated buy order. It's directly from their developer docs and uses their Python SDK.
Prerequisites
Install OpenD (Moomoo's API gateway) from their docs: Download and run it on your local machine (host: 127.0.0.1, port: 11111).
Install the Moomoo Python API:
On Windows: pip install moomoo-api
On Linux/Mac: pip3 install moomoo-api
For upgrades: Add --upgrade to the pip command.
Create a new Python file in your project (e.g., via PyCharm).
Note: For real trading, unlock your account with a trading password in the Moomoo app. This sample uses simulation mode (TrdEnv.SIMULATE) to avoid live trades.
from moomoo import * # Import the Moomoo API library
# Step 1: Create quote context and fetch market snapshot
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111) # Connect to local OpenD
print(quote_ctx.get_market_snapshot('HK.00700')) # Get snapshot for HK.00700 (Tencent)
quote_ctx.close() # Close to free connections
# Step 2: Create trade context and place a simulated order
trd_ctx = OpenSecTradeContext(host='127.0.0.1', port=11111) # Connect to local OpenD
print(trd_ctx.place_order(
price=500.0,
qty=100,
code="HK.00700",
trd_side=TrdSide.BUY,
trd_env=TrdEnv.SIMULATE # Use paper trading
)) # Place buy order for 100 shares at $500
trd_ctx.close() # Close to free connections