I need to calculate daily DAX index returns with 10 constituants.
Need it for 3 indizes: price weighted, value weighted and equally weighted.
Does any one has any advice?
Hi,
You need to provide us a more detailed example with some real data in order to understand what exactly it is you like.
Grtz
What have you tried so far? what is your specific problem?, we are more inclined towards helping you with specific coding problems rather than doing your work for you.
Could you please turn this into a selfcontained REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.
If you've never heard of a reprex before, you might want to start by reading this FAQ:
What I have so far is how to compute the discrete returns for 1 security and how to do a simple plot on it however what I do not understand is the part with "daily index returns based on the weighting schemes".
How is it even possible to compute those 3 indices in a R code. I know that once I have the value index I can use it to generate the equal weighted index but I am a confused on how to implement the formulas into the code.

For price weighted, it is the stock/sum of all stocks but this not possible of I want to have an development, then I thought that if it is for a longer period, then it is the sum of all prices for that time frame / by the numbers of stocks, but I am a bit lost on how to put it in R

For value weighted, I only have it for 1 day, the weights of my securities based on their market value.
.For equal weighted, I have nothing yet.
Hi,
Again, please provide data and code in a format shown by @andresrcs post above. We might be able to help you with the coding part, as that is the goal of this forum, but you can't expect us to know how stock market calculations work (which is besides the point anyway).
So, create a (fake) data set and provide the script where all the functions for the calculations you need are present, and then let us know where in the code it goes wrong or you're stuck.
This is an example how you can create fake data, by using the variable names you'd use in your real code and values of the correct class:
myData = data.frame(id = 1:100, index = runif(100, 0,1), group = sample(LETTERS, 100, replace = T))
Success,
PJ
Ok.
I did the following code:
if(!require("quantmod")) {install.packages("quantmod"); require("quantmod")} # Define Start and end dates ######### DStart < "20071231" #define start data for getting the data today < "20190331" # write current date (from system) in variable today today DEnd < today # end data should be today DAX < getSymbols("^GDAXI", src="yahoo", from=DStart, to=DEnd, quote="Close", compression="m",auto.assign=FALSE) TickersDAX=c('BEI.DE','ADS.DE','DBK.DE','DAI.DE','DB1.DE','BMW.DE','ALV.DE','BAYN.DE','CON.DE','BAS.DE', "^GDAXI") #Name list of current constituencies of DAX index (as of 01.04.2019) NamesDAX = c('Beiersdorf','Adidas','Deutsche Bank','Daimler','Deutsche Boerse','BMW', 'Allianz','Bayer','Continental','BASF', "DAX Index") #Define time period for which DAX data should be downloaded #Start date DStart="20071231" #End date should be current date #Get current date 'today' today < "20190331" today DEnd=today #Create a list that contains data for all stocks getSymbols(TickersDAX, from=DStart, to=DEnd, warnings=F) # Open, High, Low, Close, Volume and Adjusted for every stock # are saved in a variable #List that contains data for all stocks Ticker_list < list(BEI.DE,ADS.DE,DBK.DE,DAI.DE,DB1.DE,BMW.DE,ALV.DE,BAYN.DE,CON.DE,BAS.DE,GDAXI) head(Ticker_list) DAXClosing = merge((ADS.DE[,6]),(ALV.DE[,6]),(BAS.DE[,6]),(BAYN.DE[,6]),(BEI.DE[,6]),(BMW.DE[,6]),(CON.DE[,6]),(DAI.DE[,6]),(DB1.DE[,6]),(DBK.DE[,6])) head(DAXClosing) tail(DAXClosing) sum(is.na(DAXClosing)) names(DAXClosing) = c("Adidas","Allianz","BASF", "Bayer","Beiersdorf","BMW","Continental", "Daimler", "Deutsche Boerse","Deutsche Bank") #rename columns of the data head(DAXClosing) # "lag" function returns previous observation for each point lag(DAXClosing[,1]) lag(DAXClosing[,2]) lag(DAXClosing[,3]) lag(DAXClosing[,4]) lag(DAXClosing[,5]) lag(DAXClosing[,6]) lag(DAXClosing[,7]) lag(DAXClosing[,8]) lag(DAXClosing[,9]) lag(DAXClosing[,10]) # Computing discrete returns ############# # R[t]=P[t]/P[t1]1 DAX_ClosingReturns1 < (DAXClosing[,1]/lag(DAXClosing[,1]))1 DAX_ClosingReturns2 < (DAXClosing[,2]/lag(DAXClosing[,2]))1 DAX_ClosingReturns3 < (DAXClosing[,3]/lag(DAXClosing[,3]))1 DAX_ClosingReturns4 < (DAXClosing[,4]/lag(DAXClosing[,4]))1 DAX_ClosingReturns5 < (DAXClosing[,5]/lag(DAXClosing[,5]))1 DAX_ClosingReturns6 < (DAXClosing[,6]/lag(DAXClosing[,6]))1 DAX_ClosingReturns7 < (DAXClosing[,7]/lag(DAXClosing[,7]))1 DAX_ClosingReturns8 < (DAXClosing[,8]/lag(DAXClosing[,8]))1 DAX_ClosingReturns9 < (DAXClosing[,9]/lag(DAXClosing[,9]))1 DAX_ClosingReturns10 < (DAXClosing[,10]/lag(DAXClosing[,10]))1 DAX_ClosingReturns1 DAX_ClosingReturns2 DAX_ClosingReturns3 DAX_ClosingReturns4 DAX_ClosingReturns5 DAX_ClosingReturns6 DAX_ClosingReturns7 DAX_ClosingReturns8 DAX_ClosingReturns9 DAX_ClosingReturns10 DAX_ClosingReturn < merge(DAX_ClosingReturns1,DAX_ClosingReturns2,DAX_ClosingReturns3,DAX_ClosingReturns4,DAX_ClosingReturns5,DAX_ClosingReturns6,DAX_ClosingReturns7,DAX_ClosingReturns8,DAX_ClosingReturns9,DAX_ClosingReturns10) DAX_ClosingReturn #first return observation is NA, sine no return could be computed DAX_ClosingReturn < DAX_ClosingReturn[1] DAX_ClosingReturn plot(DAX_ClosingReturn) DAX_ClosingReturns
So now my friend added something even I am clueless about in the code:
#PRICE WEIGHRED AVERAGE METHOD
MCADS < weighted.mean(DAXClosing$ADS.DE.Close)
MCALV < weighted.mean(DAXClosing$ALV.DE.Close)
MCBAS < weighted.mean(DAXClosing$BAS.DE.Close)
MCBAYN < weighted.mean(DAXClosing$BAYN.DE.Close)
MCBEI < weighted.mean(DAXClosing$BEI.DE.Close)
MCBMW < weighted.mean(DAXClosing$BMW.DE.Close)
MCCON < weighted.mean(DAXClosing$CON.DE.Close)
MCDAI < weighted.mean(DAXClosing$DAI.DE.Close)
MCDB1 < weighted.mean(DAXClosing$DB1.DE.Close)
MCDBK < weighted.mean(DAXClosing$DBK.DE.Close)
#SUMMED UP PRICES
PRICE < sum(MCADS, MCALV, MCBAS,MCBAYN, MCBEI, MCBMW, MCCON, MCDAI, MCDB1, MCDBK)
#PRICE WEIGHTING
PRICEWEIGHTEDAVERAGE < PRICE/10
PRICEWEIGHTEDAVERAGE
plot(PRICEWEIGHTEDAVERAGE)
However when I do it I get "NaN".
Thanks if you could help
Hi,
I cleaned up the code and reduced it to much fewer lines and fixed the NaN. I don't know if that was all you needed though...
if(!require("quantmod")) {install.packages("quantmod"); require("quantmod")}
######## GET DATA ###########
#****************************
# Define Start and end dates #########
DStart < "20071231" #define start data for getting the data
DEnd < Sys.Date() # end data should be today
DAX < getSymbols("^GDAXI", src="yahoo", from=DStart, to=DEnd, quote="Close", compression="m",auto.assign=FALSE)
TickersDAX=c('BEI.DE','ADS.DE','DBK.DE','DAI.DE','DB1.DE','BMW.DE','ALV.DE','BAYN.DE','CON.DE','BAS.DE', "^GDAXI")
#Name list of current constituencies of DAX index (as of 01.04.2019)
NamesDAX = c('Beiersdorf','Adidas','Deutsche Bank','Daimler','Deutsche Boerse','BMW',
'Allianz','Bayer','Continental','BASF', "DAX Index")
#Create a list that contains data for all stocks
getSymbols(TickersDAX, from=DStart, to=DEnd, warnings=F)
DAXClosing = merge((ADS.DE[,6]),(ALV.DE[,6]),(BAS.DE[,6]),(BAYN.DE[,6]),(BEI.DE[,6]),(BMW.DE[,6]),(CON.DE[,6]),(DAI.DE[,6]),(DB1.DE[,6]),(DBK.DE[,6]))
######## Perform Functions #############
#***************************************
# Computing discrete returns #############
# R[t]=P[t]/P[t1]1
DAX_ClosingReturn = do.call(cbind, lapply(1:ncol(DAXClosing),function(x){
DAXClosing[,x]/lag(DAXClosing[,x])1
}))
#first return observation is NA, sine no return could be computed
DAX_ClosingReturn < DAX_ClosingReturn[1,]
plot(DAX_ClosingReturn)
#PRICE WEIGHRED AVERAGE METHOD
PRICEWEIGHTEDAVERAGE = sum(apply(DAXClosing, 2, weighted.mean)) / ncol(DAXClosing)
Grtz
Great!!! Thank you very much.
Could you briefly explain what "do.call" is for? Is it the same as "merge"?
Hi,
Yes it is.
The apply function is an efficient implementation of a forloop in cases where the code inside the apply function is independent for each of the iterations, meaning one iteration does not depend on the output of another iteration.
The function outputs a list, with the result of each individual iteration. The do.call function then merges all those result together by using the cbind (binds all data as columns in a data frame, can also be rbind if you want to combine the result as rows).
Grtz,
PJ
This would be a tidyverse
based approach, I personally find it more readable
library(tidyverse)
library(quantmod)
DStart < "20071231" #define start data for getting the data
DEnd < Sys.Date()
TickersDAX=c('BEI.DE','ADS.DE','DBK.DE','DAI.DE','DB1.DE','BMW.DE','ALV.DE','BAYN.DE','CON.DE','BAS.DE', "^GDAXI")
NamesDAX = c('Beiersdorf','Adidas','Deutsche Bank','Daimler','Deutsche Boerse','BMW',
'Allianz','Bayer','Continental','BASF', "DAX Index")
DAXClosing < map_dfc(TickersDAX,
~ as_tibble(getSymbols(Symbols = .,
from=DStart,
to=DEnd,
warnings = FALSE,
env = NULL))) %>%
select(ends_with("Adjusted")) %>%
rename_all(~NamesDAX)
DAX_ClosingReturn < DAXClosing %>%
mutate_all(~./lag(.)1) %>%
slice(1)
DAX_ClosingReturn
#> # A tibble: 2,926 x 11
#> Beiersdorf Adidas `Deutsche Bank` Daimler `Deutsche Boers… BMW
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 0.0231 0.00180 0.00702 2.74e2 0.0372 1.50e2
#> 2 0.00313 0.00559 0.00491 5.95e2 0.0434 2.83e2
#> 3 0.00255 0.00602 0.00573 9.13e3 0.0328 2.64e2
#> 4 0.00391 0.0244 0.00761 1.28e2 0.0758 4.76e4
#> 5 0.0236 0.0340 0.0146 2.04e2 0.0195 5.95e3
#> 6 0.00864 0.0264 0.0118 1.20e2 0.00654 8.14e3
#> 7 0.0505 0.0139 0.00477 9.41e3 0.0265 4.99e3
#> 8 0.0414 0.00246 0.00713 1.10e2 0.0277 9.07e3
#> 9 0.00533 0.0389 0.0254 3.33e2 0.0302 3.86e2
#> 10 0.00371 0.0498 0.0127 1.77e4 0.0884 2.76e3
#> # … with 2,916 more rows, and 5 more variables: Allianz <dbl>,
#> # Bayer <dbl>, Continental <dbl>, BASF <dbl>, `DAX Index` <dbl>
PRICEWEIGHTEDAVERAGE < DAXClosing %>%
summarise_all(~weighted.mean(., na.rm = TRUE)) %>%
sum() / ncol(DAXClosing)
PRICEWEIGHTEDAVERAGE
#> [1] 852.7211
^{Created on 20190714 by the reprex package (v0.3.0)}
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.