當前位置:股票大全官網 - 股票投資 - 開發炒股軟件,從哪裏獲得股票數據

開發炒股軟件,從哪裏獲得股票數據

目前市場上有很多股票行情交易軟件,各種軟件提供了豐富的分析和展示功能,而且基本上是免費的。但這些數據都是在線的、無法統壹地下載到本地進行分析,於是上網找了些資料,有的是將程序到新浪搜狐的財經頻道或其他財經類網站抓取並分析網頁,這種方法操作性不強而且準確率較低,遇到廣告或網頁變動時風險較大。於是找到了Sina股票數據接口,這個接口是通過在IE端輸入"/list="+相應股票代碼網站返回壹個文件形式的數據,也可以通過JS獲取該文件中的變量得到想要的數據字符串。

以大秦鐵路(股票代碼:601006)為例,如果要獲取它的最新行情,只需訪問新浪的股票數據接口:/list=sh601006這個url會返回壹串文本,例如:

var hq_str_sh601006="大秦鐵路, 27.55, 27.25, 26.91, 27.55, 26.20, 26.91, 26.92,

22114263, 589824680, 4695, 26.91, 57590, 26.90, 14700, 26.89, 14300,

26.88, 15100, 26.87, 3100, 26.92, 8900, 26.93, 14230, 26.94, 25150, 26.95, 15220, 26.96, 2008-01-11, 15:05:32";

這個字符串由許多數據拼接在壹起,不同含義的數據用逗號隔開了,按照程序員的思路,順序號從0開始。

0:”大秦鐵路”,股票名字;

1:”27.55″,今日開盤價;

2:”27.25″,昨日收盤價;

3:”26.91″,當前價格;

4:”27.55″,今日最高價;

5:”26.20″,今日最低價;

6:”26.91″,競買價,即“買壹”報價;

7:”26.92″,競賣價,即“賣壹”報價;

8:”22114263″,成交的股票數,由於股票交易以壹百股為基本單位,所以在使用時,通常把該值除以壹百;

9:”589824680″,成交金額,單位為“元”,為了壹目了然,通常以“萬元”為成交金額的單位,所以通常把該值除以壹萬;

10:”4695″,“買壹”申請4695股,即47手;

11:”26.91″,“買壹”報價;

12:”57590″,“買二”

13:”26.90″,“買二”

14:”14700″,“買三”

15:”26.89″,“買三”

16:”14300″,“買四”

17:”26.88″,“買四”

18:”15100″,“買五”

19:”26.87″,“買五”

20:”3100″,“賣壹”申報3100股,即31手;

21:”26.92″,“賣壹”報價

(22, 23), (24, 25), (26,27), (28, 29)分別為“賣二”至“賣四的情況”

30:”2008-01-11″,日期;

31:”15:05:32″,時間;

相應地,也可以獲得深市相關股票信息,但是這種方法的弊病是只能獲得最新的或者是當天的股票數據,無法將歷史數據導入到數據庫,當然,妳也可以以某壹天為起始點自己重新創造歷史數據。所以繼續尋找其他網站接口,終於找到了雅虎財經網站,它提供的接口可以直接把股票歷史數據導成Excel,真實太方便了!直接在瀏覽器地址中數據網址即可/table.csv?s=股票代碼,但是如果手動輸入再逐壹下載保存簡直是太麻煩了,光上證股票就800多個,估計剛手動下載完就又開盤了還得重新下載。所以我的思路是,1、利用多線程方法下載股票文件。2、將這些文件統壹導入數據庫。

1.1文件下載類:

import java.io.*;

import java.net.*;

import java.util.List;

import fatowen.stocksystem.sysconfig.data.DownLoadVO;

public class HttpDownFile {

private static int BUFFER_SIZE = 8096;

/**根據URL下載文件並保存

* @param destUrl String

* @param fileName String

* @throws Exception

*/

public void saveToFile(String destUrl, String fileName) throws IOException {

FileOutputStream fos = null;

BufferedInputStream bis = null;

HttpURLConnection /table.csv?s=";

String result = "";

String savePath = "";

public HisDataAddThread(List paramList,String savePath){

this.myParamList = paramList;

this.savePath = savePath;

}

public void run() {

while(runFlag){

downLoadData = PublicDataUtil.getDownLoadData(myParamList);

if(!Lib.isEmpty(downLoadData)){

HttpDownFile oInstance = new HttpDownFile();

try {

oInstance.saveToFile(baseUrl + downLoadData, savePath + downLoadData + ".csv");

}catch (Exception err) {

System.out.println(err.toString());

}

}else{

runFlag = false;

}

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

public List getFailureList() {

return failureList;

}

public void setFailureList(List failureList) {

this.failureList = failureList;

}

public List getSuccessList() {

return successList;

}

public void setSuccessList(List successList) {

this.successList = successList;

}

}

2.將下載完的文件統壹保存到數據庫工具類

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

public class CSVUtitl {

private BufferedReader bufferedreader = null;

private List list = new ArrayList();

public CSVUtitl(){

}

public CSVUtitl(String filename) throws IOException{

bufferedreader = new BufferedReader(new FileReader(filename));

String stemp;

while((stemp = bufferedreader.readLine()) != null){

list.add(stemp);

}

}

public List getList() throws IOException {

return list;

}

// 得到csv文件的行數

public int getRowNum(){

return list.size();

}

//得到csv文件的列數

public int getColNum(){

if(!list.toString().equals("[]")) {

//csv文件中,每列之間的是用','來分隔的

if(list.get(0).toString().contains(",")) {

return list.get(0).toString().split(",").length;

}else if(list.get(0).toString().trim().length() != 0) {

return 1;

}else{

return 0;

}

}else{

return 0;

}

}

//取得指定行的值

public String getRow(int index) {

if (this.list.size() != 0)

return (String) list.get(index);

else

return null;

}

//取得指定列的值

public String getCol(int index){

if (this.getColNum() == 0){

return null;

}

StringBuffer scol = new StringBuffer();

String temp = null;

int colnum = this.getColNum();

if (colnum > 1){

for (Iterator it = list.iterator(); it.hasNext();) {

temp = it.next().toString();

scol = scol.append(temp.split(",")[index] + ",");

}

}else{

for (Iterator it = list.iterator(); it.hasNext();) {

temp = it.next().toString();

scol = scol.append(temp + ",");

}

}

String str=new String(scol.toString());

str = str.substring(0, str.length() - 1);

return str;

}

//取得指定行,指定列的值

public String getString(int row, int col) {

String temp = null;

int colnum = this.getColNum();

if(colnum > 1){

temp = list.get(row).toString().split(",")[col];

}else if(colnum == 1) {

temp = list.get(row).toString();

}else{

temp = null;

}

return temp;

}

public void CsvClose() throws IOException {

this.bufferedreader.close();

}

public void run(String filename) throws IOException {

CSVUtitl cu = new CSVUtitl(filename);

for(int i=0;i<cu.getRowNum();i++){

String SSCCTag = formatData(cu.getString(i,1));//得到第i行.第壹列的數據.

String SiteName = formatData(cu.getString(i,2));//得到第i行.第二列的數據.

String StationId= formatData(cu.getString(i,3));

//將數據保存到數據庫中

... ...

... ...

... ...

}

cu.CsvClose();

}

public String formatData(String baseData){

String result = null;

if(!"".equals(baseData) && baseData != null){

if(baseData.length() > 1){

result = baseData.substring(1,baseData.length());

result = result.substring(0, result.length()-1);

}else{

result = baseData;

}

}else{

result = "";

}

return result.trim();

}

public static void main(String[] args) throws IOException {

CSVUtitl test = new CSVUtitl();

try{

File path = new File("e:\\data");

File[] f = path.listFiles();

List l = new ArrayList();

for(int i=0;i<f.length;i++){

if(f[i].getName().endsWith(".csv"))

l.add(f[i]); www.2cto.com

}

Iterator it = l.iterator();

while(it.hasNext()){

File ff = (File)it.next();

test.run(path.toString()+File.separator+ff.getName());

}

}catch (Exception e){

}

}

}