From 837f7383754b0d43d99b02d6f9912e7f60a08eeb Mon Sep 17 00:00:00 2001 From: Yash-Desai-1 Date: Thu, 3 Oct 2024 15:38:58 +0530 Subject: [PATCH] initial commit with all necessary changes --- changes.patch | Bin 0 -> 6492 bytes client3.py | 29 ++++++++++++++++++++++------- server3.py | 9 +++------ 3 files changed, 25 insertions(+), 13 deletions(-) create mode 100644 changes.patch diff --git a/changes.patch b/changes.patch new file mode 100644 index 0000000000000000000000000000000000000000..75bf0ab253c6ba310250d7460b1f10e7be07cfa4 GIT binary patch literal 6492 zcmd6r`%hFy6vyZ1CjB37BxWxGRvvBqR zE!w!BG|mhpW#6y$o`fH?=ZSvL!Z7Y*67ukSc&FAx zAGO zm!qh=cS3R?nuPkiT1{E#ZStiz?E`tUVio(Y+R1j*(6Kaxv+zPI9L81g!AW?d)yMielC2M9 zjV29fw$WQ(dG%xqRb4Da4AG=LM47CH8T+^!y=U{Xxh-6kyM*Ph-hX#Ci|YeX`ud7tN-t5^0{78#KV4wV82N^cF`>&8Pew3^+;`UNlzo_2Fkw1CTiO#@r_KA3#AufBW#CX zHDtvv2*w&1=m`?em_9{&j~m;P#BY5EBsYMCNwuk5|ky@ipo z$0Y1s7Odk(?H;`yYqwzahw#W`2m9JLeLeMp8LK0U#X8U|TM^?HM|yf=Wj>QkHLiJ$ zub-^YHTh@wtC$OV?GiteWMn|3GP%sA%XVQJ@OAv{HkW3)<))r-BjY-?zrRMbH@p3` z3ZUJNTpGiWS}B+RKbKuw z=7k>TC*eZ&Q1-<#iqrhk@=hn*4-d3%NAwoo6B78HR|p-|up8mwlzA4#o1W}CFUK-; zY(B9a_gJrX#vhA|GzEvcH=s}tCmSy~p?Z9_p=(yFT9e89k zxjl}WoNiInW_VhX=3~CnmmQFwzf3x;X`qK#ktvoE`>VE;)YA#ZqJUGznNvVZ!=#`M-@+W+^({kYJKccX!`jRR7XW6R6T#TjUYST z;pfk#u&y)TT5K;1i>|(J3ETDfe_Q*!>ijuvh4T4x+8jTBF6rz$uhlqx+V`5i88w_| z)XJjI-VaZd|F?81*ijE(S=bGa<;|ITp30|p!!L>g+v>6E&rc}Fs(#L->rr7pW8nq& zg#h=XXa}eIdLYX}-pAs!t26FS30tB{nM-}{w&s$T$@3F&uy{i?GSdFqINJWIT;GUx zB3Zjp4%chjOs^u_#;i^>pJ3O_alDs>wd1`kZO`jxc-MD0lnmTw5Wn9P+NerPUDdWe z^BJSY#M)jLvl7yNwxW{bq)=bm`?1p#t#9YXIu&czb?8++*?1VEPMtSZq-vAAhQF36>s+bzsVccWDf>cwh1~{bo09Tm5g2eFThC zHJDDreya4S3~&^<%2|wQ%MPiDLmaV^!J6Z@MvMaVz_rjlSXo_=-%R#{`M2vBqEY@ zZWh4(rNxJ&2_La@tJSG#lzRYT%&3SLc0M;$ea+_EDnlxx@ATV@8Sp3h^R6uEsr-2( zW=bl{9YvE(^*)Jxk2K?Pp)>agG6MN{U9;yDH`Zdzpk*_-?MB<^`@U@CEc!uDcN*Ni zOj<0qF0V;TjwYY)Mu|2)dWv-}7txYU|Gs0Q>WQ?})y}B$PU9P2w|vjebLqm)A)Z;f z^0}cj4aVH!;j@VklyGw{O;2=*roBH%68K@WW*>HIt9GJE{#WM(JyMwkOrPg; zpT6d_a|V%fuJ9L&qR#uCbWOcXTmYR9HLvn$M)cygiu}*%B1OzS*;9UrvLpI03#*0U literal 0 HcmV?d00001 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):