Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.

Moomoo and TradingView Integration

Last updated on 13 days ago
K
KevinMember
Posted 13 days ago
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
K
KevinMember
Posted 13 days ago
How It Works

Imports: from moomoo import * loads the SDK, including classes like OpenQuoteContext and OpenSecTradeContext.
Market Snapshot: Connects to OpenD (local server) and calls get_market_snapshot() to retrieve real-time data (price, volume, etc.) for the specified stock code (format: Market.StockCode, e.g., 'HK.00700' for Hong Kong stocks).
Place Order: Connects similarly and uses place_order() to submit a market/limit order. Parameters include price, quantity, stock code, side (BUY/SELL), and environment (SIMULATE for testing).
Output: Prints the quote data (a dictionary with market info) and order response (order ID or error).
Closing Contexts: Essential to avoid connection limits.

Running the Program

Ensure OpenD is running on localhost:11111.
Run the script: python your_file.py.
Expected Output Example (simplified):

Quote: {'code': 'HK.00700', 'lastPrice': 450.0, 'volume': 123456...}
Order: {'retType': 0, 'orderId': 12345...} (0 means success).
You can view all discussion threads in this forum.
You cannot start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You cannot download attachments in this forum.
Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 4
Members Online 0

Total Members: 19
Newest Member: bokovac