Securities and Futures
Futu Securities
Futu NiuNiu live trading and paper trading are supported. FutuOpenD must run on the docker's machine. For configuring the exchange object and running FutuOpenD, see the Futu Securities configuration guide.
When FutuOpenD is used for paper trading, some stock codes are not supported and cannot be traded (paper trading works in the Futu NiuNiu mobile app).
-
Call frequency
GetOrder,GetOrders,GetPositionsandGetAccountuse cached data by default, so their call frequency is not limited;FutuOpenDupdates the cache automatically when new data arrives.
exchange.IO("refresh", true)disables the cache; without the cache the limit is at most 10 queries every 30 seconds, and exceeding it returns an error. -
Stock codes
The format iscode.market, e.g.600519.SH. Market suffixes:- HK: Hong Kong stocks
- US: US stocks
- SH: Shanghai
- SZ: Shenzhen
- SG: Singapore futures
- JP: Japan futures
Set the stock code with
exchange.SetContractType()in the strategy, for example:javascriptfunction main() { var info = exchange.SetContractType("600519.SH") // set the stock 600519.SH (Moutai); the account switches to the mainland market Log(info) Log(exchange.GetAccount()) // the current stock is Moutai, so GetAccount returns the mainland market assets Log(exchange.GetTicker()) // current quote of Moutai }pythondef main(): info = exchange.SetContractType("600519.SH") Log(info) Log(exchange.GetAccount()) Log(exchange.GetTicker())rustfn main() { let info = exchange.SetContractType("600519.SH"); // set the stock 600519.SH (Moutai); the account switches to the mainland market Log!(info); Log!(exchange.GetAccount()); // the current stock is Moutai, so GetAccount returns the mainland market assets Log!(exchange.GetTicker(None)); // current quote of Moutai }exchange.SetDirection(trade direction),exchange.Buy/exchange.Sell(orders),exchange.CancelOrder(cancellation),exchange.GetOrder(order query) and the like are used the same way as in futures markets. -
Account information
Futu usesTrdMarketto tell the Hong Kong, US, mainland and other markets apart. From theFutu APIdocumentation:mylangconst ( TrdMarket_TrdMarket_Unknown TrdMarket = 0 // unknown market TrdMarket_TrdMarket_HK TrdMarket = 1 // Hong Kong market TrdMarket_TrdMarket_US TrdMarket = 2 // US market TrdMarket_TrdMarket_CN TrdMarket = 3 // mainland market TrdMarket_TrdMarket_HKCC TrdMarket = 4 // Hong Kong Stock Connect market TrdMarket_TrdMarket_Futures TrdMarket = 5 // futures market )Data returned by
exchange.GetAccount():json{ "Info": [{ "Header": { ... // omitted "TrdMarket": 1 // market ID in the raw Info data: assets of the Hong Kong market }, "Funds": { // account assets in this market ... } }, ...], "Stocks": 0, "FrozenStocks": 0, "Balance": 1000000, // assets in the current market "FrozenBalance": 0 } -
FutuOpenDdecides the region by the IP address it logs in from; accounts logged in from outside mainland China have some market data restrictions. See the officialFutuOpenD(Futu) documentation.
Interactive Brokers
-
Configure the exchange
Run "IB Gateway" or "TWS (Trader Workstation)" on the docker's machine. With TWS: after logging in, click the configuration button at the top right, open "Configure" → "API" → "Settings", uncheck "Read-Only API", check "Enable ActiveX and Socket Clients", and note the "Socket port" (TWS defaults to 7496 for live and 7497 for paper; IB Gateway to 4001 for live and 4002 for paper).
Then choose Interactive Brokers on the platform's add exchange page:- Server address: the address and port of TWS or IB Gateway, e.g.
localhost:7496. - Market data type: realtime, frozen, delayed or delayed frozen. Accounts without a realtime market data subscription can choose delayed data. It can also be switched at run time with
exchange.IO("marketDataType", n)(nfrom 1 to 4, in the order above).
- Server address: the address and port of TWS or IB Gateway, e.g.
-
Contract codes
Set withexchange.SetContractType()in the formsymbol.currency[.type[.exchange]]; the type defaults to stockSTKand the exchange toSMART:- US stocks:
AAPL.US,TSLA.US(USmeans priced in USD). - Hong Kong stocks:
symbol.HK(HKmeans priced in HKD). - Futures (
FUT):symbol-expiry[-multiplier].currency.FUT.exchange, with the expiry month written asYYYYMMand the exchange as IB's exchange code. - Options (
OPT) and futures options (FOP):symbol-expiry-C or P-strike×100[-multiplier].currency.OPT or FOP.exchange, with the strike multiplied by 100 and written as an integer. - A plain number: used directly as the IB contract ID (conId).
- US stocks:
-
Other notes
- The docker connects to TWS with the live trading ID as its client ID (clientId), so the client ID stays the same across restarts and orders placed earlier can still be cancelled or modified. TWS only lets the client ID that placed an order (or the master client) modify or cancel it.
Symbolin positions and orders is the short form (e.g.Z74.SGD);exchange.GetPositions()andexchange.GetOrders()accept either the short form or the full code used when ordering (e.g.Z74.SGD.STK.SGX).- When the gateway rejects an order, the
Rejectfield in the order'sInfoholds the reason. - After
exchange.IO("debug", true), every frame sent to or received from TWS is logged in the TWS API log format, so it can be matched against the gateway's own log.