Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions client3.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ def getDataPoint(quote):
stock = quote['stock']
bid_price = float(quote['top_bid']['price'])
ask_price = float(quote['top_ask']['price'])
price = bid_price
price = (bid_price + ask_price)/2.0 #mean of result
return stock, bid_price, ask_price, price


def getRatio(price_a, price_b):
""" Get ratio of price_a and price_b """
""" ------------- Update this function ------------- """
return 1
""" ------------- Update this function ------------- """
if(price_b ==0):#for zero exception handling
return
return price_a/price_b


# Main
Expand All @@ -52,8 +54,11 @@ def getRatio(price_a, price_b):
quotes = json.loads(urllib.request.urlopen(QUERY.format(random.random())).read())

""" ----------- Update to get the ratio --------------- """
prices = {}
for quote in quotes:
stock, bid_price, ask_price, price = getDataPoint(quote)
prices[stock] = price
print("Quoted %s at (bid:%s, ask:%s, price:%s)" % (stock, bid_price, ask_price, price))

print("Ratio %s" % getRatio(price, price))
print("Ratio %s" % getRatio(prices['ABC'], prices['DEC']))

38 changes: 34 additions & 4 deletions client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,48 @@ def test_getDataPoint_calculatePrice(self):
{'top_ask': {'price': 121.68, 'size': 4}, 'timestamp': '2019-02-11 22:06:30.572453', 'top_bid': {'price': 117.87, 'size': 81}, 'id': '0.109974697771', 'stock': 'DEF'}
]
""" ------------ Add the assertion below ------------ """

def test_getDataPoint_calculatePriceBidGreaterThanAsk(self):
for quotes in quotes:
self.assertEqual(getDataPoint(quotes), (quotes['stock'], quotes['top_bid']['price'], quotes['top_ask']['price'], (quotes['top_bid']['price'] + quotes['top_ask']['price']) / 2))

def test_getDataPoint_calculatePriceBidGreaterThanAsk(self):
quotes = [
{'top_ask': {'price': 119.2, 'size': 36}, 'timestamp': '2019-02-11 22:06:30.572453', 'top_bid': {'price': 120.48, 'size': 109}, 'id': '0.109974697771', 'stock': 'ABC'},
{'top_ask': {'price': 121.68, 'size': 4}, 'timestamp': '2019-02-11 22:06:30.572453', 'top_bid': {'price': 117.87, 'size': 81}, 'id': '0.109974697771', 'stock': 'DEF'}
]
""" ------------ Add the assertion below ------------ """

for quotes in quotes:
self.assertEqual(getDataPoint(quotes), (quotes['stock'], quotes['top_bid']['price'], quotes['top_ask']['price'], (quotes['top_bid']['price'] + quotes['top_ask']['price']) / 2))

""" ------------ Add more unit tests ------------ """


def test_getDataPoint_zeroPrices(self):
quote = {
'top_ask': {'price': 0.0, 'size': 36},
'top_bid': {'price': 0.0, 'size': 109},
'timestamp': '2019-02-11 22:06:30.572453',
'id': '0.4', 'stock': 'LMN'
}
expected_price = 0.0
self.assertEqual(getDataPoint(quote), ('LMN', 0.0, 0.0, expected_price))

def test_getDataPoint_missingAsk(self):
quote = {
'top_ask': {'size': 36}, # price missing
'top_bid': {'price': 120.0, 'size': 109},
'timestamp': '2019-02-11 22:06:30.572453',
'id': '0.5', 'stock': 'ERR'
}
with self.assertRaises(KeyError):
getDataPoint(quote)
def test_getDataPoint_nonNumericPrices(self):
quote = {
'top_ask': {'price': 'abc', 'size': 36},
'top_bid': {'price': 120.0, 'size': 109},
'timestamp': '2019-02-11 22:06:30.572453',
'id': '0.6', 'stock': 'BAD'
}
with self.assertRaises(ValueError):
getDataPoint(quote)

if __name__ == '__main__':
unittest.main()