股票涨跌预测方法之一:下载股票数据
前一阵子在同学的鼓动下,花了一个多月研究了股票行情的预测方法,熟悉了常见的炒股术语及技术指标,现总结如下,纯属兴趣,如果想依照本文的方法来短线操作获利,请绕道。
研究的第一步就是下载所有股票的历史行情数据。可以使用python开源模块tushare,缺点就是慢,所以我使用多线程来加速,并将数据保存在sqlite数据库里,下面是python代码:
# -*- coding: utf-8 -*- import tushare as ts import pickle import sqlite3 as db import datetime from queue import * import threading g_taskList = Queue(0) g_resList = Queue(0) def task(): while True: try: id, y1, y2 = g_taskList.get(False) except: break try: record = [] flag = 0 for year in range(y2,y1-1,-1): if flag>=2: break v1 = ts.get_h_data(id,start='%d-06-01'%year,end='%d-12-31'%year) if v1 is None: flag += 1 continue index = [m.date() for m in v1.index] for v in zip(index, v1.open, v1.high, v1.close, v1.low, v1.volume, v1.amount): record.append([id,]+list(v)) g_resList.put([id, record]) except Exception as e: g_taskList.put([id, y1, y2]) break g_resList.put([None,[]]) def GetAllData(dbFile='all_tushare_data2017.db', y1= 1990, y2=2017): data = list(ts.get_stock_basics().index) #这里可以获取股票代码 try: cxn = db.connect(dbFile) cur = cxn.cursor() cur.execute('create table if not exists gp_record(code char(6), date date, open float, high float, close float, low float, volume float, amout float, primary key (code, date))') #创建表格 for d in data: g_taskList.put([d,y1,y2]) count = 20 #启动20个下载线程 for k in range(count): threading.Thread(None, task).start() while True: if count<=0: break id, record = g_resList.get() if not id: count-=1; continue print(id, len(record)) for v in record: try: cur.execute('insert into gp_record values(?,?,?,?,?,?,?,?)', v) except: #print('insert error',d) pass cxn.commit() finally: cur.close() cxn.commit() cxn.close() if __name__ == '__main__': GetAllData()