dev 2
This commit is contained in:
@@ -26,19 +26,22 @@ type Position struct {
|
||||
ExpireDate string `json:"ExpireDate"`
|
||||
}
|
||||
|
||||
// Assets 对应 /api/v2/assets。
|
||||
type Assets struct {
|
||||
Total float64 `json:"total"`
|
||||
Available float64 `json:"available"`
|
||||
}
|
||||
|
||||
type accountBody struct {
|
||||
Account string `json:"account"`
|
||||
func (c *Client) Positions(ctx context.Context) ([]Position, error) {
|
||||
return c.decodePositions(ctx, "/api/v2/positions")
|
||||
}
|
||||
|
||||
func (c *Client) Positions(ctx context.Context, account string) ([]Position, error) {
|
||||
func (c *Client) Holding(ctx context.Context) ([]Position, error) {
|
||||
return c.decodePositions(ctx, "/api/holding")
|
||||
}
|
||||
|
||||
func (c *Client) decodePositions(ctx context.Context, path string) ([]Position, error) {
|
||||
raw := map[string]json.RawMessage{}
|
||||
if err := c.post(ctx, "/api/v2/positions", accountBody{Account: c.Account(account)}, &raw); err != nil {
|
||||
if err := c.post(ctx, path, map[string]any{"account": c.accountType}, &raw); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]Position, 0, len(raw))
|
||||
@@ -55,48 +58,29 @@ func (c *Client) Positions(ctx context.Context, account string) ([]Position, err
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *Client) Holding(ctx context.Context, account string) ([]Position, error) {
|
||||
raw := map[string]json.RawMessage{}
|
||||
if err := c.post(ctx, "/api/holding", accountBody{Account: c.Account(account)}, &raw); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]Position, 0, len(raw))
|
||||
for code, blob := range raw {
|
||||
var p Position
|
||||
if err := json.Unmarshal(blob, &p); err != nil {
|
||||
return nil, fmt.Errorf("holding %s: %w", code, err)
|
||||
}
|
||||
if p.StockCode == "" {
|
||||
p.StockCode = code
|
||||
}
|
||||
out = append(out, p)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *Client) Assets(ctx context.Context, account string) (*Assets, error) {
|
||||
func (c *Client) Assets(ctx context.Context) (*Assets, error) {
|
||||
var out Assets
|
||||
if err := c.post(ctx, "/api/v2/assets", accountBody{Account: c.Account(account)}, &out); err != nil {
|
||||
if err := c.post(ctx, "/api/v2/assets", map[string]any{"account": c.accountType}, &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
func (c *Client) TotalMoney(ctx context.Context, account string) (float64, error) {
|
||||
func (c *Client) TotalMoney(ctx context.Context) (float64, error) {
|
||||
var out struct {
|
||||
TotalMoney float64 `json:"total_money"`
|
||||
}
|
||||
if err := c.post(ctx, "/api/money/total", accountBody{Account: c.Account(account)}, &out); err != nil {
|
||||
if err := c.post(ctx, "/api/money/total", map[string]any{"account": c.accountType}, &out); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return out.TotalMoney, nil
|
||||
}
|
||||
|
||||
func (c *Client) AvailableMoney(ctx context.Context, account string) (float64, error) {
|
||||
func (c *Client) AvailableMoney(ctx context.Context) (float64, error) {
|
||||
var out struct {
|
||||
AvailableMoney float64 `json:"available_money"`
|
||||
}
|
||||
if err := c.post(ctx, "/api/money/available", accountBody{Account: c.Account(account)}, &out); err != nil {
|
||||
if err := c.post(ctx, "/api/money/available", map[string]any{"account": c.accountType}, &out); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return out.AvailableMoney, nil
|
||||
@@ -141,11 +125,11 @@ type OrderStatus struct {
|
||||
VolumeTraded int `json:"volume_traded"`
|
||||
}
|
||||
|
||||
func (c *Client) OrderStatusList(ctx context.Context, account string) ([]OrderStatus, error) {
|
||||
func (c *Client) OrderStatusList(ctx context.Context) ([]OrderStatus, error) {
|
||||
var out struct {
|
||||
Orders []OrderStatus `json:"orders"`
|
||||
}
|
||||
if err := c.post(ctx, "/api/order/status", accountBody{Account: c.Account(account)}, &out); err != nil {
|
||||
if err := c.post(ctx, "/api/order/status", map[string]any{"account": c.accountType}, &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out.Orders, nil
|
||||
@@ -164,29 +148,29 @@ type CancelAllResult struct {
|
||||
CanceledSysIDs []string `json:"canceled_sys_ids"`
|
||||
}
|
||||
|
||||
func (c *Client) CancelAll(ctx context.Context, account string) (*CancelAllResult, error) {
|
||||
func (c *Client) CancelAll(ctx context.Context) (*CancelAllResult, error) {
|
||||
var out CancelAllResult
|
||||
if err := c.post(ctx, "/api/order/cancel_all", accountBody{Account: c.Account(account)}, &out); err != nil {
|
||||
if err := c.post(ctx, "/api/order/cancel_all", map[string]any{"account": c.accountType}, &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
// CancelByRule 按「代码.市场 + (剩余+已成)」匹配撤单。HTTP 没有按委托号撤单,ZT 用它代替 cancel(orderId)。
|
||||
func (c *Client) CancelByRule(ctx context.Context, stock string, volume int, account string) (*CancelAllResult, error) {
|
||||
// CancelByRule 按「代码.市场 + (剩余+已成)」匹配撤单。HTTP 没有按委托号撤单。
|
||||
func (c *Client) CancelByRule(ctx context.Context, stock string, volume int) (*CancelAllResult, error) {
|
||||
var out CancelAllResult
|
||||
body := map[string]any{"stock": stock, "volume": volume, "account": c.Account(account)}
|
||||
body := map[string]any{"stock": stock, "volume": volume, "account": c.accountType}
|
||||
if err := c.post(ctx, "/api/order/cancel_order", body, &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
func (c *Client) Deals(ctx context.Context, account string) ([]map[string]string, error) {
|
||||
func (c *Client) Deals(ctx context.Context) ([]map[string]string, error) {
|
||||
var out struct {
|
||||
Deals []map[string]string `json:"deals"`
|
||||
}
|
||||
if err := c.post(ctx, "/api/order/deal", accountBody{Account: c.Account(account)}, &out); err != nil {
|
||||
if err := c.post(ctx, "/api/order/deal", map[string]any{"account": c.accountType}, &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out.Deals, nil
|
||||
|
||||
Reference in New Issue
Block a user