Public
Script
October 23, 2023
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
import { fetch } from "https://esm.town/v/std/fetch";
let { tradestationMarginsState } = await import("https://esm.town/v/simone/tradestationMarginsState");
export async function tradestationMargins() {
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 loadTradestationData() {
const cheerio = await import("npm:cheerio");
const response = await fetch(
"https://www.tradestation.com/pricing/futures-margin-requirements/"
);
const html = await response.text();
const $ = cheerio.load(html);
const table = $("table.table");
const headings = $("thead > tr > th", table)
.map((i, e) => $(e).text())
.get();
const result = $("tbody > tr", table)
.toArray()
.map((row) =>
$("td", row)
.map((i, e) => $(e).text())
.toArray()
)
.filter((a) => a.length)
.map((a) => Object.fromEntries(zip(headings, a)))
.reduce((acc, c) => ({ ...acc, [c["Symbol Root"]]: c }), {});
return result;
}
const { detailedDiff } = await import("npm:deep-object-diff");
const newMargins = await loadTradestationData();
const diff = detailedDiff(tradestationMarginsState, newMargins);
tradestationMarginsState = 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