Trading
The Go SDK provides a TradeClient that wraps all order management and account endpoints. Every method returns ([]byte, error) — the raw JSON response body.
Quick Start
package main
import (
"fmt"
"log"
"github.com/tigerfintech/openapi-sdks/go/client"
"github.com/tigerfintech/openapi-sdks/go/config"
"github.com/tigerfintech/openapi-sdks/go/model"
"github.com/tigerfintech/openapi-sdks/go/trade"
)
func main() {
cfg, err := config.NewClientConfig(
config.WithPropertiesFile("tiger_openapi_config.properties"),
)
if err != nil {
log.Fatal(err)
}
httpClient := client.NewHttpClient(cfg)
tc := trade.NewTradeClient(httpClient, cfg.Account)
// Build a limit order using the helper constructor
order := model.LimitOrder(cfg.Account, "AAPL", "STK", "BUY", 100, 150.0)
// Preview before submitting
preview, err := tc.PreviewOrder(order)
if err != nil {
log.Fatal(err)
}
fmt.Println("Preview:", string(preview))
// Place the order
result, err := tc.PlaceOrder(order)
if err != nil {
log.Fatal(err)
}
fmt.Println("Order placed:", string(result))
}Order Model
The model.Order struct represents an order. Use the convenience constructors below or build the struct manually.
Order Fields
| Field | Type | Description |
|---|---|---|
Account | string | Account identifier |
Symbol | string | Contract symbol, e.g. "AAPL" |
SecType | string | Security type: STK, OPT, FUT, WAR, IOPT |
Action | string | Order side: BUY or SELL |
OrderType | string | MKT, LMT, STP, STP_LMT, TRAIL |
TotalQuantity | int64 | Number of shares/contracts |
LimitPrice | float64 | Limit price (required for LMT / STP_LMT) |
AuxPrice | float64 | Stop price (required for STP / STP_LMT) |
TimeInForce | string | DAY, GTC, GTD |
OutsideRth | bool | Allow execution outside regular trading hours |
Expiry | string | Option expiry date (YYYYMMDD) |
Strike | float64 | Option strike price |
Right | string | Option type: PUT or CALL |
Order Constructors
// Limit order
order := model.LimitOrder(account, symbol, secType, action string, qty int64, price float64)
// Market order
order := model.MarketOrder(account, symbol, secType, action string, qty int64)Example — building orders manually:
import "github.com/tigerfintech/openapi-sdks/go/model"
// Stock limit buy
stockOrder := model.LimitOrder(cfg.Account, "AAPL", "STK", "BUY", 100, 150.0)
// Stock market sell
mktOrder := model.MarketOrder(cfg.Account, "TSLA", "STK", "SELL", 50)
// Option limit buy (call)
optOrder := model.Order{
Account: cfg.Account,
Symbol: "AAPL",
SecType: "OPT",
Action: "BUY",
OrderType: "LMT",
TotalQuantity: 1,
LimitPrice: 5.50,
TimeInForce: "DAY",
Expiry: "20250117",
Strike: 150.0,
Right: "CALL",
}Contract Query
GetContract
func (tc *TradeClient) GetContract(symbol, secType string) ([]byte, error)Returns detailed contract information for a single symbol.
| Parameter | Type | Required | Description |
|---|---|---|---|
symbol | string | Yes | Contract symbol, e.g. "AAPL" |
secType | string | Yes | Security type: STK, OPT, FUT, WAR, IOPT |
contract, err := tc.Contract("AAPL", "STK")GetContracts
func (tc *TradeClient) GetContracts(symbols []string, secType string) ([]byte, error)Returns contract information for multiple symbols in a single request.
| Parameter | Type | Required | Description |
|---|---|---|---|
symbols | []string | Yes | List of symbols |
secType | string | Yes | Security type for all symbols |
contracts, err := tc.Contracts([]string{"AAPL", "TSLA", "MSFT"}, "STK")GetQuoteContract
func (tc *TradeClient) GetQuoteContract(symbol, secType string) ([]byte, error)Returns derivative contract details (options or warrants) for the given underlying symbol.
| Parameter | Type | Required | Description |
|---|---|---|---|
symbol | string | Yes | Underlying symbol |
secType | string | Yes | Derivative type: OPT, WAR, IOPT |
quoteContract, err := tc.QuoteContract("AAPL", "OPT")Order Management
PlaceOrder
func (tc *TradeClient) PlaceOrder(order model.Order) ([]byte, error)Submits an order to the exchange. Returns the server-assigned order ID on success.
| Parameter | Type | Required | Description |
|---|---|---|---|
order | model.Order | Yes | Order details |
order := model.LimitOrder(cfg.Account, "AAPL", "STK", "BUY", 100, 150.0)
result, err := tc.PlaceOrder(order)
// result: {"id":123456789,"status":"Submitted"}PreviewOrder
func (tc *TradeClient) PreviewOrder(order model.Order) ([]byte, error)Validates an order and returns estimated commission and margin impact without actually placing it.
preview, err := tc.PreviewOrder(order)
// preview: {"commission":1.0,"marginImpact":15000.0,"valid":true}ModifyOrder
func (tc *TradeClient) ModifyOrder(id int64, order model.Order) ([]byte, error)Modifies the price or quantity of an existing open order.
| Parameter | Type | Required | Description |
|---|---|---|---|
id | int64 | Yes | Order ID to modify |
order | model.Order | Yes | Updated order parameters |
// Change limit price from 150 to 148
order.LimitPrice = 148.0
modResult, err := tc.ModifyOrder(123456789, order)CancelOrder
func (tc *TradeClient) CancelOrder(id int64) ([]byte, error)Cancels an open order by ID.
| Parameter | Type | Required | Description |
|---|---|---|---|
id | int64 | Yes | Order ID to cancel |
cancelResult, err := tc.CancelOrder(123456789)Order Query
GetOrders
func (tc *TradeClient) GetOrders() ([]byte, error)Returns all orders (pending, filled, and cancelled) for the current trading day.
orders, err := tc.Orders()GetActiveOrders
func (tc *TradeClient) GetActiveOrders() ([]byte, error)Returns only orders that are currently pending (submitted but not yet filled or cancelled).
activeOrders, err := tc.ActiveOrders()GetInactiveOrders
func (tc *TradeClient) GetInactiveOrders() ([]byte, error)Returns orders that have been cancelled or have expired.
inactiveOrders, err := tc.InactiveOrders()GetFilledOrders
func (tc *TradeClient) GetFilledOrders() ([]byte, error)Returns orders that have been fully or partially filled.
filledOrders, err := tc.FilledOrders()Positions and Assets
GetPositions
func (tc *TradeClient) GetPositions() ([]byte, error)Returns all current open positions (symbol, quantity, average cost, market value, unrealized P&L).
positions, err := tc.Positions()GetAssets
func (tc *TradeClient) GetAssets() ([]byte, error)Returns account asset summary (net liquidation value, cash balance, buying power, margin usage).
assets, err := tc.Assets()GetPrimeAssets
func (tc *TradeClient) GetPrimeAssets() ([]byte, error)Returns consolidated asset summary for prime (multi-currency) accounts, aggregated across all sub-accounts.
primeAssets, err := tc.PrimeAssets()GetOrderTransactions
func (tc *TradeClient) GetOrderTransactions(id int64) ([]byte, error)Returns the individual fill records (executions) associated with a specific order.
| Parameter | Type | Required | Description |
|---|---|---|---|
id | int64 | Yes | Order ID |
transactions, err := tc.OrderTransactions(123456789)
// transactions: [{"price":150.0,"quantity":100,"timestamp":1700000000}]Generic Raw API Call
When the SDK has not yet wrapped a specific API endpoint, use ExecuteRaw on the underlying HttpClient to call it directly:
package main
import (
"fmt"
"log"
"github.com/tigerfintech/openapi-sdks/go/client"
"github.com/tigerfintech/openapi-sdks/go/config"
)
func main() {
cfg, err := config.NewClientConfig(
config.WithPropertiesFile("tiger_openapi_config.properties"),
)
if err != nil {
log.Fatal(err)
}
httpClient := client.NewHttpClient(cfg)
// Pass the API method name and a raw JSON parameter string
resp, err := httpClient.ExecuteRaw("market_state", `{"market":"US"}`)
if err != nil {
log.Fatal(err)
}
fmt.Println("Raw response:", resp)
}Updated 6 days ago
