303 lines
11 KiB
Go
303 lines
11 KiB
Go
package sdk
|
||
|
||
import "context"
|
||
|
||
const (
|
||
OpBuy = 23
|
||
OpSell = 24
|
||
OrderTypeVolume = 1101
|
||
PrTypeLatest = 5
|
||
QuickTradeNow = 2
|
||
)
|
||
|
||
type PassorderRequest struct {
|
||
OpType int `json:"opType"`
|
||
OrderType int `json:"orderType,omitempty"`
|
||
Stock string `json:"stock"`
|
||
PrType int `json:"prType,omitempty"`
|
||
Price float64 `json:"price"`
|
||
Volume int `json:"volume"`
|
||
QuickTrade int `json:"quickTrade,omitempty"`
|
||
}
|
||
|
||
func (c *Client) Passorder(ctx context.Context, req PassorderRequest) (*OrderRefResult, error) {
|
||
var out OrderRefResult
|
||
if err := c.post(ctx, "/api/trade/passorder", req, &out); err != nil {
|
||
return nil, err
|
||
}
|
||
return &out, nil
|
||
}
|
||
|
||
// PassorderLatest 按最新价下单。服务端策略名写死为 qmt,无法传投资备注。
|
||
func (c *Client) PassorderLatest(ctx context.Context, buy bool, stock string, volume int) (*OrderRefResult, error) {
|
||
op := OpSell
|
||
if buy {
|
||
op = OpBuy
|
||
}
|
||
return c.Passorder(ctx, PassorderRequest{
|
||
OpType: op,
|
||
OrderType: OrderTypeVolume,
|
||
Stock: stock,
|
||
PrType: PrTypeLatest,
|
||
Price: -1,
|
||
Volume: volume,
|
||
QuickTrade: QuickTradeNow,
|
||
})
|
||
}
|
||
|
||
type AlgoPassorderRequest struct {
|
||
OpType int `json:"opType"`
|
||
OrderType int `json:"orderType,omitempty"`
|
||
Stock string `json:"stock"`
|
||
PrType int `json:"prType"`
|
||
Price float64 `json:"price"`
|
||
Volume int `json:"volume"`
|
||
StrategyName string `json:"strategyName,omitempty"`
|
||
QuickTrade int `json:"quickTrade,omitempty"`
|
||
UserOrderID string `json:"userOrderId,omitempty"`
|
||
UserOrderParam map[string]any `json:"userOrderParam,omitempty"`
|
||
}
|
||
|
||
func (c *Client) AlgoPassorder(ctx context.Context, req AlgoPassorderRequest) (*OrderRefResult, error) {
|
||
var out OrderRefResult
|
||
if err := c.post(ctx, "/api/trade/algo_passorder", req, &out); err != nil {
|
||
return nil, err
|
||
}
|
||
return &out, nil
|
||
}
|
||
|
||
type SmartAlgoPassorderRequest struct {
|
||
OpType int `json:"opType"`
|
||
OrderType int `json:"orderType,omitempty"`
|
||
Stock string `json:"stock"`
|
||
PrType int `json:"prType"`
|
||
Price float64 `json:"price"`
|
||
Volume int `json:"volume"`
|
||
SmartAlgoType string `json:"smartAlgoType"`
|
||
LimitOverRate int `json:"limitOverRate"`
|
||
MinAmountPerOrder int `json:"minAmountPerOrder"`
|
||
StartTime string `json:"startTime,omitempty"`
|
||
EndTime string `json:"endTime,omitempty"`
|
||
}
|
||
|
||
func (c *Client) SmartAlgoPassorder(ctx context.Context, req SmartAlgoPassorderRequest) (*OrderRefResult, error) {
|
||
var out OrderRefResult
|
||
if err := c.post(ctx, "/api/trade/smart_algo_passorder", req, &out); err != nil {
|
||
return nil, err
|
||
}
|
||
return &out, nil
|
||
}
|
||
|
||
type StyleOrderResult struct {
|
||
Status string `json:"status"`
|
||
Action string `json:"action"`
|
||
Stock string `json:"stock"`
|
||
}
|
||
|
||
func (c *Client) styleOrder(ctx context.Context, path string, body map[string]any) (*StyleOrderResult, error) {
|
||
var out StyleOrderResult
|
||
if err := c.post(ctx, path, body, &out); err != nil {
|
||
return nil, err
|
||
}
|
||
return &out, nil
|
||
}
|
||
|
||
func (c *Client) OrderLots(ctx context.Context, stock string, lots int, style string, price float64, accID string) (*StyleOrderResult, error) {
|
||
return c.styleOrder(ctx, "/api/trade/order_lots", styleBody(stock, style, price, accID, "lots", lots))
|
||
}
|
||
|
||
func (c *Client) OrderValue(ctx context.Context, stock string, value float64, style string, price float64, accID string) (*StyleOrderResult, error) {
|
||
return c.styleOrder(ctx, "/api/trade/order_value", styleBody(stock, style, price, accID, "value", value))
|
||
}
|
||
|
||
func (c *Client) OrderPercent(ctx context.Context, stock string, percent float64, style string, price float64, accID string) (*StyleOrderResult, error) {
|
||
return c.styleOrder(ctx, "/api/trade/order_percent", styleBody(stock, style, price, accID, "percent", percent))
|
||
}
|
||
|
||
func (c *Client) OrderTargetValue(ctx context.Context, stock string, tarValue float64, style string, price float64, accID string) (*StyleOrderResult, error) {
|
||
return c.styleOrder(ctx, "/api/trade/order_target_value", styleBody(stock, style, price, accID, "tar_value", tarValue))
|
||
}
|
||
|
||
func (c *Client) OrderTargetPercent(ctx context.Context, stock string, tarPercent float64, style string, price float64, accID string) (*StyleOrderResult, error) {
|
||
return c.styleOrder(ctx, "/api/trade/order_target_percent", styleBody(stock, style, price, accID, "tar_percent", tarPercent))
|
||
}
|
||
|
||
func (c *Client) OrderShares(ctx context.Context, stock string, shares int, style string, price float64, accID string) (*StyleOrderResult, error) {
|
||
return c.styleOrder(ctx, "/api/trade/order_shares", styleBody(stock, style, price, accID, "shares", shares))
|
||
}
|
||
|
||
func styleBody(stock, style string, price float64, accID, key string, val any) map[string]any {
|
||
body := map[string]any{"stock": stock, key: val, "price": price}
|
||
if style != "" {
|
||
body["style"] = style
|
||
}
|
||
if accID != "" {
|
||
body["accId"] = accID
|
||
}
|
||
return body
|
||
}
|
||
|
||
func (c *Client) futures(ctx context.Context, path, stock string, amount int, style string, price float64, accID string) (*StyleOrderResult, error) {
|
||
body := map[string]any{"stock": stock, "amount": amount, "price": price}
|
||
if style != "" {
|
||
body["style"] = style
|
||
}
|
||
if accID != "" {
|
||
body["accId"] = accID
|
||
}
|
||
return c.styleOrder(ctx, path, body)
|
||
}
|
||
|
||
func (c *Client) FuturesBuyOpen(ctx context.Context, stock string, amount int, style string, price float64, accID string) (*StyleOrderResult, error) {
|
||
return c.futures(ctx, "/api/trade/futures/buy_open", stock, amount, style, price, accID)
|
||
}
|
||
func (c *Client) FuturesBuyCloseTdayFirst(ctx context.Context, stock string, amount int, style string, price float64, accID string) (*StyleOrderResult, error) {
|
||
return c.futures(ctx, "/api/trade/futures/buy_close_tdayfirst", stock, amount, style, price, accID)
|
||
}
|
||
func (c *Client) FuturesBuyCloseYdayFirst(ctx context.Context, stock string, amount int, style string, price float64, accID string) (*StyleOrderResult, error) {
|
||
return c.futures(ctx, "/api/trade/futures/buy_close_ydayfirst", stock, amount, style, price, accID)
|
||
}
|
||
func (c *Client) FuturesSellOpen(ctx context.Context, stock string, amount int, style string, price float64, accID string) (*StyleOrderResult, error) {
|
||
return c.futures(ctx, "/api/trade/futures/sell_open", stock, amount, style, price, accID)
|
||
}
|
||
func (c *Client) FuturesSellCloseTdayFirst(ctx context.Context, stock string, amount int, style string, price float64, accID string) (*StyleOrderResult, error) {
|
||
return c.futures(ctx, "/api/trade/futures/sell_close_tdayfirst", stock, amount, style, price, accID)
|
||
}
|
||
func (c *Client) FuturesSellCloseYdayFirst(ctx context.Context, stock string, amount int, style string, price float64, accID string) (*StyleOrderResult, error) {
|
||
return c.futures(ctx, "/api/trade/futures/sell_close_ydayfirst", stock, amount, style, price, accID)
|
||
}
|
||
|
||
type TaskResult struct {
|
||
Status string `json:"status"`
|
||
TaskID any `json:"taskId"`
|
||
}
|
||
|
||
func (c *Client) task(ctx context.Context, path, taskID, accountType string) (*TaskResult, error) {
|
||
body := map[string]any{"taskId": taskID}
|
||
if accountType != "" {
|
||
body["accountType"] = accountType
|
||
}
|
||
var out TaskResult
|
||
if err := c.post(ctx, path, body, &out); err != nil {
|
||
return nil, err
|
||
}
|
||
return &out, nil
|
||
}
|
||
|
||
func (c *Client) CancelTask(ctx context.Context, taskID, accountType string) (*TaskResult, error) {
|
||
return c.task(ctx, "/api/trade/cancel_task", taskID, accountType)
|
||
}
|
||
func (c *Client) PauseTask(ctx context.Context, taskID, accountType string) (*TaskResult, error) {
|
||
return c.task(ctx, "/api/trade/pause_task", taskID, accountType)
|
||
}
|
||
func (c *Client) ResumeTask(ctx context.Context, taskID, accountType string) (*TaskResult, error) {
|
||
return c.task(ctx, "/api/trade/resume_task", taskID, accountType)
|
||
}
|
||
|
||
func (c *Client) DoOrder(ctx context.Context) (map[string]any, error) {
|
||
var out map[string]any
|
||
if err := c.post(ctx, "/api/trade/do_order", map[string]any{}, &out); err != nil {
|
||
return nil, err
|
||
}
|
||
return out, nil
|
||
}
|
||
|
||
func (c *Client) TradeDetailData(ctx context.Context, account, datatype string) ([]map[string]string, error) {
|
||
body := map[string]any{
|
||
"account": c.Account(account),
|
||
"datatype": datatype,
|
||
}
|
||
var out struct {
|
||
Data []map[string]string `json:"data"`
|
||
}
|
||
if err := c.post(ctx, "/api/trade/trade_detail_data", body, &out); err != nil {
|
||
return nil, err
|
||
}
|
||
if out.Data == nil {
|
||
return []map[string]string{}, nil
|
||
}
|
||
return out.Data, nil
|
||
}
|
||
|
||
func (c *Client) ValueByOrderID(ctx context.Context, orderID, accountType, datatype string) (map[string]string, error) {
|
||
body := map[string]any{"orderId": orderID, "accountType": accountType, "datatype": datatype}
|
||
var out struct {
|
||
OrderID string `json:"orderId"`
|
||
Data map[string]string `json:"data"`
|
||
}
|
||
if err := c.post(ctx, "/api/trade/value_by_order_id", body, &out); err != nil {
|
||
return nil, err
|
||
}
|
||
return out.Data, nil
|
||
}
|
||
|
||
func (c *Client) LastOrderID(ctx context.Context, account, datatype string) (any, error) {
|
||
body := map[string]any{"account": c.Account(account), "datatype": datatype}
|
||
var out struct {
|
||
LastOrderID any `json:"last_order_id"`
|
||
}
|
||
if err := c.post(ctx, "/api/trade/last_order_id", body, &out); err != nil {
|
||
return nil, err
|
||
}
|
||
return out.LastOrderID, nil
|
||
}
|
||
|
||
func (c *Client) CanCancelOrder(ctx context.Context, orderID, accountType string) (any, error) {
|
||
body := map[string]any{"orderId": orderID, "accountType": accountType}
|
||
var out struct {
|
||
CanCancel any `json:"can_cancel"`
|
||
}
|
||
if err := c.post(ctx, "/api/trade/can_cancel_order", body, &out); err != nil {
|
||
return nil, err
|
||
}
|
||
return out.CanCancel, nil
|
||
}
|
||
|
||
func (c *Client) contractList(ctx context.Context, path, accID string) ([]map[string]string, error) {
|
||
body := map[string]any{}
|
||
if accID != "" {
|
||
body["accId"] = accID
|
||
}
|
||
var out struct {
|
||
Data []map[string]string `json:"data"`
|
||
}
|
||
if err := c.post(ctx, path, body, &out); err != nil {
|
||
return nil, err
|
||
}
|
||
return out.Data, nil
|
||
}
|
||
|
||
func (c *Client) DebtContract(ctx context.Context, accID string) ([]map[string]string, error) {
|
||
return c.contractList(ctx, "/api/trade/debt_contract", accID)
|
||
}
|
||
func (c *Client) AssureContract(ctx context.Context, accID string) ([]map[string]string, error) {
|
||
return c.contractList(ctx, "/api/trade/assure_contract", accID)
|
||
}
|
||
func (c *Client) EnableShortContract(ctx context.Context, accID string) ([]map[string]string, error) {
|
||
return c.contractList(ctx, "/api/trade/enable_short_contract", accID)
|
||
}
|
||
|
||
func (c *Client) IPOData(ctx context.Context, typ string) (any, error) {
|
||
var out struct {
|
||
Data any `json:"data"`
|
||
}
|
||
if err := c.post(ctx, "/api/trade/ipo_data", map[string]any{"type": typ}, &out); err != nil {
|
||
return nil, err
|
||
}
|
||
return out.Data, nil
|
||
}
|
||
|
||
func (c *Client) NewPurchaseLimit(ctx context.Context, accid string) (any, error) {
|
||
body := map[string]any{}
|
||
if accid != "" {
|
||
body["accid"] = accid
|
||
}
|
||
var out struct {
|
||
Data any `json:"data"`
|
||
}
|
||
if err := c.post(ctx, "/api/trade/new_purchase_limit", body, &out); err != nil {
|
||
return nil, err
|
||
}
|
||
return out.Data, nil
|
||
}
|