撤銷訂單
該接口用於訂單撤銷。
SDK Links
Python | longport.openapi.TradeContext.cancel_order |
Rust | longport::trade::TradeContext#cancel_order |
Go | TradeContext.CancelOrder |
Node.js | TradeContext#cancelOrder |
Request
| HTTP Method | DELETE |
| HTTP URL | /v1/trade/order |
Parameters
Content-Type: application/json; charset=utf-8
| Name | Type | Required | Description |
|---|---|---|---|
| order_id | string | YES | 訂單 ID |
Request Example
python
from longport.openapi import TradeContext, Config, OAuthBuilder
oauth = OAuthBuilder("your-client-id").build(lambda url: print("Visit:", url))
config = Config.from_oauth(oauth)
ctx = TradeContext(config)
ctx.cancel_order("709043056541253632")python
import asyncio
from longport.openapi import AsyncTradeContext, Config, OAuthBuilder
async def main() -> None:
oauth = await OAuthBuilder("your-client-id").build_async(lambda url: print("Visit:", url))
config = Config.from_oauth(oauth)
ctx = AsyncTradeContext.create(config)
ctx.cancel_order("709043056541253632")
if __name__ == "__main__":
asyncio.run(main())javascript
const { Config, TradeContext, OAuth } = require('longport')
async function main() {
const oauth = await OAuth.build("your-client-id", (_, url) => { console.log("Open this URL to authorize: " + url) })
const config = Config.fromOAuth(oauth)
const ctx = TradeContext.new(config)
await ctx.cancelOrder("701276261045858304")
console.log("cancelled")
}
main().catch(console.error)java
import com.longport.*;
import com.longport.trade.*;
class Main {
public static void main(String[] args) throws Exception {
try (OAuth oauth = new OAuthBuilder("your-client-id").build(url -> System.out.println("Open to authorize: " + url)).get();
Config config = Config.fromOAuth(oauth);
TradeContext ctx = TradeContext.create(config)) {
ctx.cancelOrder("701276261045858304").get();
System.out.println("cancelled");
}
}
}rust
use std::sync::Arc;
use longport::{oauth::OAuthBuilder, trade::TradeContext, Config};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let oauth = OAuthBuilder::new("your-client-id").build(|url| println!("Open this URL to authorize: {url}")).await?;
let config = Arc::new(Config::from_oauth(oauth));
let (ctx, _) = TradeContext::new(config);
ctx.cancel_order("701276261045858304").await?;
println!("cancelled");
Ok(())
}cpp
#include <iostream>
#include <longport.hpp>
#ifdef WIN32
#include <windows.h>
#endif
using namespace longport;
using namespace longport::trade;
static void
run(const OAuth& oauth)
{
Config config = Config::from_oauth(oauth);
TradeContext ctx = TradeContext::create(config);
ctx.cancel_order("701276261045858304", [](auto res) {
if (!res) { std::cout << "failed" << std::endl; return; }
std::cout << "cancelled" << std::endl;
});
}
int main(int argc, char const* argv[]) {
#ifdef WIN32
SetConsoleOutputCP(CP_UTF8);
#endif
const std::string client_id = "your-client-id";
OAuthBuilder(client_id).build(
[](const std::string& url) {
std::cout << "Open this URL to authorize: " << url << std::endl;
},
[](auto res) {
if (!res) {
std::cout << "authorization failed: " << *res.status().message() << std::endl;
return;
}
run(*res);
});
std::cin.get();
return 0;
}go
package main
import (
"context"
"fmt"
"log"
"github.com/longportapp/openapi-go/config"
"github.com/longportapp/openapi-go/oauth"
"github.com/longportapp/openapi-go/trade"
)
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)
}
tctx, err := trade.NewFromCfg(conf)
if err != nil {
log.Fatal(err)
}
defer tctx.Close()
err = tctx.WithdrawOrder(context.Background(), "701276261045858304")
if err != nil {
log.Fatal(err)
}
fmt.Println("cancelled")
}Response
Response Headers
- Content-Type: application/json
Response Example
json
{
"code": 0,
"message": "success",
"data": {}
}Response Status
| Status | Description | Schema |
|---|---|---|
| 200 | 提交成功,訂單已委托。 | None |
| 400 | 撤單被拒絕,請求參數錯誤。 | None |