Download basic statistic of NASDAQ and London Stock Exchange and compare them
if you not have them install.packages using the Tool from menu. We will need the following libraries:
Let us load the libary ‘quantmod’
library(quantmod)
## Ładowanie wymaganego pakietu: xts
## Ładowanie wymaganego pakietu: zoo
##
## Dołączanie pakietu: 'zoo'
## Następujące obiekty zostały zakryte z 'package:base':
##
## as.Date, as.Date.numeric
## Ładowanie wymaganego pakietu: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
We will now import the NASDAQ index and FTSE index to comparer the makrest
# Download historical data for NASDAQ and London Stock Exchange
getSymbols("NDAQ", src = "yahoo")
## [1] "NDAQ"
getSymbols("^FTSE", src = "yahoo") # FTSE 100
## Warning: ^FTSE contains missing values. Some functions will not work if objects
## contain missing values in the middle of the series. Consider using na.omit(),
## na.approx(), na.fill(), etc to remove or replace them.
## [1] "FTSE"
Some of the data have missing values, thus to compare them we will need to fill in the gaps, for this purposes we will use the appropriation function
# Fill in na with an approximation data
FTSE<- na.approx(FTSE)
We will now trim the data as the stock exchanges does not operates exactly during the same time span. For our purposes we will compart them between 2008 January and May 2023.
# trim data for one widow (comparision perios)
NDAQ <- subset(NDAQ, start = "2008-01-03", end = "2023-01-05")
FTSE <- subset(FTSE, start = "2008-01-03", end = "2023-01-05")
# Just to ckeck if we are now well trimmed
NDAQ[rownames(NDAQ) >= "2007-01-02" & rownames(NDAQ) <= "2007-01-03", ]
## NDAQ.Open NDAQ.High NDAQ.Low NDAQ.Close NDAQ.Volume NDAQ.Adjusted
Now we will calculate the basic statistics of the markets index, average, median, standard deviation (how about correlation)
# Calculate basic statistics
nasdaq_stats <- c(
Mean = mean(NDAQ$NDAQ.Close, na.rm=TRUE),
Median = median(NDAQ$NDAQ.Close, na.rm=TRUE),
StDev = sd(NDAQ$NDAQ.Close, na.rm= TRUE)
#Correlation = cor(NDAQ$NDAQ.Close, FTSE$FTSE.Close)
)
lond_stats <- c(
Mean = mean(FTSE$FTSE.Close),
Median = median(FTSE$FTSE.Close),
StDev = sd(FTSE$FTSE.Close)
# Correlation = cor(FTSE$FTSE.Close, NDAQ$NDAQ.Close)
)
If you try to calculate the
Correlation = cor(NDAQ$NDAQ.Close, FTSE$FTSE.Close)
you are
likely to end up with the errors because the time series are not
necessary equal on each time data point in length. To overcome this
problem, we will put both time series into one data frame called
(aligned_data), than we will clean up the missing values, form the
frame, and then calculate correlation.
# correlation
# Assuming xts1 and xts2 are your two xts objects
aligned_data <- merge(NDAQ, FTSE)
# Remove missing values (optional)
cleaned_data <- na.omit(aligned_data)
# Calculate correlation
correlation <- cor(cleaned_data[, 1], cleaned_data[, 7])
# Print the correlation coefficient
print(correlation)
## FTSE.Open
## NDAQ.Open 0.7104715
No we might put all together and compare the markets. We will combine both statistics into the table, and round it up to 2 decimal points
# Compare the statistics
stats_comparison <- rbind(nasdaq_stats, lond_stats)
colnames(stats_comparison) <- c("Mean", "Median", "Standard Deviation" ) #, "Correlation")
round(stats_comparison,2)
## Mean Median Standard Deviation
## nasdaq_stats 25.97 19.19 19.39
## lond_stats 6527.12 6625.90 941.28
# Print the correlation coefficient
print(round(correlation,2))
## FTSE.Open
## NDAQ.Open 0.71
Note: Unless we do not standardize the data we can not make a direct comparison
Generate the code with the Open AI and compare to the code generated with Gemini asking prompt “Generate the R code to compare New York Stock Exchange, and London Stock Exchange” apply the prompt engineering to correct the code, check for the simplicity and readability, test with your on machine. What observations do you have?
For your report compare your national country local exchange index with US or London.
Comments
We have a very basic high level comparison of the exchanges consider as well: