1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
use std::sync::Arc;
use longbridge_httpcli::{HttpClient, Json, Method};
use serde::{Serialize, de::DeserializeOwned};
use tracing::{Subscriber, dispatcher, instrument::WithSubscriber};
use crate::{Config, Result, screener::types::*};
struct InnerScreenerContext {
http_cli: HttpClient,
log_subscriber: Arc<dyn Subscriber + Send + Sync>,
}
impl Drop for InnerScreenerContext {
fn drop(&mut self) {
dispatcher::with_default(&self.log_subscriber.clone().into(), || {
tracing::info!("screener context dropped");
});
}
}
/// Screener context — stock screener strategies, search, and indicators.
#[derive(Clone)]
pub struct ScreenerContext(Arc<InnerScreenerContext>);
impl ScreenerContext {
/// Create a [`ScreenerContext`]
pub fn new(config: Arc<Config>) -> Self {
let log_subscriber = config.create_log_subscriber("screener");
dispatcher::with_default(&log_subscriber.clone().into(), || {
tracing::info!(language = ?config.language, "creating screener context");
});
let ctx = Self(Arc::new(InnerScreenerContext {
http_cli: config.create_http_client(),
log_subscriber,
}));
dispatcher::with_default(&ctx.0.log_subscriber.clone().into(), || {
tracing::info!("screener context created");
});
ctx
}
/// Returns the log subscriber
#[inline]
pub fn log_subscriber(&self) -> Arc<dyn Subscriber + Send + Sync> {
self.0.log_subscriber.clone()
}
async fn get<R, Q>(&self, path: &str, query: Q) -> Result<R>
where
R: DeserializeOwned + Send + Sync + 'static,
Q: Serialize + Send + Sync,
{
Ok(self
.0
.http_cli
.request(Method::GET, path)
.query_params(query)
.response::<Json<R>>()
.send()
.with_subscriber(self.0.log_subscriber.clone())
.await?
.0)
}
async fn post<R, B>(&self, path: &str, body: B) -> Result<R>
where
R: DeserializeOwned + Send + Sync + 'static,
B: std::fmt::Debug + Serialize + Send + Sync + 'static,
{
Ok(self
.0
.http_cli
.request(Method::POST, path)
.body(Json(body))
.response::<Json<R>>()
.send()
.with_subscriber(self.0.log_subscriber.clone())
.await?
.0)
}
// ── screener_recommend_strategies ─────────────────────────────
/// Get preset built-in screener strategies.
///
/// Path: `GET /v1/quote/ai/screener/strategies/recommend`
pub async fn screener_recommend_strategies(
&self,
market: impl Into<String>,
) -> Result<ScreenerRecommendStrategiesResponse> {
#[derive(Serialize)]
struct Query {
market: String,
}
let raw: serde_json::Value = self
.get(
"/v1/quote/ai/screener/strategies/recommend",
Query {
market: market.into(),
},
)
.await?;
Ok(ScreenerRecommendStrategiesResponse { data: raw })
}
// ── screener_user_strategies ──────────────────────────────────
/// Get the current user's saved screener strategies.
///
/// Path: `GET /v1/quote/ai/screener/strategies/mine`
pub async fn screener_user_strategies(
&self,
market: impl Into<String>,
) -> Result<ScreenerUserStrategiesResponse> {
#[derive(Serialize)]
struct Query {
market: String,
}
let raw: serde_json::Value = self
.get(
"/v1/quote/ai/screener/strategies/mine",
Query {
market: market.into(),
},
)
.await?;
Ok(ScreenerUserStrategiesResponse { data: raw })
}
// ── screener_strategy ─────────────────────────────────────────
/// Get detail for one screener strategy by ID.
///
/// Path: `GET /v1/quote/ai/screener/strategy/{id}`
///
/// The `filter_` prefix is stripped from every `filters[].key` before
/// returning so callers see clean keys like `pettm` instead of
/// `filter_pettm`.
pub async fn screener_strategy(&self, id: i64) -> Result<ScreenerStrategyResponse> {
let path = format!("/v1/quote/ai/screener/strategy/{id}");
#[derive(Serialize)]
struct Empty {}
let mut raw: serde_json::Value = self.get(&path, Empty {}).await?;
// Strip filter_ prefix from filter.filters[].key
if let Some(filters) = raw["filter"]["filters"].as_array_mut() {
for f in filters.iter_mut() {
if let Some(k) = f["key"].as_str() {
let stripped = k.strip_prefix("filter_").unwrap_or(k).to_string();
f["key"] = serde_json::Value::String(stripped);
}
}
}
Ok(ScreenerStrategyResponse { data: raw })
}
// ── screener_search ───────────────────────────────────────────
/// Default return columns always included in a screener search request.
const DEFAULT_RETURNS: &'static [&'static str] = &[
"filter_prevclose",
"filter_prevchg",
"filter_marketcap",
"filter_salesgrowthyoy",
"filter_pettm",
"filter_pbmrq",
"filter_industry",
];
/// Search / screen securities using a strategy or custom conditions.
///
/// Path: `POST /v1/quote/ai/screener/search`
///
/// ## Mode A — strategy ID given
///
/// When `strategy_id` is `Some`, the strategy is fetched from
/// `GET /v1/quote/ai/screener/strategy/{id}` and its `filter.filters[]`
/// are forwarded to the search endpoint together with
/// [`DEFAULT_RETURNS`]. The `market` is taken from the strategy
/// response (falls back to `"US"` if absent or `"-"`).
///
/// ## Mode B — custom conditions
///
/// When `strategy_id` is `None` and `conditions` is non-empty each
/// element is either a `"KEY:MIN:MAX"` string **or** a JSON object with
/// `key`, `min`, `max`, and optional `tech_values` fields. The
/// supplied `market` is used directly. `DEFAULT_RETURNS` plus every
/// condition key are added to the `returns` list.
///
/// The `filter_` prefix is stripped from every `items[].indicators[].key`
/// in the response before it is returned to the caller.
///
/// `page` is 0-indexed.
pub async fn screener_search(
&self,
market: impl Into<String>,
strategy_id: Option<i64>,
conditions: Vec<ScreenerCondition>,
show: Vec<String>,
page: u32,
size: u32,
) -> Result<ScreenerSearchResponse> {
let market: String = market.into();
// ── build filters and effective market ──────────────────────────────
let (effective_market, filters) = if let Some(sid) = strategy_id {
// Mode A: fetch strategy from AI endpoint
let path = format!("/v1/quote/ai/screener/strategy/{sid}");
#[derive(Serialize)]
struct Empty {}
let strategy: serde_json::Value = self.get(&path, Empty {}).await?;
let mkt_val = strategy["market"].as_str().unwrap_or("US").to_uppercase();
let mkt = if mkt_val.is_empty() || mkt_val == "-" {
"US".to_string()
} else {
mkt_val
};
let mut filters: Vec<serde_json::Value> = Vec::new();
if let Some(f) = strategy["filter"]["filters"].as_array() {
for ind in f {
let key = ind["key"].as_str().unwrap_or("").to_string();
if key.is_empty() {
continue;
}
let min = ind["min"].as_str().unwrap_or("").to_string();
let max = ind["max"].as_str().unwrap_or("").to_string();
let tech_values = if ind["tech_values"].is_object() {
ind["tech_values"].clone()
} else {
serde_json::json!({})
};
filters.push(serde_json::json!({
"key": key,
"min": min,
"max": max,
"tech_values": tech_values,
}));
}
}
(mkt, filters)
} else {
// Mode B: typed condition objects
let filters: Vec<serde_json::Value> = conditions
.iter()
.filter(|c| !c.key.is_empty())
.map(|c| {
let api_key = if c.key.starts_with("filter_") {
c.key.clone()
} else {
format!("filter_{}", c.key)
};
let tv = if c.tech_values.is_object() {
c.tech_values.clone()
} else {
serde_json::json!({})
};
serde_json::json!({
"key": api_key,
"min": c.min,
"max": c.max,
"tech_values": tv,
})
})
.collect();
(market, filters)
};
// ── build returns list ───────────────────────────────────────────────
let mut returns: Vec<String> = Self::DEFAULT_RETURNS
.iter()
.map(|s| s.to_string())
.collect();
// add keys from filters (with filter_ prefix for the API)
for f in &filters {
if let Some(k) = f["key"].as_str() {
let api_key = if k.starts_with("filter_") {
k.to_string()
} else {
format!("filter_{k}")
};
if !returns.contains(&api_key) {
returns.push(api_key);
}
}
}
// add extra columns requested by the caller
for s in &show {
let api_key = if s.starts_with("filter_") {
s.clone()
} else {
format!("filter_{s}")
};
if !returns.contains(&api_key) {
returns.push(api_key);
}
}
// ── POST request ────────────────────────────────────────────────────
let body = serde_json::json!({
"market": effective_market,
"filters": filters,
"returns": returns,
"page": page,
"size": size,
});
let raw: serde_json::Value = self.post("/v1/quote/ai/screener/search", body).await?;
Ok(ScreenerSearchResponse {
data: strip_filter_prefix_from_search_results(raw),
})
}
// ── screener_indicators ───────────────────────────────────────
/// Get all available screener indicator definitions.
///
/// Path: `GET /v1/quote/ai/screener/indicators`
///
/// Post-processing applied before returning:
/// - `filter_` prefix is stripped from every `groups[].indicators[].key`
/// - `tech_values` is built from `tech_indicators`: `{tech_key: [{value,
/// label}]}`
pub async fn screener_indicators(&self) -> Result<ScreenerIndicatorsResponse> {
#[derive(Serialize)]
struct Empty {}
let mut raw: serde_json::Value = self
.get("/v1/quote/ai/screener/indicators", Empty {})
.await?;
if let Some(groups) = raw["groups"].as_array_mut() {
for group in groups.iter_mut() {
if let Some(indicators) = group["indicators"].as_array_mut() {
for ind in indicators.iter_mut() {
// Strip filter_ prefix from key
if let Some(k) = ind["key"].as_str() {
let stripped = k.strip_prefix("filter_").unwrap_or(k).to_string();
ind["key"] = serde_json::Value::String(stripped);
}
// Build tech_values from tech_indicators
if let Some(tech_inds) = ind["tech_indicators"].as_array().cloned() {
let tv: serde_json::Map<String, serde_json::Value> = tech_inds
.iter()
.filter_map(|ti| {
let key = ti["tech_key"].as_str()?.to_string();
let opts: Vec<serde_json::Value> = ti["tech_items"]
.as_array()
.unwrap_or(&vec![])
.iter()
.map(|item| {
serde_json::json!({
"value": item["item_value"].as_str().unwrap_or(""),
"label": item["item_name"].as_str().unwrap_or(""),
})
})
.collect();
Some((key, serde_json::Value::Array(opts)))
})
.collect();
if !tv.is_empty() {
ind["tech_values"] = serde_json::Value::Object(tv);
}
}
}
}
}
}
Ok(ScreenerIndicatorsResponse { data: raw })
}
}
/// Strip `filter_` prefix from every `items[].indicators[].key` in a raw
/// screener search result.
fn strip_filter_prefix_from_search_results(mut raw: serde_json::Value) -> serde_json::Value {
if let Some(items) = raw["items"].as_array_mut() {
for item in items.iter_mut() {
if let Some(indicators) = item["indicators"].as_array_mut() {
for ind in indicators.iter_mut() {
if let Some(k) = ind["key"].as_str() {
let stripped = k.strip_prefix("filter_").unwrap_or(k).to_string();
ind["key"] = serde_json::Value::String(stripped);
}
}
}
}
}
raw
}