This is what shows up when you search "what is R" on Google. I must say, it is truly out of the world, that you can run codes and backtest your hypotheses. Being from Mechanical Engineering background, I find coding to be a meticulous subject for me to master, respecc to all those CSE bros and sis' who do it. But with the advent of CHATGPT, and R, it is way way easier to just give your propositions, run the GPT and get codes to substitute in R studio to create visualizations.
For now, I am thinking about running backtests with basic momentum bursts in stocks, and see how they perform with an equity curve visualization.
Condition: buy if it is up 3% during the day, with initial stoploss at day range half, and sell only between 8-20% gain, within 3 to 5 days.
Code Used:
# Required Libraries
if (!require(quantmod)) install.packages("quantmod")
if (!require(dplyr)) install.packages("dplyr")
if (!require(ggplot2)) install.packages("ggplot2")
library(quantmod)
library(dplyr)
library(ggplot2)
# Download stock data for HAL.NS from Yahoo Finance
getSymbols("HAL.NS", from = "2020-01-01", to = Sys.Date(), src = "yahoo")
stock_data <- HAL.NS
# Convert to data frame for easier manipulation
stock_df <- data.frame(Date = index(stock_data), coredata(stock_data))
# Calculate daily percentage change, day's range, and stop loss price
stock_df <- stock_df %>%
mutate(
Day_Pct_Change = 100 * (HAL.NS.Close - lag(HAL.NS.Close)) / lag(HAL.NS.Close),
Day_Range = HAL.NS.High - HAL.NS.Low,
Stop_Loss_Price = HAL.NS.Close - (Day_Range / 2),
Daily_Return = (HAL.NS.Close / lag(HAL.NS.Close)) - 1
)
# Initialize signals and tracking columns
stock_df <- stock_df %>%
mutate(
Buy_Signal = ifelse(Day_Pct_Change > 3, 1, 0),
Sell_Signal = 0,
Entry_Price = NA,
Exit_Price = NA,
In_Trade = FALSE,
Trade_Start_Index = NA,
Strategy_Return = NA,
Cumulative_Return = 1 # Initialize for cumulative returns calculation
)
# Loop through the data and apply the momentum burst strategy
for (i in 2:nrow(stock_df)) {
if (!stock_df$In_Trade[i-1]) {
# Not in a trade, check for buy signal
if (stock_df$Buy_Signal[i] == 1) {
# Enter trade
stock_df$Entry_Price[i] <- stock_df$HAL.NS.Close[i]
stock_df$Stop_Loss_Price[i] <- stock_df$Entry_Price[i] - (stock_df$Day_Range[i] / 2)
stock_df$In_Trade[i] <- TRUE
stock_df$Trade_Start_Index[i] <- i
}
} else {
# In a trade, carry forward trade details
stock_df$Entry_Price[i] <- stock_df$Entry_Price[i-1]
stock_df$Stop_Loss_Price[i] <- stock_df$Stop_Loss_Price[i-1]
stock_df$Trade_Start_Index[i] <- stock_df$Trade_Start_Index[i-1]
stock_df$In_Trade[i] <- TRUE
# Calculate profit percentage and trade duration
profit_pct <- 100 * (stock_df$HAL.NS.Close[i] - stock_df$Entry_Price[i]) / stock_df$Entry_Price[i]
trade_duration <- i - stock_df$Trade_Start_Index[i] + 1
# Exit conditions
if (stock_df$HAL.NS.Close[i] <= stock_df$Stop_Loss_Price[i]) {
# Stop loss hit
stock_df$Exit_Price[i] <- stock_df$HAL.NS.Close[i]
stock_df$Sell_Signal[i] <- 1
stock_df$In_Trade[i] <- FALSE
stock_df$Strategy_Return[i] <- (stock_df$Exit_Price[i] / stock_df$Entry_Price[i-1]) - 1
} else if (trade_duration >= 3) {
if (profit_pct >= 8 && profit_pct <= 20) {
# Profit target met between 3-5 days
stock_df$Exit_Price[i] <- stock_df$HAL.NS.Close[i]
stock_df$Sell_Signal[i] <- 1
stock_df$In_Trade[i] <- FALSE
stock_df$Strategy_Return[i] <- (stock_df$Exit_Price[i] / stock_df$Entry_Price[i-1]) - 1
} else if (trade_duration >= 5) {
# Maximum trade duration reached
stock_df$Exit_Price[i] <- stock_df$HAL.NS.Close[i]
stock_df$Sell_Signal[i] <- 1
stock_df$In_Trade[i] <- FALSE
stock_df$Strategy_Return[i] <- (stock_df$Exit_Price[i] / stock_df$Entry_Price[i-1]) - 1
}
}
}
# Ensure In_Trade is logical
if (is.na(stock_df$In_Trade[i])) {
stock_df$In_Trade[i] <- FALSE
}
# Cumulative returns calculation
if (!is.na(stock_df$Strategy_Return[i])) {
stock_df$Cumulative_Return[i] <- stock_df$Cumulative_Return[i-1] * (1 + stock_df$Strategy_Return[i])
} else {
stock_df$Cumulative_Return[i] <- stock_df$Cumulative_Return[i-1]
}
}
# Extract trades for analysis
trades <- stock_df %>%
filter(Sell_Signal == 1) %>%
mutate(
Entry_Date = stock_df$Date[Trade_Start_Index],
Profit = Exit_Price - Entry_Price,
Profit_Pct = 100 * Profit / Entry_Price
) %>%
select(Entry_Date, Date, Entry_Price, Exit_Price, Profit, Profit_Pct)
# Summary Statistics
total_trades <- nrow(trades)
winning_trades <- nrow(trades %>% filter(Profit_Pct > 0))
losing_trades <- nrow(trades %>% filter(Profit_Pct <= 0))
average_profit <- mean(trades$Profit_Pct)
cumulative_return <- tail(stock_df$Cumulative_Return, 1)
win_loss_ratio <- winning_trades / losing_trades
# Display summary statistics
cat("Total Trades:", total_trades, "\n")
cat("Winning Trades:", winning_trades, "\n")
cat("Losing Trades:", losing_trades, "\n")
cat("Average Profit per Trade:", average_profit, "%\n")
cat("Cumulative Return of Strategy:", cumulative_return - 1, "\n")
cat("Win/Loss Ratio:", win_loss_ratio, "\n")
# Plot cumulative returns using ggplot2
ggplot(stock_df, aes(x = Date, y = Cumulative_Return)) +
geom_line(color = "blue", size = 1) +
labs(
title = "Cumulative Return of Momentum Burst Strategy on HAL.NS",
x = "Date",
y = "Cumulative Return",
caption = "Momentum Burst Strategy"
) +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5))
A few are given below for HAL & RVNL:
No comments:
Post a Comment