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
import { fetch } from "https://esm.town/v/std/fetch";
let { interactiveBrokersMarginsState } = await import("https://esm.town/v/simone/interactiveBrokersMarginsState");
export async function interactiveBrokersMargins() {
function zip(...arrays) {
const result = [];
for (let i = 0; i < arrays[0].length; i++) {
result.push(arrays.map((a) => a[i]));
}
return result;
}
async function loadInteractiveBrokersData() {
const cheerio = await import("npm:cheerio");
const response = await fetch(
"https://www.interactivebrokers.com/en/trading/margin-futures-fops.php"
);
const html = await response.text();
const $ = cheerio.load(html);
const result = $("div.show_table, div.hide_table")
.toArray()
.map((div) => {
const exchangeName = $("h5", div).text();
const table = $("table", div);
const headings = $("thead tr th", table)
.map((_, h) => $(h).text())
.toArray();
const rows = $("tbody tr", table)
.toArray()
.map((row) =>
$("td", row)
.map((_, cell) => $(cell).text())
.toArray()
);
return {
exchangeName,
data: rows
.map((row) => Object.fromEntries(zip(headings, row)))
.reduce((acc, c) => ({ ...acc, [c["Trading Class"]]: c }), {}),
};
})
.reduce((acc, c) => ({ ...acc, [c.exchangeName]: c.data }), {});
return result;
}
const { detailedDiff } = await import("npm:deep-object-diff");
const newMargins = await loadInteractiveBrokersData();
const diff = detailedDiff(
interactiveBrokersMarginsState,
newMargins
);
interactiveBrokersMarginsState = newMargins;
console.email(diff);
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023