同花順股票分數視覺化 | R爬蟲&視覺化第3季

weixin_33890499發表於2017-12-25

歡迎關注天善智慧,我們是專注於商業智慧BI,人工智慧AI,大資料分析與挖掘領域的垂直社群,學習,問答、求職一站式搞定!

本文作者:天善智慧社群專家徐麟

天善智慧社群地址:https://www.hellobi.com/


往期推薦:

衛視實時收視率對比 | R爬蟲&視覺化第1季

當古代文人蔘加“中國好詩人”節目 | R爬蟲&視覺化第2季

前言

前兩期的R爬蟲&視覺化專題中,與大家分別分享了關於電視臺收視率以及詩詞的爬取和視覺化的內容,得到了大家許多的反饋,希望在今後的內容中能夠不斷提高。

第三期的專題中,會與大家聊聊非常火的股市,關於本期內容,引用一句非常經典的話“我們不生產資料,我們只是資料的搬運工”。影響股市大盤漲跌的因素非常多,到了個股走勢就更加撲朔迷離,所謂“股市有風險,投資需謹慎”,本文只是與大家分享一些爬蟲&視覺化的內容,不能作為大家選股的依據。

相關Package

## 字串處理、彙總資料

library(plyr)

library(stringr)

library(sqldf)

## 爬蟲相關

library(RCurl)

library(XML)

## 讀取資料

library(data.table)

## 資料視覺化

library(ggplot2)

library(ggthemes)

library(ggradar)

##markdown檔案生成

library(knitr)

library(rmarkdown)

爬取資料

我們爬取了同花順當天對股票的打分,同花順作為一個專業機構,每天對於個股都會有技術面、資金面、訊息面、行業面、基本面等五個方面的打分。我們會分別爬取這五部分的分數,並計算其總分。

3901436-82522611c96f2535.png

爬取程式碼如下:

url <- paste('http://doctor.10jqka.com.cn/',substr(point$rcode[i],1,6),'/',sep='')

temp <- getURL(url,.encoding='utf-8')

doc <-htmlParse(temp)

points <- getNodeSet(doc,'//div[@class="chart_base"]/

            div[@class="column_3d"]/div[@class="label"]')

points <- sapply(points,xmlValue)

point$technical[i] <-as.numeric(substr(points[1],1,3))

point$funds[i] <-as.numeric(substr(points[2],1,3))

point$message[i] <-as.numeric(substr(points[3],1,3))

point$trade[i] <-as.numeric(substr(points[4],1,3))

point$basic[i] <-as.numeric(substr(points[5],1,3))

point$level[i] <-sapply(getNodeSet(doc,'//span[@class="cur"]'),xmlValue)

資料展示

首先看一下各主要行業分數的盒型圖:

ggplot(data=point_total,aes(x=area,y=total_num))+geom_boxplot()+

                    theme_economist()+ggtitle("主要行業得分分佈圖")+

                    theme(axis.text.x =element_text(size=7.85),

plot.title =element_text(hjust=0.5,size=25))

3901436-9b0d682c95b0cb9f.png

針對某一行業,我們選擇其中一部分對比其雷達圖:

p <- ggradar(plot.data = subset(point_total,select=c('name','技術面','資金面','訊息面','行業面','基本面'))[1:5,],grid.max=10,grid.mid=5,

legend.text.size=7,background.circle.transparency=0)+theme_wsj()+theme(panel.grid=element_blank(),

axis.line = element_blank(),

axis.text = element_blank())

print(p)

3901436-4a23d8d151612b37.png

MARKDOWN輸出

下面我們用RMarkdown實現將多隻股票的分數變化面積圖批量輸出的功能:

```{r pressure,echo=FALSE,fig.width=15,fig.height=5}

library(reshape2)

library(ggplot2)

library(ggthemes)

library(stringr)

point_total <- read.csv('D:/index/point_total.csv',

header =TRUE)

point_total$name<- gsub(" ","",point_total$name)

point_total <- subset(point_total,name %in% c(

'****','****','****','****','****','****','****'

))

point_total$dt<- substr(point_total$dt,1,5)

index_name <- unique(point_total$name)

for(i in1:length(index_name)){

point = subset(point_total,name == index_name[i])

print(gsub(" ","",as.character(index_name[i])))

names(point)[2:6] <- c('技術面','資金面','訊息面','行業面','基本面')

point$sum_point<- apply(point[,2:6],1,sum)

point$com1<- paste(point$sum_point,sep='')

point$com2<- paste(point$paiming,sep='')

point_view <- melt(point[,c(2:6,9)],id.vars ='dt',variable.name ='分面')

p <- ggplot()+geom_area(aes(x=dt,y=value,fill=分面,group=分面),

data=point_view)+

geom_text(aes(x=dt,y=sum_point+2,label=com2),data=point,size=4)+

geom_text(aes(x=dt,y=sum_point+4,label=com1),data=point,size=4)+

theme_wsj()+theme(

panel.grid.major = element_blank()

)

print(p)

}

```

輸出效果如下:

3901436-1cc8353c127252f1.png

R針對股票可以做更加細緻的分析,如quantmod包可以直接獲得股票的每日收盤價等重要資訊。比如如下兩行程式碼就可以畫出讓人望塵莫及,買1手都要斥巨資的貴州茅臺的K線圖(需要注意的是R中使用的是美股K線圖,紅綠表示與A股相反)

getSymbols("600519.ss",src="yahoo",from="2017-01-01", to='2017-10-30')

chartSeries(`600519.SS`)

3901436-a83c337086a9a187.png

因為本人並不炒股,對股市沒有仔細的研究,所以本文只是做一些爬取&視覺化的內容,希望對股票有深入研究的朋友能夠與我交流,共同學習。


歡迎關注天善智慧www.hellobi.com,我們是專注於商業智慧BI,人工智慧AI,大資料分析與挖掘領域的垂直社群,學習,問答、求職一站式搞定!

本文作者:天善智慧社群專家徐麟

相關文章