-
Notifications
You must be signed in to change notification settings - Fork 1
/
volatility.io
42 lines (34 loc) · 1.43 KB
/
volatility.io
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
// Simple Currency Volatility Analysis System
// Author: Vladimir Livin
//
// Run: io volality.io
// Some imports
doFile("json.io")
// Definitions
baseCurrency := "USD"
targetCurrency := "RUB"
_30days := Duration clone setDays(30)
lastMonth := Date now - _30days
// Rates API
rateForDate := method(base, currency, aDate,
URL with("http://api.fixer.io/#{aDate asString(\"%Y-%m-%d\")}?base=#{base}&symbols=#{currency}" interpolate) fetch parseJson rates doString(currency)
)
rateLatest := method(base, currency,
URL with("http://api.fixer.io/latest?base=#{base}&symbols=#{currency}" interpolate) fetch parseJson rates doString(currency)
)
// Rate fetch & calculation
rateToday := rateLatest(baseCurrency, targetCurrency)
rateLastMonth := rateForDate(baseCurrency, targetCurrency, lastMonth)
growth := (((rateToday / rateLastMonth) - 1.0))
growthInPercent := (growth * 100) round
growthAbs := growth abs
// Resolving volatility
if(growthAbs > 0.2) then(resolution := "Panic!!! Enormous volatility.") \
elseif(growthAbs > 0.05) then(resolution := "It's quite volatile!") \
elseif(growthAbs > 0.01) then(resolution := "No volatility. Okay.") \
else(resolution := "Perfect! No volatility at all!")
writeln(baseCurrency, "/", targetCurrency, " Volatility Analysis")
writeln("Today: ", rateToday asString(0, 2))
writeln("Last Month: ", rateLastMonth asString(0, 2))
writeln("Growth/Loss: ", growthInPercent, "%")
writeln("Resolution: ", resolution)