I was wondering if the community had thoughts on what is the best way to come up with BTC/USD, BTC/EUR, etc. exchange rates for pricing goods and services on a merchant site. I wrote a little script to grab the LAST trade from MtGox:
#!/usr/bin/env ruby
require 'open-uri'
btc_per_usd = 0.0
quote_str = open('http://mtgox.com/code/data/ticker.php').read
quote_str = quote_str.gsub(/["{}]/,'')
quote_str = quote_str.gsub(/ticker:/, '')
quotes = quote_str.split(',')
#puts quotes
quotes.each {|q| btc_per_usd = q.delete('a-z:').to_f if q[/last:/] }
#puts btc_per_usd
There are several problems with doing it this way. The last trade could have been unusually large or small in volume and not indicative of the true current exchange rate. Also, it doesn't make sense to continually update the exchange rate in real time because the price in BTC of items could fluctuate during the order completion process. I'm thinking that the rate should be set once per day from a cron job by taking some sort of average of the previous day's MtGox transactions. Any ideas or scripts you'd like to share?