Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Changelog

## [Unreleased]

### Added

- **Attached order (take-profit / stop-loss) support** for `SubmitOrder` and `ReplaceOrder`:
- New types: `AttachedOrderType` (`AttachedOrderTypeProfitTaker` / `AttachedOrderTypeStopLoss` / `AttachedOrderTypeBracket`), `AttachedOrderDetail`, `SubmitAttachedParams`, `ReplaceAttachedParams`
- `SubmitOrder` / `ReplaceOrder`: new `AttachedParams` field
- `Order` / `OrderDetail`: new `AttachedOrders []AttachedOrderDetail` field
- New `TradeContext.OrderDetailAttached(orderId)` and `TradeContext.CancelOrderAttached(orderId)` methods — query/cancel an attached sub-order by its own order ID
- `GetTodayOrders`: new `OrderId` and `IsAttached` fields — when combined, treats `OrderId` as an attached sub-order ID and returns that sub-order as an `Order` entry (not the parent order)

### Breaking changes

- `OrderDetail.ChargeDetail` is now `*OrderChargeDetail` (previously non-pointer). Attached orders return `nil` for this field; callers must handle the absent case.

## [v0.26.0] - 2026-07-20

### Added
Expand Down
82 changes: 82 additions & 0 deletions examples/submit_order_with_attached/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"

"github.com/longbridge/openapi-go/config"
"github.com/longbridge/openapi-go/oauth"
"github.com/longbridge/openapi-go/trade"
"github.com/shopspring/decimal"
)

func main() {
o := oauth.New("your-client-id").
OnOpenURL(func(url string) { fmt.Println("Open this URL to authorize:", url) })
if err := o.Build(context.Background()); err != nil {
log.Fatal(err)
}
conf, err := config.New(config.WithOAuthClient(o))
if err != nil {
log.Fatal(err)
}
tradeContext, err := trade.NewFromCfg(conf)
if err != nil {
log.Fatal(err)
return
}
defer tradeContext.Close()
ctx := context.Background()

tradeContext.OnTrade(func(ev *trade.PushEvent) {
log.Printf("order event: %+v\n", ev)
})

// Submit a bracket order: buy 700.HK at 12.00, with a take-profit at 13.00
// and a stop-loss at 11.00 attached to it.
order := &trade.SubmitOrder{
Symbol: "700.HK",
OrderType: trade.OrderTypeLO,
Side: trade.OrderSideBuy,
SubmittedQuantity: 200,
TimeInForce: trade.TimeTypeDay,
SubmittedPrice: decimal.NewFromFloat(12),
AttachedParams: &trade.SubmitAttachedParams{
AttachedOrderType: trade.AttachedOrderTypeBracket,
ProfitTakerPrice: decimal.NewFromFloat(13),
StopLossPrice: decimal.NewFromFloat(11),
},
}
orderId, err := tradeContext.SubmitOrder(ctx, order)
if err != nil {
log.Fatal(err)
return
}
fmt.Printf("orderId: %v\n", orderId)

detail, err := tradeContext.OrderDetail(ctx, orderId)
if err != nil {
log.Fatal(err)
return
}
for _, attached := range detail.AttachedOrders {
fmt.Printf("attached order: %+v\n", attached)

// The attached sub-order has its own order ID; query/cancel it directly
// via the *Attached variants instead of the parent order's ID.
attachedDetail, err := tradeContext.OrderDetailAttached(ctx, attached.OrderId)
if err != nil {
log.Fatal(err)
return
}
fmt.Printf("attached order detail: %+v\n", attachedDetail)
}

quitChannel := make(chan os.Signal, 1)
signal.Notify(quitChannel, syscall.SIGINT, syscall.SIGTERM)
<-quitChannel
}
37 changes: 37 additions & 0 deletions trade/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,22 @@ func (c *TradeContext) CancelOrder(ctx context.Context, orderId string) (err err
return
}

// CancelOrderAttached cancels an attached (take-profit / stop-loss) sub-order by its own order ID,
// instead of the parent order's ID.
// Reference: https://open.longbridge.com/en/docs/trade/order/withdraw
// Example:
//
// conf, err := config.NewFromEnv()
// tctx, err := trade.NewFromCfg(conf)
// err = tctx.CancelOrderAttached(context.Background(), "706388312699592705")
func (c *TradeContext) CancelOrderAttached(ctx context.Context, orderId string) (err error) {
values := url.Values{}
values.Add("order_id", orderId)
values.Add("is_attached", "true")
err = c.opts.httpClient.Delete(ctx, "/v1/trade/order", values, nil)
return
}

// AccountBalance to obtain the available, desirable, frozen, to-be-settled, and in-transit funds (fund purchase and redemption) information for each currency of the user.
// Reference: https://open.longbridge.com/en/docs/trade/asset/account
// Example:
Expand Down Expand Up @@ -350,6 +366,27 @@ func (c *TradeContext) OrderDetail(ctx context.Context, orderId string) (orderDe
return
}

// OrderDetailAttached queries detail for an attached (take-profit / stop-loss) sub-order by its
// own order ID, instead of the parent order's ID.
// Reference: https://open.longbridge.com/en/docs/trade/order/order_detail
// Example:
//
// conf, err := config.NewFromEnv()
// tctx, err := trade.NewFromCfg(conf)
// od, err := tctx.OrderDetailAttached(context.Background(), "706388312699592705")
func (c *TradeContext) OrderDetailAttached(ctx context.Context, orderId string) (orderDetail OrderDetail, err error) {
values := url.Values{}
values.Add("order_id", orderId)
values.Add("is_attached", "true")
var resp jsontypes.OrderDetail
err = c.opts.httpClient.Get(ctx, "/v1/trade/order", values, &resp)
if err != nil {
return
}
err = util.Copy(&orderDetail, resp)
return
}

// EstimateMaxPurchaseQuantity is used for estimating the maximum purchase quantity for Hong Kong and US stocks, warrants, and options.
// Reference: https://open.longbridge.com/en/docs/trade/order/estimate_available_buy_limit
// Example:
Expand Down
Loading