I have write the beneath code for momentum strategy for my thesis. It should change portofolio every week based on momentum and callulate the returns of the portofolios from this strategy on the end but it gives me the same portofolio as the optimal every week. This make me think that there is a mistake can anyone can anyone spot it? and give me some improvments? think the problem is on the potitions matrix
`# Load required libraries
library(readxl)
library(quantmod)
library(TTR)
#DATA PATH
data_path <- '/Users/kiriakos/Documents/Μεταπτυχιακό/MSc_Thesis_Momentum strategy/'
#PRICES
data <- readxl::read_excel(paste0(data_path, 'DATAMS2.xlsx'), sheet = 'Weekly_data_ATH_21', range = 'a1:z1000')
prices <- as.data.frame(data[, 2:ncol(data)])
prices <-na.omit(prices)
symbols <- as.vector(head(data[1,], -1))
#Returns
data2 <- readxl::read_excel(paste0(data_path, 'DATAMS2.xlsx'), sheet = 'Weekly_data_ATH_21b', range = 'a1:z1000')
returns <- as.data.frame(data2[, 2:ncol(data2)])
returns <-na.omit(returns)
#portofolio returns based on momentum
portofolio_returns<-data.frame(matrix(nrow=100,ncol=1))
# Define the number of rows to remove
rows_to_remove <- 100
# Remove the first 'rows_to_remove' rows from 'prices' and 'returns' using a for loop
for (i in 1:rows_to_remove) {
prices <- prices[-1, ] # Remove the first row from 'prices'
returns <- returns[-1, ] # Remove the first row from 'returns'
data<-data[-1,]
################################################################################
######################### MOMENTUM STRATEGY ###################################
#RSI
symbols <- as.vector(head(data[1,], -1))
rsi<- lapply(prices, RSI, n = 12) #<-calculate rsi of 12 weeks
rsi <- do.call(cbind, rsi)
ma <- apply(prices, 2, TTR::SMA, n = 12)
# Initialize positions
positions <- matrix(0, nrow = nrow(prices), ncol = ncol(prices))
# Enter long position when price crosses above the 3-week moving average and RSI
#is above 50
positions[prices > ma & rsi < 30] <- 1
# Enter short position when price crosses below the 3-week moving average and
#RSI is below 50
positions[prices < ma & rsi > 70] <- -1
# Compute portfolio returns
port_returns <- positions[-nrow(positions), ] * returns
# Compute asset weights based on momentum
momentum_scores <- colSums(port_returns[(nrow(port_returns) - 11):nrow(port_returns), ])
momentum_scores<-as.matrix(momentum_scores)
momentum_scores <- momentum_scores[order(-momentum_scores[,1]), ]
buy_assets <- names(momentum_scores)[1:5]
sell_assets <- names(momentum_scores)[(length(momentum_scores) - 5):length(momentum_scores)]
print("Assets to Buy:")
print(buy_assets)
print("Assets to Sell:")
print(sell_assets)
#portofolio prices
portfolio1 <- data[12:25, buy_assets]
portfolio2 <-data[12:25,sell_assets]*-1
portfolio <- data.frame(c(portfolio1, portfolio2))
# Calculate returns for each column in the portfolio data frame
mom_returns <- lapply(portfolio, function(x) c(NA, diff(x) / x[-length(x)]))
mom_returns <- as.data.frame(mom_returns)
mom_returns<-na.omit(mom_returns)
PortRet <- rowSums(mom_returns)
mom_returns <-cbind(mom_returns,rowSums(mom_returns))
total_portofolio_returns<-sum(mom_returns$`rowSums(mom_returns)`)
portofolio_returns[1+i,] <- total_portofolio_returns
RETTT<-sum(portofolio_returns[-1,])
}
print(RETTT)`