Getting Data and Calculating Returns

Now, time to download stock data! Use the function getSymbols(‘ticker’). For example, to get daily Opening, Closing, High, Low, Volume and Adjusted for Microsoft, all you need to do is type in getSymbols(‘MSFT’). If you’d like the data over a particluar date range, you can type in getSymbols(‘MSFT’, from=’2000-01-01′, to=’2005-01-01′) and thats it!

Now you should have an ‘xts’ object named MSFT displaying in your workspace. If you double click on it, RKward will give you a summary of the object. If you then click on the ‘print’ tab, you will get a display of all the values stored in the object.

First thing we will do with the stock data for Microsoft is calculate returns. There are several different returns you can calculate, and several different ways to do it. In terms of frequency, you can calculate daily, monthly, quarterly or yearly returns. To decide how to calculate returns you will first need to think about how your trading strategy will work. Will it buy at the close and sell at the close? Then you will need to calculate Close to Close returns. Will it buy at market open and sell at market close? Then you will need to calculate Open to Close returns. So depending on when your strategy is entering and exiting the market, your return calculations will vary accordingly.

Lets say we want to calculate Close to Close daily returns. We can use the following functions:

1-rets1=Delt(Cl(MSFT)). #Delt obtains the percentage difference between value t and t+1, if no second argument is specified. CL(MSFT) pulls out the Closing Price vector only from the MSFT xts object.


3-rets3=diff(log(Cl(MSFT))) #This gives the log returns instead of the artihmetic returns. Log returns can be obtained by changing the parameters of the above 2 functions as well. As they are now, you will get arithmetic returns. With log returns, you can simply add different period returns and take their exponent to get the return for over that period. With arithmetic returns you will have to add a vector of ‘1’ to your return vector and then multiply across the period you wish to obtain the return for. For example, if you wanted the cumulative result for t, t+1 and t+2, with arithmetic returns you would add ‘1’ to each of these values and multiply them together, then subtract 1. If you had log returns, you would simply add the values for t, t+1 and t+2 together and then take the exponent to get the 3 day cumulative return.

4-rets4=ROC(Cl(MSFT)) #Gives same results as rets3

To calculate monthly returns from daily data;

1-You can use the monthlyReturn function in a similar manner as the functions used above
2-Convert the MSFT data frame to monthly information by using the following functions: as.ts(to.monthly(MSFT)). You can then run return calculations on this transformed object.

Similarly, you can calculate the periodic returns for any period (Quarterly, Yearly…) by applying appropriate modifications to the methods above.

Important Note: You can pull up the help page for any function by typing ‘?functionName()’ in the console.


Leave a comment

Filed under Finance

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s