Install Tushare
Open RStudio and enter the command in the console:
> install.packages('Tushare')
Tushare's R package needs to rely on httr, tidyverse, forecast and data.table.
Since the Tushare package states dependencies, the four dependency packages will also be downloaded automatically. If the download process gets stuck and the download fails, you can try again several times. After all, the server of CRAN is not in the mainland. How to use the domestic image of CRAN will be introduced later.
Load Tushare
Just like the installation process, when Tushare is loaded, R automatically loads its dependent packages.
> library('Tushare')
You can also view Tushare's related information through help
> help('Tushare')
Tushare's index information can also be seen on R official website:
Using Tushare
Get api interface object
> api <- Tushare::pro_api(token = 'YOUR TOKEN HERE')
Just like using Tushare Pro's pro.query in Python package, passing the name of the interface you want to call and the corresponding parameters to the api (as long as you call tushare:: pro'u api to get the interface, you can use any name) can call the corresponding data.
In the api, the interface name provided by Tushare Pro must be passed (see the official website for details https://tushare.pro/), other parameters are passed in according to the corresponding interface.
Tips: the fields field is temporarily not supported in the R package of 0.1.1 version of Tushare.
Example 1: call api interface only by passing in interface name and no other parameters
> api(api_name = 'stock_basic')
Next, use an example in the pro bar documentation to demonstrate the api interface being called with the incoming interface name and other parameters.
Example 2: passing in the interface name and other parameters to call the api interface
> api(api_name = 'daily', ts_code = "000001.SZ", start_date = "20181001", end_date = "20181010")
The use of Pro bar interface
Get the pro bar interface and name it bar. Like Tushare Pro's python package, Tushare's R package also provides Pro bar to unify the use of quotation interface.
> bar <- Tushare::pro_bar(token = 'YOUR TOKEN HERE')
> bar(ts_code = "000001.SZ", start_date = "20181001", end_date = "20181010")
The bar interface can transfer the adj to simultaneously retrieve the quotation and weight recovery factors, and return the calculated results. For other interface parameters, please refer to the Tushare Pro website for details.
> bar(ts_code = "000001.SZ", start_date = "20181001", adj = "hfq", ma = c(5,10))
A sample
Finally, we will execute a program to obtain the data of Ping An Bank's post recovery rights and complete the visual display.
df = bar(ts_code="000001.SZ", start_date="20180101", adj="hfq", ma=c(5,10,20)) %>%
mutate(trade_date = as.Date(gsub('^(\\d{4})(\\d{2})(\\d{2})$', '\\1-\\2-\\3', trade_date))) %>%
mutate_at(vars(3:dim(.)[2]), as.numeric)
df$id = dim(df)[1]:1
df$candleLower = pmin(df$open, df$close)
df$candleUpper = pmax(df$open, df$close)
df$candleMiddle = (df$candleLower+df$candleUpper)/2
theme_set(theme_bw())
p = ggplot(df, aes(x=id))+
geom_boxplot(aes(lower= candleLower,
middle = candleLower,
upper = candleUpper,
ymin = low,
ymax = high,
color= ifelse(open>close,"green","red"),
width= 0.5),
stat = 'identity',
size = .5)+
scale_color_manual(values = c("green","red"))+
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.title = element_blank(),
axis.text.x = element_text(angle = 65, hjust = 1),
legend.position="none"
)
p + geom_line(aes(x=id, y=ma5), color="orange", size=.5)+
geom_line(aes(x=id, y=ma10), color="purple", size=.5)+
geom_line(aes(x=id, y=ma20), color="blue", size=.5)
How to get tushare integral
I'm a quantitative investor. I've transformed my team and changed my trading strategy.
There are idle transfers of several former colleagues on hand.
600 points: the original price is 50 yuan, only 39 yuan
1500: original price: 140 yuan, only 109 yuan (sold out)
2000: original price: 190 yuan, only 149 yuan
5000 points: the original price is 490 yuan, only 388 yuan (sold out).
There are not many. Please contact VX: a56746435 (note tushare)