-
Notifications
You must be signed in to change notification settings - Fork 0
/
portfolioticker.rb
executable file
·86 lines (72 loc) · 2.71 KB
/
portfolioticker.rb
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
#!/usr/bin/ruby
# encoding: utf-8
# <bitbar.title>Crypto Portfolio Ticker</bitbar.title>
# <bitbar.version>v1.0</bitbar.version>
# <bitbar.author>Owen Craston</bitbar.author>
# <bitbar.author.github>owencraston</bitbar.author.github>
# <bitbar.desc>A ticker of your current portfolio value.</bitbar.desc>
# <bitbar.image>https://i.imgur.com/ZaeMGF4.jpg</bitbar.image>
# <bitbar.dependencies>ruby</bitbar.dependencies>
require 'open-uri'
require 'json'
class Portfolio
# Edit the coins and number of coins you have of each:
PORTFOLIO = {
'ETH' => 18.75,
'ZRX' => 10986.57375,
'XLM' => 16714.989605,
'BTC' => 0.59526,
'KNC' => 1056,
'NEO' => 23,
'XMR' => 1,
}.freeze
CURRENCY = 'USD'
# 24 hours in seconds
TWENTY_FOUR_HOURS = 86400
def getCoinPrice(coin)
data = open("https://min-api.cryptocompare.com/data/price?fsym=#{coin}&tsyms=#{CURRENCY}").read
coinPrice = JSON.parse(data)
coinPrice.values.first.to_f
end
def getPrevDayCoinPrice(coin, timeStamp)
data = open("https://min-api.cryptocompare.com/data/pricehistorical?fsym=#{coin}&tsyms=#{CURRENCY}&ts=#{timeStamp}").read
coinPrice = JSON.parse(data)
coinPrice.values.first.values.first.to_f
end
def getPortfolioValue()
coinValues = PORTFOLIO.map{ |coin, amount| getCoinPrice(coin)*amount }
coinValues.inject(0, :+).round(3)
end
def getPrevDay()
timeStamp = (Time.now - TWENTY_FOUR_HOURS).to_i
end
def getPrevDayPortfolioValue()
timeStamp = getPrevDay()
coinValues = PORTFOLIO.map{ |coin, amount| getPrevDayCoinPrice(coin, timeStamp)*amount }
coinValues.inject(0, :+).round(3)
end
def getPercentChange
currentPortfolioValue = getPortfolioValue()
prevdayPortfolioValue = getPrevDayPortfolioValue()
amountChanged = currentPortfolioValue - prevdayPortfolioValue
percentChange = ((amountChanged/prevdayPortfolioValue) * 100).round(3)
end
def createOutputString()
currentPortfolioValue = getPortfolioValue()
prevdayPortfolioValue = getPrevDayPortfolioValue()
if currentPortfolioValue > prevdayPortfolioValue
output = "🚀 #{getPortfolioValue()} +#{getPercentChange().abs}%"
elsif currentPortfolioValue < prevdayPortfolioValue
output = "⛷ #{getPortfolioValue()} -#{getPercentChange().abs}%"
else
output = "#{getPortfolioValue()} #{getPercentChange().abs}%"
end
end
end
# Create portfolio instance
portfolio = Portfolio.new
# Begin output for bitbar
# create output variable
output = "#{portfolio.createOutputString()}\n"
# print the output
puts output