Simulator Data as of 2026-11-30
Pick a rolling flow window (1M, 3M, or 6M), choose whether to analyze core secondary-market flows (excluding 23 mega-crossings and lock-up dematerializations) or all-equity KSEI flows , and drag the regime threshold (±Rp T). Everything runs locally in your browser on the frozen monthly dataset (2021-01 → 2026-11, n = 71 months).
Show code
rawRows = FileAttachment ("data/frozen/monthly_flows.csv" ). csv ({typed : true })
gold = "var(--accent)"
teal = "var(--chart-teal)"
green = "var(--chart-positive)"
red = "var(--error)"
grey = "var(--muted)"
grid = "var(--line)"
ink = "var(--ink)"
function toDateObj (d) {
if (d instanceof Date ) return d;
return new Date (String (d). slice (0 , 10 ) + "T00:00:00Z" );
}
rows = rawRows. map ((d, idx) => ({
... d,
idx,
dt : toDateObj (d. date ),
date_str : d instanceof Date ? d. toISOString (). slice (0 , 10 ) : String (d. date ). slice (0 , 10 )
}))
Show code
viewof windowChoice = Inputs. radio (["1M" , "3M" , "6M" ], {
value : "3M" ,
label : "Rolling flow window"
})
viewof scopeChoice = Inputs. radio (
["Core (ex-crossings)" , "All IDX equities (incl. crossings)" ],
{
value : "Core (ex-crossings)" ,
label : "KSEI equity flow series"
}
)
viewof thresholdT = Inputs. range ([0 , 40 ], {
value : 10 ,
step : 1 ,
label : "Regime threshold (±Rp Trillion)"
})
Show code
function computeRegimes (data, win, scope, thresh) {
const useCore = scope. startsWith ("Core" );
const flowCol = useCore
? (win === "1M" ? "eq_core_flow_idr_t" : win === "3M" ? "eq_core_flow_3m_t" : "eq_core_flow_6m_t" )
: (win === "1M" ? "eq_flow_idr_t" : win === "3M" ? "eq_flow_3m_t" : "eq_flow_6m_t" );
const cumCol = useCore ? "eq_core_cum_flow_idr_t" : "eq_cum_flow_idr_t" ;
const tagged = data. map ((d, i) => {
const val = Number (d[flowCol]);
const regime = val > thresh ? "Inflow" : val < - thresh ? "Outflow" : "Neutral" ;
const prevDt = i > 0 ? data[i - 1 ]. dt : new Date (d. dt . getTime () - 30 * 86400e3 );
const nextDt = i < data. length - 1 ? data[i + 1 ]. dt : new Date (d. dt . getTime () + 30 * 86400e3 );
const x0 = new Date ((prevDt. getTime () + d. dt . getTime ()) / 2 );
const x1 = new Date ((d. dt . getTime () + nextDt. getTime ()) / 2 );
return {
... d,
x0,
x1,
signal_flow : val,
cum_flow : Number (d[cumCol]),
regime
};
});
const states = ["Outflow" , "Neutral" , "Inflow" ];
// Empirical spell durations
const spells = new Map (states. map (s => [s, []]));
let cur = tagged[0 ]. regime ;
let run = 1 ;
for (let i = 1 ; i < tagged. length ; i++ ) {
if (tagged[i]. regime === cur) {
run += 1 ;
} else {
spells. get (cur). push (run);
cur = tagged[i]. regime ;
run = 1 ;
}
}
spells. get (cur). push (run);
// Empirical 3x3 transition matrix
const counts = new Map ();
const rowTot = new Map (states. map (s => [s, 0 ]));
for (const a of states) {
for (const b of states) counts. set (` ${ a} -> ${ b} ` , 0 );
}
for (let i = 0 ; i < tagged. length - 1 ; i++ ) {
const a = tagged[i]. regime ;
const b = tagged[i + 1 ]. regime ;
counts. set (` ${ a} -> ${ b} ` , counts. get (` ${ a} -> ${ b} ` ) + 1 );
rowTot. set (a, rowTot. get (a) + 1 );
}
const transitions = [];
for (const a of states) {
const tot = rowTot. get (a);
for (const b of states) {
const cnt = counts. get (` ${ a} -> ${ b} ` );
const prob = tot > 0 ? (cnt / tot) * 100 : 0 ;
transitions. push ({
from : a,
to : b,
count : cnt,
total : tot,
prob,
label : tot > 0 ? ` ${ prob. toFixed (0 )} % ( ${ cnt} )` : "—"
});
}
}
const meanClean = arr => arr. length > 0 ? d3. mean (arr) : null ;
const regimeSummary = states. map (st => {
const sub = tagged. filter (d => d. regime === st);
const fx = sub. map (d => d. usdidr_ret_1m_pct ). filter (v => v != null && ! Number . isNaN (v));
const sbn = sub. map (d => d. sbn_flow_idr_t ). filter (v => v != null && ! Number . isNaN (v));
const f1 = sub. map (d => d. ihsg_fwd_1m_pct ). filter (v => v != null && ! Number . isNaN (v));
const f3 = sub. map (d => d. ihsg_fwd_3m_pct ). filter (v => v != null && ! Number . isNaN (v));
const sig = sub. map (d => d. signal_flow );
const sp = spells. get (st);
const stay = rowTot. get (st) > 0 ? (counts. get (` ${ st} -> ${ st} ` ) / rowTot. get (st)) * 100 : null ;
return {
regime : st,
months : sub. length ,
share_pct : (sub. length / tagged. length ) * 100 ,
avg_dur : sp. length > 0 ? d3. mean (sp) : 0 ,
stay_pct : stay,
mean_signal : meanClean (sig),
mean_fx_1m : meanClean (fx),
mean_sbn_1m : meanClean (sbn),
mean_fwd_1m : meanClean (f1),
mean_fwd_3m : meanClean (f3),
n_fwd_3m : f3. length
};
});
return {tagged, transitions, regimeSummary};
}
sim = computeRegimes (rows, windowChoice, scopeChoice, thresholdT)
outRow = sim. regimeSummary . find (d => d. regime === "Outflow" )
neuRow = sim. regimeSummary . find (d => d. regime === "Neutral" )
inRow = sim. regimeSummary . find (d => d. regime === "Inflow" )
fmt = (v, dec= 2 , plus= true ) => v == null ? "—" : ` ${ plus && v >= 0 ? "+" : "" }${ v. toFixed (dec)} `
Show code
md `** ${ windowChoice} ${ scopeChoice} at ±Rp ${ thresholdT} T:** **Outflow** = ${ outRow. months } mos (avg ${ outRow. avg_dur . toFixed (1 )} m, stay ${ outRow. stay_pct == null ? "—" : outRow. stay_pct . toFixed (0 ) + "%" } , ΔUSD/IDR ** ${ fmt (outRow. mean_fx_1m , 2 )} %**, next-3M IHSG ** ${ fmt (outRow. mean_fwd_3m , 2 )} %**) · **Neutral** = ${ neuRow. months } mos (ΔUSD/IDR ** ${ fmt (neuRow. mean_fx_1m , 2 )} %**) · **Inflow** = ${ inRow. months } mos (avg ${ inRow. avg_dur . toFixed (1 )} m, stay ${ inRow. stay_pct == null ? "—" : inRow. stay_pct . toFixed (0 ) + "%" } , ΔUSD/IDR ** ${ fmt (inRow. mean_fx_1m , 2 )} %**, next-3M IHSG ** ${ fmt (inRow. mean_fwd_3m , 2 )} %**)`
Show code
cumExtent = d3. extent (sim. tagged , d => d. cum_flow )
yPad = Math . max (20 , (cumExtent[1 ] - cumExtent[0 ]) * 0.12 )
yLo = Math . min (0 , cumExtent[0 ] - yPad)
yHi = Math . max (10 , cumExtent[1 ] + yPad)
Plot. plot ({
title : `Cumulative foreign equity flow (Rp T) shaded by ${ windowChoice} regime (±Rp ${ thresholdT} T)` ,
subtitle : "Green shading = Inflow regime · Red shading = Outflow regime · Unshaded = Neutral · Source: PT KSEI" ,
style : {background : "transparent" , color : ink, fontFamily : "Satoshi, Arial, sans-serif" },
width : 780 ,
height : 360 ,
marginLeft : 58 ,
marginRight : 20 ,
grid : true ,
x : {label : null , type : "utc" },
y : {label : "Cumulative net foreign equity flow (Rp T) ↑" , domain : [yLo, yHi]},
marks : [
Plot. rect (sim. tagged . filter (d => d. regime === "Inflow" ), {
x1 : "x0" ,
x2 : "x1" ,
y1 : yLo,
y2 : yHi,
fill : green,
fillOpacity : 0.16
}),
Plot. rect (sim. tagged . filter (d => d. regime === "Outflow" ), {
x1 : "x0" ,
x2 : "x1" ,
y1 : yLo,
y2 : yHi,
fill : red,
fillOpacity : 0.22
}),
Plot. ruleY ([0 ], {stroke : grid}),
Plot. lineY (sim. tagged , {
x : "dt" ,
y : "cum_flow" ,
stroke : gold,
strokeWidth : 2.5 ,
tip : true ,
title : d => ` ${ d. year_month } ( ${ d. regime } ) \n${ windowChoice} flow: ${ fmt (d. signal_flow , 1 )} T \n Cum flow: ${ fmt (d. cum_flow , 1 )} T \n ΔUSD/IDR: ${ fmt (d. usdidr_ret_1m_pct , 2 )} % \n Next-3M IHSG: ${ fmt (d. ihsg_fwd_3m_pct , 2 )} %`
})
]
})
Show code
Plot. plot ({
title : `Empirical month-to-month regime transition matrix ( ${ windowChoice} , ±Rp ${ thresholdT} T)` ,
subtitle : "Row = regime at month t · Column = regime at month t+1 · Cell shows P(t+1 | t) and transition count" ,
style : {background : "transparent" , color : ink, fontFamily : "Satoshi, Arial, sans-serif" },
width : 560 ,
height : 270 ,
marginLeft : 95 ,
marginBottom : 48 ,
padding : 0.08 ,
x : {label : "Next month regime (t+1) →" , domain : ["Outflow" , "Neutral" , "Inflow" ]},
y : {label : "Current regime (t)" , domain : ["Outflow" , "Neutral" , "Inflow" ]},
color : {
type : "linear" ,
domain : [0 , 100 ],
range : [
getComputedStyle (document . body ). getPropertyValue ("--surface" ). trim (),
getComputedStyle (document . body ). getPropertyValue ("--chart-caution" ). trim (),
getComputedStyle (document . body ). getPropertyValue ("--accent" ). trim ()
],
legend : false
},
marks : [
Plot. cell (sim. transitions , {
x : "to" ,
y : "from" ,
fill : "prob" ,
rx : 4
}),
Plot. text (sim. transitions , {
x : "to" ,
y : "from" ,
text : "label" ,
fill : d => d. prob > 45 ? "var(--on-accent)" : ink,
fontWeight : "600" ,
fontSize : 13
})
]
})
Regime Statistics Summary
Show code
Inputs. table (
sim. regimeSummary . map (d => ({
Regime : d. regime ,
"Months" : d. months ,
"Share (%)" : d. share_pct . toFixed (1 ) + "%" ,
"Avg Duration (mo)" : d. avg_dur . toFixed (1 ),
"Stay Prob (%)" : d. stay_pct == null ? "—" : d. stay_pct . toFixed (1 ) + "%" ,
[`Mean ${ windowChoice} Flow (Rp T)` ]: fmt (d. mean_signal , 1 ),
"Mean ΔUSD/IDR 1M (%)" : fmt (d. mean_fx_1m , 2 ) + "%" ,
"Mean SBN Flow 1M (Rp T)" : fmt (d. mean_sbn_1m , 1 ),
"Mean Next-1M IHSG (%)" : d. mean_fwd_1m == null ? "—" : fmt (d. mean_fwd_1m , 2 ) + "%" ,
"Mean Next-3M IHSG (%)" : d. mean_fwd_3m == null ? "—" : fmt (d. mean_fwd_3m , 2 ) + "%"
})),
{layout : "auto" }
)
How the model works
Split-adjusted KSEI flow: Each month-end from December 2020 through November 2026 (72 snapshots → 71 flow months), stock-level equity foreign flows are computed from PT KSEI BalanceposEfek + StatisEfek using \(\Delta(\text{foreign\_total}_{i,t} / \text{sec\_num}_{i,t}) \times \text{mcap}_{i,t}\) with a denominator-lag guard when sec_num and scripless shares temporarily desynchronize.
Core vs All-Equity scope: Core (ex-crossings) strips 23 stock-month events where a single stock’s flow exceeds Rp 15 T or reflects a >25 pp scripless lock-up dematerialization with >Rp 3 T flow (e.g., GOTO Dec 2022, BYAN Aug 2024, TPIA Jan–Feb 2026, MORA Dec 2025/Apr 2026).
Threshold rule: Let \(F_{t}^{(w)}\) be the rolling \(w\) -month sum (1M, 3M, or 6M) of the chosen flow series and \(\theta\) be your slider threshold (Rp T). Month \(t\) is classified as Inflow if \(F_t^{(w)} > \theta\) , Outflow if \(F_t^{(w)} < -\theta\) , and Neutral otherwise.
Conventions: Positive ΔUSD/IDR means rupiah depreciation (more IDR per USD). Forward IHSG (^JKSE) returns exclude tail months whose horizon extends beyond the 2026-11-30 cutoff.
Limits of this simulator
Hindsight vs real-time signals: Thresholds are evaluated across the frozen 2021–2026 sample; setting \(\theta\) after seeing the history risks overfitting, and rolling 3M/6M sums react with a lag at turning points.
Monthly granularity: Because daily IDX broker flow (idx.co.id) is Cloudflare-blocked, intra-month washouts and rebounds within a single calendar month are netted out.
No trading-rule endorsement: As detailed in the case study , overlapping 3M forward IHSG returns have wide Newey-West standard errors and actually mean-revert after extreme inflow or outflow spikes—treat flow regimes as a contemporaneous FX and liquidity risk gauge , not a market-timing signal.
← Back to the study · Story · Case study