Skip to content

Instantly share code, notes, and snippets.

@Pe8er
Created April 19, 2018 20:18
Show Gist options
  • Save Pe8er/952a3d246bcafb746cf34e78f80e42f8 to your computer and use it in GitHub Desktop.
Save Pe8er/952a3d246bcafb746cf34e78f80e42f8 to your computer and use it in GitHub Desktop.
on run (argv)
--use this to debug. Options: myPortfolio, btc, ltc
if class of argv is script then set argv to {"myPortfolio"}
set myInvestment to 1528.73 --how much USD you invested
set myBTC to 0.0758 --how much BTC you have
set myETH to 1.7872 --how much ETH you have
set myLTC to 4.9431 --how much LTC you have
set valueBTC to checkPrice("btc")
set valueETH to checkPrice("eth")
set valueLTC to checkPrice("ltc")
set portfolioBTC to getValue(myBTC, valueBTC)
set portfolioETH to getValue(myETH, valueETH)
set portLTC to getValue(myLTC, valueLTC)
set myPortfolio to roundThis(portfolioBTC + portfolioETH + portLTC, 2)
set myProfit to roundThis(myPortfolio - myInvestment, 2)
if myInvestment < myPortfolio then
set symbol to "+"
else
set symbol to "-"
end if
try
if item 1 of argv is "myPortfolio" then
return "$" & format(myPortfolio) & space & "(" & symbol & "$" & format(myProfit) & ")" as string
else if item 1 of argv is "btc" then
return "$" & format(valueBTC)
else if item 1 of argv is "eth" then
return "$" & format(valueETH)
else if item 1 of argv is "ltc" then
return "$" & format(valueLTC)
end if
on error
return "…"
end try
end run
on getValue(myAmount, coinValue)
try
myAmount * coinValue
on error
return "…"
end try
end getValue
on checkPrice(coin)
set json to (do shell script "curl https://www.bitstamp.net/api/v2/ticker/" & coin & "usd")
tell application "JSON Helper"
set result to read JSON from json
set price to |last| of result as string
end tell
end checkPrice
on roundThis(n, numDecimals)
set x to 10 ^ numDecimals
tell n * x to return (it div 0.5 - it div 1) / x
end roundThis
on format(num)
set num to num as string
set AppleScript's text item delimiters to "."
try
set numA to text item 1 of num
set numB to "." & text item 2 of num
on error
set numA to num
set numB to ""
end try
set AppleScript's text item delimiters to ""
if numB is ".00" then set numB to ""
if length of numA > 3 then
set numA to items 1 thru ((length of numA) - 3) of numA & "," & items ((length of numA) - 2) thru -1 of numA as string
end if
return numA & numB as string
end format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment