From ef1759f82e345687b5695e7eedba393cde8e2b79 Mon Sep 17 00:00:00 2001 From: Ichhita Shukla <118386928+Ichhita02@users.noreply.github.com> Date: Thu, 4 Jul 2024 18:46:28 +0530 Subject: [PATCH 1/2] Update client3.py --- client3.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/client3.py b/client3.py index 3fc09b75f16..c42a47d370d 100644 --- a/client3.py +++ b/client3.py @@ -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 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 + if (price_b == 0): + return + return price_a/price_b # Main @@ -56,4 +58,4 @@ def getRatio(price_a, price_b): stock, bid_price, ask_price, price = getDataPoint(quote) 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["DEF"])) From 4971eafc579a07de91969a89392ae8872e084b8c Mon Sep 17 00:00:00 2001 From: Ichhita Shukla <118386928+Ichhita02@users.noreply.github.com> Date: Thu, 4 Jul 2024 18:47:07 +0530 Subject: [PATCH 2/2] Update client_test.py --- client_test.py | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/client_test.py b/client_test.py index af2bf26b3cc..2b958b74146 100644 --- a/client_test.py +++ b/client_test.py @@ -2,24 +2,23 @@ from client3 import getDataPoint class ClientTest(unittest.TestCase): - def test_getDataPoint_calculatePrice(self): - quotes = [ - {'top_ask': {'price': 121.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 ------------ """ - - 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 ------------ """ - - - """ ------------ Add more unit tests ------------ """ + def test_getDataPoint_calculatePrice(self): + quotes = [ + {'top_ask': {'price': 121.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'} + ] + for quote in quotes: + self.assertEqual(getDataPoint(quote), (quote['stock'], quote['top_bid']['price'], quote['top_ask']['price'], (quote['top_bid']['price'] + quote['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'} + ] + for quote in quotes: + self.assertEqual(getDataPoint(quote), (quote['stock'], quote['top_bid']['price'], quote['top_ask']['price'], (quote['top_bid']['price'] + quote['top_ask']['price']) / 2)) + """ ------------ Add more unit tests ------------ """ if __name__ == '__main__': unittest.main()