Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { addMonths, format, subMonths } from "https://esm.sh/date-fns";
function generateFutureEvents(months = 6) {
const events = [];
const startDate = new Date();
const endDate = addMonths(startDate, months);
const countries = ["USA", "EU", "China", "UK", "Japan", "Australia", "Canada", "Switzerland"];
const eventTypes = [
"Interest Rate Decision",
"GDP Release",
"CPI Data",
"Unemployment Rate",
"Retail Sales",
"PMI Data",
"Trade Balance",
"Non-Farm Payrolls",
];
while (startDate < endDate) {
if (startDate.getDay() >= 1 && startDate.getDay() <= 5) { // Weekdays only
const eventCount = Math.floor(Math.random() * 3) + 1; // 1 to 3 events per day
for (let i = 0; i < eventCount; i++) {
const country = countries[Math.floor(Math.random() * countries.length)];
const event = eventTypes[Math.floor(Math.random() * eventTypes.length)];
const impact = Math.random() < 0.3 ? "High" : (Math.random() < 0.7 ? "Medium" : "Low");
events.push({
date: new Date(startDate.getTime() + Math.random() * 24 * 60 * 60 * 1000),
event: `${country} ${event}`,
country,
impact,
});
}
}
startDate.setDate(startDate.getDate() + 1);
}
return events.sort((a, b) => a.date - b.date);
}
async function server(request: Request): Promise<Response> {
const url = new URL(request.url);
if (url.pathname === "/api/calendar") {
const currentDate = new Date("2024-09-14");
const futureEvents = generateFutureEvents().map(event => ({
...event,
date: addMonths(event.date, 15), // Shift events 15 months into the future
}));
const predefinedEvents = [
{
date: addMonths(new Date("2024-09-15T12:00:00Z"), 0),
event: "US Non-Farm Payrolls",
country: "USA",
impact: "High",
},
{ date: addMonths(new Date("2024-09-16T09:00:00Z"), 0), event: "EU CPI", country: "EU", impact: "Medium" },
{ date: addMonths(new Date("2024-09-17T02:00:00Z"), 0), event: "China PMI", country: "China", impact: "High" },
{ date: addMonths(new Date("2024-09-18T08:00:00Z"), 0), event: "UK GDP", country: "UK", impact: "High" },
{
date: addMonths(new Date("2024-09-19T23:30:00Z"), 0),
event: "Australia Interest Rate Decision",
country: "Australia",
impact: "High",
},
{
date: addMonths(new Date("2024-09-25T18:00:00Z"), 0),
event: "US Federal Reserve Interest Rate Decision",
country: "USA",
impact: "High",
},
{
date: addMonths(new Date("2024-10-03T11:00:00Z"), 0),
event: "EU ECB Interest Rate Decision",
country: "EU",
impact: "High",
},
{
date: addMonths(new Date("2024-10-10T02:30:00Z"), 0),
event: "Japan BOJ Interest Rate Decision",
country: "Japan",
impact: "High",
},
{
date: addMonths(new Date("2024-10-17T11:00:00Z"), 0),
event: "UK BOE Interest Rate Decision",
country: "UK",
impact: "High",
},
];
const allEvents = [...predefinedEvents, ...futureEvents].sort((a, b) => a.date.getTime() - b.date.getTime());
return new Response(JSON.stringify(allEvents), { headers: { "Content-Type": "application/json" } });
}
// ... (keep other API endpoints)
samweist-forexdatahub.web.val.run
September 14, 2024