diff --git a/changes.patch b/changes.patch new file mode 100644 index 00000000000..75bf0ab253c Binary files /dev/null and b/changes.patch differ diff --git a/client3.py b/client3.py index 3fc09b75f16..433d53e4373 100644 --- a/client3.py +++ b/client3.py @@ -35,25 +35,40 @@ 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 None + + return price_a/price_b # Main if __name__ == "__main__": - # Query the price once every N seconds. - for _ in iter(range(N)): + for _ in range(N): quotes = json.loads(urllib.request.urlopen(QUERY.format(random.random())).read()) - """ ----------- Update to get the ratio --------------- """ + # Initialize variables to store prices of both stocks + prices = {} + + # Process the quotes for quote in quotes: stock, bid_price, ask_price, price = getDataPoint(quote) - print("Quoted %s at (bid:%s, ask:%s, price:%s)" % (stock, bid_price, ask_price, price)) + prices[stock] = price + print("Quoted %s at (bid: %s, ask: %s, price: %s)" % (stock, bid_price, ask_price, price)) + + # Calculate the ratio of the two stock prices + if 'ABC' in prices and 'DEF' in prices: + ratio = getRatio(prices['ABC'], prices['DEF']) + if ratio is not None: + print("Ratio of ABC to DEF: %s" % ratio) + else: + print("Cannot calculate ratio due to division by zero.") + + - print("Ratio %s" % getRatio(price, price)) diff --git a/server3.py b/server3.py index 1836de22ee5..67c87c98362 100644 --- a/server3.py +++ b/server3.py @@ -19,7 +19,6 @@ # DEALINGS IN THE SOFTWARE. import csv -# from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer import http.server import json import operator @@ -27,7 +26,6 @@ import re import threading from datetime import timedelta, datetime -# from itertools import izip from random import normalvariate, random from socketserver import ThreadingMixIn @@ -53,7 +51,6 @@ OVERLAP = 4 - ################################################################################ # # Test Data @@ -148,7 +145,7 @@ def order_book(orders, book, stock_name): def generate_csv(): """ Generate a CSV of order history. """ - with open('test.csv', 'wb') as f: + with open('test.csv', 'w') as f: # Fixed file mode from 'wb' to 'w' writer = csv.writer(f) for t, stock, side, order, size in orders(market()): if t > MARKET_OPEN + SIM_LENGTH: @@ -269,8 +266,8 @@ def _current_book_1(self): if REALTIME: while t > self._sim_start + (datetime.now() - self._rt_start): yield t, bids, asks - else: - yield t, bids, asks + + @property def _current_book_2(self):