Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Assignment #3

CSCI 201 Spring 2024

Title

JoesStocks v2.0

Topics Covered

Networking

Multi-Threading

Concurrency Issues

API Querying

Introduction

After a stressful and sweaty pitch, the Council of SAL is satisfied with your trade-scheduling prototype demo application and has unanimously decided to give you the green light to fully implement your application. This involves pulling real data from FINNHUB API, issuing trades from JoesStocks trading platform to traders in real time over the network, and chaining trades for traders, much like some other stock   trade application whose name shall not be mentioned. A successful implementation will gain you 5 points and possibly a good word from the director.

FINNHUB API

In addition to reading in data from two hard-coded csv files, you will use the FINNHUB API to retrieve company stock prices. In other words, the company stock price should be pulled from this API, and you will only have to read from the csv files for the schedule of trades and initial trader balances. You can learn more about the FINNHUB API here:https://finnhub.io/docs/api/quote . The data will be returned in JSON format.

Please keep in mind that there are minutes and seconds limits for the FINNHUB API calls, so do not wait to start your assignment until the last day. When you exceed those limits, your API calls will be blocked. The limits for “Free” accounts are: 60 API calls/minute, and 30 API calls/second. You will also need to generate an API Key, so start the assignment early to allow enough time for testing.

Assignment

In this assignment, you will create two different programs - a server and a client. You will implement a networked delivery system where clients (which represent traders)   receive trades from the server (which represents the trading platform), and the traders will complete the trades of the appropriate company stocks.

There will be some concurrency issues since traders will have to wait on other traders before starting the trade. There will be many ways to design the program, so  it would be wise to spend some time designing the program before you begin coding.

Like the previous assignment, here is a sample schedule.csv file:

0,AAPL,2

0,AAPL,-1

0,AMD,5

3,MSFT,-1

3,MSFT,-2

9,MSFT,-1

9,MSFT,1

9,TSLA,2

12,TSLA,10

12,TSLA,-3

A description of the fields in each line of the schedule.csv file is provided in the previous assignment.

Aside from the schedule.csv file, you will need to read in a second file named traders.csv, also in CSV format. Below is a layout of traders.csv, which contains information about the traders:

1,1000

2,2000

On each line of the CSV file, you will have the following fields:

●   The first field indicates the serial number of the trader, an integer.

●   The second field indicates the initial starting balance of the trader. The initial starting balance should also be an integer, with no fractional part.

The program should check for “validity” of both CSV files as in the previous assignment:

1) each line is in the correct CSV format: comma separated strings.

2) all values listed in the spec are present.

3) the type of each value in the CSV file is also correct.

The details of the FINNHUB API API Key, the Quote endpoint and the response can  be found in an additional document provided on D2L, Finnhub API Documentation.

Server Functionality

When your server first runs, prompt the user for the name of the schedule file. Then prompt the user for the name of the trader’s file. The number of traders dispatched will be equal to the number of records in the traders.csv file. For example, if there are 2 records corresponding to 2 traders in the traders.csv file that means the server will  not send out any orders until 2 clients (i.e., 2 traders) have connected to the server. Afterwards, the server should begin listening on port 3456 for client connections.

Every time a client connects to the server, verify that the connection was made by printing an output from the server, like the sample output below.

Client Functionality

The client will begin by welcoming the user to the program and will prompt the user for the server hostname and port. If a valid connection is made, the program should let the user know how many more traders are needed before the trades are started. For example, in the case when there are 2 records in the traders.csv file, if there is currently only 1 connection, the client should print a message saying that 1 more trader is needed before the trades can be started.

Once all traders have connected, the trades are initiated, and the client will be responsible for completing the trade of the appropriate company stocks. The client  should also be handling the returned data from the API calls to determine the stock prices of each company's stock.

Program Execution

Unlike the previous homework assignment, traders can take up and be responsible for more than one trade at a time. Additionally, traders can take up and be responsible for trades of multiple company stocks at a time.

At any given time, a trader should take up as many trades as possible, provided that the sum obtained on totalling the money required for executing the purchase trades  is less than or equal to the current balance of the trader. This means that if there are 5 trades that all have the same timestamp and all are purchase trades of value $100 each, a trader having a current remaining balance of $400 would be able to pick up and be responsible for only 4 of those 5 trades. If there are available traders, trades should be assigned promptly. If there are no available traders, the trade request will  remain in the queue until a trader completes their trade. Once a trader completes, the trader should pick up as many of the queued trade requests (with respect to the current time) as are permissible under that trader’s balance limit.

Points to keep in mind:

1.  The execution of a purchase trade should result in the reduction of the

current balance of the trader who executed that trade, by an amount equal to the cost of that purchase trade.

2.  The execution of a selling trade should result in no changes to the current  balance of the trader. In particular, the selling gain will not increase the value  of the balance. You could consider the initial balance of each trader as a limit that they should not cross for total stock purchases.

3.  All sales are unrelated to the trader’s stock portfolio. That is, any trader is assumed to have stock at hand to sell for any company.

4.  If at a point in time there are multiple free traders, waiting for trades to be

assigned to them, the assignment of trades should be done in the ascending order of the serial numbers of the traders.

5.  The program should sleep for 1 second for each trade (buy or sell) that is being executed.

6.  The trades should be assigned in the order they are present in the schedule.csv file.

7.  Consider the situation where the following trade is not executed because one of the traders does not have enough balance:

8.

12, TSLA, 10

At the end you need to report incomplete trades in the following format:

[Timestamp] Incomplete Trade: (StartTime, Ticker,

Quantity, Date & Time of Trade)

For example:

[00:00:13.001] Incomplete Trade: (12, TSLA, 10,

02/03/2024 00:39:08)

If there are no incomplete trades, then print: Incomplete trades: NONE

9.  At the end also print the Total Profit from Sales by each client (trader), which is the sum of the sales that the trader did, and the Total Cost from

Purchases, which is the sum of the costs of all successful purchases.

10.Timestamps should start from all 0s, for hours, minutes and seconds. You can use the same code from the previous assignment to format timestamps.

11.After processing is complete, all programs, server and clients should terminate.

Please follow the sample executions below and duplicate the output format.

Sample Executions

Schedule1.csv

Server

What is the name of the schedule file?

schedule1.csv

The schedule file has been properly read.

What is the name of the traders file?

traders.csv

The traders file has been properly read.

Listening on port 3456.

Waiting for traders...

Connection from: /127.0.0.1

Waiting for 1 more trader(s)...

Connection from: /127.0.0.1

Starting service.

Processing complete.

Client 1

Welcome to JoesStocks v2.0!

Enter the server hostname:

localhost

Enter the server port:

3456

1 more trader is needed before the service can begin.

Waiting...

All traders have arrived!

Starting service.

[00:00:00.006] Assigned purchase of 2 stock(s) of AAPL. Total cost estimate = 185.85 * 2 = 371.70. [00:00:00.008] Assigned sale of 1 stock(s) of AAPL. Total gain estimate = 185.85 * 1 = 185.85.

[00:00:00.008] Starting purchase of 2 stock(s) of AAPL. Total cost = 185.85 * 2 = 371.70.

[00:00:01.013] Finished purchase of 2 stock(s) of AAPL.

[00:00:01.014] Starting sale of 1 stock(s) of AAPL. Total gain = 185.85 * 1 = 185.85.

[00:00:02.016] Finished sale of 1 stock(s) of AAPL.

[00:00:02.996] Assigned sale of 1 stock(s) of MSFT. Total gain estimate = 411.22 * 1 = 411.22.

[00:00:02.997] Assigned sale of 2 stock(s) of MSFT. Total gain estimate = 411.22 * 2 = 822.44.

[00:00:02.998] Starting sale of 1 stock(s) of MSFT. Total gain = 411.22 * 1 = 411.22.

[00:00:03.998] Finished sale of 1 stock(s) of MSFT.

[00:00:03.999] Starting sale of 2 stock(s) of MSFT. Total gain = 411.22 * 2 = 822.44.

[00:00:05.004] Finished sale of 2 stock(s) of MSFT.

[00:00:08.996] Assigned sale of 1 stock(s) of MSFT. Total gain estimate = 411.22 * 1 = 411.22.

[00:00:08.997] Assigned purchase of 1 stock(s) of MSFT. Total cost estimate = 411.22 * 1 = 411.22. [00:00:08.997] Starting sale of 1 stock(s) of MSFT. Total gain = 411.22 * 1 = 411.22.

[00:00:10.001] Finished sale of 1 stock(s) of MSFT.

[00:00:10.002] Starting purchase of 1 stock(s) of MSFT. Total cost = 411.22 * 1 = 411.22.

[00:00:11.004] Finished purchase of 1 stock(s) of MSFT.

[00:00:11.996] Assigned sale of 3 stock(s) of TSLA. Total gain estimate = 187.91 * 3 = 563.73.

[00:00:11.996] Starting sale of 3 stock(s) of TSLA. Total gain = 187.91 * 3 = 563.73.

[00:00:13.001] Finished sale of 3 stock(s) of TSLA.

[00:00:13.002] Incomplete Trades: (12, TSLA, 10, 02/04/2024 03:16:05)

Total Profit Earned from Sales: $2394.46.

Total Cost from Purchases: $782.92.

Processing complete.

Client 2

Welcome to JoesStocks v2.0!

Enter the server hostname:

localhost

Enter the server port:

3456

All traders have arrived!

Starting service.

[00:00:00.005] Assigned purchase of 5 stock(s) of AMD. Total cost estimate = 177.66 * 5 = 888.30.

[00:00:00.007] Starting purchase of 5 stock(s) of AMD. Total cost = 177.66 * 5 = 888.30.

[00:00:01.009] Finished purchase of 5 stock(s) of AMD.

[00:00:08.980] Assigned purchase of 2 stock(s) of TSLA. Total cost estimate = 187.91 * 2 = 375.82. [00:00:08.981] Starting purchase of 2 stock(s) of TSLA. Total cost = 187.91 * 2 = 375.82.

[00:00:09.985] Finished purchase of 2 stock(s) of TSLA.

[00:00:11.981] Incomplete Trades: (12, TSLA, 10, 02/04/2024 03:16:05)

Total Profit Earned from Sales: $0.00.

Total Cost from Purchases: $1264.12.

Processing complete.

Schedule2.csv

Server

What is the name of the schedule file?

schedule2.csv

The schedule file has been properly read.

What is the name of the traders file?

traders.csv

The traders file has been properly read.

Listening on port 3456.

Waiting for traders...

Connection from: /127.0.0.1

Waiting for 1 more trader(s)...

Connection from: /127.0.0.1

Starting service.

Processing complete.

Client 1

Welcome to JoesStocks v2.0!

Enter the server hostname:

localhost

Enter the server port:

3456

1 more trader is needed before the service can begin.

Waiting...

All traders have arrived!

Starting service.

[00:00:00.006] Assigned purchase of 2 stock(s) of AAPL. Total cost estimate = 185.85 * 2 = 371.70. [00:00:00.008] Assigned sale of 1 stock(s) of AAPL. Total gain estimate = 185.85 * 1 = 185.85.

[00:00:00.008] Starting purchase of 2 stock(s) of AAPL. Total cost = 185.85 * 2 = 371.70.

[00:00:01.011] Finished purchase of 2 stock(s) of AAPL.

[00:00:01.012] Starting sale of 1 stock(s) of AAPL. Total gain = 185.85 * 1 = 185.85.

[00:00:02.013] Finished sale of 1 stock(s) of AAPL.

[00:00:02.997] Assigned sale of 1 stock(s) of MSFT. Total gain estimate = 411.22 * 1 = 411.22.

[00:00:02.998] Assigned sale of 2 stock(s) of MSFT. Total gain estimate = 411.22 * 2 = 822.44.

[00:00:02.998] Starting sale of 1 stock(s) of MSFT. Total gain = 411.22 * 1 = 411.22.

[00:00:04.001] Finished sale of 1 stock(s) of MSFT.

[00:00:04.001] Starting sale of 2 stock(s) of MSFT. Total gain = 411.22 * 2 = 822.44.

[00:00:05.003] Finished sale of 2 stock(s) of MSFT.

[00:00:08.997] Assigned sale of 1 stock(s) of MSFT. Total gain estimate = 411.22 * 1 = 411.22.

[00:00:08.997] Assigned purchase of 1 stock(s) of MSFT. Total cost estimate = 411.22 * 1 = 411.22. [00:00:08.998] Starting sale of 1 stock(s) of MSFT. Total gain = 411.22 * 1 = 411.22.

[00:00:10.001] Finished sale of 1 stock(s) of MSFT.

[00:00:10.002] Starting purchase of 1 stock(s) of MSFT. Total cost = 411.22 * 1 = 411.22.

[00:00:11.006] Finished purchase of 1 stock(s) of MSFT.

[00:00:11.996] Assigned sale of 1 stock(s) of TSLA. Total gain estimate = 187.91 * 1 = 187.91.

[00:00:11.997] Assigned sale of 3 stock(s) of TSLA. Total gain estimate = 187.91 * 3 = 563.73.

[00:00:11.997] Starting sale of 1 stock(s) of TSLA. Total gain = 187.91 * 1 = 187.91.

[00:00:13.002] Finished sale of 1 stock(s) of TSLA.

[00:00:13.002] Starting sale of 3 stock(s) of TSLA. Total gain = 187.91 * 3 = 563.73.

[00:00:14.008] Finished sale of 3 stock(s) of TSLA.

[00:00:14.009] Incomplete Trades: NONE

Total Profit from Sales: $2582.37.

Total Cost from Purchases: $782.92.

Processing complete.

Client 2

Welcome to JoesStocks v2.0!

Enter the server hostname:

localhost

Enter the server port:

3456

All traders have arrived!

Starting service.

[00:00:00.005] Assigned purchase of 5 stock(s) of AMD. Total cost estimate = 177.66 * 5 = 888.30. [00:00:00.006] Starting purchase of 5 stock(s) of AMD. Total cost = 177.66 * 5 = 888.30.

[00:00:01.008] Finished purchase of 5 stock(s) of AMD.

[00:00:08.978] Assigned purchase of 2 stock(s) of TSLA. Total cost estimate = 187.91 * 2 = 375.82. [00:00:08.978] Starting purchase of 2 stock(s) of TSLA. Total cost = 187.91 * 2 = 375.82.

[00:00:09.982] Finished purchase of 2 stock(s) of TSLA.

[00:00:11.979] Incomplete Trades: NONE

Total Profit from Sales: $0.00.

Total Cost from Purchases: $1264.12.

Processing complete.

Schedule3.csv

Server

What is the name of the schedule file?

schedule3.csv

The schedule file has been properly read.

What is the name of the traders file?

traders.csv

The traders file has been properly read.

Listening on port 3456.

Waiting for traders...

Connection from: /127.0.0.1

Waiting for 1 more trader(s)...

Connection from: /127.0.0.1

Starting service.

Processing complete.

Client 1

Welcome to JoesStocks v2.0!

Enter the server hostname:

localhost

Enter the server port:

3456

1 more trader is needed before the service can begin.

Waiting...

All traders have arrived!

Starting service.

[00:00:00.006] Assigned purchase of 1 stock(s) of NVDA. Total cost estimate = 661.60 * 1 = 661.60. [00:00:00.008] Assigned sale of 1 stock(s) of NVDA. Total gain estimate = 661.60 * 1 = 661.60.

[00:00:00.009] Starting purchase of 1 stock(s) of NVDA. Total cost = 661.60 * 1 = 661.60.

[00:00:01.010] Finished purchase of 1 stock(s) of NVDA.

[00:00:01.010] Starting sale of 1 stock(s) of NVDA. Total gain = 661.60 * 1 = 661.60.

[00:00:02.013] Finished sale of 1 stock(s) of NVDA.

[00:00:02.998] Assigned sale of 1 stock(s) of MSFT. Total gain estimate = 411.22 * 1 = 411.22.

[00:00:02.998] Assigned sale of 2 stock(s) of MSFT. Total gain estimate = 411.22 * 2 = 822.44.

[00:00:02.999] Starting sale of 1 stock(s) of MSFT. Total gain = 411.22 * 1 = 411.22.

[00:00:04.002] Finished sale of 1 stock(s) of MSFT.

[00:00:04.002] Starting sale of 2 stock(s) of MSFT. Total gain = 411.22 * 2 = 822.44.

[00:00:05.008] Finished sale of 2 stock(s) of MSFT.

[00:00:08.998] Assigned sale of 1 stock(s) of NVDA. Total gain estimate = 661.60 * 1 = 661.60.

[00:00:08.998] Starting sale of 1 stock(s) of NVDA. Total gain = 661.60 * 1 = 661.60.

[00:00:10.003] Finished sale of 1 stock(s) of NVDA.

[00:00:11.998] Assigned purchase of 1 stock(s) of TSLA. Total cost estimate = 187.91 * 1 = 187.91. [00:00:11.998] Assigned sale of 3 stock(s) of TSLA. Total gain estimate = 187.91 * 3 = 563.73.

[00:00:11.998] Starting purchase of 1 stock(s) of TSLA. Total cost = 187.91 * 1 = 187.91.

[00:00:13.003] Finished purchase of 1 stock(s) of TSLA.

[00:00:13.004] Starting sale of 3 stock(s) of TSLA. Total gain = 187.91 * 3 = 563.73.

[00:00:14.008] Finished sale of 3 stock(s) of TSLA.

[00:00:14.008] Incomplete Trades: NONE

Total Profit from Sales: $3120.59.

Total Cost from Purchases: $849.51.

Processing complete.

Client 2

Welcome to JoesStocks v2.0!

Enter the server hostname:

localhost

Enter the server port:

3456

All traders have arrived!

Starting service.

[00:00:00.003] Assigned purchase of 1 stock(s) of NVDA. Total cost estimate = 661.60 * 1 = 661.60. [00:00:00.005] Starting purchase of 1 stock(s) of NVDA. Total cost = 661.60 * 1 = 661.60.

[00:00:01.009] Finished purchase of 1 stock(s) of NVDA.

[00:00:08.982] Assigned purchase of 1 stock(s) of MSFT. Total cost estimate = 411.22 * 1 = 411.22. [00:00:08.982] Assigned purchase of 2 stock(s) of TSLA. Total cost estimate = 187.91 * 2 = 375.82.

[00:00:08.983] Starting purchase of 1 stock(s) of MSFT. Total cost = 411.22 * 1 = 411.22.

[00:00:09.987] Finished purchase of 1 stock(s) of MSFT.

[00:00:09.987] Starting purchase of 2 stock(s) of TSLA. Total cost = 187.91 * 2 = 375.82.

[00:00:10.991] Finished purchase of 2 stock(s) of TSLA.

[00:00:11.982] Incomplete Trades: NONE

Total Profit from Sales: $0.00.

Total Cost from Purchases: $1448.64.

Processing complete.

Grading Criteria

The way you go about implementing the solution is not specifically graded, but the output must match exactly what you see in the executions above. The maximum  number of points earned is 5.

Networking (1.0)

0.2 The first client can connect to the server.

0.3 Only the number of clients, which is determined by the number of records in

traders.csv, can connect to the server.

0.5 Server output is correct.

Data I/O (1.0)

0.3 The schedule file and traders file are read appropriately.

0.2 Data is parsed from the Finnhub API

0.5 Client output is correct.

Program Execution (3.0)

1.0 The order of stock buy/sell execution is correct.

1.0 The timing of stock buy/sell execution is correct.

1.0 Stock orders are executed as expected with no exceptions, crashing, deadlock, starvation, or freezing.