def insert_hist_trade(self): self.set_data() db = Db() engine = db._get_engine() sql_stocklist = "select code,name from stock_code" codes = pd.read_sql_query(sql_stocklist, engine) codes = codes.to_dict('records') i = 1 for row in codes: gta = td.get_hist_data(code=row['code'], start=self.nowdate, end=self.nowdate, ktype='D', retry_count=3, pause=0.001) gta['datain_date'] = self.nowtime gta['code'] = row['code'] gta['name'] = row['name'] gta['c_yearmonthday'] = gta.index gta = gta.to_dict('records') try: db.insertmany( """INSERT INTO trade_hist(c_yearmonthday,code,name,open,high,close,low,volume,price_change,p_change,ma5,ma10,ma20,v_ma5,v_ma10,v_ma20,turnover,datain_date) VALUES (%(c_yearmonthday)s,%(code)s,%(name)s,%(open)s,%(high)s,%(close)s,%(low)s,%(volume)s,%(price_change)s,%(p_change)s,%(ma5)s,%(ma10)s,%(ma20)s,%(v_ma5)s,%(v_ma10)s,%(v_ma20)s,%(turnover)s,%(datain_date)s)""", gta) except Exception, e: log.error('insert error:%s ', e) log.info('%s stock insert finished,%s,%s', i, row['code'], row['name'].decode('utf-8')) i += 1
def hdf(): df = fd.get_hist_data('000875') # df.to_hdf('c:/day/store.h5','table') store = HDFStore(path + '/data/store.h5') store['000875'] = df store.close()
def appends(): filename = path + '/data/bigfile.csv' for code in ['000875', '600848', '000981']: df = fd.get_hist_data(code) if os.path.exists(filename): df.to_csv(filename, mode='a', header=None) else: df.to_csv(filename)
def test_get_hist_data(self): self.set_data() print(fd.get_hist_data(self.code, self.start))
def test_histData(self): self.set_data() print td.get_hist_data(self.code, start=self.start, end=self.end)
def json(): df = fd.get_hist_data('000875') df.to_json(path + '/data/000875.json', orient='records') # 或者直接使用 print(df.to_json(orient='records'))
def xls(): df = fd.get_hist_data('000875') # 直接保存 df.to_excel(path + '/data/000875.xlsx', startrow=2, startcol=5)
def csv(): df = fd.get_hist_data('000875') df.to_csv(path + '/data/000875.csv', columns=['open', 'high', 'low', 'close'])