Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial commit with all necessary changes #841

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
Binary file added changes.patch
Binary file not shown.
29 changes: 22 additions & 7 deletions client3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
9 changes: 3 additions & 6 deletions server3.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@
# DEALINGS IN THE SOFTWARE.

import csv
# from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
import http.server
import json
import operator
import os.path
import re
import threading
from datetime import timedelta, datetime
# from itertools import izip
from random import normalvariate, random
from socketserver import ThreadingMixIn

Expand All @@ -53,7 +51,6 @@

OVERLAP = 4


################################################################################
#
# Test Data
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down