Get all the latest stock data with R language

Source: Internet
Author: User

This article uses the method mentioned here to improve, in order to obtain all the stock data in bulk, and the stock data is simple statistics.
The first use of the program requires a CSV file that records the names and codes of each stock.
The format is as follows:

As for the production, or quite simple, Baidu a stock code, or directly here, you can easily get all the stock code, and then put into Excel by the Space division, processing a bit can be. Here is a copy of my good, but only the Shanghai stock is interested can take to stockid.csv.
Note that if it is a different stock, please refer to the post I mentioned above to modify the code accordingly.

The SSE code is SS, deep Proof code is SZ, Hong Kong stock code is HK

such as Maotai: 6000519.SS, Vanke 000002.sz, Changjiang industrial 0001.hk

In the code, ‘.ss‘ change to something else.

library(quantmod)stock=read.csv(‘F:/Program Files/RStudio/stockid.csv‘,stringsAsFactors=F)data=list()forin1:length(stock$id)){  try(setSymbolLookup(TEMP=list(name=paste0(stock$id[i],‘.ss‘))))  try(getSymbols("TEMP",warnings=F))  try(data[stock$name[i]]<-list(TEMP))}

Data at this time is a list that stores all of the stock data in your CSV and can be used for example data$浦发银行 to get information about the stock.
Sample data:

Today is August 25, 2015, so the data obtained are the latest historical data.
You can see a total of 6 columns of data, each of which means:

    • Open Price Open
    • High prices
    • Lowest price
    • Close Price Close
    • Volume volume
    • Adjusted Price Adjust Prices

Here's a little bit of a look at the stock market and provide an example for everyone. The closing price of all stocks is extracted, and then the maximum minimum mean value of each stock closing price is calculated and so on.

library(plyr)closedata<-lapply(data,function(x){  x=as.data.frame(x)  return(list(x[,4])) #提取第4列,即收盘价})ldply(closedata,function(x)summary(x[[1]])) #对每个股票求summary

Partial Run Result:

> ldply (Closedata, function(x) Summary (x[[1]]). ID Min.1St Qu. Median Mean3Rd Qu. Max.1Pu FA Bank7.11   9.610 13.890 17.760   21.80 61.592Baiyun Airport5.97   7.050  8.920 10.060   11.37 23.033Wisco shares1.99   2.730  4.605  6.106    7.67 22.864Dongfeng Motor2.42   3.130  4.530  4.948    5.97 15.195China International Trade6.00   9.790 10.800 11.950   12.84 25.586Pioneering shares3.73   5.705  6.720  8.073    8.18 23.457Shanghai Airport10.35  12.930 14.170 17.880   19.35 42.628Baotou Steel shares2.09   3.910  4.630  4.971    5.95 10.129Huaneng int ' I4.06   5.640  6.710  7.508    8.11 18.73TenWan Tong Expressway3.36   4.240  5.215  5.792    6.49 20.05

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Get all the latest stock data with R language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.