R Basics for Financial Engineering

R Studio - Financial Engineering

In this post, I would like to share some basic information about the uses of R as a tool for Financial analysis. In the first part, I will introduce how plot prices of NASDAQ Composite (08-05-2015 to 08-04-2017) and then I will show how to calculate its daily returns. Finally, I will present a brief analysis of returns distribution.

Start your future with a Data Analysis Certificate.

NASDAQ composite Prices


In the chart, it is shown adjusted close prices for NASDAQ.

There is a clear uptrend in NASDAQ prices. In fact, there are a few all-time highs. However, there are a few drawdowns that can be stressful for passive investors to keep a long position of the index.

# Stock Prices line plot ------
 type = "l",
 main = "NASDAQ Adj.Price",
 col = "red", xlab = "Days", ylab = "NASDAQ")

Daily returns of NASDAQ composite


In the chart, we can see the daily returns for the 2 years. There are such extreme values, in special on the first year of analysis. The second year is visibly less volatile on daily basis compared with the first year.

# Data Loading -------------
NASDAQ <- read.csv(file = "NDAQ.csv", header = TRUE)
# Daily Returns -------------
NASDAQ$DailyReturn <- c(0,diff(log(NASDAQ$Adj.Close),lag=1))
plot (NASDAQ$DailyReturn,
 type = "h",
 main = "Daily Return",
 col = "blue", xlab = "Days", ylab = " Daily Returns")
abline(h = 0,
 col = "black",
 lwd = 2)

Daily return values – Histogram

NASDAQ daily returns are not distributed normally. For reaching this conclusion, I performed a Shapiro-Wilk normally test on R. The Null hypothesis states that “the samples come from a Normal Distribution” and the alternative hypothesis “the samples do not come from a Normal Distribution”. The p-value is 6.778e-10 which is less than 0.05, thus, I reject the null hypothesis.

Moreover, even when Median and Mean are similar, skewness is 0.597, indicating a positive skewness. On the other hand, daily returns have a positive kurtosis which is clearly identified with a visible fat-tailed distribution. That is why is to be said that is right-skewness and leptokurtic.


Based on measures of central tendency, there are risks of being long since a drawback larger than associated with normal distribution probability is expected. This makes the risk estimation difficult and, in some cases, unrealistic. However, greater rewards can be expected since there is a right-skewness that makes more plausible positive returns in the future.

# Histogram Daily returns -----
# Mean, Std, median ------
mu <- mean(NASDAQ$DailyReturn)
sigma <- sd(NASDAQ$DailyReturn)
med <- median(NASDAQ$DailyReturn)
# Histogram with central measures plot -----
abline(v = mu,
 col = "royalblue",
 lwd = 2)
abline(v = med,
 col = "red",
 lwd = 2)
abline(v = mu+sigma,
 col = "blue",
 lwd = 2,
 lty = 2)
abline(v = mu-sigma,
 col = "blue",
 lwd = 2,
 lty = 2)
abline(v = mu+2*sigma,
 col = "red",
 lwd = 2,
 lty = 2)
abline(v = mu-2*sigma,
 col = "red",
 lwd = 2,
 lty = 2)
legend(x = "topright", # location of legend within plot area
 c("Mean", "Median","mu±sigma","mu±2*sigma"),
 col = c("royalblue", "red","blue","red"),
 lwd = c(2, 2,2,2),
 lty = c(1, 1,2,2))
# Normal Distribution Test

Find the data at the following link: NDAQ.xlsx


1. http://www.r-tutor.com/elementary-statistics/numerical-measures/skewness
2. http://www.r-tutor.com/elementary-statistics/numerical-measures/kurtosis
3. https://stackoverflow.com/questions/15427692/perform-a-shapiro-wilk-normality-test

3 thoughts on “R Basics for Financial Engineering

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Connecting to %s

%d bloggers like this: