Những lời gọi hay dùng nhất của vnstock-js 2.x trong một trang.
import { vn } from "vnstock-js";
const fpt = vn.stock("FPT"); // tạo đồng bộ, chưa gọi mạng
const quote = await fpt.price(); // giá, trần, sàn, tham chiếu, khối lượng, giá trị
const daily = await fpt.history("6mo"); // nến ngày, giá bằng đồng
const weekly = await fpt.history({ from: "2025-01-01", interval: "1w" });
const profile = await fpt.company();
const holders = await fpt.shareholders();
const income = await fpt.financials("income", { period: "quarter" });
console.log(quote.price, quote.changePct, daily.at(-1)?.close, weekly.length, profile.industry, holders.length, income.rows.length);
Mã sai không làm hỏng lúc tạo handle; lời gọi đầu tiên mới báo lỗi kèm gợi ý, ví dụ vn.stock("FTP") nhận Did you mean "FPT"?.
import { vn } from "vnstock-js";
const vnindex = await vn.index("VNINDEX").history("1y"); // điểm
const overview = await vn.market({ exchange: "HOSE" }); // chỉ số, thanh khoản, độ rộng, khối ngoại
const vn30 = await vn.prices({ group: "VN30" });
const { gainers, losers } = await vn.topMovers({ limit: 5 });
console.log(vnindex.at(-1)?.close, overview.liquidity.value, vn30.length, gainers, losers);
import { vn } from "vnstock-js";
const rows = await vn.screen({
group: "VN30",
where: { pe: { lt: 15 }, roe: { gt: 0.15 } }, // ROE là phân số: 0.15 = 15%
sort: "-roe",
limit: 10,
});
import { vn } from "vnstock-js";
import { sma, rsi, supportResistance } from "vnstock-js/indicators";
const bars = await vn.stock("FPT").history("1y");
const sma20 = sma(bars, { period: 20 });
const rsi14 = rsi(bars);
const zones = supportResistance(bars);
import { vn } from "vnstock-js";
const stop = vn.watch(["FPT", "VCB"], (t) => console.log(t.symbol, t.price, t.changePct));
setTimeout(stop, 60000); // nhớ dừng, nếu không tiến trình chạy mãi