Skip to content

Instantly share code, notes, and snippets.

@mihar
Last active December 10, 2015 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mihar/25c239d4e04d211d1e2a to your computer and use it in GitHub Desktop.
Save mihar/25c239d4e04d211d1e2a to your computer and use it in GitHub Desktop.

Orderbook price calculation

Write a function btc2usd that calculates how much dollars you need to buy X bitcoin at this moment.

A sample asks part of the orderbook looks like this:

asks = [['1.00000000', '100.00', 1], ['2.00000000', '200.00', 2], ['4.00000000', '400.00', 4], ['8.00000000', '800.00', 8], ['16.00000000', '1600.00', 16]]
//        |              |       |    
// amount of btc  price per BTC  no. of equal orders

Example results

# btc2usd(1) => 100
# btc2usd(2) => 300
# btc2usd(3) => 500
# btc2usd(16) => 5300
  1 (100) + 4x200 (800) + 11x400 (4400) = 5300
# btc2usd(0) => 0
# btc2usd(-1) => Exception Invalid input
# btc2usd(true) => Exception Invalid input
# btc2usd(342) => Exception Not enough liquidity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment