def sendConcernedStkPicToSelf_V2(): """ 将自己关心的数据打印出图,发送到qq 相较于之前版本,本次改进为: 1、使用json文件中的code列表代替数据库中的列表 2、进行判断,出发发送条件再予以发送 :return: """ towho = '影子2' send_qq(towho, '以下是已入的stk的形势图:') code_list = read_config()['buy_stk'] for x in code_list + ['sh', 'sz', 'cyb']: df = get_k_data_JQ(x, 400) fig, _ = genStkPicForQQ(df) plt.title(str(x)) send_pic_qq(towho, fig) # plt.show() plt.close() fig, _ = genStkIdxPicForQQ(df) plt.title(str(x)) send_pic_qq(towho, fig) # plt.show() plt.close() send_W_M_MACD(x, towho)
def sendConcernedStkPicToSelf_T(): """ 将自己关心的数据打印出图,发送到qq :return: """ towho = '影子2' send_qq(towho, '以下是已入的stk的形势图:') stk_buy = read_config()['buy_stk'] for x in stk_buy + ['sh', 'sz', 'cyb']: df = get_k_data_JQ(x, 400) fig, _, attention = genStkPicForQQ(df, x) if attention: send_pic_qq(towho, fig) send_W_M_MACD(x, towho) plt.close() # 打印第二张套图 fig, _, attention = genStkIdxPicForQQ(df, x) if attention: send_pic_qq(towho, fig) plt.close()
def printConcernedPredict2Self(): towho = u'影子2' for stk in ['000001', '000333', '300508']: close_today = ts.get_k_data( stk, start=add_date_str(get_current_date_str(), -5)).tail(1)['close'].values[0] r = [(label, '%0.2f' % predict_tomorrow(stk, label, N_STEPS=N_STEPS, feature_cols=feature_cols, HIDDEN_SIZE=HIDDEN_SIZE, NUM_LAYERS=NUM_LAYERS)) for label in ['high', 'low', 'close']] # 增加与今天收盘价的对比 r_contrast = [ (x[0], x[1], '%0.2f' % ((float(x[1]) - close_today) / close_today * 100) + '%') for x in r ] # stk2name = { # 'sh': '上证', # 'sz': '深证', # 'cyb': '创业板' # } print(stk + ':\n' + str(r_contrast)) send_qq(towho, stk + ':\n' + str(r_contrast))
def updatePotInfo(): """ :param df_H_L_Pot: :return: """ # h_l_pot_info_url = '.\InfoRestore\df_H_L_Pot.pkl' if os.path.exists(h_l_pot_info_url): with open(h_l_pot_info_url, 'rb') as f: try: h_l_pot_info = pickle.load(f) except: h_l_pot_info = pd.DataFrame() else: h_l_pot_info = pd.DataFrame() if h_l_pot_info.empty: h_l_pot_info = initPotInfo(get_h_l_pot(stk_list)) else: new_pot_info = get_h_l_pot(stk_list) h_l_pot_info = pd.concat([ h_l_pot_info.drop([ 'half_year_high', 'half_year_low', 'month_high', 'month_low', 'year_high', 'year_low' ], 1), new_pot_info.drop('stk', 1) ], axis=1) # 将结果序列化 with open(h_l_pot_info_url, 'wb') as f: pickle.dump(h_l_pot_info, f) send_qq(u'影子', '高底线信息更新成功!')
def judgeAndSendMsg(): """ 按频率调用, :return: """ if os.path.exists(h_l_pot_info_url): with open(h_l_pot_info_url, 'rb') as f: h_l_pot_info = pickle.load(f) else: print('函数 judgeAndSendMsg: 加载高低信息失败!') return df_H_L_Pot = h_l_pot_info for stk in df_H_L_Pot.index: # 获取该stk的实时价格 current_price = float( ts.get_realtime_quotes(df_H_L_Pot.loc[stk, 'stk'])['price'].values[0]) # 将当前价格保存,用于验证计算准确性 df_H_L_Pot.loc[stk, 'current_price'] = current_price """ 年线判断 """ df_H_L_Pot = lineJudge(df_H_L_Pot_index=stk, current_price=current_price, df_info=df_H_L_Pot, line_str='year') """ ----------------- 半年线判断 -----------------""" df_H_L_Pot = lineJudge(df_H_L_Pot_index=stk, current_price=current_price, df_info=df_H_L_Pot, line_str='half_year') """ ----------------- 月线判断 ----------------""" df_H_L_Pot = lineJudge(df_H_L_Pot_index=stk, current_price=current_price, df_info=df_H_L_Pot, line_str='month') """ 检查并发送消息 """ for idx in df_H_L_Pot.index: # 遍历年线、半年线和月线 for sts in ['year_status', 'half_year_status', 'month_status']: if (df_H_L_Pot.loc[idx, sts] != u'正常') & (df_H_L_Pot.loc[ idx, sts] != df_H_L_Pot.loc[idx, sts + '_last']): send_qq( u'影子', 'stk:' + get_name_by_stk_code( g_total_stk_info_mysql, df_H_L_Pot.loc[idx, 'stk']) + '\n' + '当前价格:' + str(df_H_L_Pot.loc[idx, 'current_price']) + '\n' + '事件: “' + df_H_L_Pot.loc[idx, sts + '_last'] + '” --> “' + df_H_L_Pot.loc[idx, sts] + '”' + '\n\n') df_H_L_Pot.loc[idx, sts + '_last'] = df_H_L_Pot.loc[idx, sts] with open(h_l_pot_info_url, 'wb') as f: pickle.dump(h_l_pot_info, f) print('函数 judgeAndSendMsg: 完成本次判断!')
def printMainRank(): """ 打印重要的index """ r = [(x, '%.2f' % (calRealtimeRankWithGlobal(stk_code=x)[0])) for x in ['sh', 'sz', 'cyb']] send_qq('影子', "Main_Index:\n" + str(r))
def checkWeekStrayForAll(): towho = '影子2' send_qq(towho, '以下是今晚海选结果:') df_total = ts.get_stock_basics() # 过滤掉年龄小于四岁的 df_age_filter = df_total[df_total.apply(lambda x: int( str(x['timeToMarket'])[:4]) <= int(get_current_date_str()[:4]) - 4, axis=1)] # 根据week反转情况进行过滤,保留有反转的单位 df_age_filter_stray = list(df_age_filter.reset_index().apply( lambda x: (x['code'], week_MACD_stray_judge(x['code'], towho)), axis=1)) # 过滤掉非反转的情况 df_age_f2 = list(filter(lambda x: x[1][0], df_age_filter_stray)) # 增加stk的水平信息 df_level = [(x[0], x[1][1], cal_stk_p_level(x[1][1]['close'].values)) for x in df_age_f2] # 按总体水平排序,筛选优异者 df_level.sort(key=lambda x: x[2]['total_last']) df_level = df_level[:math.floor(len(df_level) / 3 * 2)] # 按照近30的波动率进行排序,筛选优异者 df_level.sort(key=lambda x: x[2]['std'], reverse=True) df_level = df_level[:math.floor(len(df_level) / 3 * 2)] # 按照近30的水平排序,留下最后8只 df_level.sort(key=lambda x: x[2]['t30_last'], reverse=False) df_level = df_level[:np.min([math.floor(len(df_level) / 3 * 2), 15])] # 打印信息 stk_list = [k[0] for k in df_level] for stk in stk_list: # 打印周与月信息 send_W_M_MACD(stk_code=stk, towho=towho) # 打印日线信息 df = get_k_data_JQ(stk, count=400, end_date=get_current_date_str()) fig, _, _ = genStkPicForQQ(df) plt.title(str(stk)) send_pic_qq(towho, fig) plt.close() fig, _, _ = genStkIdxPicForQQ(df) plt.title(str(stk)) send_pic_qq(towho, fig) plt.close()
def updateRSVRecord(): try: code_list = read_config()['buy_stk'] # global RSV_Record for stk in code_list: RSV_Record[stk] = cal_rsv_rank(stk, 5) except Exception as e: send_qq('影子2', 'RSV数据更新失败!\n' + str(e))
def update_price_ratio_info(): """ :param df_H_L_Pot: :return: """ # 将结果序列化 with open(price_ratio_info_url, 'wb') as f: pickle.dump(h_l_pot_info, f) send_qq(u'影子', '高底线信息更新成功!')
def printMainRankForPublic(): """ 打印重要的index """ r = [(x, '%.2f' % (calRealtimeRankWithGlobal(stk_code=x)[0]), '%.2f' % (calRealtimeRankWithGlobal(stk_code=x)[2])) for x in ['sh', 'sz', 'cyb']] note = str([x[0]+'\n'+'上涨概率:'+str(x[1])+'%\n'+'当前值:'+str(x[2])+'\n' for x in r])\ .replace('sh', '上证')\ .replace('sz', '深证')\ .replace('cyb', '创业板')\ .replace('[', '')\ .replace(']', '')\ .replace("'", '')\ .replace('\\n', "\n")\ .replace(',', '--------------------\n') send_qq('大盘上涨概率公示', '\n\n--------------------\n' + note)
def myPrint(str_gui, str_temp, method='n', towho=''): """ :param gui: :param str_gui: :param method: :param towho: :return: """ if method is 'n': print(str_temp) elif method is 'gm': str_gui['msg'] = str_gui['msg'] + str_temp + '\n\n' elif method is 'gn': str_gui['note'] = str_gui['note'] + str_temp + '\n\n' elif method is 'qq': send_qq(towho, str_temp) return str_gui
def week_MACD_stray_judge(stk_code, towho, debug_plot=False): try: # 获取今天的情况,涨幅没有超过3%的不考虑 df_now = get_k_data_JQ(stk_code, count=2, end_date=get_current_date_str()).reset_index() if (df_now.tail(1)['close'].values[0] - df_now.head(1) ['close'].values[0]) / df_now.head(1)['close'].values[0] < -0.05: print('函数week_MACD_stray_judge:' + stk_code + '涨幅不够!') return False, pd.DataFrame() df = get_k_data_JQ(stk_code, count=400, end_date=get_current_date_str()).reset_index() if len(df) < 350: print('函数week_MACD_stray_judge:' + stk_code + '数据不足!') return False, pd.DataFrame() # 规整 df_floor = df.tail(math.floor(len(df) / 20) * 20 - 19) # 增加每周的星期几 df_floor['day'] = df_floor.apply(lambda x: calendar.weekday( int(x['date'].split('-')[0]), int(x['date'].split('-')[1]), int(x['date'].split('-')[2])), axis=1) # 增加每周的星期几 df_floor['day'] = df_floor.apply(lambda x: calendar.weekday( int(x['date'].split('-')[0]), int(x['date'].split('-')[1]), int(x['date'].split('-')[2])), axis=1) # 隔着5个取一个 if df_floor.tail(1)['day'].values[0] != 4: df_floor_slice_5 = pd.concat( [df_floor[df_floor.day == 4], df_floor.tail(1)], axis=0) else: df_floor_slice_5 = df_floor[df_floor.day == 4] # 计算指标 df_floor_slice_5['MACD'], df_floor_slice_5[ 'MACDsignal'], df_floor_slice_5['MACDhist'] = talib.MACD( df_floor_slice_5.close, fastperiod=6, slowperiod=12, signalperiod=9) # 隔着20个取一个(月线) df_floor_slice_20 = df_floor.loc[::20, :] # 计算指标 df_floor_slice_20['MACD'], df_floor_slice_20[ 'MACDsignal'], df_floor_slice_20['MACDhist'] = talib.MACD( df_floor_slice_20.close, fastperiod=4, slowperiod=8, signalperiod=9) # 获取最后的日期 date_last = df_floor_slice_5.tail(1)['date'].values[0] # 判断背离 MACD_5 = df_floor_slice_5.tail(3)['MACD'].values MACD_20 = df_floor_slice_20.tail(4)['MACD'].values if (MACD_5[1] == np.min(MACD_5)) & (MACD_20[1] != np.max(MACD_20)) & ( MACD_20[2] != np.max(MACD_20)): if debug_plot: """ --------------------------------------- 生成图片 -------------------------------------""" fig, ax = plot_W_M(df_floor_slice_5, df_floor_slice_20) # 增加标题 plt.title(stk_code + 'month-stray' + date_last) # 发送图片 send_pic_qq(towho, fig) # 关闭图片 plt.close() return True, df else: return False, pd.DataFrame() except Exception as e: send_qq(towho, stk_code + '出错:\n' + str(e)) return False, pd.DataFrame()
def autoShutdown(): send_qq('影子', '120秒后将自动关机!') os.system('shutdown -s -f -t 120')
# encoding=utf-8
def sendRelaLevel2QQ(): send_qq(towho='影子2', msge='注意低位囤货:') calRelaPLevel(readConfig()['safe_stk'], -720, '影子2')
# encoding=utf-8