def main(): act = input( "1.执行今日操作\n2.进行数据模拟\n 3.拉取全部基金列表\n4.拉取指定基金数据\n5.进行当前数据排名\n6.修复拉取忽略列表\n" ) if act == "1": for code in code_list: market = simulation_market() market.code = code market.handel_today() elif act == "2": market = simulation_market() code = input("fund code = ") start_day = input("start date = ") end_day = input("end date = ") start_money = int(input("start money = ")) market.auto_running(code, start_day, end_day, start_money) elif act == "3": fund_data_manager.get_ins().check_out_all_fund_code() elif act == "4": fund_data_manager.get_ins().check_out_fund_base_data() elif act == "5": fund_filter.rank_fund_base_data() elif act == "6": db = db_loader.get_ins().load_db("fund_base_data") code_list_cur = db.get_all_keys() ignore_db = db_loader.get_ins().load_db("ignore_base_data_pull_list") ignore_db.set_info("ignore_list", code_list_cur) db_loader.get_ins().close()
def rank_fund_base_data(): db = db_loader.get_ins().load_db("fund_base_data") fund_list_db = db_loader.get_ins().load_db("fund_list") key_list = db.get_all_keys() filter_list = [] filter_type = ["股票型", "指数型"] for key in key_list: if not fund_list_db.get_info(key)[3] in filter_type: continue dic = db.get_info(key) filter1 = False or "sharpe_rate" in dic filter2 = False or "standard_dev" in dic filter3 = False or "rank4" in dic filter4 = False or "same_rank" in dic filter5 = False or "stage_rate" in dic filter6 = False or "same_avg" in dic filter7 = False or "hs_300" in dic if filter1 and filter2 and filter3 and filter4 and filter5 and filter6 and filter7: filter_list.append(key) score_list = [] for code in filter_list: item = filter_code(code) score_list.append(item) sort_list = [] for item in score_list: is_insert = True for index in range(0, len(sort_list)): if sort_list[index][1] < item[1]: is_insert = False sort_list.insert(index, item) break if is_insert: sort_list.append(item) f = open('fund_rank.csv', "w", encoding='utf-8-sig', newline='') csv_writer = csv.writer(f) csv_writer.writerow( ["代码", "基金名", "四分位", "夏普率", "标准差", "同类排名", "阶段涨幅", "总分"]) fund_db = db_loader.get_ins().load_db("fund_list") for item in sort_list: code = item[0] name = fund_db.get_info(code)[2] score_list = item[2] all_score = item[1] rank4 = score_list[0] sharpe = score_list[1] standard = score_list[2] same = score_list[3] stage = score_list[4] csv_writer.writerow( [code, name, rank4, sharpe, standard, same, stage, all_score]) f.close()
def handel_today(self): if self.code == None: self.code = input("input code = ") else: print("====================> handel ", self.code) is_exist = fund_data_manager.get_ins().check_fund_data_exist(self.code) if not is_exist: print("缺少基础数据库,需要先checkout当前基金数据") fund_data_puller.main() tactics = None if fund_data_manager.get_ins().check_fund_data_exist("running_data_" + self.code): print("加载交易进程") tactics = every_day_tactics(0, "", self.code) tactics.load_today() else: print("不存在该进程") if input("初始化交易进程?(Y/N) ") == "y": start_date = input("start date (year-mon-day) ") if start_date == "": today_struct = time.localtime() start_date = "{0}-{1}-{2}".format( str(today_struct[0]).zfill(2), str(today_struct[1]).zfill(2), str(today_struct[2]).zfill(2)) start_money = input("start money ") tactics = every_day_tactics(int(start_money), start_date, self.code) db_loader.get_ins().load_db("running_data_" + self.code) else: return self.tactics.append(tactics) if fund_data_manager.get_ins().check_today_need_pull(self.code): tactics.update_db_data() today_struct = time.localtime() today = "{0}-{1}-{2}".format( str(today_struct[0]).zfill(2), str(today_struct[1]).zfill(2), str(today_struct[2]).zfill(2)) print("今日交易日期", today, "操作代码", self.code) is_go = True self.today = today tactics.on_end_today(self.today) self.simulation() tactics.save_today() if input("显示折线图?(Y/N) ") == "y": ret = self.filter_tactic_record() self.draw_plt(ret)
def check_today_need_pull(self,code): pull_record_db = db_loader.get_ins().load_db("pull_record") date = pull_record_db.get_info(code) if date == None : return True else: today_struct = time.localtime() today_date = "{0}-{1}-{2}".format(str(today_struct[0]).zfill(2),str(today_struct[1]).zfill(2),str(today_struct[2]).zfill(2)) today_stamp = time.mktime(time.strptime(today_date,'%Y-%m-%d')) last_stamp = time.mktime(time.strptime(date,'%Y-%m-%d')) return today_stamp > last_stamp
def get_day_data(self,code,day): db = db_loader.get_ins().load_db(code) data = db.get_info(day) if not data: db.set_info(day,"sub") return "sub" else: if data != "sub": data[1] = float(data[1]) data[2] = float(data[2]) return data
def main(): act = str(input(" update or check out ? (1/2) ")) code = str(input("input code = ")) if act == "2": if db_loader.get_ins().check_db_exist(code): go = input("this code data is exist ! go on ? y/n ") if go == "y": update(code) return elif go == "n": print("cancel CHECK OUT") return else: check_out(code) return elif act == "1": if db_loader.get_ins().check_db_exist(code): update(code) return else: check_out(code) return
def save_today(self): db = db_loader.get_ins().get_db("running_data_" + self.code) db.set_info("start_money", self.start_money) db.set_info("trading_list", self.trading_list) db.set_info("cur_money", self.cur_money) db.set_info("hold_stock", self.hold_stock) db.set_info("last_money", self.last_money) db.set_info("start_day", self.start_day) db.set_info("today", self.today) db.set_info("record_list", self.record_list) db.set_info("today_trading_money", self.today_trading_money) db.set_info("today_sell_vol", self.today_sell_vol) db.set_info("temp_data", self.temp_data) db.set_info("buy_list", self.buy_list) db.set_info("risk_per", self.risk_per) db.set_info("price_list", self.price_list) db.set_info("sell_per", self.sell_per) db.set_info("wave", self.wave) db.set_info("wave_dir", self.wave_dir) db.set_info("last_dir", self.last_dir) db.set_info("limit_price", self.limit_price) db.flush()
def filter_code(code): print(code) db = db_loader.get_ins().load_db("fund_base_data") dic = db.get_info(code) rank4 = filter_rank4(dic["rank4"]) sharpe = filter_sharpe_rate(dic["sharpe_rate"]) standard = filter_standard_dev(dic["standard_dev"]) same = filter_rank(dic["same_rank"]) ret = filter_stage_rate(dic["stage_rate"], dic["same_avg"], dic["hs_300"]) stage = ret[0] samve_avg = ret[1] hs = ret[2] nrank4 = np.array(rank4) nsharpe = np.array(sharpe) nstandard = np.array(standard) nsame = np.array(same) nstage = np.array(stage) nsame_avg = np.array(samve_avg) nhs = np.array(hs) nstage = nstage - nsame_avg + (nstage - nhs) score = 0 data = { "rank4": nrank4, "sharpe": nsharpe, "standard": nstandard, "same": nsame, "stage": nstage, } score_list = [] for key in data: arg1 = data[key] arg2 = cost_list[key] cur_score = np.multiply(arg1, arg2).sum() * fix_list[key] score += cur_score score_list.append(round(cur_score, 3)) return (code, score, score_list)
def load_today(self): db = db_loader.get_ins().get_db("running_data_" + self.code) self.start_money = db.get_info("start_money") self.last_money = db.get_info("last_money") self.cur_money = db.get_info("cur_money") self.trading_list = db.get_info("trading_list") self.hold_stock = db.get_info("hold_stock") self.start_day = db.get_info("start_day") self.today = db.get_info("today") self.record_list = db.get_info("record_list") self.today_trading_money = db.get_info("today_trading_money") self.today_sell_vol = db.get_info("today_sell_vol") self.temp_data = db.get_info("temp_data") self.buy_list = db.get_info("buy_list") self.risk_per = db.get_info("risk_per") self.price_list = db.get_info("price_list") self.sell_per = db.get_info("sell_per") date = self.find_next_trad_day(self.start_day) self.start_price = fund_data_manager.get_ins().get_day_data( self.code, date) self.wave = db.get_info("wave") or [] self.wave_dir = db.get_info("wave_dir") or 0 self.last_dir = db.get_info("last_dir") or 0 self.limit_price = db.get_info("limit_price") or 0
def check_out_fund_base_data(self): db = db_loader.get_ins().load_db("fund_list") code_list = db.get_all_keys() spider.catch_fund_base_data(code_list)
def check_out_all_fund_code(self): data_list = spider.catch_all_fund_list() db = db_loader.get_ins().load_db("fund_list") for item in data_list: db.set_info(item[0],item)
def save_check_out_date(self,code,date): pull_record_db = db_loader.get_ins().load_db("pull_record") pull_record_db.set_info(code,date)
def check_fund_data_exist(self,code): return db_loader.get_ins().check_db_exist(code)
def set_history_data(self,code,server_data): db = db_loader.get_ins().load_db(code) for item in server_data: key = item[0] db.set_info(key,item) db.flush()
def test(): db = db_loader.get_ins().load_db("fund_list") info = db.get_info("004698") print(info)
def catch_fund_base_data(code_list): db = db_loader.get_ins().load_db("fund_base_data") ignore_db = db_loader.get_ins().load_db("ignore_base_data_pull_list") ignore_list = ignore_db.get_info("ignore_list") or [] base_url = 'http://fund.eastmoney.com/{0}.html?spm=001.1.swh' sharpe_url = 'http://fundf10.eastmoney.com/tsdata_{0}.html' driver = webdriver.PhantomJS( executable_path= r"C:\\Users\\myppc\\Desktop\\hzy\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe" ) for code in code_list: if code in ignore_list: continue info = {} try: url = base_url.format(code) driver.get(url) body_element = driver.find_element_by_xpath( "//div[@id='body']").find_element_by_xpath( "//div[@class='quotationItem_left quotationItem_left02']") gain_element = body_element.find_element_by_xpath( "//div[@id='IncreaseAmount']").find_element_by_xpath( "//li[@id='increaseAmount_stage']") gain_text = gain_element.text gain_text_list = re.split( "阶段涨幅\n|同类平均\n|沪深300\n|跟踪标的\n|同类排名\n|四分位排名\n", gain_text)[1:] if "跟踪标的\n" in gain_text: del gain_text_list[3] name_list = [ "stage_rate", "same_avg", "hs_300", "same_rank", "rank4" ] index = 0 for item in gain_text_list: name = name_list[index] info[name] = item.split("\n") index += 1 db.set_info(code, info) print("succ stage1 ", code) except BaseException: print("check out error 1 ", code) db.flush() time.sleep(round(random.uniform(0.5, 2.5), 2)) try: url = sharpe_url.format(code) driver.get(url) sharpe_element = driver.find_element_by_xpath( "//div[@class='boxitem w790']").find_element_by_xpath( "//table[@class='fxtb']") sharpe_text = sharpe_element.text sharpe_text_list = re.split("\n标准差 |\n夏普比率 |\n信息比率", sharpe_text)[1:] if "\n信息比率" in sharpe_text: del sharpe_text_list[2] name_list = ["standard_dev", "sharpe_rate"] index = 0 for item in sharpe_text_list: name = name_list[index] info[name] = item.split(" ") index += 1 db.set_info(code, info) print("succ stage2 ", code) except BaseException: print("check out error 2 ", code) db.flush() ignore_list.append(code) ignore_db.set_info("ignore_list", ignore_list) time.sleep(round(random.uniform(0.5, 2.5), 2)) db.flush() ignore_db.flush()