예제 #1
0
파일: pdSql.py 프로젝트: taolaptop/pytrade
 def is_histdata_uptodate(self):
     latest_date_str = tt.get_latest_trade_date(date_format='%Y/%m/%d')
     last_date_str = tt.get_last_trade_date(date_format='%Y/%m/%d')
     
     latest_datetime_str = latest_date_str + ' 15:00'
     last_datetime_str = last_date_str +  ' 15:00'
     print('last_date = ',last_datetime_str)
     print('latest_date_str=',latest_datetime_str)
     systime_dict = self.get_systime()
     tdx_update_time_str = systime_dict['tdx_update_time']
     pos_update_time_str = systime_dict['pos_update_time']
     backtest_time_str = systime_dict['backtest_time']
     is_tdx_uptodate,is_pos_uptodate,is_backtest_uptodate=False,False,False
     """
     if tdx_update_time_str:
         is_tdx_uptodate = tdx_update_time_str>=latest_date_str
     if pos_update_time_str:
         is_pos_uptodate = pos_update_time_str>=latest_date_str
     
     if backtest_time_str:
         is_backtest_uptodate = backtest_time_str>=last_datetime_str
     """
     #deltatime=datetime.datetime.now()-starttime
     #print('update duration=',deltatime.days*24*3600+deltatime.seconds)
     this_day = datetime.datetime.now()
     if (this_day.hour>=0 and this_day.hour<9) or (this_day.hour==9 and this_day.minute<15):
         is_tdx_uptodate = tdx_update_time_str>latest_datetime_str
         is_pos_uptodate = pos_update_time_str>latest_datetime_str
         is_backtest_uptodate = backtest_time_str>=latest_datetime_str
     elif this_day.hour>=16:
         is_tdx_uptodate = tdx_update_time_str>=latest_datetime_str
         is_pos_uptodate = pos_update_time_str>=latest_datetime_str
         is_backtest_uptodate = backtest_time_str>=latest_datetime_str
         
     else:
         is_tdx_uptodate = tdx_update_time_str>=last_datetime_str
         is_pos_uptodate = pos_update_time_str>=last_datetime_str
         is_backtest_uptodate = backtest_time_str>=last_datetime_str
         
     return is_tdx_uptodate,is_pos_uptodate,is_backtest_uptodate,systime_dict
예제 #2
0
파일: pdSql.py 프로젝트: taolaptop/pytrade
 def update_sql_index(self, index_list=['sh','sz','zxb','cyb','hs300','sh50'],force_update=False):
     index_symbol_maps = {'sh':'999999','sz':'399001','zxb':'399005','cyb':'399006',
                      'sh50':'000016','sz300':'399007','zx300':'399008','hs300':'000300'}
     FIX_FACTOR = 1.0
     scz_code_str='399001'
     zxb_code_str='399005'
     chy_code_str='399006'
     shz ='999999'
     shz_50 = '000016'
     hs_300 = '000300'
     zx_300 ='399008'
     sz_300 ='399007'
     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)
     try:
         quotation_index_df = qq.get_qq_quotations(['sh','sz','zxb','cyb','hs300','sh50'], ['code','date','open','high','low','close','volume','amount'])
         #quotation_index_df = ts.get_index()
     except:
         sleep(3)
         quotation_index_df = qq.get_qq_quotations(['sh','sz','zxb','cyb','hs300','sh50'], ['code','date','open','high','low','close','volume','amount'])
     #quotation_index_df[['open','high','low','close']]=quotation_index_df[['open','high','low','close']].round(2)
     #quotation_index_df['amount'] = quotation_index_df['amount']*(10**8)
     #quotation_index_df['date'] = latest_date_str
     quotation_index_df['factor'] = 1.0
     #print(quotation_index_df)
     need_to_send_mail = []
     sub = ''
     #table_update_times = self.get_table_update_time()
     for index_name in index_list:
         yh_symbol = index_symbol_maps[index_name]
         yh_file_name = YH_SOURCE_DATA_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'] = FIX_FACTOR
         try:
             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 len(date_data)==0: #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 = '多于两天没有更新指数数据库'
                 self.drop_table(table_name=index_name)
                 self.insert_table(data_frame=yh_index_df,table_name=index_name)
             elif len(date_data) == 1: # update by last date
                 """只需要更新当天数据"""
                 self.update_sql_index_today(index_name,latest_date_str,quotation_index_df,index_symbol_maps)
                 pass
             elif len(date_data) == 2: #update to  latest date
                 """要更新盘中获取的当天数据"""
                 print(' %s index updated to %s.' % (index_name,latest_date_str))
                 if force_update:
                     print(' force update %s index' % index_name)
                     self.delete_data(table_name=index_name,condition="date='%s'" % latest_date_str)
                     self.update_sql_index_today(index_name,latest_date_str,quotation_index_df,index_symbol_maps)
                     pass
                 else:
                     pass
             else:
                 pass
         #print(date_data)
         except:
             sub = '数据表不存在'
             need_to_send_mail.append(index_name)
             print('Table %s not exist.'% index_name)
             try:
                 self.drop_table(table_name=yh_index_df)
             except:
                 pass
             self.insert_table(data_frame=yh_index_df,table_name=index_name,is_index=False)
             print('Created the table %s.' % index_name)
     if need_to_send_mail:
         content = '%s 数据表更新可能异常' % need_to_send_mail
         sm.send_mail(sub,content,mail_to_list=None)
예제 #3
0
파일: pdSql.py 프로젝트: taolaptop/pytrade
 def update_one_stock(self, symbol,force_update=False):
     index_symbol_maps = {'sh':'999999','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)
     quotation_date = ''
     try:
         quotation_index_df = qq.get_qq_quotations([symbol], ['code','date','open','high','low','close','volume','amount'])
         quotation_date = quotation_index_df.iloc[0]['date']
         #quotation_index_df = ts.get_index()
     except:
         sleep(3)
         quotation_index_df = qq.get_qq_quotations([symbol], ['code','date','open','high','low','close','volume','amount'])
         quotation_date = quotation_index_df.iloc[0]['date']
     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 = YH_SOURCE_DATA_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'] = 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 True:
             #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
                 """只需要更新当天数据"""
                 yh_index_df = yh_index_df.append(quotation_index_df, ignore_index=True)
                 print(yh_index_df)
                 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')
             dir='C:/hist/day/data/'
             file_name = dir+ '%s.csv' % index_name
             try:
                 os.remove(file_name)
                 print('Delete and update the csv file')
             except:
                 pass
             yh_index_df.to_csv(file_name ,encoding='utf-8')
     return yh_index_df
예제 #4
0
파일: pdSql0.py 프로젝트: taolaptop/pytrade
def get_exit_price(hold_codes=['300162'],data_path='C:/中国银河证券海王星/T0002/export/' ):#, has_update_history=False):
    #exit_dict={'300162': {'exit_half':22.5, 'exit_all': 19.0},'002696': {'exit_half':17.10, 'exit_all': 15.60}}
    has_update_history = True
    """
    if not has_update_history:
        easyhistory.init('D', export='csv', path="C:/hist",stock_codes=hold_codes)
        easyhistory.update(path="C:/hist",stock_codes=hold_codes)
        #has_update_history = True
    """
    #his = easyhistory.History(dtype='D', path='C:/hist',codes=hold_codes)
    #data_path = 'C:/hist/day/data/'
    #data_path = 'C:/中国银河证券海王星/T0002/export/' 
    exit_dict = dict()
    his = easyhistory.History(dtype='D', path=data_path, type='csv',codes=hold_codes)
    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)
    for code in hold_codes:
        #code_hist_df = hist[code].MA(1).tail(3).describe()
        if code=='sh000001' or code=='sh':
            code = '999999'
        if code=='cyb':
            code = '399006'
        exit_data = dict()
        hist_df  =his[code].ROC(1) 
        hist_last_date = hist_df.tail(1).iloc[0].date
        #print('hist_last_date=',hist_last_date)
        tolerance_exit_rate = 0.0
        t_rate = 0.0
        min_close = 0.0
        min_low =0.0
        if hist_last_date<last_date_str:
            hist_df['l_change'] = ((hist_df['low']-hist_df['close'].shift(1))/hist_df['close'].shift(1)).round(3)
            hist_df['h_change'] = ((hist_df['high']-hist_df['close'].shift(1))/hist_df['close'].shift(1)).round(3)
            hist_low_describe = hist_df.tail(60).describe()
            #print(hist_low_describe)
            tolerance_exit_rate = round(hist_low_describe.loc['25%'].l_change,4)
            t_rate = round(hist_low_describe.loc['75%'].h_change,4)
            #print('hist_low_change=',hist_low_change)
            #if hist_low_change< tolerance_exit_rate:
            #tolerance_exit_rate = hist_low_change
            #print('tolerance_exit_rate=',tolerance_exit_rate)
        else:
            hist_df['l_change'] = ((hist_df['low']-hist_df['close'].shift(1))/hist_df['close'].shift(1)).round(3)
            hist_df['h_change'] = ((hist_df['high']-hist_df['close'].shift(1))/hist_df['close'].shift(1)).round(3)
            hist_low_describe = hist_df.tail(60).describe()
            tolerance_exit_rate = round(hist_low_describe.loc['25%'].l_change,4)
            t_rate = round(hist_low_describe.loc['75%'].h_change,4)
            #tolerance_exit_rate = hist_low_change
            #print('tolerance_exit_rate=',tolerance_exit_rate)
            hist_df = hist_df[hist_df.date<=last_date_str]
            describe_df = his[code].MA(1).tail(3).describe()
            min_low =round(describe_df.loc['min'].low, 2)
            min_close = round(round(describe_df.loc['min'].close,2),2)
            max_close = round(describe_df.loc['max'].close,2)
            max_high = round(describe_df.loc['max'].high,2)
        exit_data['exit_half'] = min_close
        exit_data['exit_all'] = min_low
        exit_data['exit_rate'] = tolerance_exit_rate
        exit_data['t_rate'] = t_rate
        exit_dict[code] = exit_data
    #print('exit_dict=%s' % exit_dict)
    return exit_dict
예제 #5
0
파일: pdSql0.py 프로젝트: taolaptop/pytrade
def update_one_stock(symbol,realtime_update=False,dest_dir='C:/hist/day/data/', 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 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)
    if dest_df_last_date<latest_date_str:     
        quotation_date = ''
        try:
            quotation_index_df = qq.get_qq_quotations([symbol], ['code','date','open','high','low','close','volume','amount'])
            quotation_date = quotation_index_df.iloc[0]['date']
            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([symbol], ['code','date','open','high','low','close','volume','amount'])
            quotation_date = quotation_index_df.iloc[0]['date']
            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
                    """只需要更新当天数据"""
                    if realtime_update and 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)
                        #print(yh_index_df)
                    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
    else:
        print('No need to update data')
        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)
            #print(dest_df0)
            dest_df = dest_df0.append(quotation_index_df, ignore_index=True)
            #print(dest_df)
            if quotation_index_df.empty:
                pass
            else:
                dest_df.to_csv(dest_file_name ,encoding='utf-8')
        else:
            pass
    return dest_df
예제 #6
0
def get_stock_exit_price(hold_codes=['300162'],data_path='C:/中国银河证券海王星/T0002/export/' ):#, has_update_history=False):
    """获取包括股票的止损数据"""
    #exit_dict={'300162': {'exit_half':22.5, 'exit_all': 19.0},'002696': {'exit_half':17.10, 'exit_all': 15.60}}
    has_update_history = True
    """
    if not has_update_history:
        easyhistory.init('D', export='csv', path="C:/hist",stock_codes=hold_codes)
        easyhistory.update(path="C:/hist",stock_codes=hold_codes)
        #has_update_history = True
    """
    #his = easyhistory.History(dtype='D', path='C:/hist',codes=hold_codes)
    #data_path = 'C:/hist/day/data/'
    #data_path = 'C:/中国银河证券海王星/T0002/export/' 
    exit_dict = dict()
    his = easyhistory.History(dtype='D', path=data_path, type='csv',codes=hold_codes)
    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)
    for code in hold_codes:
        #code_hist_df = hist[code].MA(1).tail(3).describe()
        if code=='sh000001' or code=='sh':
            code = '999999'
        if code=='cyb':
            code = '399006'
        exit_data = dict()
        hist_df  =his[code].ROC(1) 
        hist_last_date = hist_df.tail(1).iloc[0].date
        #print('hist_last_date=',hist_last_date)
        tolerance_exit_rate = 0.0
        t_rate = 0.0
        min_close = 0.0
        min_low =0.0
        if hist_last_date<last_date_str:
            hist_df['l_change'] = ((hist_df['low']-hist_df['close'].shift(1))/hist_df['close'].shift(1)).round(3)
            hist_df['h_change'] = ((hist_df['high']-hist_df['close'].shift(1))/hist_df['close'].shift(1)).round(3)
            hist_low_describe = hist_df.tail(60).describe()
            #print(hist_low_describe)
            tolerance_exit_rate = round(hist_low_describe.loc['25%'].l_change,4)
            t_rate = round(hist_low_describe.loc['75%'].h_change,4)
            #print('hist_low_change=',hist_low_change)
            #if hist_low_change< tolerance_exit_rate:
            #tolerance_exit_rate = hist_low_change
            #print('tolerance_exit_rate=',tolerance_exit_rate)
        else:
            hist_df['l_change'] = ((hist_df['low']-hist_df['close'].shift(1))/hist_df['close'].shift(1)).round(3)
            hist_df['h_change'] = ((hist_df['high']-hist_df['close'].shift(1))/hist_df['close'].shift(1)).round(3)
            hist_low_describe = hist_df.tail(60).describe()
            tolerance_exit_rate = round(hist_low_describe.loc['25%'].l_change,4)
            t_rate = round(hist_low_describe.loc['75%'].h_change,4)
            #tolerance_exit_rate = hist_low_change
            #print('tolerance_exit_rate=',tolerance_exit_rate)
            hist_df = hist_df[hist_df.date<=last_date_str]
            describe_df = his[code].MA(1).tail(3).describe()
            min_low =round(describe_df.loc['min'].low, 2)
            min_close = round(round(describe_df.loc['min'].close,2),2)
            max_close = round(describe_df.loc['max'].close,2)
            max_high = round(describe_df.loc['max'].high,2)
        exit_data['exit_half'] = min_close
        exit_data['exit_all'] = min_low
        exit_data['exit_rate'] = tolerance_exit_rate
        exit_data['t_rate'] = t_rate
        exit_dict[code] = exit_data
    #print('exit_dict=%s' % exit_dict)
    return exit_dict
예제 #7
0
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