#===================================================================
#下载tick数据
# encoding: UTF-8
#下载指数tick数据
import time
import datetime
import tushare as ts
import pandas as pd
from pymongo import MongoClient
from pymongo import *
import traceback as tb
class index_tick(object):
def __init__(self):
self.m=0
self.df1=[]
self.client=MongoClient('localhost',27017)
self.db_IH=self.client['IH_index']
self.db_IC=self.client['IC_index']
self.db_IF=self.client['IF_index']
collection_time1=datetime.datetime.now()
collection_time=str(collection_time1.date())
self.IH_collection=self.db_IH['IH_'+collection_time]
self.IF_collection=self.db_IF['IF_'+collection_time]
self.IC_collection=self.db_IC['IC_'+collection_time]
def data_insert(self,data1,data2,data3):
tick_dict={}
tick_dict['close']=data1
tick_dict['time']=data2
tick_dict['code']=data3
tick_dict['_id']=data2+data3
return tick_dict
def index_download(self):
try:
t = datetime.datetime.now()
self.df1=ts.get_index()
self.df1.index=self.df1['name']
del self.df1['name']
self.df1=self.df1.T
ih1=self.df1['上证50'][4]
ih2='IH_index'
ih3=str(t)
if1=self.df1['沪深300'][4]
if2='IF_index'
if3=str(t)
ic1=self.df1['中证500'][4]
ic2='IC_index'
ic3=str(t)
IH_dict=self.data_insert(ih1,ih2,ih3)
IF_dict=self.data_insert(if1,if2,if3)
IC_dict=self.data_insert(ic1,ic2,ic3)
self.IH_collection.save(IH_dict)
self.IF_collection.save(IF_dict)
self.IC_collection.save(IC_dict)
print(ih1,ih2,ih3)
print(if1,if2,if3)
print(ic1,ic2,ic3)
except Exception:
print('error')
tb.print_exc()
print(self.m)
self.m=self.m+1
#===========================================================================
#-下载日线数据
# encoding: UTF-8
import xlrd
import sys
import json
import pymongo
import pandas as pd
import tushare as ts
from pymongo import MongoClient
class Daily_bar_downloading(object):
def __init__(self):
self.stock_list = ts.get_stock_basics()
self.stock_list = self.stock_list.index
self.stock_list = self.stock_list.values
#一个函数里计算生成的值,另一个函数里要用,需要return出来
#初始化的时候def要写self,函数里的变量全部要加self,下面引用这个变量也要全部加self
self.client = pymongo.MongoClient('localhost',27017) #pymongo和MongoClient都要import
self.db = self.client['Daily_bar_downloading_test'] #连接database
#-----------------------------------------------------------------------
#获得所有股票代码列表
#def stockList(self):
#stock_list = ts.get_stock_basics()
#stock_list = stock_list.index
#stock_list = stock_list.values
#return stock_list
#-------------------------------------------------------------------
def stock_price_insert(self,row): #创建函数
bar_dict = {}
bar_dict['close'] = row['close'] #创建属性,内容为收盘价
bar_dict['date'] = row['date']
bar_dict['high'] = row['high']
bar_dict['low'] = row['low']
bar_dict['open'] = row['open']
bar_dict['volume'] = row['volume']
return bar_dict
#查找缺失----------------------------------------------------------------------------------
def print_missing_list(self,stock_list):
missing_list=[]
for x in stock_list:
try:
out = self.db[x]
data = pd.DataFrame(list(out.find()))
del data['_id']
except Exception as error:
missing_list.append(x) #没有的,存进missing_list
print('***********************************************************')
print('********************missing_list***************************')
print missing_list
return missing_list
#------------------------------------------------------------------------------------------
#补足数据-----------------------------------------------------------------------------------
def missing_stock_price_download(self, client , missing_list):
#stock_db=client.Stock_Price_3years#创建名为Stock的database
#stock_collection=stock_db.stock_collection
#把缺的补数据拿来,存进MongoDB.-第一次
for symbol in missing_list:
stock_collection = self.db[symbol] #stock_db要预先定义。在stock_db中创立名为x的collection
df = ts.get_k_data(symbol, start='2000-01-01',end = '2018-07-25',ktype='D') #ts要导入tusahre模块
#df = df.fillna(method='pad')
df = df.dropna()
for index, row in df.iterrows():
bar_dict = self.stock_price_insert(row) #stock_price_insert 这个函数要导入
stock_collection.save(bar_dict)
print(symbol)
print('*******************************************************************************')
print('*******************************************************************************')
print("download and the save missing stock to MongoDB")
print('*********************************************************************************')
print('********************************************************************************')
#--------------------------------------------------------------------------------------------
def daily_bar_download(self):
#执行代码:----------------------------------------------------------------
missing_list = []
for x in self.stock_list:
try:
out = self.db[x]
data = pd.DataFrame(list(out.find()))
del data['_id']
except Exception as error:
print (error)
print (x)
missing_list.append(x) #没有的,存进missing_list
print missing_list
m = 1
while missing_list:
#client = pymongo.MongoClient('localhost',27017)
print('===============================================================================')
print('===============================================================================')
self.missing_stock_price_download(self.client, missing_list) #补充数据
print('===================================================================================')
print('===================================================================================')
print(u'=====================================查找缺失开始=================================')
print('================================================================================')
missing_list=self.print_missing_list(self.stock_list) #查找缺失
print('===================================================================================')
#print ('print missing list for %d times'(m) )
m+=1
print(m)
#===============================================================
#跑数据用的 run_service.py
# encoding: UTF-8
#这个是计时器
#--------------------------------------------------
#通用库
#--------------------------------------------------
import time
import datetime
#--------------------------------------------------
#自有库
#--------------------------------------------------
from index_tick.indexdata import index_tick
from daily_bar.daily_bar_downloading import Daily_bar_downloading
#---------------------------------------------------
#taskTime_end0 = datetime.time(hour=9, minute=14)
#taskTime_start1 = datetime.time(hour=9, minute=14)
#taskTime_end1 = datetime.time(hour=14, minute=00)
#taskTime_start2 = datetime.time(hour=11, minute=30)
#taskTime_end2 = datetime.time(hour=12, minute=59)
#taskTime_start3 = datetime.time(hour=13, minute=59)
#taskTime_end3 = datetime.time(hour=15, minute=00)
#taskTime_start4 = datetime.time(hour=15, minute=00)
#taskTime_end4 = datetime.time(hour=18, minute=00)
taskTime_start5 = datetime.time(hour=15, minute=00)
taskTime_end5 = datetime.time(hour=20, minute=00)
time_now=datetime.datetime.now()
time_now2=time_now.time()
#while time_now2taskTime_start1 and time_now2taskTime_start2 and time_now2taskTime_start3 and time_now2taskTime_start4 and time_now2taskTime_start4 and time_now2taskTime_start5 and time_now2