Created
April 6, 2017 18:00
-
-
Save fogonthedowns/f37a90fcd2981030fcb63845d8e6f10e to your computer and use it in GitHub Desktop.
Build a market
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Imagine I wish to buy 5 btc. Iterate over order_book ask price and amounts. fullfill order, return lowest possible cost | |
| order_book = {asks:[{price:2, amount:1, id:'id'},{price:2, amount:4, id:'id'}], bids:{}} | |
| user_amount = 5 | |
| # 1000 btc for $2.5/each | |
| def get_asks(order_book, user_amount) | |
| asks = order_book[:asks] | |
| returned_value = [] | |
| total_price = 0 | |
| total_sold = 0 | |
| until user_amount == total_sold do | |
| asks.each_with_index do |ask, index| | |
| ask_price = determine_price(ask) | |
| total_sold += ask[:amount] | |
| total_price += ask_price | |
| end | |
| end | |
| total_price | |
| end | |
| def determine_price(ask) | |
| ask[:amount] * ask[:price] | |
| end | |
| def check_get_asks | |
| order_book = {asks:[{price:2, amount:1, id:'id'},{price:2, amount:4, id:'id'}], bids:{}} | |
| a = get_asks(order_book, 5) | |
| a == 10 | |
| end | |
| check_get_asks() | |
| get_asks(order_book, user_amount) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment