Ask Computer Engineering Expert

For the scripts created below. The entire console needs to be displayed. So the graphics and the script can be seen.

Create a function. The creation of a function is an iterative process. The first step of that process (explained in #3 below) is to get the code working as standard R code with all of the values hard coded. After the process has been successfully tested with the hard coded values, convert the process to a function and replace the desired hard coded values with function parameters. You may want to delay sending output to PDF until you have your manual process working as desired.

An exponentially weighted moving average (EMA) is frequently used in the finance industry to form a stock price trend indicator while smoothing price action and filtering out noise. EMA for a given time period is calculated using the following formula:

EMAi = (Pi x α) + (EMAi-1 x (1- α)) where α = 2/1+N

P is the closing price at the end of that time period and N is the number of periods to include in the moving average. For those who may not know, the "a" looking character is the Greek letter alpha.

Along with the header, your R script will include two major sections. The first section will graph the EMA of stock prices (adjusted closing price) using hard coded values. The second section will contain the code to create the function, the appropriate graphics setup, and two calls to the function. The console and PDF output should represent only the final submission of your R script after restarting R.

1. The file cisco.csv contains stock price information for every day since Cisco stock started trading publicly. Download the file from eCampus and become familiar with its contents. Write a line of code that reads this file into a data frame in your R session for use in this assignment.

2. When you define your PDF document and open the device, set the size to 11 X 8.5 inches to match the size of a normal letter size sheet of paper turned so that it is wider than tall. (Research the help documentation on pdf to determine the correct options to use.)

3. Create and plot a 30 Day Exponential Moving Average with these steps:

a. Go ahead and assign a value for the alpha component of the formula so the EMA code does not get too long later on. If you wish you may also assign a scalar the value of 30 for our N value and use it in your code since this will be an argument to the function later in the assignment.

b. Create a vector populated with zeroes that will be used to contain the EMA values. The length of this vector needs to be the same as the length of the vector of adjusted closing prices from the Cisco data.

c. One of the problems with moving averages is that they rely on a previous value and, at some point in the beginning, there is no previous value. We are going to solve this problem in this instance by populating the EMA on day 30 with the simple average adjusted closing prices of the first 30 days in which Cisco stock was traded. Create an assignment statement that puts the 30 day average in the 30th member of the EMA vector.

d. Use a loop to run the EMA formula repetitively from day 31 to the last day for which Cisco stock prices are available. Each iteration of the loop will use the corresponding day's adjusted closing price to calculate the EMA value for that day and store the calculated value in the appropriate place in the EMA vector.

e. There are roughly 260 weekdays in a year. Plot a line showing the last 260 EMA values. You may simply use the numbers 1 through 260 as the x values and then use the EMA values for y. This line should be purple. The range of the Y axis is to be 0 to 35. To make our program fully adaptive, we would need to use an algorithm to base the upper limit on the maximum stock price but this assignment is going to take a long time without adding this requirement so hard code the values of 0 and 35 for now. We will provide an argument to adjust this in the function. Set the title and axis labels as shown.

f. At the bottom of the graph, add the EMA formula as shown. Use the point 0, 2 as the reference point for the formula line.

g. Add to the graph a green2 line showing the actual adjusted closing prices for the last 260 days available in the Cisco data.

4. Convert the completed code to a function including the code that will plot the graph. The first argument to the function is to be the name of the vector containing the adjusted closing prices so that we can analyze data from any stock without having to rewrite our function. The second argument will be the number of days over which the EMA is computed. The default value will be 30 like we used above. Be sure to replace all references to the price vector and the number of days with the appropriate function argument. Note that any value in your original code that was based on the 30 day average will use the N argument or an algebraic expression based on N. The third argument will set the upper limit for the Y axis. Make the default 35 and make sure the graphing expressions use this argument. The graph title and formula line must be constructed so they automatically change according to the value of N used in the function call.

5. Set up your graphics for this section of the script so that there will be two columns of graphics on the output page. We want to set the overall margins of the graphics within the page to be 1.5 inches on the top to allow space for punched holes and 0.5 inches on the bottom, right, and left. Set the margins of the individual graph to be 2 lines on top, 4 lines left, 0 lines right and 4 lines bottom.

6. Call your function twice. The first call should have the reference to the adjusted closing price column as its only argument. For the second call, add a value of 100 for the N argument. (When you are testing your program, you should experiment with an N of 10 and a few other values to see how the size of the EMA affects the graph. But, do not include the results of these tests in your homework submission.)

7. Write the system time in the bottom outer margin of the page. Left justify the time as shown in the sample.

8. After you have finished writing and testing your code close R. Then reopen R and run your script. Submit to WebAssign the script, console, and output from this final run.

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M91996094
  • Price:- $90

Guranteed 48 Hours Delivery, In Price:- $90

Have any Question?


Related Questions in Computer Engineering

Does bmw have a guided missile corporate culture and

Does BMW have a guided missile corporate culture, and incubator corporate culture, a family corporate culture, or an Eiffel tower corporate culture?

Rebecca borrows 10000 at 18 compounded annually she pays

Rebecca borrows $10,000 at 18% compounded annually. She pays off the loan over a 5-year period with annual payments, starting at year 1. Each successive payment is $700 greater than the previous payment. (a) How much was ...

Jeff decides to start saving some money from this upcoming

Jeff decides to start saving some money from this upcoming month onwards. He decides to save only $500 at first, but each month he will increase the amount invested by $100. He will do it for 60 months (including the fir ...

Suppose you make 30 annual investments in a fund that pays

Suppose you make 30 annual investments in a fund that pays 6% compounded annually. If your first deposit is $7,500 and each successive deposit is 6% greater than the preceding deposit, how much will be in the fund immedi ...

Question -under what circumstances is it ethical if ever to

Question :- Under what circumstances is it ethical, if ever, to use consumer information in marketing research? Explain why you consider it ethical or unethical.

What are the differences between four types of economics

What are the differences between four types of economics evaluations and their differences with other two (budget impact analysis (BIA) and cost of illness (COI) studies)?

What type of economic system does norway have explain some

What type of economic system does Norway have? Explain some of the benefits of this system to the country and some of the drawbacks,

Among the who imf and wto which of these governmental

Among the WHO, IMF, and WTO, which of these governmental institutions do you feel has most profoundly shaped healthcare outcomes in low-income countries and why? Please support your reasons with examples and research/doc ...

A real estate developer will build two different types of

A real estate developer will build two different types of apartments in a residential area: one- bedroom apartments and two-bedroom apartments. In addition, the developer will build either a swimming pool or a tennis cou ...

Question what some of the reasons that evolutionary models

Question : What some of the reasons that evolutionary models are considered by many to be the best approach to software development. The response must be typed, single spaced, must be in times new roman font (size 12) an ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As