214k views
5 votes
# You work at a low latency trading firm and are asked to deliver the order book

data provided in order_book_data.txt to a superior
# The problem is that the data isn't formatted correctly. Please complete the
following steps to apropriately format the data
# Notice, the first column is a ticker, the second column is a date, the third
column is a Bid, the fourth column is an Ask, and the fifth column is a currency
type
# 1. Open order_book_data.txt
# 2. Remove the order book lines. i.e. ***** Order Book: ###### *****
# 3. Get rid empty lines
# 4. Get rid of spaces
# 5. Notice that there are two currencies in the order book; USD and YEN. Please
convert both the Bid and Ask price to USD (if not already)
# The Bid and Ask are the 3rd and 4th column, respectively
# 6. Create a header line Ticker, Date, Bid, Ask
# 7. Save the header line and propely formatted lines to a comma seperated value
file called mktDataFormat.csv

I'm stuck on how to one, remove the blank lines. And two properly apply the fx rate. I've attached what I've done so far.

# You work at a low latency trading firm and are asked to deliver the order book data-example-1
# You work at a low latency trading firm and are asked to deliver the order book data-example-1
# You work at a low latency trading firm and are asked to deliver the order book data-example-2

1 Answer

2 votes

Answer:

Step-by-step explanation:

To complete the task, follow these steps:

Open the file order_book_data.txt using a text editor or programming language.

Remove all the lines containing "Order Book" using regular expressions or string matching.

Remove all empty lines using regular expressions or string matching.

Remove all spaces using regular expressions or string matching.

Convert the Bid and Ask prices to USD if they are in YEN by using the exchange rate. For example, if 1 YEN is equivalent to 0.0094 USD, then the formula to convert from YEN to USD would be Bid/100 * 0.0094 and Ask/100 * 0.0094, assuming the Bid and Ask prices are in YEN per 100 units.

Create a header line with the columns Ticker, Date, Bid, Ask.

Write the header line and formatted lines to a comma-separated value (CSV) file called mktDataFormat.csv using a text editor or programming language.

Here is an example Python code that could be used to accomplish these steps:

import re

# Open the file

with open('order_book_data.txt', 'r') as f:

data = f.read()

# Remove Order Book lines

data = re.sub(r'\*+ Order Book: \d+ \*+', '', data)

# Remove empty lines

data = re.sub(r'\\\s*\\', '\\', data)

# Remove spaces

data = re.sub(r'\s', '', data)

# Convert YEN to USD

yen_to_usd_rate = 0.0094

data = re.sub(r'(\w+),(\d+),(\d+),(\d+),YEN', lambda m: f"{m.group(1)},{m.group(2)},{int(m.group(3))*yen_to_usd_rate:.4f},{int(m.group(4))*yen_to_usd_rate:.4f},USD", data)

# Create header line

header = 'Ticker,Date,Bid,Ask\\'

# Write formatted data to file

with open('mktDataFormat.csv', 'w') as f:

f.write(header)

f.write(data)

Note that this is just one possible implementation, and there are many other ways to achieve the same result using different programming languages or text ed

User VKolev
by
8.3k points