library(ggplot2)
install.packages("plyr")
library(plyr)
library(reshape2)
library(zoo)
library(XML)
setwd("E:\\Program Files\\RStudio\\work\\4建模分析股票市场数据")
finviz<-read.csv("finviz.csv")
head(finviz)
summary(finviz)
clean_numeric<-function(s){
s<-gsub("%|\\$|,|\\)|\\(","",s)
s<-as.numeric(s)
}
finviz<-cbind(finviz[,1:6],apply(finviz[,7:68],2,clean_numeric))
hist(finviz$Price,breaks=100,main="Price Distribution",xlab="Price")
hist(finviz$Price[finviz$Price<150],breaks=100,main="PriceDistribution",xlab="Price")
sector_avg_prices<-aggregate(Price~Sector,data=finviz,FUN="mean")
colnames(sector_avg_prices)[2]<-"Sector_Avg_Price"
ggplot(sector_avg_prices,aes(x=Sector,y=Sector_Avg_Price,fill=Sector))+geom_bar(stat="identity")+ggtitle("Sector Avg Prices")+
theme(axis.text.x=element_text(angle=90,hjust=1))
industry_avg_prices<-aggregate(Price~Sector+Industry,data=finviz,FUN="mean")
industry_avg_prices<-industry_avg_prices[order(industry_avg_prices$Sector,industry_avg_prices$Industry),]
colnames(industry_avg_prices)[3]<-"Industry_Avg_Price"
industry_chart<-subset(industry_avg_prices,Sector=="Financial")
ggplot(industry_chart,aes(x=Industry,y=Industry_Avg_Price,fill=Industry))+geom_bar(stat="identity")+theme(legend.position="none")+
ggtitle("Industry Avg Prices")+
theme(axis.text.x=element_text(angle=90,hjust=1))
company_chart<-subset(finviz,Industry=="Property & Casualty Insurance")
ggplot(company_chart,aes(x=Company,y=Price,fill=Company))+geom_bar(stat="identity")+theme(legend.position="none")+ggtitle("Company Avg Prices")+
theme(axis.text.x=element_text(angle=90,hjust=1))
finviz<-subset(finviz,Ticker!="BRK-A")
sector_avg_prices<-aggregate(Price~Sector,data=finviz,FUN="mean")
colnames(sector_avg_prices)[2]<-"Sector_Avg_Price"
ggplot(sector_avg_prices,aes(x=Sector,y=Sector_Avg_Price,fill=Sector))+geom_bar(stat="identity")+ggtitle("Sector_Avg_Price")+
theme(axis.text.x=element_text(angle=90,hjust=1))
sector_avg<-melt(finviz,id="Sector")
sector_avg<-subset(sector_avg,variable%in%c("Price","P.E","PEG","P.S","P.B"))
sector_avg<-(na.omit(sector_avg))
sector_avg$value<-as.numeric(sector_avg$value)
sector_avg<-dcast(sector_avg,Sector~variable,mean)
colnames(sector_avg)[2:6]<-c("SAvgPE","SAvgPEG","SAvgPS","SAvgPB","SAvgPrice")
industry_avg<-melt(finviz,id=c("Sector","Industry"))
industry_avg<-subset(industry_avg,variable%in%c("Price","P.E","PEG","P.S","P.B"))
industry_avg<-(na.omit(industry_avg))
industry_avg$value<-as.numeric(industry_avg$value)
industry_avg<-dcast(industry_avg,Sector+Industry~variable,mean)
industry_avg<-(na.omit(industry_avg))
colnames(industry_avg)[3:7]<-c("IAvgPE","IAvgPEG","IAvgPS","IAvgPB","IAvgPrice")
finviz<-merge(finviz,sector_avg,by.x="Sector",by.y="Sector")
finviz<-merge(finviz,industry_avg,by.x=c("Sector","Industry"),by.y=c("Sector","Industry"))
finviz$SPEUnder<-0
finviz$SPEGUnder<-0
finviz$SPSUnder<-0
finviz$SPBUnder<-0
finviz$SPriceUnder<-0
finviz$IPEUnder<-0
finviz$IPEGUnder<-0
finviz$IPSUnder<-0
finviz$SPEUnder[finviz$P.E<finviz$SAvgPE]<-1
finviz$SPEGUnder[finviz$PEG<finviz$SAvgPEG]<-1
finviz$SPSUnder[finviz$P.S<finviz$SAvgPS]<-1
finviz$SPBUnder[finviz$P.B<finviz$SAvgPB]<-1
finviz$SPriceUnder[finviz$Price<finviz$SAvgPrice]<-1
finviz$IPEUnder[finviz$P.E<finviz$IAvgPE]<-1
finviz$IPEGUnder[finviz$PEG<finviz$IAvgPEG]<-1
finviz$IPSUnder[finviz$P.S<finviz$IAvgPS]<-1
finviz$IPBUnder[finviz$P.B<finviz$IAvgPB]<-1
finviz$IPriceUnder[finviz$Price<finviz$IAvgPrice]<-1
finviz$RelValIndex<-apply(finviz[79:88],1,sum)
potentially_undervalued<-subset(finviz,RelValIndex>=8)
target_stocks<-subset(finviz,Price>20&Price<100&Volume>10000&Country=="USA"&
EPS..ttm.>0&
EPS.growth.next.year>0&
EPS.growth.next.5.years>0&
Total.Debt.Equity<1&Beta<1.5&
Institutional.Ownership<30&
RelValIndex>8)
stock0<-read.csv("NRCIB.CSV")
stock0<-cbind(symbol="NRCIB",stock0)
stock1<-read.csv("WPZ.csv")
stock1<-cbind(symbol="WPZ",stock1)
stock<-rbind(stock0,stock1)
stock2<-read.csv("TCAP.csv")
stock2<-cbind(symbol="TCAP",stock2)
stock<-rbind(stock,stock2)
stock3<-read.csv("AAN.csv")
stock3<-cbind(symbol="AAN",stock3)
stock<-rbind(stock,stock3)
stock4<-read.csv("OMI.csv")
stock4<-cbind(symbol="OMI",stock4)
stock<-rbind(stock,stock4)
stock5<-read.csv("MAIN.csv")
stock5<-cbind(symbol="MAIN",stock5)
stock<-rbind(stock,stock5)
maxrow<-nrow(stock5)-49
ma50<-cbind(stock5[1:maxrow,1:2],rollmean(stock5$Adj.Close,50,align="right"))
maxrow<-nrow(stock5)-199
ma200<-cbind(stock5[1:maxrow,1:2],rollmean(stock5$Adj.Close,200,align="right"))
stock5<-merge(stock5,ma50,by.x=c("symbol","Date"),by.y=c("symbol","Date"),all.x=TRUE)
colnames(stock5)[9]<-"MovAvg50"
stock5<-merge(stock5,ma200,by.x=c("symbol","Date"),by.y=c("symbol","Date"),all.x=TRUE)
colnames(stock5)[10]<-"MovAvg200"
stock5[,2]<-as.Date(stock5[,2])
price_chart<-melt(stock5[,c(1,2,7,9,10)],id=c("symbol","Date"))
qplot(Date,value,data=price_chart,geom="line",color=variable,main=paste("OMI","Daily Stock Price"),ylab="Price")
ggsave(filename=paste0("stock_price_",5,".png"))
评论5