#MYSQL链接
library(RODBC)
library(zoo)
library(TTR)
channel<-odbcConnect(dsn="mysql")
odbcGetInfo(channel=channel) #得到数据库信息
start_date<-"2015-01-01"
close_date<-as.character(Sys.Date()-1)
sql<-paste("select","Date,2_industry,todayConsume","from","industry2_consume","where","Date",">=","‘",start_date,"‘","and","Date","<","‘",close_date,"‘","and","2_departmentID=200","and","2_industry!=‘其他‘",";",sep=" ")
raw_data<-sqlQuery(channel=channel,query=sql)
#数据消费处理
col_consuption<-raw_data[order(raw_data[,2]),]
matrix_consuption<-split(col_consuption$todayConsume,list(col_consuption[[2]]))
matrix_industry_result<-do.call(‘cbind‘,matrix_consuption)
frame_industry_result<-data.frame(matrix_industry_result)
#移动平均幅度
ma<-function(cdata){
ldata<-cdata
ldata<-(SMA(cdata,7)-SMA(cdata,14))/SMA(cdata,14)
ldata<-na.locf(ldata,fromLast=TRUE)
return(ldata)
}
result<-lapply(frame_industry_result,ma)
for (m in 1:length(result)){
if (length(result[[m]]) == 0)
result[[m]]<-NULL
}
hot_industry<-NULL
cold_industry<-NULL
mid_industry<-NULL
for (k in 1:length(result)){
if (result[[k]][length(result[[k]])] >=0.045) hot_industry<-c(hot_industry,names(result)[k])
else if (result[[k]][length(result[[k]])] <=-0.00) cold_industry<-c(cold_industry,names(result)[k])
else mid_industry<-c(mid_industry,names(result)[k])
}