Create a function

Assignment Help Computer Engineering
Reference no: EM131240228

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.

Reference no: EM131240228

Questions Cloud

Create a bubble chart of given data : Create a bubble chart of this data, with Home Value on the vertical axis, Population Density on the horizontal axis, and Median Income represented by the size of the bubble.
Explain what impact change in currency exchange rate have : Assume that the plant produced and sold product in the local economy. Explain what impact this change in the currency exchange rate would have on the project's internal rate of return.
What is the gage pressure at a stagnation point : What is the gage pressure at a stagnation point on the structure if it is at sea level where the air density is 1.23 kg/m3 ?
Explore the stanford prison experiment website : Select at least two (2) research methods from the text (e.g., observations, case studies, experiments, etc.), and explain each method's purpose in social psychology. Next, identify at least one (1) situation in which you could apply one (1) of the..
Create a function : 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
Explain the major ethical implications of the selected study : Describe research methods that are used for studying social psychology. Develop an increased understanding of the dynamic between society and individuals. Use technology and information resources to research issues in social psychology.
Determine the axial component of the anchoring force : Determine the axial component of the anchoring force required to hold the contraction in place.
Evaluate the magistrates court - discuss the ethical issues : Your individual field work activity will involve attending the Magistrates' Court of Victoria. The report will require you to research, review and comment on a case before the Magistrates' Court.
Illustrate the trend over the period with graph of the data : Find National Trade Data. Determine the trade balance between the U.S. and China for the most recent five year period. Illustrate the trend over this period with a graph of the data.

Reviews

len1240228

10/12/2016 7:36:38 AM

Please remember I need the entire console and all graphics. Last time I failed because it did not produce anything with the console. For the scripts created in assignment. The entire console needs to be displayed. So the graphics and the script can be seen.

Write a Review

Computer Engineering Questions & Answers

  Dns within a windows server 2008 environment

Write three pages about DNS and how we use DNS within a Windows Server 2008 environment. In your paper , please focus on the two items. First, focus on DNS and how it functions.

  Write down a c++ program for little man''s computer

A text file containing machine code (not assembly code) for little man's computer following instruction set . Instructions are in different lines (no need for semicolon at the end of each instruction)

  What could possibly be used as a key field

I need help with doing some web search research on relational databases and define briefly how such a database works, and basic components.

  Write a function userplay

Write a function playNovice(marbles) that given the number of marbles returns a random number between 1 and half of the number of marbles.

  What are the steps you have taken to maintain and redesign

what are the steps you have taken to maintain and redesign your site over the past several weeks? how is the process

  Construct a bcd-to-excess-three-code converter

Construct a BCD-to-excess-3-code converter with a 4-bit adder. Remember that the ex-cess-3 code digit is obtained by adding three to the corresponding BCD digit. What must be done to change the circuit to an excess-3-to-BCD-code converter?

  Express between declarative and procedural knowledge

Distinguish between declarative and procedural knowledge. Also give some examples of declarative knowledge and procedural knowledge.

  Choose two of the security features you recognize and write

third-party assurance providers such as bbbonline inc. and truste sell their services to businesses that want to

  We considered building a balanced or full bst from a sorted

we considered building a balanced or full bst from a sorted array. assume that the array has n 2k-1 elements in sorted

  Kinds of digital and logical evidence

State some of the kinds of the digital and logical evidence, and special content data investigators may require to gather during an actual cyber-terrorism case.

  Solves a system of linear equations

Write a function that solves a system of linear equations or calculates the inverse of a matrix by Gauss-Jordan elimination.

  Explain how css are used in developing websites

explain how CSS are used in developing websites.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd