For my final project I decided to test a potential trading strategy idea I had in regards to stock market. The strategy involves following the short interest list and initiating trades based on that list. The short interest list is released biweekly and shows how many shares are current held short vs shares outstanding for the particular stock. So the idea is to follow the list and detect the large changes in the shares outstanding to see if that is a good indicator of where the stock will move in the near term. For this study I decided to do the following: Find the 25 stocks with the largest positive change in percentage of shares shorted and 25 largest declines. Calculate the performance of the stock in a suggested direction for 1 week and 2 weeks. Perform the above for all 21 reports and test it vs the Index. I this study I decided to only use NASDAQ stocks, so we will be testing the relative performance vs QQQ ETF which tracks NASDAQ Composite Index.
Finding the data turned out to be harder then I anticipated. I ended up signing up for a service which promised to have the the short lists available: http://WallStreetCourier.com. It turned out they only have historical data, so I was able to obtain data for 2015-01-15 to 2015-11-13 which includes 21 biweekly reports and represents almost a full year. For the purposes of this study I decided that data is sufficient. The reports were converted to csv format and loaded into R.
We will need market data to test the theory and I will be using quandmod library which pulls data from yahoo finance, Google finance and other sources: http://www.quantmod.com/
#Read all .csv files the directory and store result in datafreames each dataframe will have a name of the date of the report: 2015_01_15
path <- "C:/Users/joseph/Downloads/Short Interest/NASDAQ 2015/"
files <- list.files(path=path, pattern="*.csv")
for(file in files)
{
perpos <- which(strsplit(file, "")[[1]]==".")
assign(
gsub(" ","",substr(file, 1, perpos-1)),
read.csv(paste(path,file,sep=""), stringsAsFactors = FALSE))
}
#View the first
library(knitr)
kable(head(`2015_01_15`))
| Ticker | Company.Name | Short.Interest | X..SI.Change | SI.Ratio | SIR.Change | X..Float | X..Float.Change |
|---|---|---|---|---|---|---|---|
| FLWS | 1-800-Flowers.com Inc | 716141 | 1.783851 | 4.5379663 | 0.1880785 | 3.0358593 | 0.0532061 |
| PIH | 1347 Property Insurance Holdings Inc | 7396 | -43.589352 | 0.3893246 | -0.4280694 | 0.1569416 | -0.1212712 |
| FCTY | 1st Century Bancshares Inc | 18131 | 16.545607 | 3.7437539 | -0.5009937 | 0.2091340 | 0.0296901 |
| FCCY | 1st Constitution Bancorp | 1763 | -33.396298 | 1.8855615 | 0.9907001 | 0.0302318 | -0.0151588 |
| SRCE | 1st Source Corp | 326060 | 3.115019 | 10.4785166 | 4.0067605 | 2.7945525 | 0.0216076 |
| VNET | 21Vianet Group Inc | 7042721 | 7.117814 | 9.7824265 | -0.3209361 | 0.0000000 | 0.0000000 |
Next we will setup a calendar function which will return a next business day, Order the data frames by the percentage of float changes and create new data frames holding top and bottom 25 results for each of the reports and finally download the market data for QQQ stock.
library(tools)
DFs = file_path_sans_ext(list.files(path=path, pattern = "*.csv"))
library(TTR)
tickersList <- stockSymbols()
library(quantmod)
library(timeDate)
library(data.table)
#Setup a callendar function. it is called with a date string and number of business-days to offset and returns the coresponding date as a string.
#http://stackoverflow.com/questions/20709654/add-1-business-day-to-date-in-r
calfunction <- function(buydate, days)
{
cal <- data.table(date=seq(from=as.Date("2015-01-01"), by=1, length=365), key="date")
cal2 <- copy(cal)
cal2[,nextBizDay:=date+days]
cal2 <- cal2[isBizday(as.timeDate(nextBizDay)),]
cal <- cal2[cal,,roll=-Inf]
return(cal[cal$date == buydate]$nextBizDay)
}
#creating top and bottom dataframes
#This was perfectly working in .R but not in .rmarkdown
#for (i in 1:length(DFs))
#{
# assign(DFs[i], get(DFs[i])[order(get(DFs[i])$X..Float.Change),])
# assign(paste0('bottom', DFs[i]), tail(get(DFs[i]),25) )
# assign(paste0('top', DFs[i]), head(get(DFs[i]),25) )
#}
`2015_01_15`=`2015_01_15`[order(`2015_01_15`$X..Float.Change),]
bottom2015_01_15 = tail(`2015_01_15`,25)
top2015_01_15 = head(`2015_01_15`,25)
`2015_01_30`=`2015_01_30`[order(`2015_01_30`$X..Float.Change),]
bottom2015_01_30 = tail(`2015_01_30`,25)
top2015_01_30 = head(`2015_01_30`,25)
`2015_02_13`=`2015_02_13`[order(`2015_02_13`$X..Float.Change),]
bottom2015_02_13 = tail(`2015_02_13`,25)
top2015_02_13 = head(`2015_02_13`,25)
`2015_02_27`=`2015_02_27`[order(`2015_02_27`$X..Float.Change),]
bottom2015_02_27 = tail(`2015_02_27`,25)
top2015_02_27 = head(`2015_02_27`,25)
`2015_03_15`=`2015_03_15`[order(`2015_03_15`$X..Float.Change),]
bottom2015_03_15 = tail(`2015_03_15`,25)
top2015_03_15 = head(`2015_03_15`,25)
`2015_03_31`=`2015_03_31`[order(`2015_03_31`$X..Float.Change),]
bottom2015_03_31 = tail(`2015_03_31`,25)
top2015_03_31 = head(`2015_03_31`,25)
`2015_04_15`=`2015_04_15`[order(`2015_04_15`$X..Float.Change),]
bottom2015_04_15 = tail(`2015_04_15`,25)
top2015_04_15 = head(`2015_04_15`,25)
`2015_04_30`=`2015_04_30`[order(`2015_04_30`$X..Float.Change),]
bottom2015_04_30 = tail(`2015_04_30`,25)
top2015_04_30 = head(`2015_04_30`,25)
`2015_05_15`=`2015_05_15`[order(`2015_05_15`$X..Float.Change),]
bottom2015_05_15 = tail(`2015_05_15`,25)
top2015_05_15 = head(`2015_05_15`,25)
`2015_05_29`=`2015_05_29`[order(`2015_05_29`$X..Float.Change),]
bottom2015_05_29 = tail(`2015_05_29`,25)
top2015_05_29 = head(`2015_05_29`,25)
`2015_06_15`=`2015_06_15`[order(`2015_06_15`$X..Float.Change),]
bottom2015_06_15 = tail(`2015_06_15`,25)
top2015_06_15 = head(`2015_06_15`,25)
`2015_06_30`=`2015_06_30`[order(`2015_06_30`$X..Float.Change),]
bottom2015_06_30 = tail(`2015_06_30`,25)
top2015_06_30 = head(`2015_06_30`,25)
`2015_07_15`=`2015_07_15`[order(`2015_07_15`$X..Float.Change),]
bottom2015_07_15 = tail(`2015_07_15`,25)
top2015_07_15 = head(`2015_07_15`,25)
`2015_07_31`=`2015_07_31`[order(`2015_07_31`$X..Float.Change),]
bottom2015_07_31 = tail(`2015_07_31`,25)
top2015_07_31 = head(`2015_07_31`,25)
`2015_08_14`=`2015_08_14`[order(`2015_08_14`$X..Float.Change),]
bottom2015_08_14 = tail(`2015_08_14`,25)
top2015_08_14 = head(`2015_08_14`,25)
`2015_08_31`=`2015_08_31`[order(`2015_08_31`$X..Float.Change),]
bottom2015_08_31 = tail(`2015_08_31`,25)
top2015_08_31 = head(`2015_08_31`,25)
`2015_09_15`=`2015_09_15`[order(`2015_09_15`$X..Float.Change),]
bottom2015_09_15 = tail(`2015_09_15`,25)
top2015_09_15 = head(`2015_09_15`,25)
`2015_09_30`=`2015_09_30`[order(`2015_09_30`$X..Float.Change),]
bottom2015_09_30 = tail(`2015_09_30`,25)
top2015_09_30 = head(`2015_09_30`,25)
`2015_10_15`=`2015_10_15`[order(`2015_10_15`$X..Float.Change),]
bottom2015_10_15 = tail(`2015_10_15`,25)
top2015_10_15 = head(`2015_10_15`,25)
`2015_10_30`=`2015_10_30`[order(`2015_10_30`$X..Float.Change),]
bottom2015_10_30 = tail(`2015_10_30`,25)
top2015_10_30 = head(`2015_10_30`,25)
`2015_11_13`=`2015_11_13`[order(`2015_11_13`$X..Float.Change),]
bottom2015_11_13 = tail(`2015_11_13`,25)
top2015_11_13 = head(`2015_11_13`,25)
#get QQQ market data for benchmark
getSymbols('QQQ', from="2015-01-01")
## [1] "QQQ"
Next we will process 2 data frames top and bottom for each of the 21 stocks. Since there are some modifications that need to be done such as checking the tickers for validity and removing expired tickers, we will have to run this per report and generate a lot of code.
Each block of code will do the following: Set buy date and sell dates ( 1week and 2 weeks), Check symbols for validity and if the symbols are valid, download market data for those symbols. Pull up the open prices of each stock for those dates and record it to portfolio data frame. Next we will calculate PNL and record it to appropriate data frames.
# 2015-01-15 Long
top2015_01_15<-top2015_01_15[!(top2015_01_15$Ticker=="ZU"),]
j = 1
portfolio = data.frame()
long_pnl = data.frame()
buydate = '2015-01-15'
selldate5 = calfunction(buydate, 5)
selldate10 = calfunction(buydate, 10)
for (i in 1:length(top2015_01_15) )
{
if (any(grep(top2015_01_15$Ticker[i], tickersList$Symbol)))
{
getSymbols(top2015_01_15$Ticker[i], from="2015-01-01")
portfolio[j,1] = top2015_01_15$Ticker[i]
portfolio[j,2] = as.numeric(get(top2015_01_15$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(top2015_01_15$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(top2015_01_15$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V3/portfolio$V2
portfolio$net10 = portfolio$V4/portfolio$V2
long_trades = rbind(portfolio)
long_pnl <- rbind(long_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
# 2015-01-15 Short
j = 1
portfolio = data.frame()
short_pnl = data.frame()
for (i in 1:length(bottom2015_01_15) )
{
if (any(grep(bottom2015_01_15$Ticker[i], tickersList$Symbol)))
{
getSymbols(bottom2015_01_15$Ticker[i], from="2015-01-01")
portfolio[j,1] = bottom2015_01_15$Ticker[i]
portfolio[j,2] = as.numeric(get(bottom2015_01_15$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(bottom2015_01_15$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(bottom2015_01_15$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V2/portfolio$V3
portfolio$net10 = portfolio$V2/portfolio$V4
short_trades = rbind(portfolio)
short_pnl <- rbind(short_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
#####################################################################################################
#2015_01_30 Long
top2015_01_30<-top2015_01_30[!(top2015_01_30$Ticker=="ZU"),]
j = 1
portfolio = data.frame()
buydate = '2015-01-30'
selldate5 = calfunction(buydate, 5)
selldate10 = calfunction(buydate, 10)
for (i in 1:length(top2015_01_30) )
{
if (any(grep(top2015_01_30$Ticker[i], tickersList$Symbol)))
{
getSymbols(top2015_01_30$Ticker[i], from="2015-01-01")
portfolio[j,1] = top2015_01_30$Ticker[i]
portfolio[j,2] = as.numeric(get(top2015_01_30$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(top2015_01_30$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(top2015_01_30$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V3/portfolio$V2
portfolio$net10 = portfolio$V4/portfolio$V2
long_trades = rbind(long_trades, portfolio)
long_pnl <- rbind(long_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
# 2015-01-30 Short
j = 1
portfolio = data.frame()
for (i in 1:length(bottom2015_01_30) )
{
if (any(grep(bottom2015_01_30$Ticker[i], tickersList$Symbol)))
{
getSymbols(bottom2015_01_30$Ticker[i], from="2015-01-01")
portfolio[j,1] = bottom2015_01_30$Ticker[i]
portfolio[j,2] = as.numeric(get(bottom2015_01_30$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(bottom2015_01_30$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(bottom2015_01_30$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V2/portfolio$V3
portfolio$net10 = portfolio$V2/portfolio$V4
short_trades = rbind(short_trades, portfolio)
short_pnl <- rbind(short_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
#####################################################################################################
#2015_02_13 Long
top2015_02_13<-top2015_02_13[!(top2015_02_13$Ticker=="ZU"),]
j = 1
portfolio = data.frame()
buydate = '2015-02-13'
selldate5 = calfunction(buydate, 5)
selldate10 = calfunction(buydate, 10)
for (i in 1:length(top2015_02_13) )
{
if (any(grep(top2015_02_13$Ticker[i], tickersList$Symbol)))
{
getSymbols(top2015_02_13$Ticker[i], from="2015-01-01")
portfolio[j,1] = top2015_02_13$Ticker[i]
portfolio[j,2] = as.numeric(get(top2015_02_13$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(top2015_02_13$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(top2015_02_13$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V3/portfolio$V2
portfolio$net10 = portfolio$V4/portfolio$V2
long_trades = rbind(long_trades, portfolio)
long_pnl <- rbind(long_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
# 2015-01-30 Short
j = 1
portfolio = data.frame()
for (i in 1:length(bottom2015_02_13) )
{
if (any(grep(bottom2015_02_13$Ticker[i], tickersList$Symbol)))
{
getSymbols(bottom2015_02_13$Ticker[i], from="2015-01-01")
portfolio[j,1] = bottom2015_02_13$Ticker[i]
portfolio[j,2] = as.numeric(get(bottom2015_02_13$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(bottom2015_02_13$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(bottom2015_02_13$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V2/portfolio$V3
portfolio$net10 = portfolio$V2/portfolio$V4
short_trades = rbind(short_trades, portfolio)
short_pnl <- rbind(short_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
#####################################################################################################
#2015_02_27 Long
top2015_02_27 = top2015_02_27[-1,]
top2015_02_27<-top2015_02_27[!(top2015_02_27$Ticker=="ZU"),]
j = 1
portfolio = data.frame()
buydate = '2015-02-27'
selldate5 = calfunction(buydate, 5)
selldate10 = calfunction(buydate, 10)
for (i in 1:length(top2015_02_27) )
{
if (any(grep(top2015_02_27$Ticker[i], tickersList$Symbol)))
{
#print(top2015_02_27$Ticker[i])
getSymbols(top2015_02_27$Ticker[i], from="2015-01-01")
portfolio[j,1] = top2015_02_27$Ticker[i]
portfolio[j,2] = as.numeric(get(top2015_02_27$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(top2015_02_27$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(top2015_02_27$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V3/portfolio$V2
portfolio$net10 = portfolio$V4/portfolio$V2
long_trades = rbind(long_trades, portfolio)
long_pnl <- rbind(long_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
# 2015-01-30 Short
j = 1
portfolio = data.frame()
for (i in 1:length(bottom2015_02_27) )
{
if (any(grep(bottom2015_02_27$Ticker[i], tickersList$Symbol)))
{
getSymbols(bottom2015_02_27$Ticker[i], from="2015-01-01")
portfolio[j,1] = bottom2015_02_27$Ticker[i]
portfolio[j,2] = as.numeric(get(bottom2015_02_27$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(bottom2015_02_27$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(bottom2015_02_27$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V2/portfolio$V3
portfolio$net10 = portfolio$V2/portfolio$V4
short_trades = rbind(short_trades, portfolio)
short_pnl <- rbind(short_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
#####################################################################################################
#2015_03_15 Long
top2015_03_15<-top2015_03_15[!(top2015_03_15$Ticker=="ZU"),]
j = 1
portfolio = data.frame()
buydate = '2015-03-16'
selldate5 = calfunction(buydate, 5)
selldate10 = calfunction(buydate, 10)
for (i in 1:length(top2015_03_15) )
{
if (any(grep(top2015_03_15$Ticker[i], tickersList$Symbol)))
{
#print(top2015_03_15$Ticker[i])
getSymbols(top2015_03_15$Ticker[i], from="2015-01-01")
portfolio[j,1] = top2015_03_15$Ticker[i]
portfolio[j,2] = as.numeric(get(top2015_03_15$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(top2015_03_15$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(top2015_03_15$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V3/portfolio$V2
portfolio$net10 = portfolio$V4/portfolio$V2
long_trades = rbind(long_trades, portfolio)
long_pnl <- rbind(long_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
# 2015-01-30 Short
j = 1
portfolio = data.frame()
for (i in 1:length(bottom2015_03_15) )
{
if (any(grep(bottom2015_03_15$Ticker[i], tickersList$Symbol)))
{
getSymbols(bottom2015_03_15$Ticker[i], from="2015-01-01")
portfolio[j,1] = bottom2015_03_15$Ticker[i]
portfolio[j,2] = as.numeric(get(bottom2015_03_15$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(bottom2015_03_15$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(bottom2015_03_15$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V2/portfolio$V3
portfolio$net10 = portfolio$V2/portfolio$V4
short_trades = rbind(short_trades, portfolio)
short_pnl <- rbind(short_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
#####################################################################################################
#2015_03_31 Long
top2015_03_31<-top2015_03_31[!(top2015_03_31$Ticker=="ZU"),]
j = 1
portfolio = data.frame()
buydate = '2015-03-31'
selldate5 = calfunction(buydate, 5)
selldate10 = calfunction(buydate, 10)
for (i in 1:length(top2015_03_31) )
{
if (any(grep(top2015_03_31$Ticker[i], tickersList$Symbol)))
{
getSymbols(top2015_03_31$Ticker[i], from="2015-01-01")
portfolio[j,1] = top2015_03_31$Ticker[i]
portfolio[j,2] = as.numeric(get(top2015_03_31$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(top2015_03_31$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(top2015_03_31$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V3/portfolio$V2
portfolio$net10 = portfolio$V4/portfolio$V2
long_trades = rbind(long_trades, portfolio)
long_pnl <- rbind(long_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
# 2015-01-30 Short
j = 1
portfolio = data.frame()
for (i in 1:length(bottom2015_03_31) )
{
if (any(grep(bottom2015_03_31$Ticker[i], tickersList$Symbol)))
{
getSymbols(bottom2015_03_31$Ticker[i], from="2015-01-01")
portfolio[j,1] = bottom2015_03_31$Ticker[i]
portfolio[j,2] = as.numeric(get(bottom2015_03_31$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(bottom2015_03_31$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(bottom2015_03_31$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V2/portfolio$V3
portfolio$net10 = portfolio$V2/portfolio$V4
short_trades = rbind(short_trades, portfolio)
short_pnl <- rbind(short_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
#####################################################################################################
#2015_04_15 Long
top2015_04_15<-top2015_04_15[!(top2015_04_15$Ticker=="ZU"),]
j = 1
portfolio = data.frame()
buydate = '2015-04-15'
selldate5 = calfunction(buydate, 5)
selldate10 = calfunction(buydate, 10)
for (i in 1:length(top2015_04_15) )
{
if (any(grep(top2015_04_15$Ticker[i], tickersList$Symbol)))
{
getSymbols(top2015_04_15$Ticker[i], from="2015-01-01")
portfolio[j,1] = top2015_04_15$Ticker[i]
portfolio[j,2] = as.numeric(get(top2015_04_15$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(top2015_04_15$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(top2015_04_15$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V3/portfolio$V2
portfolio$net10 = portfolio$V4/portfolio$V2
long_trades = rbind(long_trades, portfolio)
long_pnl <- rbind(long_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
# 2015-01-30 Short
j = 1
portfolio = data.frame()
for (i in 1:length(bottom2015_04_15) )
{
if (any(grep(bottom2015_04_15$Ticker[i], tickersList$Symbol)))
{
getSymbols(bottom2015_04_15$Ticker[i], from="2015-01-01")
portfolio[j,1] = bottom2015_04_15$Ticker[i]
portfolio[j,2] = as.numeric(get(bottom2015_04_15$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(bottom2015_04_15$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(bottom2015_04_15$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V2/portfolio$V3
portfolio$net10 = portfolio$V2/portfolio$V4
short_trades = rbind(short_trades, portfolio)
short_pnl <- rbind(short_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
#####################################################################################################
#2015_04_30 Long
top2015_04_30<-top2015_04_30[!(top2015_04_30$Ticker=="ZU"),]
bottom2015_04_30<-bottom2015_04_30[!(bottom2015_04_30$Ticker=="ZU"),]
j = 1
portfolio = data.frame()
buydate = '2015-04-30'
selldate5 = calfunction(buydate, 5)
selldate10 = calfunction(buydate, 10)
for (i in 1:length(top2015_04_30) )
{
if (any(grep(top2015_04_30$Ticker[i], tickersList$Symbol)))
{
getSymbols(top2015_04_30$Ticker[i], from="2015-01-01")
portfolio[j,1] = top2015_04_30$Ticker[i]
portfolio[j,2] = as.numeric(get(top2015_04_30$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(top2015_04_30$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(top2015_04_30$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V3/portfolio$V2
portfolio$net10 = portfolio$V4/portfolio$V2
long_trades = rbind(long_trades, portfolio)
long_pnl <- rbind(long_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
# 2015-01-30 Short
j = 1
portfolio = data.frame()
for (i in 1:length(bottom2015_04_30) )
{
if (any(grep(bottom2015_04_30$Ticker[i], tickersList$Symbol)))
{
getSymbols(bottom2015_04_30$Ticker[i], from="2015-01-01")
portfolio[j,1] = bottom2015_04_30$Ticker[i]
portfolio[j,2] = as.numeric(get(bottom2015_04_30$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(bottom2015_04_30$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(bottom2015_04_30$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V2/portfolio$V3
portfolio$net10 = portfolio$V2/portfolio$V4
short_trades = rbind(short_trades, portfolio)
short_pnl <- rbind(short_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
#####################################################################################################
#2015_05_15 Long
top2015_05_15<-top2015_05_15[!(top2015_05_15$Ticker=="SBLK"),]
top2015_05_15<-top2015_05_15[!(top2015_05_15$Ticker=="ZU"),]
j = 1
portfolio = data.frame()
buydate = '2015-05-15'
selldate5 = calfunction(buydate, 5)
selldate10 = calfunction(buydate, 11)
for (i in 1:length(top2015_05_15) )
{
if (any(grep(top2015_05_15$Ticker[i], tickersList$Symbol)))
{
#print(top2015_05_15$Ticker[i])
getSymbols(top2015_05_15$Ticker[i], from="2015-01-01")
portfolio[j,1] = top2015_05_15$Ticker[i]
portfolio[j,2] = as.numeric(get(top2015_05_15$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(top2015_05_15$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(top2015_05_15$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V3/portfolio$V2
portfolio$net10 = portfolio$V4/portfolio$V2
long_trades = rbind(long_trades, portfolio)
long_pnl <- rbind(long_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
# 2015-05-15 Short
j = 1
portfolio = data.frame()
for (i in 1:length(bottom2015_05_15) )
{
if (any(grep(bottom2015_05_15$Ticker[i], tickersList$Symbol)))
{
getSymbols(bottom2015_05_15$Ticker[i], from="2015-01-01")
portfolio[j,1] = bottom2015_05_15$Ticker[i]
portfolio[j,2] = as.numeric(get(bottom2015_05_15$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(bottom2015_05_15$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(bottom2015_05_15$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V2/portfolio$V3
portfolio$net10 = portfolio$V2/portfolio$V4
short_trades = rbind(short_trades, portfolio)
short_pnl <- rbind(short_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
#####################################################################################################
#2015_05_29 Long
top2015_05_29<-top2015_05_29[!(top2015_05_29$Ticker=="ZU"),]
j = 1
portfolio = data.frame()
buydate = '2015-05-29'
selldate5 = calfunction(buydate, 5)
selldate10 = calfunction(buydate, 10)
for (i in 1:length(top2015_05_29) )
{
if (any(grep(top2015_05_29$Ticker[i], tickersList$Symbol)))
{
getSymbols(top2015_05_29$Ticker[i], from="2015-01-01")
portfolio[j,1] = top2015_05_29$Ticker[i]
portfolio[j,2] = as.numeric(get(top2015_05_29$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(top2015_05_29$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(top2015_05_29$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V3/portfolio$V2
portfolio$net10 = portfolio$V4/portfolio$V2
long_trades = rbind(long_trades, portfolio)
long_pnl <- rbind(long_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
# 2015-05-29 Short
j = 1
portfolio = data.frame()
for (i in 1:length(bottom2015_05_29) )
{
if (any(grep(bottom2015_05_29$Ticker[i], tickersList$Symbol)))
{
getSymbols(bottom2015_05_29$Ticker[i], from="2015-01-01")
portfolio[j,1] = bottom2015_05_29$Ticker[i]
portfolio[j,2] = as.numeric(get(bottom2015_05_29$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(bottom2015_05_29$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(bottom2015_05_29$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V2/portfolio$V3
portfolio$net10 = portfolio$V2/portfolio$V4
short_trades = rbind(short_trades, portfolio)
short_pnl <- rbind(short_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
#####################################################################################################
#2015_06_15 Long
top2015_06_15<-top2015_06_15[!(top2015_06_15$Ticker=="ZU"),]
j = 1
portfolio = data.frame()
buydate = '2015-06-15'
selldate5 = calfunction(buydate, 5)
selldate10 = calfunction(buydate, 10)
for (i in 1:length(top2015_06_15) )
{
if (any(grep(top2015_06_15$Ticker[i], tickersList$Symbol)))
{
getSymbols(top2015_06_15$Ticker[i], from="2015-01-01")
portfolio[j,1] = top2015_06_15$Ticker[i]
portfolio[j,2] = as.numeric(get(top2015_06_15$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(top2015_06_15$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(top2015_06_15$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V3/portfolio$V2
portfolio$net10 = portfolio$V4/portfolio$V2
long_trades = rbind(long_trades, portfolio)
long_pnl <- rbind(long_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
# 2015-01-30 Short
j = 1
portfolio = data.frame()
for (i in 1:length(bottom2015_06_15) )
{
if (any(grep(bottom2015_06_15$Ticker[i], tickersList$Symbol)))
{
getSymbols(bottom2015_06_15$Ticker[i], from="2015-01-01")
portfolio[j,1] = bottom2015_06_15$Ticker[i]
portfolio[j,2] = as.numeric(get(bottom2015_06_15$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(bottom2015_06_15$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(bottom2015_06_15$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V2/portfolio$V3
portfolio$net10 = portfolio$V2/portfolio$V4
short_trades = rbind(short_trades, portfolio)
short_pnl <- rbind(short_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
#####################################################################################################
#2015_06_30 Long
top2015_06_30<-top2015_06_30[!(top2015_06_30$Ticker=="ZU"),]
j = 1
portfolio = data.frame()
buydate = '2015-06-30'
selldate5 = calfunction(buydate, 5)
selldate10 = calfunction(buydate, 10)
for (i in 1:length(top2015_06_30) )
{
if (any(grep(top2015_06_30$Ticker[i], tickersList$Symbol)))
{
getSymbols(top2015_06_30$Ticker[i], from="2015-01-01")
portfolio[j,1] = top2015_06_30$Ticker[i]
portfolio[j,2] = as.numeric(get(top2015_06_30$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(top2015_06_30$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(top2015_06_30$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V3/portfolio$V2
portfolio$net10 = portfolio$V4/portfolio$V2
long_trades = rbind(long_trades, portfolio)
long_pnl <- rbind(long_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
# 2015-01-30 Short
j = 1
portfolio = data.frame()
for (i in 1:length(bottom2015_06_30) )
{
if (any(grep(bottom2015_06_30$Ticker[i], tickersList$Symbol)))
{
getSymbols(bottom2015_06_30$Ticker[i], from="2015-01-01")
portfolio[j,1] = bottom2015_06_30$Ticker[i]
portfolio[j,2] = as.numeric(get(bottom2015_06_30$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(bottom2015_06_30$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(bottom2015_06_30$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V2/portfolio$V3
portfolio$net10 = portfolio$V2/portfolio$V4
short_trades = rbind(short_trades, portfolio)
short_pnl <- rbind(short_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
#####################################################################################################
#2015_07_15 Long
top2015_07_15<-top2015_07_15[!(top2015_07_15$Ticker=="ZU"),]
j = 1
portfolio = data.frame()
buydate = '2015-07-15'
selldate5 = calfunction(buydate, 5)
selldate10 = calfunction(buydate, 10)
for (i in 1:length(top2015_07_15) )
{
if (any(grep(top2015_07_15$Ticker[i], tickersList$Symbol)))
{
getSymbols(top2015_07_15$Ticker[i], from="2015-01-01")
portfolio[j,1] = top2015_07_15$Ticker[i]
portfolio[j,2] = as.numeric(get(top2015_07_15$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(top2015_07_15$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(top2015_07_15$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V3/portfolio$V2
portfolio$net10 = portfolio$V4/portfolio$V2
long_trades = rbind(long_trades, portfolio)
long_pnl <- rbind(long_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
# 2015-01-30 Short
j = 1
portfolio = data.frame()
for (i in 1:length(bottom2015_07_15) )
{
if (any(grep(bottom2015_07_15$Ticker[i], tickersList$Symbol)))
{
getSymbols(bottom2015_07_15$Ticker[i], from="2015-01-01")
portfolio[j,1] = bottom2015_07_15$Ticker[i]
portfolio[j,2] = as.numeric(get(bottom2015_07_15$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(bottom2015_07_15$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(bottom2015_07_15$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V2/portfolio$V3
portfolio$net10 = portfolio$V2/portfolio$V4
short_trades = rbind(short_trades, portfolio)
short_pnl <- rbind(short_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
#####################################################################################################
#2015_07_31 Long
top2015_07_31<-top2015_07_31[!(top2015_07_31$Ticker=="ZU"),]
j = 1
portfolio = data.frame()
buydate = '2015-07-31'
selldate5 = calfunction(buydate, 5)
selldate10 = calfunction(buydate, 10)
for (i in 1:length(top2015_07_31) )
{
if (any(grep(top2015_07_31$Ticker[i], tickersList$Symbol)))
{
getSymbols(top2015_07_31$Ticker[i], from="2015-01-01")
portfolio[j,1] = top2015_07_31$Ticker[i]
portfolio[j,2] = as.numeric(get(top2015_07_31$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(top2015_07_31$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(top2015_07_31$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V3/portfolio$V2
portfolio$net10 = portfolio$V4/portfolio$V2
long_trades = rbind(long_trades, portfolio)
long_pnl <- rbind(long_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
# 2015-01-30 Short
j = 1
portfolio = data.frame()
for (i in 1:length(bottom2015_07_31) )
{
if (any(grep(bottom2015_07_31$Ticker[i], tickersList$Symbol)))
{
getSymbols(bottom2015_07_31$Ticker[i], from="2015-01-01")
portfolio[j,1] = bottom2015_07_31$Ticker[i]
portfolio[j,2] = as.numeric(get(bottom2015_07_31$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(bottom2015_07_31$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(bottom2015_07_31$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V2/portfolio$V3
portfolio$net10 = portfolio$V2/portfolio$V4
short_trades = rbind(short_trades, portfolio)
short_pnl <- rbind(short_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
#####################################################################################################
#2015_08_14 Long
top2015_08_14<-top2015_07_31[!(top2015_08_14$Ticker=="ZU"),]
j = 1
portfolio = data.frame()
buydate = '2015-08-14'
selldate5 = calfunction(buydate, 5)
selldate10 = calfunction(buydate, 10)
for (i in 1:length(top2015_08_14) )
{
if (any(grep(top2015_08_14$Ticker[i], tickersList$Symbol)))
{
getSymbols(top2015_08_14$Ticker[i], from="2015-01-01")
portfolio[j,1] = top2015_08_14$Ticker[i]
portfolio[j,2] = as.numeric(get(top2015_08_14$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(top2015_08_14$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(top2015_08_14$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V3/portfolio$V2
portfolio$net10 = portfolio$V4/portfolio$V2
long_trades = rbind(long_trades, portfolio)
long_pnl <- rbind(long_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
# 2015-01-30 Short
j = 1
portfolio = data.frame()
for (i in 1:length(bottom2015_08_14) )
{
if (any(grep(bottom2015_08_14$Ticker[i], tickersList$Symbol)))
{
getSymbols(bottom2015_08_14$Ticker[i], from="2015-01-01")
portfolio[j,1] = bottom2015_08_14$Ticker[i]
portfolio[j,2] = as.numeric(get(bottom2015_08_14$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(bottom2015_08_14$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(bottom2015_08_14$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V2/portfolio$V3
portfolio$net10 = portfolio$V2/portfolio$V4
short_trades = rbind(short_trades, portfolio)
short_pnl <- rbind(short_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
#####################################################################################################
#2015_08_31 Long
top2015_08_31<-top2015_08_31[!(top2015_08_31$Ticker=="ZU"),]
top2015_08_31<-top2015_08_31[!(top2015_08_31$Ticker=="REXX"),]
j = 1
portfolio = data.frame()
buydate = '2015-08-31'
selldate5 = calfunction(buydate, 8)
selldate10 = calfunction(buydate, 10)
for (i in 1:length(top2015_08_31) )
{
if (any(grep(top2015_08_31$Ticker[i], tickersList$Symbol)))
{
#print(top2015_08_31$Ticker[i])
getSymbols(top2015_08_31$Ticker[i], from="2015-01-01")
portfolio[j,1] = top2015_08_31$Ticker[i]
portfolio[j,2] = as.numeric(get(top2015_08_31$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(top2015_08_31$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(top2015_08_31$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V3/portfolio$V2
portfolio$net10 = portfolio$V4/portfolio$V2
long_trades = rbind(long_trades, portfolio)
long_pnl <- rbind(long_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
# 2015-01-30 Short
j = 1
portfolio = data.frame()
for (i in 1:length(bottom2015_08_31) )
{
if (any(grep(bottom2015_08_31$Ticker[i], tickersList$Symbol)))
{
getSymbols(bottom2015_08_31$Ticker[i], from="2015-01-01")
portfolio[j,1] = bottom2015_08_31$Ticker[i]
portfolio[j,2] = as.numeric(get(bottom2015_08_31$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(bottom2015_08_31$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(bottom2015_08_31$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V2/portfolio$V3
portfolio$net10 = portfolio$V2/portfolio$V4
short_trades = rbind(short_trades, portfolio)
short_pnl <- rbind(short_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
#####################################################################################################
#2015_09_15 Long
top2015_09_15<-top2015_09_15[!(top2015_09_15$Ticker=="ZU"),]
j = 1
portfolio = data.frame()
buydate = '2015-09-15'
selldate5 = calfunction(buydate, 5)
selldate10 = calfunction(buydate, 10)
for (i in 1:length(top2015_09_15) )
{
if (any(grep(top2015_09_15$Ticker[i], tickersList$Symbol)))
{
getSymbols(top2015_09_15$Ticker[i], from="2015-01-01")
portfolio[j,1] = top2015_09_15$Ticker[i]
portfolio[j,2] = as.numeric(get(top2015_09_15$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(top2015_09_15$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(top2015_09_15$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V3/portfolio$V2
portfolio$net10 = portfolio$V4/portfolio$V2
long_trades = rbind(long_trades, portfolio)
long_pnl <- rbind(long_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
# 2015-01-30 Short
j = 1
portfolio = data.frame()
for (i in 1:length(bottom2015_09_15) )
{
if (any(grep(bottom2015_09_15$Ticker[i], tickersList$Symbol)))
{
getSymbols(bottom2015_09_15$Ticker[i], from="2015-01-01")
portfolio[j,1] = bottom2015_09_15$Ticker[i]
portfolio[j,2] = as.numeric(get(bottom2015_09_15$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(bottom2015_09_15$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(bottom2015_09_15$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V2/portfolio$V3
portfolio$net10 = portfolio$V2/portfolio$V4
short_trades = rbind(short_trades, portfolio)
short_pnl <- rbind(short_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
#####################################################################################################
#2015_09_30 Long
top2015_09_30<-top2015_09_30[!(top2015_09_30$Ticker=="ZU"),]
j = 1
portfolio = data.frame()
buydate = '2015-09-30'
selldate5 = calfunction(buydate, 5)
selldate10 = calfunction(buydate, 10)
for (i in 1:length(top2015_09_30) )
{
if (any(grep(top2015_09_30$Ticker[i], tickersList$Symbol)))
{
getSymbols(top2015_09_30$Ticker[i], from="2015-01-01")
portfolio[j,1] = top2015_09_30$Ticker[i]
portfolio[j,2] = as.numeric(get(top2015_09_30$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(top2015_09_30$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(top2015_09_30$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V3/portfolio$V2
portfolio$net10 = portfolio$V4/portfolio$V2
long_trades = rbind(long_trades, portfolio)
long_pnl <- rbind(long_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
# 2015-01-30 Short
j = 1
portfolio = data.frame()
for (i in 1:length(bottom2015_09_30) )
{
if (any(grep(bottom2015_09_30$Ticker[i], tickersList$Symbol)))
{
getSymbols(bottom2015_09_30$Ticker[i], from="2015-01-01")
portfolio[j,1] = bottom2015_09_30$Ticker[i]
portfolio[j,2] = as.numeric(get(bottom2015_09_30$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(bottom2015_09_30$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(bottom2015_09_30$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V2/portfolio$V3
portfolio$net10 = portfolio$V2/portfolio$V4
short_trades = rbind(short_trades, portfolio)
short_pnl <- rbind(short_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
#####################################################################################################
#2015_10_15 Long
top2015_10_15<-top2015_10_15[!(top2015_10_15$Ticker=="LIVN"),]
top2015_10_15<-top2015_10_15[!(top2015_10_15$Ticker=="NRX"),]
top2015_10_15<-top2015_10_15[!(top2015_10_15$Ticker=="ZU"),]
j = 1
portfolio = data.frame()
buydate = '2015-10-15'
selldate5 = calfunction(buydate, 5)
selldate10 = calfunction(buydate, 10)
for (i in 1:length(top2015_10_15) )
{
if (any(grep(top2015_10_15$Ticker[i], tickersList$Symbol)))
{
#print(top2015_10_15$Ticker[i])
getSymbols(top2015_10_15$Ticker[i], from="2015-01-01")
portfolio[j,1] = top2015_10_15$Ticker[i]
portfolio[j,2] = as.numeric(get(top2015_10_15$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(top2015_10_15$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(top2015_10_15$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V3/portfolio$V2
portfolio$net10 = portfolio$V4/portfolio$V2
long_trades = rbind(long_trades, portfolio)
long_pnl <- rbind(long_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
# 2015-01-30 Short
j = 1
portfolio = data.frame()
for (i in 1:length(bottom2015_10_15) )
{
if (any(grep(bottom2015_10_15$Ticker[i], tickersList$Symbol)))
{
getSymbols(bottom2015_10_15$Ticker[i], from="2015-01-01")
portfolio[j,1] = bottom2015_10_15$Ticker[i]
portfolio[j,2] = as.numeric(get(bottom2015_10_15$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(bottom2015_10_15$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(bottom2015_10_15$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V2/portfolio$V3
portfolio$net10 = portfolio$V2/portfolio$V4
short_trades = rbind(short_trades, portfolio)
short_pnl <- rbind(short_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
#####################################################################################################
#2015_10_30 Long
top2015_10_30<-top2015_10_30[!(top2015_10_30$Ticker=="NRX"),]
j = 1
portfolio = data.frame()
buydate = '2015-10-30'
selldate5 = calfunction(buydate, 5)
selldate10 = calfunction(buydate, 10)
for (i in 1:length(top2015_10_30) )
{
if (any(grep(top2015_10_30$Ticker[i], tickersList$Symbol)))
{
getSymbols(top2015_10_30$Ticker[i], from="2015-01-01")
portfolio[j,1] = top2015_10_30$Ticker[i]
portfolio[j,2] = as.numeric(get(top2015_10_30$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(top2015_10_30$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(top2015_10_30$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V3/portfolio$V2
portfolio$net10 = portfolio$V4/portfolio$V2
long_trades = rbind(long_trades, portfolio)
long_pnl <- rbind(long_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
# 2015-01-30 Short
j = 1
portfolio = data.frame()
for (i in 1:length(bottom2015_10_30) )
{
if (any(grep(bottom2015_10_30$Ticker[i], tickersList$Symbol)))
{
getSymbols(bottom2015_10_30$Ticker[i], from="2015-01-01")
portfolio[j,1] = bottom2015_10_30$Ticker[i]
portfolio[j,2] = as.numeric(get(bottom2015_10_30$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(bottom2015_10_30$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(bottom2015_10_30$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V2/portfolio$V3
portfolio$net10 = portfolio$V2/portfolio$V4
short_trades = rbind(short_trades, portfolio)
short_pnl <- rbind(short_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
#####################################################################################################
#2015_11_13 Long
top2015_10_15<-top2015_11_13[!(top2015_11_13$Ticker=="NRX"),]
j = 1
portfolio = data.frame()
buydate = '2015-11-13'
selldate5 = calfunction(buydate, 5)
selldate10 = calfunction(buydate, 10)
for (i in 1:length(top2015_11_13) )
{
if (any(grep(top2015_11_13$Ticker[i], tickersList$Symbol)))
{
getSymbols(top2015_11_13$Ticker[i], from="2015-01-01")
portfolio[j,1] = top2015_11_13$Ticker[i]
portfolio[j,2] = as.numeric(get(top2015_11_13$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(top2015_11_13$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(top2015_11_13$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V3/portfolio$V2
portfolio$net10 = portfolio$V4/portfolio$V2
long_trades = rbind(long_trades, portfolio)
long_pnl <- rbind(long_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
# 2015-01-30 Short
j = 1
portfolio = data.frame()
for (i in 1:length(bottom2015_11_13) )
{
if (any(grep(bottom2015_11_13$Ticker[i], tickersList$Symbol)))
{
getSymbols(bottom2015_11_13$Ticker[i], from="2015-01-01")
portfolio[j,1] = bottom2015_11_13$Ticker[i]
portfolio[j,2] = as.numeric(get(bottom2015_11_13$Ticker[i])[buydate,1])
portfolio[j,3] = as.numeric(get(bottom2015_11_13$Ticker[i])[selldate5,1])
portfolio[j,4] = as.numeric(get(bottom2015_11_13$Ticker[i])[selldate10,1])
j = j + 1
}
}
portfolio$net5 = portfolio$V2/portfolio$V3
portfolio$net10 = portfolio$V2/portfolio$V4
short_trades = rbind(short_trades, portfolio)
short_pnl <- rbind(short_pnl, data.frame("date"=buydate, "net5"=sum(portfolio$net5)/(nrow(portfolio)), "net10"=sum(portfolio$net10)/(nrow(portfolio)), "NASDAQ5" = as.numeric(get("QQQ")[selldate5,1])/as.numeric(get("QQQ")[buydate,1]), "NASDAQ10" = as.numeric(get("QQQ")[selldate10,1])/as.numeric(get("QQQ")[buydate,1])))
As a result of data preparation we have the following datasets: long_pnl, short_pnl, trades_long and trades_short. Long and short pnl contains overall performance of the strategy per report so each has 21 entries. trades long and short contain all individual trades and performance for long and short strategy respectively.
kable(head(`long_pnl`))
| date | net5 | net10 | NASDAQ5 | NASDAQ10 |
|---|---|---|---|---|
| 2015-01-15 | 0.9731664 | 0.9923764 | 1.0000000 | 1.0267179 |
| 2015-01-30 | 0.9704629 | 0.9650194 | 1.0072691 | 1.0091356 |
| 2015-02-13 | 1.0047027 | 0.9620054 | 1.0054562 | 1.0192850 |
| 2015-02-27 | 1.0063600 | 1.0189153 | 0.9969681 | 0.9888828 |
| 2015-03-16 | 0.9835372 | 0.9644926 | 1.0259150 | 0.9892178 |
| 2015-03-31 | 0.9694813 | 1.0696576 | 0.9818660 | 1.0078925 |
kable(head(`long_trades`))
| V1 | V2 | V3 | V4 | net5 | net10 |
|---|---|---|---|---|---|
| SAGE | 40.10 | 38.00 | 37.71 | 0.9476310 | 0.9403990 |
| FMI | 49.99 | 47.31 | 47.50 | 0.9463893 | 0.9501900 |
| TERP | 28.86 | 30.31 | 32.11 | 1.0502425 | 1.1126126 |
| KPTI | 28.49 | 27.02 | 27.53 | 0.9484029 | 0.9663040 |
| ZFGN | 40.50 | 38.37 | 37.86 | 0.9474074 | 0.9348148 |
| VSAR | 18.58 | 18.00 | 17.40 | 0.9687836 | 0.9364909 |
Next we can look at 4 histograms and validate the data collected. We will take a look at long performance for 1 week and 2 weeks and short performance for 1 week and 2 weeks(net5 = 1 week, net10 = 2 weeks) The center of histogram is at 1, so for example, 1.05 represents a 5 % gain and 0.95 represents a 5% loss for each of the trades.
hist(short_trades$net5)
hist(long_trades$net5)
hist(short_trades$net10)
hist(long_trades$net10)
For net10 the data distribution appears to be pretty close to normal. net5 has a data skewed, but since we have n>30 we can proceed with the test. It should be noted that the data is not independent and was collected based on the specific selection, but we can go ahead with the test for this study.
We will setup the following Hypotheses for 4 tests:
H0 - mean of performance of portfolio = Mean on QQQ performance Ha - means are not equal.
t.test(short_pnl$net5,short_pnl$NASDAQ5)
##
## Welch Two Sample t-test
##
## data: short_pnl$net5 and short_pnl$NASDAQ5
## t = -0.79064, df = 24.746, p-value = 0.4367
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.02718080 0.01210656
## sample estimates:
## mean of x mean of y
## 0.998944 1.006481
t.test(short_pnl$net10,short_pnl$NASDAQ10)
##
## Welch Two Sample t-test
##
## data: short_pnl$net10 and short_pnl$NASDAQ10
## t = 0.76277, df = 31.526, p-value = 0.4513
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.02264581 0.04973363
## sample estimates:
## mean of x mean of y
## 1.016563 1.003019
t.test(long_pnl$net5,long_pnl$NASDAQ5)
##
## Welch Two Sample t-test
##
## data: long_pnl$net5 and long_pnl$NASDAQ5
## t = 0.58205, df = 23.072, p-value = 0.5662
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.01733999 0.03092164
## sample estimates:
## mean of x mean of y
## 1.013272 1.006481
t.test(long_pnl$net5,long_pnl$NASDAQ5)
##
## Welch Two Sample t-test
##
## data: long_pnl$net5 and long_pnl$NASDAQ5
## t = 0.58205, df = 23.072, p-value = 0.5662
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.01733999 0.03092164
## sample estimates:
## mean of x mean of y
## 1.013272 1.006481
Looks like we can reject the null hypotheses and conclude that the means are not equal.
Next We want to plot relative performance, but to do that we need to convert data in long_pnl and short_pnl from wide to long format
library(tidyr)
long <- long_pnl %>% gather(Type, Net, 2:5)
short <- short_pnl %>% gather(Type, Net, 2:5)
Next we plot the long and short strategies and compare them with NASDAQ 1 week and 2 week performance.
library(ggplot2)
ggplot(data=long,
aes(x=date, y=Net, colour = Type, group = Type)) + geom_point() +
geom_line() +
labs(x = "Date", y = "Performance",
title = "Relative performance of 1 week and 2 week long strategy vs NASDAQ 5 day and NASDAQ 10 day")
ggplot(data=short,
aes(x=date, y=Net, colour = Type, group = Type)) + geom_point() +
geom_line() +
labs(x = "Date", y = "Performance",
title = "Relative performance of 1 week and 2 week short strategy vs NASDAQ 5 day and NASDAQ 10 day")
Next we will aggregate the data and create a synthetic portfolio of $1000 and see how it performs for the long strategy, short strategy and long/short strategy and benchmark it against overall performance of QQQ for the period of 01-15-2015 to 11-13-2015. I will need to export data to excel to create a compounded table and import it back to R. For QQQ we will purchase 9.859016 shares of QQQ on 2015-01-15 for the price of 101.43 for the total of $1000.00 and then track the performance of 9.859016 shares.
first lets model portfolio starting with $1000.00 invested into QQQ
dates = c('2015-01-15', '2015-01-30', '2015-02-13', '2015-02-17', '2015-03-16', '2015-03-31', '2015-04-15', '2015-04-30', '2015-05-15', '2015-05-29', '2015-06-15', '2015-06-30', '2015-07-15', '2015-07-31', '2015-08-14', '2015-08-31', '2015-09-15', '2015-09-30', '2015-10-15', '2015-10-30', '2015-11-13')
NASDAQ.df = data.frame()
for (i in 1:21)
{
NASDAQ.df[i,1] = as.numeric(QQQ[dates[i],1])*9.859016
NASDAQ.df[i,2] = long_pnl$date[i]
}
kable(NASDAQ.df)
| V1 | V2 |
|---|---|
| 1000.0000 | 2015-01-15 |
| 1003.6479 | 2015-01-30 |
| 1048.0134 | 2015-02-13 |
| 1054.0274 | 2015-02-27 |
| 1042.3938 | 2015-03-16 |
| 1049.2951 | 2015-03-31 |
| 1059.3512 | 2015-04-15 |
| 1071.9708 | 2015-04-30 |
| 1083.1115 | 2015-05-15 |
| 1090.8015 | 2015-05-29 |
| 1064.1822 | 2015-06-15 |
| 1059.7456 | 2015-06-30 |
| 1089.1255 | 2015-07-15 |
| 1108.4492 | 2015-07-31 |
| 1085.2805 | 2015-08-14 |
| 1035.4924 | 2015-08-31 |
| 1040.1262 | 2015-09-15 |
| 994.6761 | 2015-09-30 |
| 1048.2106 | 2015-10-15 |
| 1122.8433 | 2015-10-30 |
| 1099.9704 | 2015-11-13 |
next we import the csv with aggregated data and finally plot the results
port.df = read.csv("C:/Users/joseph/Downloads/Short Interest/NASDAQ 2015/portfolio.csv")
ggplot(data = port.df, aes(X1, Long_net5)) +geom_line() +geom_point() +labs(x = "Jan 15 2015 - Nov 13 2015", y = "$ Performance", title = "$1000 invested into Long 1week Strategy")
ggplot(data = port.df, aes(X1, Long_net10)) +geom_line() +geom_point() +labs(x = "Jan 15 2015 - Nov 13 2015", y = "$ Performance", title = "$1000 invested into Long 2week Strategy")
ggplot(data = port.df, aes(X1, Short_net5)) +geom_line() +geom_point() +labs(x = "Jan 15 2015 - Nov 13 2015", y = "$ Performance", title = "$1000 invested into Short 1week Strategy")
ggplot(data = port.df, aes(X1, Short_net10)) +geom_line() +geom_point() +labs(x = "Jan 15 2015 - Nov 13 2015", y = "$ Performance", title = "$1000 invested into Short 2week Strategy")
ggplot(data = port.df, aes(X1, LSnet5)) +geom_line() +geom_point() +labs(x = "Jan 15 2015 - Nov 13 2015", y = "$ Performance", title = "$1000 invested into Long/Short 1week Strategy")
ggplot(data = port.df, aes(X1, LSnet10)) +geom_line() +geom_point() +labs(x = "Jan 15 2015 - Nov 13 2015", y = "$ Performance", title = "$1000 invested into Long/Short 2 week Strategy")
ggplot(data = NASDAQ.df, aes(V2, V1, group=1)) +geom_line() +geom_point() +labs(x = "Jan 15 2015 - Nov 13 2015", y = "$ Performance", title = "$1000 invested into NASDAQ COMPOSITE")
This has been an interesting project. I encountered a number of issues that I have not anticipated, but yet I think I have interesting results that raised more questions and will most likely push me to dig deeper into the data and search for better results. Before we get to the final conclusions, I want to note that this simulation has not taken into consideration any commission charged by the broker and the fees related to the interest that needs to be paid for holding any short positions. Also I had to ignore a lot of data since the API I am using does not provide market data for delisted stocks. I have found that there were a number of companies that have been aquired and a number of companies that have been delisted or moved to pink sheets and there was no market data available for those stocks, which is unfortunate, because it is safe to assume that those exact companies are usually the ones generating significant move in a stock price ahead of acquisition or a bankruptcy which we unfortunately did not capture in this study due to limitations of market data API which was used.
The results are interesting and surprisingly not very consistent between 1 week and 2 week strategies. So, for the Long strategies we have 32% gain for 1 week and 10% gain for 2 weeks and for the Short strategy we have -5% for 1 week and 29% gain for 2 week strategy. So we have a significant out performance of 1 week strategy for the longs and significant out performance of the 2 week strategy for the shorts. This does perfectly balance out for the Long/Short strategy which assumes 50% invested into shorts and 50% invested into longs and for the Long/Short strategy we have a gains of 13% for 1 week and 15% gains for the 2 weeks which both beat the performance of Nasdaq which was 10%.
The Long/Short strategy did outperform the market and also showed less volatility and did not produce more then 5% decline over the period. Also it might seem that the long strategies performed better, but it seems that the overall market was bullish for the period and during the bearish moves in the market the 2 week long strategy showed significant losses for the time and was down 30% before sharply rebounding, so clearly a market neutral strategy is a much safer choice unless one is bias towards the potential market move direction.
I think there are lot of improvements that could be made to the strategy: 1. Stop/loss triggers. Some of the significant losses where mostly due to several highly volatile stocks and when a strategy did not pick the correct direction, the losing stock would produce heavy losses. a 5 or 10% stop loss order placed beforehand could potentially prevent heavy losses from single highly volatile stocks. 2. If we were to update the stratgey and look for changes in % of float for stocks that have upcoming Earnings, I believe we could potentially improve the strategy. This theory is worth testing. 3. Better source of market data would certainly be more beneficial. 4. Different source of reports with more up to date data would certainly help. 5. Some code improvement would make this project easier to continue working on. Once we have more standardized and reliable data, functions can be used to process each report instead of going through each report individually.