def update(self, code): try: self.logger.info("更新股票代码: " + code) sess = sessionmaker(bind=dbu.get_engine())() stock = sess.query(StockInfo).filter_by(code=code).one() data = crawl_company_info(code) stock.type = data["type"] stock.region = data["region"] stock.short_name = data["short_name"] stock.address = data["address"] stock.full_name = data["full_name"] stock.telephone = data["telephone"] stock.email = data["email"] stock.english_name = data["english_name"] stock.capital = data["capital"] stock.chairman = data["chairman"] stock.main_business = data["main_business"] stock.industry = data["industry"] sess.add(stock) sess.commit() except Exception as ex: sess.rollback() self.logger.error(ex) finally: sess.close() self.logger.info("更新完毕!")
def get_stock_codes(self): codes = [] sess = sessionmaker(bind=dbu.get_engine())() for stock in sess.query(StockInfo).order_by(StockInfo.code): codes.append((stock.code, stock.last_update_date)) self.logger.debug("列表长度:" + str(len(codes))) return codes
def load_report(self, date=datetime.now().strftime("%Y-%m-%d")): self.logger.info("********今日导入报告********") sql = "SELECT count(0) as result FROM tb_stock_list where LAST_UPDATE_DATE <>'" + date + "'" df = pd.read_sql(sql=sql, con=database.get_engine()).sort_index(ascending=False) stock_not_updated = df["result"].item() self.logger.info("没有更新到" + date + "的股票个数为:" + str(stock_not_updated))
def test_stock_daily_basic(): engine = dbu.get_engine() session = StockBasicDailyDataDaoImpl(engine) df = session.get_data_from_163("002384", start_date="2021-10-01", end_date="2021-10-13") print(df["tclose"].values[-1]) df_max_idx = df.index.values.max() print(df_max_idx) print("=======================================") df.drop(labels=df_max_idx, axis=0, inplace=True) print(df)
def create_process(tech_config, code, start_date, end_date): engine = dbu.get_engine() # sess_factory = sessionmaker(bind=dbu.get_engine()) logger = logging.getLogger("appLogger") if not bool(logger.handlers): app.config_logger() logger.info("正在处理股票代码" + code + " 开始日期" + start_date + ",结束日期" + end_date) sbdd = StockBasicDailyDataDaoImpl(engine) df = sbdd.get_data_from_163(code, start_date, end_date) if df is None or df.shape[0] == 0: logger.warning("没有从163获得数据...") return df = sbdd.validate_last_record(code, df) if df is None or df.shape[0] == 0: logger.warning("股票代码" + code + "没有有效数据...") return sbdd.save_data_to_database(df) logger.info("股票代码" + code + "的行情信息已保存...") sbdd.update_last_update_date(code, end_date, df) logger.info("开始计算股票代码" + code + "的技术指标...") stdd = StockTechDailyDataDaoImpl(engine) tech_data_list = [] for each_date in df.index.sort_values().values: tech_data_list.append(stdd.calc_tech_data(code, each_date, tech_config)) stdd.save_data_to_database(tech_data_list) logger.info("股票代码" + code + "的技术指标信息已保存...") return
def test_update_with_sess(code): engine = dbu.get_engine() with Session(engine) as db_sess: db_sess.query(StockInfo).filter_by(code=code).update( {"last_update_date": "2021-07-09"}) db_sess.commit()
def add(self, sse_file_path, szse_file_path): stock_list = [] self.logger.info("读取上交所文件...") sse_df = pd.read_csv( sse_file_path, delimiter="\t", encoding="gbk", header=0, names=["gsdm", "gsjc", "dm", "jc", "ssrq", "blank5"], dtype={ "gsdm": str, "gsjc": str, "dm": str, "jc": str, "ssrq": str }) sse_df.drop("blank5", inplace=True, axis="columns") for idx, row in sse_df.iterrows(): code = row["gsdm"] name = row["gsjc"] first_date_to_market = row["ssrq"] stock = StockInfo(code=code, name=name, last_update_date=DEFAULT_LAST_UPDATE_DATE, first_date_to_market=first_date_to_market) stock_list.append(stock) self.logger.info("读取深交所文件...") szse_df = pd.read_excel(szse_file_path, header=0, names=[ "bk", "gsqc", "ywmz", "zcdz", "agdm", "agjc", "agssrq", "agzgb", "agltgb", "bgdm", "bgjc", "bgssrq", "bgzgb", "bgltgb", "dq", "sf", "cs", "sshy", "gswz" ]) for idx, row in szse_df.iterrows(): code = str(row["agdm"]).zfill(6) name = row["agjc"] first_date_to_market = row["agssrq"] stock = StockInfo(code=code, name=name, last_update_date=DEFAULT_LAST_UPDATE_DATE, first_date_to_market=first_date_to_market) stock_list.append(stock) self.logger.info("股票的总数为:" + str(len(stock_list))) sess = sessionmaker(bind=dbu.get_engine())() for each in stock_list: self.logger.debug("处理股票代码:" + each.code) try: _ = sess.query(StockInfo).filter_by(code=each.code).one() self.logger.debug("股票代码" + each.code + "已存在") pass except sqlalchemy.orm.exc.NoResultFound as _: sess.add(each) self.logger.debug("股票代码" + each.code + "为新股,将存入数据库") except Exception as ex: logging.error(ex) finally: sess.commit() sess.close() self.logger.info("更新完毕!")
# -*- coding: UTF-8 -*- from warnings import simplefilter simplefilter(action='ignore', category=FutureWarning) from biz.entity.tech_indicator import TechIndicatorCalculator from utils.database import get_engine calc = TechIndicatorCalculator(engine=get_engine(), code="000001", date="2021-04-30") # param = {"time_period": 26} # vr = calc.calcVR(**param) # print(vr) # VR137.3 MAVR 122.96 # param = {"time_period1": 6, "time_period2": 12} # print(calc.calcPSY(**param)) # TODO: BUG FIX param = {"time_period1": 6, "time_period2": 12, "time_period3": 24} print(calc.calcRSI(**param)) print(66.96, 63.58, 57.75) # param = {"time_period1": 12, "time_period2": 6} # print(calc.calcROC(**param)) # param = {"time_period1": 10, "time_period2": 50, "time_period3": 10} # print(calc.calcDMA(**param)) # param = {"time_period1": 12, "time_period2": 6} # print(calc.calcMTM(**param))