-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.w
48 lines (34 loc) · 1.59 KB
/
main.w
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
bring cloud;
bring http;
let tickerSymbol = "GME";
class TwelveDataApi {
key: cloud.Secret;
new() {
this.key = new cloud.Secret(
name: "twelve-data-api-key"
);
}
pub inflight stockUpdates(tickerSymbol: str): http.Response {
return http.get("https://api.twelvedata.com/time_series?symbol={tickerSymbol}&interval=1min&outputsize=1&apikey={this.key.value()}");
}
}
let recentStockPriceCache = new cloud.Counter(
initial: 0
);
let stockUpdatesQueue = new cloud.Queue();
let twelveDataApi = new TwelveDataApi();
let stockUpdatesFetchSchedule = new cloud.Schedule(rate: 2m); // Twelve Data free tier gives you 800 API credits per day. So with a rate of 2 minutes, you use 720 API credits per day
let stockUpdatesPoller = stockUpdatesFetchSchedule.onTick(inflight () => {
let stockUpdates = twelveDataApi.stockUpdates(tickerSymbol);
log("Status: {stockUpdates.status}");
log("Body: {stockUpdates.body}");
let stockUpdatesBody = stockUpdates.body;
log("Received this stock updates: {stockUpdatesBody}");
let stockUpdatesBodyJson = Json.parse(stockUpdatesBody);
let latestStockPriceStr = stockUpdatesBodyJson.get("values").getAt(0).get("close").asStr();
let latestStockPrice = num.fromStr(latestStockPriceStr);
let previousStockPrice = recentStockPriceCache.peek(tickerSymbol);
log("Stock price for {tickerSymbol} changed from {previousStockPrice} to {latestStockPrice} with a difference of: {latestStockPrice - previousStockPrice}");
recentStockPriceCache.set(latestStockPrice, tickerSymbol);
stockUpdatesQueue.push(stockUpdatesBody);
});