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?

  • 3
    You might want to post this as a feature request for the XChange project in GitHub. See github.com/timmolter/XChange
    – Gary
    Commented Aug 22, 2012 at 21:04
  • 3
    Unless 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.
    – ThePiachu
    Commented Aug 23, 2012 at 17:28

1 Answer 1

    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.
    – Maestro
    Commented Nov 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.
    – smickles
    Commented Nov 27, 2012 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.