mirror of
https://github.com/0xrsydn/idx-cli.git
synced 2026-11-07 09:45:36 +00:00
feat: bandar and broker totals for one investor side
IPOT's investor filter splits each broker row by client domicile, but its footer then gives only that side's buying (BBCA 2026-10-16: foreign 1.7 T, domestic 4.9 T of 6.6 T). The foreign net gives the selling on all boards: foreign 1.7 + 2.3 = 4.0 T (IDX: about 3.95 T regular), domestic 4.9 - 2.3 = 2.6 T. - BrokerSummary gains buy_value and sell_value: the total for all investors, the footer and the derived selling for one side (selling is null with a board filter, where the net covers other boards). - Bandar weighs the top N buyers' share of the value bought against the top N sellers' share of the value sold, so `--investor foreign|domestic` reads that side alone; for all investors the readings are unchanged. One side on one board is refused, as there is no selling to weigh. - Tables print what the side bought and sold; fixtures for foreign and domestic on all boards were recorded from IPOT. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01V3syn2fN2LNo5pvTSqc6eh
This commit is contained in:
parent
83783684cb
commit
5f64319157
23 changed files with 717 additions and 86 deletions
|
|
@ -2,10 +2,13 @@
|
|||
//! brokers than selling? No network; see docs/ARCHITECTURE.md.
|
||||
//!
|
||||
//! The summary lists the top 10 buyers and the top 10 sellers by gross lots.
|
||||
//! For N in 1, 3, 5 and 10 the top N buyers' value is set against the top N
|
||||
//! sellers' value, as a share of the value traded:
|
||||
//! For N in 1, 3, 5 and 10 the top N buyers' share of the value bought is set
|
||||
//! against the top N sellers' share of the value sold. For all investors both
|
||||
//! are the value traded, so this is the top N's buy value less their sell
|
||||
//! value as a share of it; for one investor side (foreign or domestic), each
|
||||
//! is that side's own buying or selling.
|
||||
//!
|
||||
//! | top-N buy - sell value, % of value | signal |
|
||||
//! | top-N buy share - sell share, % | signal |
|
||||
//! |------------------------------------|--------------------|
|
||||
//! | >= +15 | `big_accumulation` |
|
||||
//! | +5 to +15 | `accumulation` |
|
||||
|
|
@ -79,11 +82,12 @@ pub struct BandarTier {
|
|||
pub sell_value: Option<i64>,
|
||||
/// `buy_value - sell_value`, whole IDR.
|
||||
pub imbalance_value: Option<i64>,
|
||||
/// `buy_share_bps - sell_share_bps`, computed exactly: for all investors,
|
||||
/// `imbalance_value` in basis points of the value traded.
|
||||
pub imbalance_bps: Option<i64>,
|
||||
/// The top N buyers' share of the value traded, basis points.
|
||||
/// The top N buyers' share of the value bought, basis points.
|
||||
pub buy_share_bps: Option<i64>,
|
||||
/// The top N sellers' share of the value traded, basis points.
|
||||
/// The top N sellers' share of the value sold, basis points.
|
||||
pub sell_share_bps: Option<i64>,
|
||||
/// Volume-weighted average price the top N buyers paid, whole IDR.
|
||||
pub buy_average: Option<i64>,
|
||||
|
|
@ -109,8 +113,12 @@ pub struct BandarReport {
|
|||
/// The headline tier's imbalance, basis points of value traded.
|
||||
pub imbalance_bps: Option<i64>,
|
||||
pub tiers: Vec<BandarTier>,
|
||||
/// Value traded over the range, whole IDR.
|
||||
/// Value traded over the range, whole IDR; `None` for one investor side.
|
||||
pub total_value: Option<i64>,
|
||||
/// Value the selected investors bought, whole IDR.
|
||||
pub buy_value: Option<i64>,
|
||||
/// Value the selected investors sold, whole IDR.
|
||||
pub sell_value: Option<i64>,
|
||||
/// Average traded price, whole IDR.
|
||||
pub average_price: Option<i64>,
|
||||
/// Foreign net value, whole IDR; `None` for a filtered summary.
|
||||
|
|
@ -175,15 +183,28 @@ pub fn bandar_tier(summary: &BrokerSummary, top: u8) -> BandarTier {
|
|||
let imbalance_value = buy_value
|
||||
.zip(sell_value)
|
||||
.and_then(|(buy, sell)| buy.checked_sub(sell));
|
||||
let imbalance_bps = imbalance_value.and_then(|value| bps(value, summary.total_value));
|
||||
let imbalance_bps = match (buy_value, sell_value, summary.buy_value, summary.sell_value) {
|
||||
(Some(buy), Some(sell), Some(bought), Some(sold)) if bought > 0 && sold > 0 => {
|
||||
// buy/bought - sell/sold over one denominator, so equal totals
|
||||
// give exactly (buy - sell) / total.
|
||||
let (buy, sell, bought, sold) = (
|
||||
i128::from(buy),
|
||||
i128::from(sell),
|
||||
i128::from(bought),
|
||||
i128::from(sold),
|
||||
);
|
||||
i64::try_from((buy * sold - sell * bought) * 10_000 / (bought * sold)).ok()
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
BandarTier {
|
||||
top,
|
||||
buy_value,
|
||||
sell_value,
|
||||
imbalance_value,
|
||||
imbalance_bps,
|
||||
buy_share_bps: buy_value.and_then(|value| bps(value, summary.total_value)),
|
||||
sell_share_bps: sell_value.and_then(|value| bps(value, summary.total_value)),
|
||||
buy_share_bps: buy_value.and_then(|value| bps(value, summary.buy_value)),
|
||||
sell_share_bps: sell_value.and_then(|value| bps(value, summary.sell_value)),
|
||||
buy_average: vwap(buyers),
|
||||
sell_average: vwap(sellers),
|
||||
signal: imbalance_bps.map(BandarSignal::from_imbalance_bps),
|
||||
|
|
@ -223,6 +244,8 @@ pub fn analyze_bandar(summary: &BrokerSummary, top: u8) -> BandarReport {
|
|||
imbalance_bps: headline.imbalance_bps,
|
||||
tiers,
|
||||
total_value: summary.total_value,
|
||||
buy_value: summary.buy_value,
|
||||
sell_value: summary.sell_value,
|
||||
average_price: summary.average_price,
|
||||
foreign_net_value: summary.foreign_net_value,
|
||||
foreign_net_bps: summary
|
||||
|
|
@ -262,6 +285,8 @@ mod tests {
|
|||
total_value: Some(total),
|
||||
total_lots: None,
|
||||
average_price: None,
|
||||
buy_value: Some(total),
|
||||
sell_value: Some(total),
|
||||
foreign_net_value: Some(-total / 10),
|
||||
rounded: false,
|
||||
}
|
||||
|
|
@ -359,6 +384,36 @@ mod tests {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bandar_tier_weighs_one_side_against_its_own_buying_and_selling() {
|
||||
// Foreign investors bought 1,000 and sold 4,000: the top buyer took
|
||||
// half of the buying, the top seller a quarter of the selling.
|
||||
let mut s = summary(
|
||||
vec![flow("AA", Some(500), 100)],
|
||||
vec![flow("BB", Some(1_000), 100)],
|
||||
0,
|
||||
);
|
||||
s.investor = BrokerInvestor::Foreign;
|
||||
s.total_value = None;
|
||||
s.buy_value = Some(1_000);
|
||||
s.sell_value = Some(4_000);
|
||||
s.foreign_net_value = Some(-3_000);
|
||||
let report = analyze_bandar(&s, 1);
|
||||
let tier = &report.tiers[0];
|
||||
assert_eq!(
|
||||
(tier.buy_share_bps, tier.sell_share_bps),
|
||||
(Some(5000), Some(2500))
|
||||
);
|
||||
assert_eq!(tier.imbalance_bps, Some(2500));
|
||||
assert_eq!(tier.imbalance_value, Some(-500));
|
||||
assert_eq!(report.signal, Some(BandarSignal::BigAccumulation));
|
||||
assert_eq!(report.foreign_net_bps, None, "no total to weigh it against");
|
||||
|
||||
// Without the selling total (one side on one board) there is no reading.
|
||||
s.sell_value = None;
|
||||
assert_eq!(analyze_bandar(&s, 1).signal, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_analyze_bandar_never_counts_a_missing_figure_as_zero() {
|
||||
let s = summary(
|
||||
|
|
|
|||
|
|
@ -31,14 +31,15 @@ pub struct Comparison {
|
|||
pub failures: Vec<(String, IdxError)>,
|
||||
}
|
||||
|
||||
/// Readings weigh brokers against the value traded, which the source does
|
||||
/// not total for one investor side.
|
||||
fn check_investor(investor: BrokerInvestor) -> Result<(), IdxError> {
|
||||
if investor == BrokerInvestor::All {
|
||||
/// One investor side is weighed against its own selling, which the source
|
||||
/// gives only across all boards.
|
||||
fn check_scope(investor: BrokerInvestor, board: BrokerBoard) -> Result<(), IdxError> {
|
||||
if investor == BrokerInvestor::All || board == BrokerBoard::All {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(IdxError::InvalidInput(format!(
|
||||
"bandar needs --investor all: the broker summary has no total for {} investors alone",
|
||||
"bandar for {} investors needs --board all: the broker summary gives one side's \
|
||||
selling only across all boards",
|
||||
investor.as_str()
|
||||
)))
|
||||
}
|
||||
|
|
@ -70,7 +71,7 @@ impl Analysis<'_> {
|
|||
policy: CachePolicy,
|
||||
) -> Result<Fetched<BandarReport>, IdxError> {
|
||||
let top = check_top(query.top)?;
|
||||
check_investor(query.investor)?;
|
||||
check_scope(query.investor, query.board)?;
|
||||
let summary =
|
||||
self.idx
|
||||
.broker()
|
||||
|
|
@ -87,7 +88,7 @@ impl Analysis<'_> {
|
|||
policy: CachePolicy,
|
||||
) -> Result<BandarScreen, IdxError> {
|
||||
check_top(query.top)?;
|
||||
check_investor(query.investor)?;
|
||||
check_scope(query.investor, query.board)?;
|
||||
let mut reports = Vec::new();
|
||||
let mut failures = Vec::new();
|
||||
for symbol in symbols {
|
||||
|
|
|
|||
|
|
@ -155,13 +155,27 @@ impl Broker<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
/// The public summary of one parsed page. With one investor side selected,
|
||||
/// the page's footer covers only part of the trades the lists show (foreign
|
||||
/// buying, not foreign selling), so no totals are given; `F. NVal` ignores
|
||||
/// both filters, so the foreign net goes with any filter.
|
||||
/// The public summary of one parsed page.
|
||||
///
|
||||
/// With one investor side selected, the page's footer (`T. Val`) covers only
|
||||
/// that side's buying, so it is the side's `buy_value`, not a total of the
|
||||
/// trades the lists show. `F. NVal`, the foreign net, ignores both filters:
|
||||
/// on all boards it gives the side's selling (foreign buying less the foreign
|
||||
/// net; domestic buying plus it), and with a board filter it is dropped.
|
||||
fn summary_from_raw(symbol: &str, query: &BrokerQuery, raw: BrokerSummaryRaw) -> BrokerSummary {
|
||||
let one_side = query.investor != BrokerInvestor::All;
|
||||
let filtered = one_side || query.board != BrokerBoard::All;
|
||||
let all_boards = query.board == BrokerBoard::All;
|
||||
let foreign_net = raw.foreign_net_value.filter(|_| all_boards);
|
||||
let buy_value = raw.total_value;
|
||||
let sell_value = match query.investor {
|
||||
BrokerInvestor::All => raw.total_value,
|
||||
BrokerInvestor::Foreign => buy_value
|
||||
.zip(foreign_net)
|
||||
.and_then(|(buy, net)| buy.checked_sub(net)),
|
||||
BrokerInvestor::Domestic => buy_value
|
||||
.zip(foreign_net)
|
||||
.and_then(|(buy, net)| buy.checked_add(net)),
|
||||
};
|
||||
let mut summary = BrokerSummary {
|
||||
symbol: symbol.to_string(),
|
||||
from: raw.from.unwrap_or(query.from),
|
||||
|
|
@ -173,7 +187,9 @@ fn summary_from_raw(symbol: &str, query: &BrokerQuery, raw: BrokerSummaryRaw) ->
|
|||
total_value: raw.total_value.filter(|_| !one_side),
|
||||
total_lots: raw.total_lots.filter(|_| !one_side),
|
||||
average_price: raw.average_price.filter(|_| !one_side),
|
||||
foreign_net_value: raw.foreign_net_value.filter(|_| !filtered),
|
||||
buy_value,
|
||||
sell_value,
|
||||
foreign_net_value: foreign_net,
|
||||
rounded: raw.rounded,
|
||||
};
|
||||
for flow in summary.buyers.iter_mut().chain(summary.sellers.iter_mut()) {
|
||||
|
|
@ -228,7 +244,7 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_summary_from_raw_drops_totals_that_do_not_cover_the_lists() {
|
||||
fn test_summary_from_raw_gives_each_side_its_own_totals() {
|
||||
let day = date(2026, 10, 16);
|
||||
let read = |name: &str| {
|
||||
let html = std::fs::read_to_string(format!("tests/fixtures/{name}")).unwrap();
|
||||
|
|
@ -243,26 +259,68 @@ mod tests {
|
|||
assert_eq!(all.foreign_net_value, Some(-2_300_000_000_000));
|
||||
assert_eq!(all.sellers[0].name.as_deref(), Some("MANDIRI SEKURITAS"));
|
||||
|
||||
// Foreign, regular board: the footer's 1.6 T is less than the 2.0 T
|
||||
// its top seller alone sold.
|
||||
let foreign = BrokerQuery {
|
||||
investor: BrokerInvestor::Foreign,
|
||||
board: BrokerBoard::Regular,
|
||||
..query(day, day)
|
||||
assert_eq!(
|
||||
(all.buy_value, all.sell_value),
|
||||
(all.total_value, all.total_value)
|
||||
);
|
||||
|
||||
// Foreign on all boards: the footer's 1.7 T is foreign buying; with
|
||||
// the -2.3 T foreign net, foreign selling is 4.0 T (IDX: about
|
||||
// 3.95 T). Domestic buying 4.9 T makes up the rest of 6.6 T, and its
|
||||
// selling is 2.6 T.
|
||||
let side = |investor, board, name: &str| {
|
||||
let query = BrokerQuery {
|
||||
investor,
|
||||
board,
|
||||
..query(day, day)
|
||||
};
|
||||
summary_from_raw("BBCA.JK", &query, read(name))
|
||||
};
|
||||
let foreign = side(
|
||||
BrokerInvestor::Foreign,
|
||||
BrokerBoard::All,
|
||||
"ipot_brokersummary_bbca_20261016_foreign_all.html",
|
||||
);
|
||||
assert_eq!(
|
||||
(foreign.buy_value, foreign.sell_value),
|
||||
(Some(1_700_000_000_000), Some(4_000_000_000_000))
|
||||
);
|
||||
assert_eq!(foreign.total_value, None);
|
||||
assert_eq!(foreign.foreign_net_value, Some(-2_300_000_000_000));
|
||||
let domestic = side(
|
||||
BrokerInvestor::Domestic,
|
||||
BrokerBoard::All,
|
||||
"ipot_brokersummary_bbca_20261016_domestic_all.html",
|
||||
);
|
||||
assert_eq!(
|
||||
(domestic.buy_value, domestic.sell_value),
|
||||
(Some(4_900_000_000_000), Some(2_600_000_000_000))
|
||||
);
|
||||
|
||||
// Foreign, regular board: the footer's 1.6 T is regular foreign
|
||||
// buying, less than the 2.0 T its top seller alone sold, and the net
|
||||
// covers all boards, so selling cannot be derived.
|
||||
let raw = read("ipot_brokersummary_bbca_20261016_foreign_regular.html");
|
||||
assert!(raw.total_value < raw.sellers[0].value);
|
||||
let filtered = summary_from_raw("BBCA.JK", &foreign, raw);
|
||||
let regular = side(
|
||||
BrokerInvestor::Foreign,
|
||||
BrokerBoard::Regular,
|
||||
"ipot_brokersummary_bbca_20261016_foreign_regular.html",
|
||||
);
|
||||
assert_eq!(
|
||||
(
|
||||
filtered.total_value,
|
||||
filtered.total_lots,
|
||||
filtered.average_price
|
||||
regular.total_value,
|
||||
regular.total_lots,
|
||||
regular.average_price
|
||||
),
|
||||
(None, None, None)
|
||||
);
|
||||
assert_eq!(filtered.foreign_net_value, None);
|
||||
assert_eq!(filtered.sellers[0].value, Some(2_000_000_000_000));
|
||||
assert_eq!(
|
||||
(regular.buy_value, regular.sell_value),
|
||||
(Some(1_600_000_000_000), None)
|
||||
);
|
||||
assert_eq!(regular.foreign_net_value, None);
|
||||
assert_eq!(regular.sellers[0].value, Some(2_000_000_000_000));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -215,9 +215,18 @@ pub struct BrokerSummary {
|
|||
/// Average traded price, whole IDR; `None` when nothing traded, or as
|
||||
/// for `total_value`.
|
||||
pub average_price: Option<i64>,
|
||||
/// Value the selected investors bought over the range on the selected
|
||||
/// board, whole IDR: `total_value` for all investors, and for one side
|
||||
/// the source's footer, which then covers that side's buying.
|
||||
pub buy_value: Option<i64>,
|
||||
/// Value the selected investors sold, whole IDR: `total_value` for all
|
||||
/// investors; for one side on all boards, its buying less its net
|
||||
/// (foreign net, or its negative for domestic investors). `None` for one
|
||||
/// side on one board, where the source gives no net to derive it from.
|
||||
pub sell_value: Option<i64>,
|
||||
/// Foreign buy value minus foreign sell value over the range, whole IDR,
|
||||
/// across all boards. The source does not apply the investor or board
|
||||
/// filter to it, so it is `None` for a filtered summary.
|
||||
/// across all boards. The source does not apply the board filter to it,
|
||||
/// so it is `None` for a summary of one board.
|
||||
pub foreign_net_value: Option<i64>,
|
||||
/// True when the source abbreviated at least one figure (`1.6 M` lots,
|
||||
/// `841.2 B` IDR), so those figures carry only one decimal of precision.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue