I need to display at my website what a certain amount of Bitcoins will likely cost. I can fetch the latest price using the MtGox API and multiply it by the amount, but for large amounts this is inaccurate because the buy itself will move the price. The only other option seems to fetch the whole orderbook, and calculate it myself each time, but isn't there an API for this?
while amount > 0: amount_i = int(mtgox_depth['bids'][index]['amount_int']) price_i = int(mtgox_depth['bids'][index]['price_int']) if amount_i < amount: total_value += amount_i * price_i amount -= amount_i elif amount_i >= amount: total_value += amount * price_i amount = 0 index -= 1 spot = int(total_value / float(total_amount)) return spot
Something like this, where mtgox_depth is a json object of https://mtgox.com/api/1/BTCUSD/depth?raw, should work for you.