資源簡介
實現了從通達信軟件的數據文件中提取數據保存至MySQL的功能,每運行一次就同步到最新狀態
代碼片段和文件信息
#!/usr/bin/env?python
#coding:?utf-8
#
import?sys?urllib?urllib2?json?thread?threading?datetime?time
import?MySQLdb?as?mysql
import?os?struct
class?Stock(threading.Thread):
????LAST?=?19700101
????def?__init__(selfmarket?=?None?path?=?None?prefix?=?None):
????????self.market?=?market
????????self.path?=?path
????????self.prefix?=?prefix
????????self.codes?=?[]
????????self.daliy?=?[]
????????threading.Thread.__init__(self)
????#獲取日線歷史數據文件
????def?get_stock_codes(self):
????????self.codes?=?[?f?for?f?in?os.listdir(self.path)?if?f.endswith(‘.day‘)?and?f.startswith(self.prefix)]
????def?get_daliy_data(selfcode):
????????daliy?=?“{0}{1}“.format(self.pathcode)
????????f?=?open(daliy‘rb‘)
????????while?1:
????????????data?=?f.read(32)
????????????if?data?==?‘‘:
????????????????break
????????
????????????daliy_data?=?tuple(struct.unpack(‘iiiiifii‘data))?+?(code[2:8])
????????????#只有提取日期大于已保存的最大日期
????????????#print?type(Stock.LAST)?type(daliy_data[0])
????????????if?daliy_data[0]?>?Stock.LAST:
????????????????self.daliy.append(daliy_data)??
????#獲取已存取的最大日期
????#如果日期不存在返回默認值19700101
????@staticmethod
????def?get_last_daliy():
????????db?=?mysql.connect(host?=?‘localhost‘?user?=?‘root‘?passwd?=?‘‘?db?=?‘stock‘?charset=‘utf8‘)
????????cursor?=?db.cursor()
????????try:
????????????sql?=?‘‘‘SELECT?MAX(st_date)?FROM?daliy‘‘‘
????????????cursor.execute(sql?)
????????????result?=?cursor.fetchone()
????????????#print?result
????????????Stock.LAST?=?19700101?if?result[0]?==?None?else?int(result[0].strftime(‘%Y%m%d‘))
????????except:
????????????pass
????????cursor.close()
????????db.close()
????#st_date?st_open?st_high?st_low?st_close?st_amount?st_vol?st_reservation?=?struct.unpack(‘iiiiifii‘data)????
????#print?st_date?code[2:8]?float(st_open)/100?floa
評論
共有 條評論