Youtube Channel: https://www.youtube.com/c/TechAnswers88
Youtube link for the video: https://youtu.be/_7J6BbDgqrA
We will create a data frame first which will have the daily price of a share. Create a data fame with Day Number and the Price
library(dplyr)
library(flextable)
library(ggplot2)
m1 <- c(1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10)
m2 <- c(10 ,12 ,14 ,10 ,6 ,8 ,10 ,10 ,8 ,6 )
m <- data.frame(m1,m2)
names(m) <- c('Day', 'Price')
flextable(m)
Day | Price |
1 | 10 |
2 | 12 |
3 | 14 |
4 | 10 |
5 | 6 |
6 | 8 |
7 | 10 |
8 | 10 |
9 | 8 |
10 | 6 |
First see what the lag() and lead() command do .
Using the lag() we can refer to the previous period price in the data frame And using the lead() we can refer to the next period price in a similar way See the example below
mc <- m%>%
dplyr::mutate(Previous = lag(Price))
flextable(mc)
Day | Price | Previous |
1 | 10 | |
2 | 12 | 10 |
3 | 14 | 12 |
4 | 10 | 14 |
5 | 6 | 10 |
6 | 8 | 6 |
7 | 10 | 8 |
8 | 10 | 10 |
9 | 8 | 10 |
10 | 6 | 8 |
mc <- m%>%
dplyr::mutate(Previous = lag(Price),
Next = lead(Price))
flextable(mc)
Day | Price | Previous | Next |
1 | 10 | 12 | |
2 | 12 | 10 | 14 |
3 | 14 | 12 | 10 |
4 | 10 | 14 | 6 |
5 | 6 | 10 | 8 |
6 | 8 | 6 | 10 |
7 | 10 | 8 | 10 |
8 | 10 | 10 | 8 |
9 | 8 | 10 | 6 |
10 | 6 | 8 |
Now calculate the rate of change Not that we can calculate the rate of change in two ways
mc <- m%>%
dplyr::mutate(Previous = lag(Price))
mc <- m%>%
dplyr::mutate(Previous = lag(Price),
Change = Price - Previous,
ChangePercentage = (Change/Previous) * 100)
flextable(mc)
Day | Price | Previous | Change | ChangePercentage |
1 | 10 | |||
2 | 12 | 10 | 2 | 20.00000 |
3 | 14 | 12 | 2 | 16.66667 |
4 | 10 | 14 | -4 | -28.57143 |
5 | 6 | 10 | -4 | -40.00000 |
6 | 8 | 6 | 2 | 33.33333 |
7 | 10 | 8 | 2 | 25.00000 |
8 | 10 | 10 | 0 | 0.00000 |
9 | 8 | 10 | -2 | -20.00000 |
10 | 6 | 8 | -2 | -25.00000 |
#We can do the same thing by
mc <- m%>%
dplyr::mutate(Previous = lag(Price),
Next = lead(Price),
Change = Price - Previous,
pct_change = (Price/lag(Price) - 1) * 100 )
flextable(mc)
Day | Price | Previous | Next | Change | pct_change |
1 | 10 | 12 | |||
2 | 12 | 10 | 14 | 2 | 20.00000 |
3 | 14 | 12 | 10 | 2 | 16.66667 |
4 | 10 | 14 | 6 | -4 | -28.57143 |
5 | 6 | 10 | 8 | -4 | -40.00000 |
6 | 8 | 6 | 10 | 2 | 33.33333 |
7 | 10 | 8 | 10 | 2 | 25.00000 |
8 | 10 | 10 | 8 | 0 | 0.00000 |
9 | 8 | 10 | 6 | -2 | -20.00000 |
10 | 6 | 8 | -2 | -25.00000 |
Now calculate the rate of change from the previous day price. ( In other words comparing today’s price with yesterday’s price)
mc <- m%>%
dplyr::mutate(Previous = lag(Price),
Next = lead(Price),
Change = Price - Previous)
flextable(mc)
Day | Price | Previous | Next | Change |
1 | 10 | 12 | ||
2 | 12 | 10 | 14 | 2 |
3 | 14 | 12 | 10 | 2 |
4 | 10 | 14 | 6 | -4 |
5 | 6 | 10 | 8 | -4 |
6 | 8 | 6 | 10 | 2 |
7 | 10 | 8 | 10 | 2 |
8 | 10 | 10 | 8 | 0 |
9 | 8 | 10 | 6 | -2 |
10 | 6 | 8 | -2 |
DayOnePrice <- m[1,c("Price")]
print(DayOnePrice)
FALSE [1] 10
mc <- m%>%
dplyr::mutate(Previous = lag(Price),
Next = lead(Price),
Change = Price - Previous,
ChangePercentage = (Change/Previous) * 100)
flextable(mc)
Day | Price | Previous | Next | Change | ChangePercentage |
1 | 10 | 12 | |||
2 | 12 | 10 | 14 | 2 | 20.00000 |
3 | 14 | 12 | 10 | 2 | 16.66667 |
4 | 10 | 14 | 6 | -4 | -28.57143 |
5 | 6 | 10 | 8 | -4 | -40.00000 |
6 | 8 | 6 | 10 | 2 | 33.33333 |
7 | 10 | 8 | 10 | 2 | 25.00000 |
8 | 10 | 10 | 8 | 0 | 0.00000 |
9 | 8 | 10 | 6 | -2 | -20.00000 |
10 | 6 | 8 | -2 | -25.00000 |
library(ggplot2)
pl <- ggplot(data = mc)
pl <- pl + geom_line(aes(x = factor(Day), y = Price), color ="blue", group =1)
pl <- pl + geom_col(aes(x = factor(Day), y = ChangePercentage, fill =ChangePercentage > 0), alpha = 0.8)
pl <- pl + geom_point(aes(x = factor(Day), y = Price))
pl <- pl + geom_text(aes(x = factor(Day), y = ChangePercentage, label = paste0(round(ChangePercentage,2),"%")), size = 3, vjust = -0.5)
pl <- pl + theme_classic()
pl <- pl + labs(title ="Daily price with daily % change")
pl <- pl + labs(x ="Day",y ="Price")
pl <- pl + scale_fill_manual(name ="Change", values =c( "red","green"),labels =c("Decrease", "Increase"))
pl
Now calculate the rate of change from the start (Day 1 price). This tells us if the current price is better or worse than the price we originally started with on Day 1.
flextable(m)
Day | Price |
1 | 10 |
2 | 12 |
3 | 14 |
4 | 10 |
5 | 6 |
6 | 8 |
7 | 10 |
8 | 10 |
9 | 8 |
10 | 6 |
mc <- m%>%
dplyr::mutate(Previous = lag(Price),
Next = lead(Price),
Change = Price - Previous)
flextable(mc)
Day | Price | Previous | Next | Change |
1 | 10 | 12 | ||
2 | 12 | 10 | 14 | 2 |
3 | 14 | 12 | 10 | 2 |
4 | 10 | 14 | 6 | -4 |
5 | 6 | 10 | 8 | -4 |
6 | 8 | 6 | 10 | 2 |
7 | 10 | 8 | 10 | 2 |
8 | 10 | 10 | 8 | 0 |
9 | 8 | 10 | 6 | -2 |
10 | 6 | 8 | -2 |
DayOnePrice <- m[1,c("Price")]
print(DayOnePrice)
FALSE [1] 10
# Calculate the rate of change from the previous day and also the rate of Change from the Day 1
mc <- m%>%
dplyr::mutate(Previous = lag(Price),
Next = lead(Price),
Change = Price - Previous,
ChangePercentage = (Change/Previous) * 100,
ChangeFromDayOne = (Price/DayOnePrice -1) * 100)
flextable(mc)
Day | Price | Previous | Next | Change | ChangePercentage | ChangeFromDayOne |
1 | 10 | 12 | 0 | |||
2 | 12 | 10 | 14 | 2 | 20.00000 | 20 |
3 | 14 | 12 | 10 | 2 | 16.66667 | 40 |
4 | 10 | 14 | 6 | -4 | -28.57143 | 0 |
5 | 6 | 10 | 8 | -4 | -40.00000 | -40 |
6 | 8 | 6 | 10 | 2 | 33.33333 | -20 |
7 | 10 | 8 | 10 | 2 | 25.00000 | 0 |
8 | 10 | 10 | 8 | 0 | 0.00000 | 0 |
9 | 8 | 10 | 6 | -2 | -20.00000 | -20 |
10 | 6 | 8 | -2 | -25.00000 | -40 |
library(ggplot2)
pl <- ggplot(data = mc)
pl <- pl + geom_line(aes(x = factor(Day), y = Price), color ="blue", group =1)
pl <- pl + geom_col(aes(x = factor(Day), y = ChangeFromDayOne, fill =ChangeFromDayOne > 0), alpha = 0.8)
pl <- pl + geom_text(aes(x = factor(Day), y = ChangeFromDayOne, label = paste0(round(ChangeFromDayOne,2),"%")), size = 3, vjust = -0.5)
pl <- pl + theme_classic()
pl <- pl + labs(title ="Daily price with % change from Day 1")
pl <- pl + labs(x ="Day",y ="Price")
pl <- pl + scale_fill_manual(name ="Change", values =c( "red","green"),labels =c("Decrease", "Increase"))
pl
Lets say that we purchased certain share and the purchase price is our baseline price and then we want to calculate the percentage change from that price using the he current price.
library(dplyr)
library(flextable)
library(ggplot2)
library(scales)
m1 <- c('Share1' ,'Share2' ,'Share3' ,'Share4' ,'Share5' ,'Share6'
,'Share7' ,'Share8' ,'Share9' ,'Share10')
m2 <- c(10 ,12 ,14 ,10 ,6 ,8 ,10 ,10 ,8 ,6 )
m3 <- c(11 ,19 ,13 ,10 ,9 ,3 ,12 ,16 ,18 ,6 )
m <- data.frame(m1,m2, m3)
names(m) <- c('Share', 'BaseLinePrice' ,'CurrentPrice')
flextable(m)
Share | BaseLinePrice | CurrentPrice |
Share1 | 10 | 11 |
Share2 | 12 | 19 |
Share3 | 14 | 13 |
Share4 | 10 | 10 |
Share5 | 6 | 9 |
Share6 | 8 | 3 |
Share7 | 10 | 12 |
Share8 | 10 | 16 |
Share9 | 8 | 18 |
Share10 | 6 | 6 |
mc <- m%>%
dplyr::mutate(Change = CurrentPrice - BaseLinePrice,
ChangePercentage = (Change/BaseLinePrice))
flextable(mc)
Share | BaseLinePrice | CurrentPrice | Change | ChangePercentage |
Share1 | 10 | 11 | 1 | 0.10000000 |
Share2 | 12 | 19 | 7 | 0.58333333 |
Share3 | 14 | 13 | -1 | -0.07142857 |
Share4 | 10 | 10 | 0 | 0.00000000 |
Share5 | 6 | 9 | 3 | 0.50000000 |
Share6 | 8 | 3 | -5 | -0.62500000 |
Share7 | 10 | 12 | 2 | 0.20000000 |
Share8 | 10 | 16 | 6 | 0.60000000 |
Share9 | 8 | 18 | 10 | 1.25000000 |
Share10 | 6 | 6 | 0 | 0.00000000 |
pl <- ggplot(data = mc)
pl <- pl + geom_col(aes(x = Share, y = ChangePercentage, fill =ChangePercentage > 0), alpha = 0.8)
pl <- pl + geom_text(aes(x = Share, y = ChangePercentage, label = paste0(round(ChangePercentage * 100,2),"%")), size = 3, vjust = -0.5)
pl <- pl + theme_classic()
pl <- pl + labs(title ="Percentage change of share prices from baseline")
pl <- pl + labs(x ="Share",y ="Perc Change")
pl <- pl + scale_fill_manual(name ="Change", values =c( "red","green"),labels =c("Decrease", "Increase"))
pl <- pl + scale_y_continuous(labels = scales::percent)
pl