def update_index_chooce_time(self,yh_index='999999',rate_to_confirm=0.001): pre_stock_trade_state = 0 s_stock=Stockhistory(yh_index,'D',test_num=0,source='YH',rate_to_confirm=rate_to_confirm) result_df = s_stock.form_temp_df(yh_index) test_result = s_stock.regression_test(rate_to_confirm) last_trade_date =test_result.last_trade_date last_trade_price=test_result.last_trade_price positon=test_result.position print('last_trade_price=',last_trade_price) print('last_trade_date=',last_trade_date) if last_trade_price<0: pdsqlc.is_system_risk(indexs=['sh','cyb'],index_exit_data=pdsqlc.get_index_exit_data(['sh','cyb'])) df = qq.get_qq_quotations_df(codes=['sh','cyb'],set_columns=['datetime','open','high','low','close','volume','amount']) print(df) pass else: pass return
def get_HO_dapan(dapan_codes=[],ho_rate=0.0026, stock_sql=None): """ 找出高开的大盘股,并触发email """ codes = ['600029', '600018', '000776', '600016', '600606', '601668', '600050', '601688', '600030', '600104', '601377', '601633', '600585', '601186', '600036', '002450', '000538', '601818', '601898', '002304', '601628', '600276', '601800', '002027', '600000', '601318', '601088', '601601', '000001', '601988', '601390', '600015', '002673', '600547', '600340', '601238', '601006', '000783', '001979', '601857', '000768', '601766', '600518', '600011', '000166', '002024', '000002', '600519', '600048', '600383', '300498', '600028', '600999', '002142', '601018', '600887', '601336', '600958', '002252', '601328', '002594', '601398', '600115', '000063', '601618', '601727', '000895', '601985', '300104', '600900', '601989', '600019', '601899', '600663', '600690', '000333', '600649', '600795', '002415', '000725', '601211', '000625', '000651', '601169', '601111', '601788', '002736', '601009', '601669', '600837', '601939', '603993', '601288', '601166', '000858', '601998', '600705'] if dapan_codes: codes = dapan_codes ho_codes = [] if stock_sql: codes = stock_sql.get_dapan(table='dapan_gu') this_datas = qq.get_qq_quotations_df(codes) if this_datas.empty: return ho_codes ho_datas = this_datas[(this_datas['open']>=(1 + ho_rate) * this_datas['close0'])] if ho_datas.empty: return ho_codes else: ho_codes = ho_datas['code'].values.tolist() ho_datas['ho_chg'] = (ho_datas['open']/ho_datas['close0'] - 1)*100.0 mail_columns = ['code','name','ho_chg','PE','increase_rate','open','high','low','close', 'PB', 'total_market','datetime'] ho_datas = ho_datas.sort_values(axis=0, by='ho_chg', ascending=False) ho_datas = ho_datas[mail_columns] sub = '[大盘股机会] 大盘股高开比率:%s%%, 今日高开大盘股有: %s' % (round(len(ho_codes),2)/len(codes)*100,ho_codes) content = '高开大盘股: \n %s' % ho_datas sm.send_mail(sub,content,mail_to_list=None) return list(set(ho_codes))
def update_one_stock(symbol,realtime_update=False,dest_dir='C:/中国银河证券海王星/T0002/export/', force_update_from_YH=False): """ 运行之前先下载及导出YH历史数据 """ """ :param symbol: string type, stock code :param realtime_update: bool type, True for K data force update during trade time :param dest_dir: string type, like csv dir :param force_update_from_YH: bool type, force update K data from YH :return: Dataframe, history K data for stock """ index_symbol_maps = {'sh':'999999','sz':'399001','zxb':'399005','cyb':'399006', 'sh50':'000016','sz300':'399007','zx300':'399008','hs300':'000300'} qq_index_symbol_maps = {'sh':'000001','sz':'399001','zxb':'399005','cyb':'399006', 'sh50':'000016','sz300':'399007','zx300':'399008','hs300':'000300'} FIX_FACTOR = 1.0 d_format='%Y/%m/%d' last_date_str = tt.get_last_trade_date(date_format=d_format) latest_date_str = tt.get_latest_trade_date(date_format=d_format) #print('last_date_str=',last_date_str) #print('latest_date_str=',latest_date_str) next_date_str = tt.get_next_trade_date(date_format=d_format) #print(next_date_str) dest_file_name = dest_dir+ '%s.csv' % symbol dest_df = get_raw_hist_df(code_str=symbol) file_type='csv' RAW_HIST_DIR = "C:/中国银河证券海王星/T0002/export/" yh_file_name = RAW_HIST_DIR+symbol+'.'+file_type if symbol in index_symbol_maps.keys(): symbol = index_symbol_maps[symbol] dest_file_name = dest_dir+ '%s.csv' % symbol #print('dest_file_name=',dest_file_name) if dest_df.empty: if symbol in index_symbol_maps.keys(): symbol = index_symbol_maps[symbol] yh_file_name = RAW_HIST_DIR+symbol+'.'+file_type #yh_index_df = get_yh_raw_hist_df(code_str=symbol) yh_index_df = pd.read_csv(yh_file_name) #yh_index_df['factor'] = 1.0 yh_df = yh_index_df.set_index('date') yh_df.to_csv(dest_file_name ,encoding='utf-8') dest_df = yh_index_df #del dest_df['rmb'] return yh_df #print(dest_df) dest_df_last_date = dest_df.tail(1).iloc[0]['date'] #print('dest_df_last_date=',dest_df_last_date) quotation_datetime = datetime.datetime.now() if dest_df_last_date<latest_date_str: quotation_date = '' try: quotation_index_df = qq.get_qq_quotations_df([symbol], ['code','date','open','high','low','close','volume','amount']) quotation_date = quotation_index_df.iloc[0]['date'] #quotation_date = quotation_index_df.iloc[0]['date'] #quotation_datetime = quotation_index_df.iloc[0]['datetime'] #del quotation_index_df['datetime'] if dest_df_last_date==quotation_date: return dest_df #quotation_index_df = ts.get_index() except: time.sleep(3) quotation_index_df = qq.get_qq_quotations_df([symbol], ['code','date','open','high','low','close','volume','amount']) print(quotation_index_df) quotation_date = quotation_index_df.iloc[0]['date'] #quotation_datetime = quotation_index_df.iloc[0]['datetime'] #del quotation_index_df['datetime'] if dest_df_last_date==quotation_date: return dest_df #print('quotation_date=',quotation_date) #print(quotation_index_df) #quotation_index_df['factor'] = 1.0 quotation_index_df = quotation_index_df[['date','open','high','low','close','volume','amount']]#,'factor']] #quotation_index_df.iloc[0]['volume'] = 0 #quotation_index_df.iloc[0]['amount'] = 0 #print(quotation_index_df) #print(quotation_index_df) need_to_send_mail = [] sub = '' index_name = symbol #table_update_times = self.get_table_update_time() if quotation_date: yh_symbol = symbol if symbol in index_symbol_maps.keys(): yh_symbol = index_symbol_maps[index_name] yh_file_name = RAW_HIST_DIR+yh_symbol+'.'+file_type #yh_index_df = get_yh_raw_hist_df(code_str=symbol) yh_index_df = pd.read_csv(yh_file_name,encoding='GBK') #yh_index_df['factor'] = FIX_FACTOR yh_last_date = yh_index_df.tail(1).iloc[0]['date'] #print('yh_last_date=',yh_last_date) #print( yh_index_df)#.head(len(yh_index_df)-1)) if yh_last_date>dest_df_last_date: #dest_df_last_date<latest_date_str #date_data = self.query_data(table=index_name,fields='date',condition="date>='%s'" % last_date_str) #data_len = len(date_data) #this_table_update_time = table_update_times[index_name] #print('this_table_update_time=', this_table_update_time) if yh_last_date<last_date_str: #no update more than two day """需要手动下载银河客户端数据""" print('Need to manual update %s index from YH APP! Please make suere you have sync up YH data' % index_name) need_to_send_mail.append(index_name) sub = '多于两天没有更新指数数据库' content = '%s 数据表更新可能异常' % need_to_send_mail sm.send_mail(sub,content,mail_to_list=None) elif yh_last_date==last_date_str: # update by last date """只需要更新当天数据""" realtime_update = tt.is_trade_time_now() if realtime_update: if yh_last_date<latest_date_str: print(' force update %s index' % symbol) yh_index_df = yh_index_df.append(quotation_index_df, ignore_index=True) #elif yh_last_date==latest_date_str: # print(' delete last update, then force update %s index' % symbol) # yh_index_df=yh_index_df[:-1] # yh_index_df = yh_index_df.append(quotation_index_df, ignore_index=True) else: pass #print(yh_index_df) else: pass else:# yh_last_date>latest_date_str: #update to latest date """YH已经更新到今天,要更新盘中获取的当天数据""" print(' %s index updated to %s; not need to update' % (index_name,latest_date_str)) """ if force_update: print(' force update %s index' % index_name) yh_index_df0 = yh_index_df.head(len(yh_index_df)-1) print(yh_index_df0) yh_index_df = yh_index_df0.append(quotation_index_df, ignore_index=True) print(yh_index_df) else: pass """ yh_index_df = yh_index_df.set_index('date') """ try: os.remove(file_name) print('Delete and update the csv file') except: pass """ yh_index_df.to_csv(dest_file_name ,encoding='utf-8') else: if force_update_from_YH and yh_last_date==dest_df_last_date: yh_index_df = yh_index_df.set_index('date') yh_index_df.to_csv(dest_file_name ,encoding='utf-8') pass elif dest_df_last_date==latest_date_str: print('No need to update data') realtime_update = tt.is_trade_time_now() if realtime_update: quotation_index_df = qq.get_qq_quotations([symbol], ['code','date','open','high','low','close','volume','amount']) #quotation_index_df['factor'] = 1.0 quotation_index_df = quotation_index_df[['date','open','high','low','close','volume','amount']]#'factor']] #print(quotation_index_df) print(' force update %s index' % symbol) dest_df0 = dest_df if dest_df_last_date==latest_date_str: dest_df0 = dest_df.head(len(dest_df)-1) #dest_df0 = dest_df0[:-1] #print(dest_df0) dest_df = dest_df0.append(quotation_index_df, ignore_index=True) #print(dest_df) if quotation_index_df.empty: pass else: yh_index_df = yh_index_df.set_index('date') dest_df.to_csv(dest_file_name ,encoding='utf-8') else: pass else: pass return dest_df