def saveStkMRankHistoryData(stk_code, history_days, m_days, save_dir): """ 保存stk的历史数据,用来实时计算均线离心度分数,需要存历史数据,尽量不要用! :param stk_code: :param history_days: :param save_dir: './M_data/' :return: """ df = get_k_data_JQ(stk_code, 400) if len(df) < history_days*0.8: print('函数 calSingleStkRank: 该stk历史数据不足!') return -1 # 测试相对均值偏移度 df['m9'] = df['close'].rolling(window=m_days).mean() df['diff_m9'] = df.apply(lambda x: (x['close'] - x['m9'])/x['close'], axis=1) df = df.dropna() dict_restore = { 'stk_code': stk_code, 'history_M_diverge_data': list(df['diff_m9'].values), 'latest_data': list(df.tail(m_days-1)['close'].values), 'update_date': df.tail(1)['date'].values[0] } shelveP( data=dict_restore, saveLocation=save_dir, fileName=stk_code+'_M'+str(m_days))
def initScale(stk_list, list_name): """ :param stk_list: :return: """ shelveP(dict([(x, -1) for x in stk_list]), LastScale, list_name)
def checkDivergeLowLevel_Sub(stk_list, stk_list_name, scale_threshold, hist_data_dir, qq_win_name, desk=2, logic=True): """ :param stk_list: :param stk_list_name: :param scale_threshold: :param hist_data_dir: :param desk: :param logic: 为真时,大于阈值会触发,为假时,小于阈值会触发 :return: """ # 判断是否存在上次分数,存在则加载,否则初始化一个 if os.path.exists(LastScale+stk_list_name+'.dat'): lastscale_stk_pool = loadLastScale(stk_list_name) else: initScale(stk_list, stk_list_name) lastscale_stk_pool = loadLastScale(stk_list_name) for stk in stk_list: r, history_data, p_now, update_date = calRealtimeRankWithGlobal(stk_code=stk) # 生成语言描述 if (r-lastscale_stk_pool[stk]) > 0: note = '分数上涨'+'%0.1f' % (r-lastscale_stk_pool[stk]) else: note = '分数下落'+'%0.1f' % (r - lastscale_stk_pool[stk]) if logic: if considerMainIndex(stk, r): if math.fabs(r-lastscale_stk_pool[stk]) > desk: # 更新上次分数 lastscale_stk_pool[stk] = r send_qq(qq_win_name, 'Attention:\n' + stk + note + '\nscore:'+str('%0.2f') % r + '\np_now:' + str(p_now) + '\nhistory:' + str(history_data) + '\nupdate_date:' + str(update_date)) shelveP(lastscale_stk_pool, LastScale, stk_list_name) else: print('与上次命令相同!') else: print(stk+'分数处于正常状态!分数为:'+str('%0.2f') % r) else: if r < scale_threshold: if math.fabs(r-lastscale_stk_pool[stk]) > desk: # 更新上次分数 lastscale_stk_pool[stk] = r send_qq(qq_win_name, 'Attention:\n' + stk + note + '\nscore:' + str('%0.2f') % r + '\np_now:' + str(p_now) + '\nhistory:' + str(history_data) + '\nupdate_date:' + str(update_date)) shelveP(lastscale_stk_pool, LastScale, stk_list_name) else: print('与上次命令相同!') else: print(stk + '分数处于正常状态!分数为:' + str('%0.2f') % r)
# encoding=utf-8