Financial Analysis Portfolio w/ Code

1. Setting the Working Directory

Set the working directory where your data files are located. This helps in easily reading and writing data files.

# Set working directory
setwd("path/to/your/directory")

2. Installing and Loading Libraries

Install and load the necessary libraries for financial analysis.
Install necessary libraries (run this only once)
install.packages(c("quantmod", "xts", "forecast", "PerformanceAnalytics", "PortfolioAnalytics", "ROI", "ROI.plugin.glpk", "dplyr", "ggplot2"))

# Load the libraries
library(quantmod)
library(xts)
library(forecast)
library(PerformanceAnalytics)
library(PortfolioAnalytics)
library(ROI)
library(ROI.plugin.glpk)
library(dplyr)
library(ggplot2)

3. Inputting Data

Read financial data into R. This example uses stock data from Yahoo Finance

# Load financial data
getSymbols("NVDL", src = "yahoo", from = "2024-01-01", to = "2024-08-01")
nvdl_data <- NVDL

4. Data Manipulation

Manipulate the data as required for analysis.

# Convert to data frame and manipulate data
nvdl_df <- data.frame(Date = index(nvdl_data), coredata(nvdldata))

# Example of data manipulation: calculate daily returns
nvdl_df <- nvdl_df %>%
  mutate(Return = (NVDL.Adjusted / lag(NVDL.Adjusted) - 1))

5. Performing Financial Analysis

Perform various financial analyses like time series analysis, forecasting, and portfolio optimization.
Time Series Analysis

# Plotting time series data
ggplot(nvdl_df, aes(x = Date, y = NVDL.Adjusted)) +
  geom_line() +
  labs(title = "Apple Stock Adjusted Closing Prices", x = "Date", y = "Adjusted Closing Price")

Forecasting

# Forecasting example using ARIMA
apple_ts <- ts(apple_df$AAPL.Adjusted, frequency = 252)  # Assuming 252 trading days in a year
fit <- auto.arima(apple_ts)
forecasted <- forecast(fit, h = 30)  # Forecast next 30 days
plot(forecasted)

Portfolio Optimization

# Portfolio optimization example
data(edhec)
returns <- edhec[, 1:4]  # Use first 4 assets for example

# Define portfolio specification
port_spec <- portfolio.spec(assets = colnames(returns))
port_spec <- add.constraint(portfolio = port_spec, type = "full_investment")
port_spec <- add.constraint(portfolio = port_spec, type = "long_only")
port_spec <- add.objective(portfolio = port_spec, type = "risk", name = "StdDev")

# Optimize portfolio
opt_port <- optimize.portfolio(R = returns, portfolio = port_spec, optimize_method = "ROI", trace = TRUE)
print(opt_port)

6. Outputting Data

Save the results to a CSV file or other formats.

# Save manipulated data to CSV
write.csv(nvdl_df, "nvdl_stock_data.csv")

# Save forecast results
write.csv(forecasted, "nvdl_stock_forecast.csv")

VISUALIZATIONS

Time Series Plot

# Time Series Plot
ggplot(nvdl_df, aes(x = Date, y = NVDL.Adjusted)) +
  geom_line(color = "blue") +
  labs(title = "NVDL Stock Adjusted Closing Prices", x = "Date", y = "Adjusted Closing Price") +
  theme_minimal()

Forecast Plot

# Forecast Plot
autoplot(forecasted) +
  labs(title = "30-Day Forecast for NVDLStock Prices", x = "Date", y = "Adjusted Closing Price") +
  theme_minimal()

Portfolio Weights Plot

# Portfolio Weights Plot
weights <- extractWeights(opt_port)
weights_df <- data.frame(Asset = names(weights), Weight = weights)

ggplot(weights_df, aes(x = Asset, y = Weight)) +
  geom_bar(stat = "identity", fill = "green") +
  labs(title = "Optimized Portfolio Weights", x = "Asset", y = "Weight") +
  theme_minimal()

You'll only receive email when they publish something new.

More from 35043
All posts