def sys_position_test(): #sys_df=sys_risk_analyse() need_update=tt.is_trade_time_now() sys_df = sys_risk_analyse(is_realtime_update=need_update) print(sys_df.tail(10)) stock_df = get_stock_position(stock_synbol='300162')#,is_realtime_update=True) #sys_df = get_stock_position(stock_synbol='002673',is_realtime_update=True) #sys_df = get_stock_position(stock_synbol='000680')#,is_realtime_update=True) sys_score,position,operation,latest_day=get_sys_risk_info(stock_df) #se.send_position_mail(position_df=sys_df,symbol=None) revised_position(sys_risk_analyse_position=position,recent_100d_great_dropdown=-0.48,recent_100d_great_increase=0.2,max_position=0.85)
given_codes=[], except_stocks=except_stock, type='stock') print('First back_test completed: ', datetime.datetime.now()) updated_date_count = 1 while True: #this_time=datetime.datetime.now() #hour=this_time.hour #minute=this_time.minute stock_sql = pds.StockSQL() last_year_date = datetime.datetime.now() + datetime.timedelta( days=-365) date_str = last_year_date.strftime('%Y-%m-%d') sleep_seconds = 60 if tt.is_trade_date(): if tt.is_trade_time_now(): #if datetime.datetime.now().minute%2==0: """更新持仓信息到数据库""" #stock_sql.update_sql_position(users={'36005':{'broker':'yh','json':'yh.json'},'38736':{'broker':'yh','json':'yh1.json'}}) """更新指数历史数据到数据库""" #stock_sql.update_sql_index(index_list=['sh','sz','zxb','cyb','hs300','sh50'],force_update=True) #sleep_seconds=60 pass else: if datetime.datetime.now().hour == 18: #easyhistory.init('D', export='csv', path="C:/hist") updated_date_count = updated_date_count + 1 """更新股票数据""" easyhistory.update(path="C:/hist", stock_codes=given_codes) """更新指数csv数据"""
print("First update completed: ", datetime.datetime.now()) last_year_date = datetime.datetime.now() + datetime.timedelta(days=-365) date_str = last_year_date.strftime("%Y-%m-%d") mybt.back_test(k_num=date_str, given_codes=[], except_stocks=except_stock, type="stock") print("First back_test completed: ", datetime.datetime.now()) updated_date_count = 1 while True: # this_time=datetime.datetime.now() # hour=this_time.hour # minute=this_time.minute stock_sql = pds.StockSQL() last_year_date = datetime.datetime.now() + datetime.timedelta(days=-365) date_str = last_year_date.strftime("%Y-%m-%d") sleep_seconds = 60 if tt.is_trade_date(): if tt.is_trade_time_now(): # if datetime.datetime.now().minute%2==0: """更新持仓信息到数据库""" # stock_sql.update_sql_position(users={'36005':{'broker':'yh','json':'yh.json'},'38736':{'broker':'yh','json':'yh1.json'}}) """更新指数历史数据到数据库""" # stock_sql.update_sql_index(index_list=['sh','sz','zxb','cyb','hs300','sh50'],force_update=True) # sleep_seconds=60 pass else: if datetime.datetime.now().hour == 18: # easyhistory.init('D', export='csv', path="C:/hist") updated_date_count = updated_date_count + 1 """更新股票数据""" easyhistory.update(path="C:/hist", stock_codes=given_codes) """更新指数csv数据"""
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