def get_history_data(date): df = pd.read_csv(HISTORY_FILE) data = df.loc[df[STOCK_NAMES[0]] == date_utils.lxrDate2csvDate(date)] result = data.values if len(result) > 0: return result[0] else: return None # result = get_history_data('2012') # print(result) # y_axises = [] # generate_history_csv() # import wendu_trend_chart # wendu_trend_chart.generate(STOCK_NAMES[1:], date_index, y_axises)
def get_result(date): result = {} # 遍历指数集合,计算每一个指数的温度及50日均线信号(指定日期50均值与收盘价比较) for index, stock in enumerate(constants.STOCK_CODES): # 读取数据目录下的指数csv文件,获取当前指数数据 df = read_data(stock) # 取得指定日期行的数据 if date is None: date = date_utils.csvDate2lxrDate(df[constants.DATE][0]) # 取得指定日期之前最新一天的数据 data_result = df.loc[ df[constants.DATE].map(lambda x: date_utils.csvDate2lxrDate(x)) <= date_utils.lxrDate2csvDate(date)] if len(data_result.values) == 0: print("指定日期不存在", stock, date) return date, None # 买点1:判断50日均线突破,当天收盘价是否突破50日均线 # 买点2:判断100日均线突破,当天收盘价是否突破100日均线 # 卖点1,卖点2,同上述相反 # 连续突破情况,首日突破表示买入,后续情况多仓、持仓 # 上一交易日50日均线信号 data_index = data_result.index.values[0] data = data_result.loc[data_index] data_before = data_result.loc[data_index + 1] fifty_signal = dp.check_signal(data_before, data, constants.FIFTY_MEDIAN, 1) hundred_signal = dp.check_signal(data_before, data, constants.HUNDRED_MEDIAN, 2) result_stock = { 'name': constants.STOCK_NAMES[index], constants.DATE: data[constants.DATE], constants.PB_PERCENTILE: data[constants.PB_PERCENTILE], constants.PE_PERCENTILE: data[constants.PE_PERCENTILE], constants.FIFTY_SIGNAL: fifty_signal, constants.HUNDRED_SIGNAL: hundred_signal } print('指数数据:', date, result_stock) result[stock] = result_stock return date, result
def data_download_stock(stock, url): # 请求数据 request_data = { "token": constants.TOKEN, "stockCodes": [stock], "metricsList": ["pb", "pe_ttm", "sp"], "startDate": "2000-01-01" } result = requests.post(url, json=request_data) pbs = [] pes = [] cps = [] date = [] if result.status_code == 200 and result.json()['message'] == 'success': for data in result.json()['data']: if 'sp' in data.keys(): cp = data['sp'] cps.append(cp) else: continue split_date = data['date'].split('T') date.append(dateutils.lxrDate2csvDate(split_date[0])) pb = '' if 'pb' in data.keys(): pb = data['pb'] pbs.append(pb) pe = '' if 'pe_ttm' in data.keys(): pe = data['pe_ttm'] pes.append(pe) df = pd.DataFrame() df.insert(0, constants.DATE, date) df.insert(1, constants.CP, cps) df.insert(2, constants.PB, pbs) df.insert(3, constants.PE, pes) return df
def generate(stock, input_date, show_signal): stock_name, df = download(stock) if stock_name is None or df is None: print("没有获取到数据,请确认代码输入是否正确") return print('生成股票温度图:', stock_name) date = input_date # 取得指定日期行的数据 if date is None: date = date_utils.csvDate2lxrDate(df[constants.DATE][0]) data = df.loc[df[constants.DATE] == date_utils.lxrDate2csvDate(date)] if len(data.values) == 0: print("指定日期不存在", stock, date) return # 买点1:判断50日均线突破,当天收盘价是否突破50日均线 # 买点2:判断100日均线突破,当天收盘价是否突破100日均线 # 卖点1,卖点2,同上述相反 # 连续突破情况,首日突破表示买入,后续情况多仓、持仓 # 上一交易日50日均线信号 data_index = data.index.values[0] data = df.loc[data_index] data_before = df.loc[data_index + 1] fifty_signal = dp.check_signal(data_before, data, constants.FIFTY_MEDIAN, 1) hundred_signal = dp.check_signal(data_before, data, constants.HUNDRED_MEDIAN, 2) result_stock = { constants.PB_PERCENTILE: data[constants.PB_PERCENTILE], constants.PE_PERCENTILE: data[constants.PE_PERCENTILE], constants.FIFTY_SIGNAL: fifty_signal, constants.HUNDRED_SIGNAL: hundred_signal } generate_chart(stock, stock_name, date, result_stock, show_signal)
def get_new_data(stock, start_date_fix): new_data = pd.DataFrame() result_data = download_data(stock, start_date_fix) dates = [] pbs = [] pes = [] cps = [] for data in result_data: if start_date_fix == data[constants.DATE]: # 开始日期的数据在csv中已存在,不需要再放入新的data frame continue pes.append(data[constants.PE]) pbs.append(data[constants.PB]) cps.append(data[constants.CP]) dates.append(date_utils.lxrDate2csvDate(data[constants.DATE])) # 将新的数据插入到data frame,然后返回 new_data.insert(0, constants.DATE, dates) new_data.insert(1, constants.CP, cps) new_data.insert(2, constants.PB, pbs) new_data.insert(3, constants.PE, pes) return new_data