Update Alert
Enable or disable a price alert. Python and Java expose enable(alert_id) / disable(alert_id); other languages use update(item) after setting item.enabled = true or false.
SDK Links
Python | longport.openapi.AlertContext.enable |
Rust | longport::alert::AlertContext#enable |
Go | AlertContext.Enable |
Node.js | AlertContext#enable |
Parameters
SDK method parameters.
| Name | Type | Required | Description |
|---|---|---|---|
| alert_id | string | YES | Alert ID (from list() response indicators[].id) |
Request Example
python
from longport.openapi import AlertContext, Config, OAuthBuilder
oauth = OAuthBuilder("your-client-id").build(lambda url: print("Visit:", url))
config = Config.from_oauth(oauth)
ctx = AlertContext(config)
# Get the alert ID from list()
alerts = ctx.list()
alert_id = alerts.lists[0].indicators[0].id
# Enable the alert
ctx.enable(alert_id)
# Disable the alert
ctx.disable(alert_id)python
import asyncio
from longport.openapi import AsyncAlertContext, 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 = AsyncAlertContext.create(config)
alerts = await ctx.list()
alert_id = alerts.lists[0].indicators[0].id
await ctx.enable(alert_id)
# await ctx.disable(alert_id) # to disable
if __name__ == "__main__":
asyncio.run(main())javascript
const { Config, AlertContext, 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 = AlertContext.new(config)
const alerts = await ctx.list()
const item = alerts.lists[0].indicators[0]
item.enabled = true // set false to disable
await ctx.update(item)
}
main().catch(console.error)java
import com.longport.*;
import com.longport.alert.*;
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);
AlertContext ctx = AlertContext.create(config)) {
var alerts = ctx.getList().get();
String alertId = alerts.getLists().get(0).getIndicators().get(0).getId();
ctx.enable(alertId).get();
// ctx.disable(alertId).get(); // to disable
}
}
}rust
use std::sync::Arc;
use longport::{oauth::OAuthBuilder, alert::AlertContext, Config};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let oauth = OAuthBuilder::new("your-client-id").build(|url| println!("Open: {url}")).await?;
let config = Arc::new(Config::from_oauth(oauth));
let ctx = AlertContext::new(config);
let list = ctx.list().await?;
let mut item = list.lists[0].indicators[0].clone();
item.enabled = true; // set false to disable
ctx.update(&item).await?;
Ok(())
}cpp
#include <iostream>
#include <longport.hpp>
using namespace longport;
using namespace longport::alert;
int main() {
OAuthBuilder("your-client-id").build(
[](const std::string& url) { std::cout << "Open: " << url << std::endl; },
[](auto res) {
if (!res) return;
Config config = Config::from_oauth(*res);
AlertContext ctx = AlertContext::create(config);
ctx.list([&ctx](auto list_resp) {
if (!list_resp) return;
auto item = (*list_resp).lists[0].indicators[0];
item.enabled = true; // set false to disable
ctx.update(item, [](auto resp) {
if (resp) std::cout << "OK" << std::endl;
});
});
});
std::cin.get();
}go
package main
import (
"context"
"fmt"
"log"
"github.com/longportapp/openapi-go/config"
"github.com/longportapp/openapi-go/oauth"
"github.com/longportapp/openapi-go/alert"
)
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)
}
c, err := alert.NewFromCfg(conf)
if err != nil {
log.Fatal(err)
}
defer c.Close()
list, err := c.List(context.Background())
if err != nil {
log.Fatal(err)
}
alertID := list.Lists[0].Indicators[0].ID
if err = c.Enable(context.Background(), alertID); err != nil {
log.Fatal(err)
}
fmt.Println("OK")
}Response
Response Example
json
{
"code": 0,
"message": "success",
"data": {}
}Response Status
| Status | Description | Schema |
|---|---|---|
| 200 | Success | None |
| 400 | Bad request | None |