Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a question on creating a code for the below specifications. I have a working code shown below at the bottom of the question.

I have a question on creating a code for the below specifications. I have a working code shown below at the bottom of the question. Thank you in advance.

Table of Contents

Source Code and other Submission Filenames: 1

project2.c 1

Policies 2

Submission Instructions 3

Goals (Requirements): 4

Background 4

Preparation 4

Functional Specifications 5

Design 5

Simulator Design 5

void initializeSimulator(char* filename, StockPrice* pMyStocks, int* pMyStocksSize); 6

StockPrice* findStockPrice(StockPrice* pMyStocks, int myStocksSize,char* ticker); 6

double priceSimulator(StockPrice* pMyStocks, int myStocksSize, char* ticker); 6

Trading Design 6

void initializePortfolio(Portfolio* myPortfolio, double balance); 7

void savePortfolio(char* filename, StockPortfolio* pMyPortfolio); 7

void readPortfolio(char* filename, Portfolio* pMyPortfolio); 7

void printStock(Stock* pStock); 8

void printPortfolio(Portfolio* pMyPortfolio); 8

double getBalance(Portfolio* pMyPortfolio); 8

void buy(StockPrice* pMyStocks, Portfolio* pMyPortfolio, char* ticker, double shares); 8

void sell(StockPrice* pMyStocks, Portfolio* pMyPortfolio, char* ticker, double shares); 8

void saveStockPrices(char* filename, StockPrice* pMyStocks, int myStocksSize); 8

Implementation 9

FAQ about this Project: 9

Goals (Requirements):

You will simulate a trading application. There will be two components: A simulation for the stock market, and a facility that will allow you to trade five stocks several times. It will also maintain the portfolio and balances of your stock and money.

Background

We buy and sell stocks[1] from the Stock Market. You can check the price of the stock before you place an order to buy or sell. If you place an order within a short period of time after that, it is very likely the the transaction will take place at that price.

Investors and traders hope to be able to buy stocks at a low price and sell at higher price so that they make a profit.

On the Stock Market, price of a stock fluctuates unpredictably. In this project you will simulate these price fluctuations using a random number generator.

To provide you with the service of buying and selling, the market charges a small fee for every traded. For the sake of simplification, we will ignore that fee in this project.

Preparation

Create a mystocks.txt file that contains todays stock price[1] [2] for five stocks that you want to trade. Pick them from this list.

Example: my mystocks.txt looks like:

AMD 16.61

CLNE 2.92

FEYE 16.47

EXTR 8.61

FSLR 54.83

Functional Specifications

1. Read your stocks from your mystocks.txt file. You can use this information for the simulator.

2. The stock price simulator will return the current price of the stock. The current prices is the prices of the requested ticker + a random number.

3. We will assume that the stock price remains constant after the price check till your trade gets executed. Therefore, a buy or a sell order placed will be assumed to be executed at the price returned at the the time of price check (called quote).

4. The application will be able to facilitate purchase and sale of a stock.

5. The application will be user oriented and will keep a record of the five stocks the user owns, allow the user to buy more shares, and sell shares of only those stocks.

6. The application will also maintain the balance of money the user has deposited from which to buy stocks. Initial balance can be $20,000. When the user buys shares, the cost is taken out of this balance. When the user sells, the proceeds are returned to this balance.

7. The application should save all the information in a file called portfolio.txt when the application closes it saves. It loads the information from that same file when the application starts if the file exists. File does not exist, the application will assume that the user has never traded before.

8. Test your functions to make sure that you can make at least 20 trades.

Design

Simulator Design

/*- When you read the mystocks.txt you should store the contents in an array of a struct called StockPrice. StockPrice struct has two components: ticker symbol and its price that are both read from the file.*/

typedef struct{

char ticker[10];

double price;

} StockPrice;

/*- The five StockPrices will be stored in an array called myStocks[].

This array is used to run the market simulator. This array will be initialized by the function: */

void initializeSimulator(char* filename, StockPrice* pMyStocks, int* pMyStocksSize);

(F.K.A: void readStockPrices(char* filename, StockPrice* pMyStocks, int* pMyStocksSize); )

/*This function opens the file containing ticker and base price data for the stocks (test() function will set this filename to be mystocks.txt).

The priceSimulator() function take a ticker symbol and the myStocks[] array (along with its size) as input parameters and returns a modified price of that particular the ticker symbol.*/

StockPrice* findStockPrice(StockPrice* pMyStocks, int myStocksSize,char* ticker);

//This function finds the stock corresponding to the ticker symbol provided and return the pointer to that StockPrice struct.

double priceSimulator(StockPrice* pMyStocks, int myStocksSize, char* ticker);

//Alternatively:

void priceSimulator(StockPrice* pMyStocks, int myStocksSize, char* ticker, double* pNewPrice);

/*This function allows user to obtain the current price of a specific stock by typing in the ticker. The new price is also saved in the myStocks for that stock. This function can only be called after a call to initalizeSimulator() specified above.*/

Trading Design

//Each stock will be maintained by the application as a struct, that contains the number of shares the user owns for that stock, and the ticker symbol.

typedef struct{

char ticker[10];

double shares;

} Stock;

//The application is a single user application, so we need one struct to maintain all the stocks that user trades and the balance.

typedef struct{

Stock myStocks[MAX_MY_STOCKS]; // MAX_MY_STOCKS is five.

double balance;

} Portfolio;

void initializePortfolio(Portfolio* myPortfolio, double balance,int capacity);

*/This function will read the mystocks.txt file and populate the portfolio with the provided balance and the proper ticker symbols for all the stocks but the shares are all set to 0.0. In other words, this function sets up an account,

[3] */

void savePortfolio(char* filename, StockPortfolio* pMyPortfolio);

*/This function saves the portfolio into the file name (will be set as portfolio.txt by the test() function).

upon user exit, save the current portfolio in the txt file.*/

//Propose functions for trading and other desirable actions that a user will have to take.

void readPortfolio(char* filename, Portfolio* pMyPortfolio);

//Reads the previous trading info from portfolio.txt. If the file does not exist, error is reported.

void printStock(Stock* pStock);

//Prints a single stock: Ticker and shares.

void printPortfolio(Portfolio* pMyPortfolio);

//Prints the portfolio: all stocks and balances

double getBalance(Portfolio* pMyPortfolio);

//Returns previous balance.

void buy(StockPrice* pMyStocks, Portfolio* pMyPortfolio, char* ticker, double shares);

/*The functions purchases shares number of shares of the stock indicated by the ticker stock provided that there is enough balance. The total purchase price is deducted from the balance. This function should only be invoked after invoking priceSimulator().*/

void sell(StockPrice* pMyStocks, Portfolio* pMyPortfolio, char* ticker, double shares);

/*The functions sells the shares number of shares of the stock indicated by the ticker stock provided that there are enough shares to sell. The total proceeds are added to the balance. This function should only be invoked after invoking priceSimulator().*/

void saveStockPrices(char* filename, StockPrice* pMyStocks, int myStocksSize);

*/Saves the new stock prices in the mystocks.txt file. This is optional because even if we dont save the new price, each time the priceSimulate() will give a different value based on the original price. However, to make it more realistic, each new simulation should be based on the previous simulation, instead of a static starting value.*/

//Below is the code I have succesfully compiled and executed so far. Thank you for your help! (Ansi-Style in C).

//Update: Here is current code. The functions for the portfolio and trading stocks need to be implemented, I have those prototyped and ready to work on in the code above the test function. Those also need to be invoked inside the test function.

#include #include #include #include

#define RANGE 10 #define MAX_MY_STOCKS 5

typedef struct { char ticker[10]; double price; } StockPrice;

typedef struct { char ticker[10]; double shares; } Stock;

typedef struct { Stock myStocks[MAX_MY_STOCKS]; // MAX_MY_STOCKS is five. double balance; } Portfolio;

//Prototyping

void initializeSimulator(char* fileName, StockPrice* pMyStocks, int* pMyStocksSize,int capacity); StockPrice* findStockPrice(StockPrice* pMyStocks, int myStocksSize,char* ticker); double priceSimulator(StockPrice* pMyStocks, int myStocksSize, char* ticker); void initializePortfolio(char* fileName, Portfolio* pMyPortfolio, double balance,int capacity); void savePortfolio(char* filename, Portfolio* pMyPortfolio,int myStocksSize); void readPortfolio(char* filename, Portfolio* pMyPortfolio); void printStock(Stock* pStock); void printPortfolio(Portfolio* pMyPortfolio,int myStocksSize); double getBalance(Portfolio* pMyPortfolio); void buy(StockPrice* pMyStocks, Portfolio* pMyPortfolio, char* ticker, double shares,int myStocksSize); void sell(StockPrice* pMyStocks, Portfolio* pMyPortfolio, char* ticker, double shares,int myStocksSize); void saveStockPrices(char* filname, StockPrice* pMyStocks, int myStocksSize);

//Begin Hard Coding void initializeSimulator(char* fileName, StockPrice* pMyStocks, int* pMyStocksSize,int capacity) { FILE* pFile =0; pFile = fopen(fileName,"r"); *pMyStocksSize =0; if(pFile==NULL) { printf("File did not open "); return; }

while(fscanf(pFile,"%s %lf",pMyStocks[(*pMyStocksSize)].ticker,&(pMyStocks[(*pMyStocksSize)].price))!=EOF) { (*pMyStocksSize)++; if (*pMyStocksSize>=capacity) return; }

if(pFile) fclose(pFile); }

double priceSimulatorInternal(double price) { double perturbation=1/price; double multiplier; multiplier = ((double) rand())/RAND_MAX*perturbation; multiplier -= perturbation/2; multiplier += 1; return multiplier*price;

}

//This function finds the stock corresponding to the ticker symbol provided and //returns the pointer to that StockPrice struct.

StockPrice* findStockPrice(StockPrice* pMyStocks, int myStocksSize,char* ticker) {

int i; for(i=0; i

//This function allows user to obtain the current price of a specific stock by //typing in the ticker. The new price is also saved in the myStocks for that stock. //This function can only be called after a call to initalizeSimulator() specified above.

double priceSimulator(StockPrice* pMyStocks, int myStocksSize, char* ticker) { StockPrice* pStockPrice; double newPrice; pStockPrice = findStockPrice(pMyStocks, myStocksSize,ticker); newPrice = priceSimulatorInternal(pStockPrice->price);

pStockPrice->price = newPrice; return newPrice; }

void printStockPrices(StockPrice* pMyStocks, int myStocksSize) { int i; printf(" myStocks[] "); for( i=0; i

//Portfolio Coding

void initializePortfolio(char* fileName, Portfolio* pMyPortfolio, double balance,int capacity) void savePortfolio(char* filename, Portfolio* pMyPortfolio,int myStocksSize) void readPortfolio(char* filename, Portfolio* pMyPortfolio) void printStock(Stock* pStock) void printPortfolio(Portfolio* pMyPortfolio,int myStocksSize) double getBalance(Portfolio* pMyPortfolio) void buy(StockPrice* pMyStocks, Portfolio* pMyPortfolio, char* ticker, double shares,int myStocksSize) void sell(StockPrice* pMyStocks, Portfolio* pMyPortfolio, char* ticker, double shares,int myStocksSize) void saveStockPrices(char* filname, StockPrice* pMyStocks, int myStocksSize)

void test() { int i; double p = 390.39; double price;

StockPrice myStocks[MAX_MY_STOCKS]; int myStocksSize;

StockPrice* pStockPrice;

double newPrice;

srand(time(0));

//Test for initializeSimulator() initializeSimulator("mystocks.txt", myStocks, &myStocksSize,MAX_MY_STOCKS); for( i=0; i

//Testing findStockPrice() printf(" Testing findStockPrice() "); pStockPrice = findStockPrice(myStocks, myStocksSize,"MITK"); if(pStockPrice) { printf("Ticker=%s; Price=%lf ",pStockPrice->ticker,pStockPrice->price); } else { printf("Ticker Not found "); }

//Testing findStockPrice() printf(" Testing findStockPrice() "); pStockPrice = findStockPrice(myStocks, myStocksSize,"NESR"); if(pStockPrice) { printf("Ticker=%s; Price=%lf ",pStockPrice->ticker,pStockPrice->price); } else { printf("Ticker Not found "); }

//Testing priceSimulator() printf(" Testing priceSimulator() "); for( i=0; i<6; i++) { //multiplier = ((double) rand())/RAND_MAX*pertubation -pertubation/2+1; //price = priceSimulatorInternal(p); newPrice = priceSimulator(myStocks,myStocksSize, "MITK"); printf(" -->%lf ",newPrice); printStockPrices(myStocks,myStocksSize); }

//Testing priceSimulator() printf(" Testing priceSimulator() "); for( i=0; i<6; i++) { //multiplier = ((double) rand())/RAND_MAX*pertubation -pertubation/2+1; //price = priceSimulatorInternal(p); newPrice = priceSimulator(myStocks,myStocksSize, "NESR"); printf(" -->%lf ",newPrice); printStockPrices(myStocks,myStocksSize); } //Printing myStocks[] printStockPrices(myStocks,myStocksSize); }

int main(void) { test(); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 15th International Conference Dasfaa 2010 International Workshops Gdm Benchmarx Mcis Snsmw Diew Udm Tsukuba Japan April 2010 Revised Selected Papers Lncs 6193

Authors: Masatoshi Yoshikawa ,Xiaofeng Meng ,Takayuki Yumoto ,Qiang Ma ,Lifeng Sun ,Chiemi Watanabe

2010th Edition

3642145884, 978-3642145889

More Books

Students also viewed these Databases questions

Question

I want to do this page in pho and html and css

Answered: 1 week ago

Question

4. What are the current trends in computer software platforms?

Answered: 1 week ago