要做量化投资,数据是基础,正所谓“巧妇难为无米之炊”

在免费数据方面,各大网站的财经板块其实已提供相应的api,如新浪、雅虎、搜狐。。。可以通过urlopen相应格式的网址获取数据

而TuShare正是这么一个免费、开源的python财经数据接口包,已将各类数据整理为dataframe类型供我们使用。

主要用到的函数:

1.实时行情获取

tushare.get_today_all()

一次性获取当前交易所有股票的行情数据(如果是节假日,即为上一交易日,结果显示速度取决于网速)

2.历史数据获取

tushare.get_hist_data(code, start, end,ktype, retry_count,pause)

参数说明:

  • code:股票代码,即6位数字代码,或者指数代码(sh=上证指数 sz=深圳成指 hs300=沪深300指数 sz50=上证50 zxb=中小板 cyb=创业板)
  • start:开始日期,格式YYYY-MM-DD
  • end:结束日期,格式YYYY-MM-DD
  • ktype:数据类型,D=日k线 W=周 M=月 5=5分钟 15=15分钟 30=30分钟 60=60分钟,默认为D
  • retry_count:当网络异常后重试次数,默认为3
  • pause:重试时停顿秒数,默认为0

具体可参考官网http://tushare.org/index.html

而如果要进行完备详细的回测,每次在线获取数据无疑效率偏低,因此还需要入库

下面是数据库设计部分

表1:stocks

股票表,第一列为股票代码,第二列为名称,如果get_today_all()中存在的股票stocks表中没有,则插入之。

表2:hdata_date

日线表,由于分钟线只能获取一周内的数据,我们先对日线进行研究。

字段和get_hist_data返回值基本一致,多了stock_code列,并将record_date列本来是dataframe的index

stock_code,record_date,  //主键
open,high,close,low,    //开盘,最高,收盘,最低
volume,          //成交量
price_change,p_change,  //价差,涨幅
ma5,ma10,ma20     //k日收盘均价
v_ma5,v_ma10,v_ma20,  //(k日volume均值)
turnover        //换手率

 

python工程目前有3个文件,main.py(主程序),Stocks.py(“股票们”类)以及Hdata.py(历史数据类)

main.py

 

import psycopg2 #使用的是PostgreSQL数据库
import tushare as ts
from Stocks import*
from HData import*
import  datetime

stocks=Stocks("postgres","123456")
hdata=HData("postgres","123456")

# stocks.db_stocks_create()#如果还没有表则需要创建
#print(stocks.db_stocks_update())#根据todayall的情况更新stocks表

#hdata.db_hdata_date_create()

nowdate=datetime.datetime.now().date()

codestock_local=stocks.get_codestock_local()

hdata.db_connect()#由于每次连接数据库都要耗时0.0几秒,故获取历史数据时统一连接
for i in range(0,len(codestock_local)):
    nowcode=codestock_local[i][0]

    #print(hdata.get_all_hdata_of_stock(nowcode))

    print(i,nowcode,codestock_local[i][1])

    maxdate=hdata.db_get_maxdate_of_stock(nowcode)
    print(maxdate, nowdate)
    if(maxdate):
        if(maxdate>=nowdate):#maxdate小的时候说明还有最新的数据没放进去
            continue
        hist_data=ts.get_hist_data(nowcode, str(maxdate+datetime.timedelta(1)),str(nowdate), 'D', 3, 0.001)
        hdata.insert_perstock_hdatadate(nowcode, hist_data)
    else:#说明从未获取过这只股票的历史数据
        hist_data = ts.get_hist_data(nowcode, None, str(nowdate), 'D', 3, 0.001)
        hdata.insert_perstock_hdatadate(nowcode, hist_data)

hdata.db_disconnect()

 

 

Stocks.py

import tushare as ts
import psycopg2
class Stocks(object):#这个类表示"股票们"的整体(不是单元)
    def get_today_all(self):
        self.todayall=ts.get_today_all()

    def get_codestock_local(self):#从本地获取所有股票代号和名称
        conn = psycopg2.connect(database="wzj_quant", user=self.user, password=self.password, host="127.0.0.1",
                                port="5432")
        cur = conn.cursor()
        # 创建stocks表
        cur.execute('''
                select * from stocks;
               ''')
        rows =cur.fetchall()
        conn.commit()
        conn.close()

        return rows
        pass
    def __init__(self,user,password):
        # self.aaa = aaa
        self.todayall=[]
        self.user=user
        self.password=password

    def db_perstock_insertsql(self,stock_code,cns_name):#返回的是插入语句
        sql_temp="insert into stocks values("
        sql_temp+="\'"+stock_code+"\'"+","+"\'"+cns_name+"\'"
        sql_temp +=");"
        return sql_temp
        pass

    def db_stocks_update(self):# 根据gettodayall的情况插入原表中没的。。gettodayall中有的源表没的保留不删除#返回新增行数
        ans=0
        conn = psycopg2.connect(database="wzj_quant", user=self.user, password=self.password, host="127.0.0.1", port="5432")
        cur = conn.cursor()
        self.get_today_all()

        for i in range(0,len(self.todayall)):
            sql_temp='''select * from stocks where stock_code='''
            sql_temp+="\'"+self.todayall["code"][i]+"\';"
            cur.execute(sql_temp)
            rows=cur.fetchall()
            if(len(rows)==0):
                #如果股票代码没找到就插
                ans+=1
                cur.execute(self.db_perstock_insertsql(self.todayall["code"][i],self.todayall["name"][i]))
                pass
        conn.commit()
        conn.close()
        print("db_stocks_update finish")
        return ans

    def db_stocks_create(self):
        conn = psycopg2.connect(database="wzj_quant", user=self.user, password=self.password, host="127.0.0.1", port="5432")
        cur = conn.cursor()
        # 创建stocks表
        cur.execute('''
            drop table if exists stocks;
            create table stocks(stock_code varchar primary key,cns_name varchar);
        ''')
        conn.commit()
        conn.close()
        print("db_stocks_create finish")
        pass

 

HData.py

import  psycopg2
import tushare as ts
import pandas as pd
from time import clock

class HData(object):
    def __init__(self,user,password):
        # self.aaa = aaa
        self.hdata_date=[]
        self.user=user
        self.password=password

        self.conn=None
        self.cur=None


    def db_connect(self):
        self.conn = psycopg2.connect(database="wzj_quant", user=self.user, password=self.password, host="127.0.0.1",
                                port="5432")
        self.cur = self.conn.cursor()

    def db_disconnect(self):

        self.conn.close()

    def db_hdata_date_create(self):
        conn = psycopg2.connect(database="wzj_quant", user=self.user, password=self.password, host="127.0.0.1",
                                port="5432")
        cur = conn.cursor()
        # 创建stocks表
        cur.execute('''
                drop table if exists hdata_date;
                create table hdata_date(stock_code varchar,record_date date,
                    open float,high float,close float,low float,
                    volume float,
                    price_change float,p_change float,
                    ma5 float,ma10 float,ma20 float,
                    v_ma5 float,v_ma10 float,v_ma20 float,
                    turnover float
                );
                alter table hdata_date add primary key(stock_code,record_date);
                ''')
        conn.commit()
        conn.close()
        print("db_hdata_date_create finish")
        pass

    def db_get_maxdate_of_stock(self,stock_code):#获取某支股票的最晚日期
        self.cur.execute("select max(record_date) from hdata_date where stock_code="+"\'"+stock_code+"\'"+";")
        ans=self.cur.fetchall()
        if(len(ans)==0):
            return None
        return ans[0][0]
        self.conn.commit()
        pass

    def insert_perstock_hdatadate(self,stock_code,data):#插入一支股票的所有历史数据到数据库#如果有code和index相同的不重复插入
        t1=clock()

        for i in range(0,len(data)):
            str_temp=""

            str_temp+="\'"+stock_code+"\'"+","
            str_temp+="\'"+data.index[i]+"\'"



            for j in range(0,data.shape[1]):
                str_temp+=","+"\'"+str(data.iloc[i,j])+"\'"
            sql_temp="values"+"("+str_temp+")"
            self.cur.execute("insert into hdata_date "+sql_temp+";")
        self.conn.commit()

        print(clock()-t1)

        print(stock_code+" insert_perstock_hdatadate finish")

    def get_all_hdata_of_stock(self,stock_code):#将数据库中的数据读取并转为dataframe格式返回
        conn = psycopg2.connect(database="wzj_quant", user=self.user, password=self.password, host="127.0.0.1",
                                port="5432")
        cur = conn.cursor()

        sql_temp="select * from hdata_date where stock_code="+"\'"+stock_code+"\';"
        cur.execute(sql_temp)
        rows = cur.fetchall()

        conn.commit()
        conn.close()

        dataframe_cols=[tuple[0] for tuple in cur.description]#列名和数据库列一致
        df = pd.DataFrame(rows, columns=dataframe_cols)
        return df
        pass

 

main.py的控制台输出示例:

python抓取股票数据分析,再选股 python股票数据获取_python

 

HData中的函数get_all_hdata_of_stock结果示例:

stock_code record_date   open   high  close    low     volume  \
0       603999  2015-12-10  14.07  14.07  14.07  14.07     337.00   
1       603999  2015-12-11  15.48  15.48  15.48  15.48     119.00   
2       603999  2015-12-14  17.03  17.03  17.03  17.03     267.00   
3       603999  2015-12-15  18.73  18.73  18.73  18.73     244.00   
..         ...         ...    ...    ...    ...    ...        ...   
397     603999  2017-08-01   9.62   9.97   9.79   9.61   36337.80   
398     603999  2017-08-02   9.80   9.85   9.61   9.59   32135.60   
     price_change  p_change     ma5    ma10    ma20      v_ma5     v_ma10  \
0            4.30     44.01  14.070  14.070  14.070     337.00     337.00   
1            1.41     10.02  14.775  14.775  14.775     228.00     228.00   
2            1.55     10.01  15.527  15.527  15.527     241.00     241.00   
3            1.70      9.98  16.328  16.328  16.328     241.75     241.75   
..            ...       ...     ...     ...     ...        ...        ...   
397          0.16      1.66   9.680   9.709   9.924   36754.46   49436.88   
398         -0.18     -1.84   9.698   9.741   9.863   36513.38   49998.51   
        v_ma20  turnover  
0       337.00      0.06  
1       228.00      0.02  
2       241.00      0.04  
3       241.75      0.04  
..         ...       ...  
397   42602.09      1.58  
398   42114.31      1.39

 

数据库中的数据示例

stocks表

python抓取股票数据分析,再选股 python股票数据获取_sql_02

hdata_date表

python抓取股票数据分析,再选股 python股票数据获取_tushare_03