Simple BitCoin Price-Checking Functions

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: heatherlaine, Klaus, FourthWorld, robinmiller, kevinmiller

Post Reply
sefrojones
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 447
Joined: Mon Jan 23, 2012 12:46 pm

Simple BitCoin Price-Checking Functions

Post by sefrojones » Fri Aug 01, 2014 7:39 pm

I wrote a couple of functions to check the current and average price of bitcoin from a few of the bigger exchanges/services. (BitStamp,BitPay,CoinBase,Bter, and BitCoinAverage) I figured I'd post them here, maybe someone could use them. They are very simple and do not use arrays as the amount of data returned is minimal.

Code: Select all

--These functions return the current Bitcoin Rate from their respective services. The sCurrency parameter should be 
--a valid three letter currency code. (i.e. USD,EUR,CNY, etc)

--note: Some of these functions require the three letter currency codes to be capitalized , better to just always capitalize....
--note: BitStampRate() requires no parameters as is is only USD


--Bitpay: The currency codes that can be used with this function are listed here:  https://bitpay.com/bitcoin-exchange-rates
function BitPayRate sCurrency
    put ("https://bitpay.com/api/rates/"&sCurrency) into tBitPayURL
    get url tBitPayURL
    put it into tBitPayData
    set the itemdelimiter to colon
   put item 4 of tBitPayData into tBitPayData
   replace  "}" with empty in tBitPayData
   if tBitPayData is a number then
      return sCurrency&&":"&&tBitPayData
   else 
      return "There was a problem with your request."
      end if
end BitPayRate

--Coinbase: The supported Currency Codes are available here:  https://coinbase.com/api/v1/currencies
function CoinBaseRate sCurrency
   put "https://coinbase.com/api/v1/prices/spot_rate?currency="&sCurrency into tCoinbaseURL
   get URL tCoinBaseURL
   put it into tCoinBaseData
   put item 1 of tCoinbaseData into tCoinbaseData
   set the itemdelimiter to colon
   put item 2 of tCoinBaseData into tCoinBaseData
   replace quote with Empty in tCoinBaseData
   if tCoinBaseData is a number then
      return sCurrency&&":"&& tCoinBaseData
   else
   else return "There was a problem with your request."
   end if
end CoinBaseRate

--Bter: Currently only works with USD or CNY calls.

function BterRate sCurrency
   put "http://data.bter.com/api/1/ticker/btc_"&sCurrency into tBterURL
   get URL tBterURL
   put it into tBterData
   put item 2 of  tBterData into tBterData
   set the itemdelimiter to colon
   if item 2 of tBterData is a number then
      return sCurrency&&":"&& item 2 of tBterData
   else
       else return "There was a problem with your request."
   end if
end BterRate



--This one requires the three letter currency code be capitalized, and returns the 24 hour Bitcoin Average 
--supported currencies: USD,EUR,CNY,GBP,CAD,PLN,RUB,AUD,SEK,BRL,NZD,SGD,ZAR,NOK,ILS,CHF,TRY,HKD,RON,MXN,IDR

function BitCoinAverage sCurrency
   put "https://api.bitcoinaverage.com/ticker/global/"&sCurrency&"/" into tBitCoinAverageURL
   get URL tBitCoinAverageURL
   put it into tBitCoinAverageData
   put item 1 of  tBitCoinAverageData into tBitCoinAverageData  
   set the itemdelimiter to colon
    replace space with empty in tBitCoinAverageData 
   if  item 2 of tBitCoinAverageData is a number then
  return sCurrency&&":"&& item 2 of tBitCoinAverageData 
   else
       else return "There was a problem with your request."
 end if
end BitCoinAverage


--BitStamp: this one has no parameters because bitstamp only deals in USD/BTC trades as far as I can tell. Although I think they do have a USD/EUR market as well.

function BitStampRate  
   get url "https://www.bitstamp.net/api/ticker/"
   put it into tBitStampData
   put item  2 of tBitStampData into tBitStampData
   set the itemdelimiter to colon
   put item 2 of tBitStampData into tBitStampData
   replace quote with empty in tBitStampData
   if tBitStampData is a number then
      return "USD :"&& tBitStampData
   else
       else return "There was a problem with your request."
   end if
end BitStampRate
Edit: attached a a simple stack



--Sefro
Attachments
bitcoin functions.livecode.zip
(2.37 KiB) Downloaded 98 times

tjframe
Posts: 14
Joined: Wed Dec 05, 2007 8:52 am

Re: Simple BitCoin Price-Checking Functions

Post by tjframe » Fri Jan 05, 2018 2:17 pm

Thanks for these! Just what I needed to help track my crypto trading for tax purposes.

I really hate the fact that exchanges use like 8+ decimal places to compare as a ratio to BTC, which itself fluctuates in wild swings. I know why they do it, but it's not human friendly.

Such a pain the butt to do back of the envelope mental calculations on things.
You end up doing a lot of things like "What is the value of my trade well let see .0000053457 X 12539 X 15,678.43" or "This coins has gone from 2,500 Satoshis to 5,600 Satoshis" What's the freaking nominal value USD!!!!!!!

Anyway, I'm working on a stack to manage all this stuff for me, and your functions area great first start.

Post Reply

Return to “Talking LiveCode”