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?
-
3You might want to post this as a feature request for the XChange project in GitHub. See github.com/timmolter/XChange– GaryAug 22, 2012 at 21:04
-
3Unless you want to move really big amount of Bitcoins, the order itself shoudldn't move the Bitcoin price too much. You can always go for the current price +- some spread.– ThePiachuAug 23, 2012 at 17:28
1 Answer
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.
-
Thanks. I already use code like that right now, but it requires to the download the whole orderbook each time, so I hope there was some API to do this serverside.– MaestroNov 27, 2012 at 7:54
-
Although I don't have the answer you want, be sure you're not actually downloading the whole order book. I believe that link I provided in the answer is only a partial order book, even as it is a lot of orders.– smicklesNov 27, 2012 at 17:35