def sh50_buy_worst_ma(engine, start_day, end_day, max_hold, howlong, base_code, base_name): # N日相对均线最遭 #从DB抓日线数据 conn = engine.connect() # 获取日线数据 # 返回数组 # T_day1, {证券1:证券1的行情, 证券2:证券2的行情, ... } # T_day2, {证券1:证券1的行情, 证券2:证券2的行情, ... } # T_day3, {证券1:证券1的行情, 证券2:证券2的行情, ... } # ... # 其中‘行情’ 是 [收盘价,前日收盘,涨幅, 涨停标志,停牌标志] his_md = db_operator.db_fetch_dailyline(conn, start_day) # 在日线数据中,扩充加入指标数据 # 返回时,数组md_his_data扩充为 # T_day1, {证券1:证券1的行情, 证券2:证券2的行情, ... }, {证券1:证券1的行情, 证券2:证券2的行情, ... } # T_day2, {证券1:证券1的行情, 证券2:证券2的行情, ... }, {证券1:证券1的行情, 证券2:证券2的行情, ... } # T_day3, {证券1:证券1的行情, 证券2:证券2的行情, ... }, {证券1:证券1的行情, 证券2:证券2的行情, ... } # ... # ‘指标’ 是 [可买标志,均线偏离度, 均线] make_indices_by_MA_delta(conn, his_md, howlong) result, trans_num, trans_cost = sim_rotate_buy_worst( his_md, max_hold, howlong, base_code, start_day, end_day) # Output: 2-D array , 交易数, 交易成本 # 日期 基准收盘价 策略净值 交易次数 换仓详细 # ... # #util.bp( result) #准备画图 #chart_head = ['日期', '50指数', 'MA10' ] #chart_data=[] #for entry in his_md: # row = [ entry[0], entry[1][FH_BASE_CODE][0] , entry[2][FH_BASE_CODE][2] ] # chart_data.append(row) #plotter.simple_generate_line_chart( chart_head, chart_data) base_info = data_struct.SecurityInfo() base_info.code = base_code base_info.name = base_name secs = [base_info] suffix = ".from_%s" % start_day plotter.generate_htm_chart_for_faster_horse2(secs, result, suffix) #show summary t_day_num = len(result) base_delta = result[t_day_num - 1][1] / result[0][1] policy_delta = result[t_day_num - 1][2] / result[0][2] print "%d日最糟, %s ~ %s, %d个交易日,交易%d笔,交易成本%f,基准表现%f,策略表现%f" % ( howlong, result[0][0], result[t_day_num - 1][0], t_day_num, trans_num, trans_cost, base_delta, policy_delta)
def bt_brk_pb_policy(engine, start_day, end_day,max_hold, threshold ): #从DB抓日线数据 conn = engine.connect() # 获取日线数据 # 返回数组 # T_day1, {证券1:证券1的行情, 证券2:证券2的行情, ... } # T_day2, {证券1:证券1的行情, 证券2:证券2的行情, ... } # T_day3, {证券1:证券1的行情, 证券2:证券2的行情, ... } # ... # 其中‘行情’ 是 [收盘价,前日收盘,涨幅, 涨停标志,停牌标志, PB, 换手] his_md = db_operator. db_fetch_dailyline_w_valuation(conn, threshold ) # 在日线数据中,扩充加入指标数据 # 返回时,数组md_his_data扩充为 # T_day1, {证券1:证券1的行情, 证券2:证券2的行情, ... }, {证券1:证券1的指标, 证券2:证券2的指标, ... } # T_day2, {证券1:证券1的行情, 证券2:证券2的行情, ... }, {证券1:证券1的指标, 证券2:证券2的指标, ... } # T_day3, {证券1:证券1的行情, 证券2:证券2的行情, ... }, {证券1:证券1的指标, 证券2:证券2的指标, ... } # ... # ‘指标’ 是 [可买标志,PB偏离度, PB, N日PB平均,N日PB标准差] make_indices_by_pb_standard_deviation( conn, his_md , threshold) result, trans_num, trans_cost = sim_brk_pb_policy( conn,his_md, max_hold , BRK_INDEX, start_day, end_day ) # Output: 2-D array , 交易数, 交易成本 # 日期 基准收盘价 策略净值 交易次数 换仓详细 # ... # #util.bp( result) #准备画图 #chart_head = ['日期', '50指数', 'MA10' ] #chart_data=[] #for entry in his_md: # row = [ entry[0], entry[1][FH_BASE_CODE][0] , entry[2][FH_BASE_CODE][2] ] # chart_data.append(row) #plotter.simple_generate_line_chart( chart_head, chart_data) base_info = data_struct.SecurityInfo() base_info.code = BRK_INDEX base_info.name = BRK_INDEX_NAME secs = [ base_info ] suffix = ".from_%s" % start_day plotter.generate_htm_chart_for_faster_horse2( secs, result , suffix) #show summary t_day_num = len(result) base_delta = result[ t_day_num - 1][1] / result[ 0][1] policy_delta = result[ t_day_num - 1][2] / result[ 0][2] print "券商PB策略(%d日), %s ~ %s, %d个交易日,交易%d笔,交易成本%f,基准表现%f,策略表现%f" % ( threshold , result[0][0], result[ t_day_num - 1][0], t_day_num , trans_num, trans_cost , base_delta, policy_delta )
def fh50_until_now(engine, start_year): now = datetime.now() #从DB抓日线数据 start_day = "%d-01-01" % start_year conn = engine.connect() # 获取日线数据 # 返回数组 # T_day1, {证券1:证券1的行情, 证券2:证券2的行情, ... } # T_day2, {证券1:证券1的行情, 证券2:证券2的行情, ... } # T_day3, {证券1:证券1的行情, 证券2:证券2的行情, ... } # ... # 其中‘行情’ 是 [收盘价,前日收盘,涨幅, 涨停标志,停牌标志] his_md = db_operator.db_fetch_dailyline(conn, start_day) # 在日线数据中,扩充加入指标数据 # 返回时,数组md_his_data扩充为 # T_day1, {证券1:证券1的行情, 证券2:证券2的行情, ... }, {证券1:证券1的行情, 证券2:证券2的行情, ... } # T_day2, {证券1:证券1的行情, 证券2:证券2的行情, ... }, {证券1:证券1的行情, 证券2:证券2的行情, ... } # T_day3, {证券1:证券1的行情, 证券2:证券2的行情, ... }, {证券1:证券1的行情, 证券2:证券2的行情, ... } # ... # ‘指标’ 是 [可买标志,三日累计涨幅] make_indices_by_delta(conn, his_md) #util.bp_as_json( his_md) #util.bp( his_md) result, trans_num, trans_cost = sim_rotate(his_md, 3, FH_BASE_CODE, start_day, end_day) # Output: 2-D array , 交易数, 交易成本 # 日期 基准收盘价 策略净值 交易次数 换仓详细 # ... # #util.bp( result) #准备画图 base_info = data_struct.SecurityInfo() base_info.code = FH_BASE_CODE base_info.name = FH_BASE_NAME secs = [base_info] suffix = ".from_%d" % start_year plotter.generate_htm_chart_for_faster_horse2(secs, result, suffix) #show summary t_day_num = len(result) base_delta = result[t_day_num - 1][1] / result[0][1] policy_delta = result[t_day_num - 1][2] / result[0][2] print "%s ~ %s, %d个交易日,交易%d笔,交易成本%f,基准表现%f,策略表现%f" % ( result[0][0], result[t_day_num - 1][0], t_day_num, trans_num, trans_cost, base_delta, policy_delta)
def load_md_his(filepath ,dbcur, inventory_ranges ): the_file = io.open( filepath, "r", encoding='utf-8') reader = csv.DictReader( the_file, dialect = 'excel-tab') #reader = csv.reader( the_file, dialect = 'excel-tab') row_num = 0 info = data_struct.SecurityInfo( ) info.parse_from_filepath( filepath ) the_code = info.code #info.dump() db_operator.save_sec_info_to_db(dbcur, info) if the_code in inventory_ranges: the_range = inventory_ranges[the_code] else: the_range = None #print the_range for row in reader: row_num +=1 if 1== row_num: verify_his_csv_format(filepath, row ) #s = str(row).decode('string_escape').decode('utf8') # 能显示 DectReader吐的中文 #s = row[0].decode('utf8') #<= OK,能显示 plain reader 吐的 #.decode('string_escape') #print s #pdb.set_trace() md_record = data_struct.MdRecord() md_record.code = the_code md_record.load_from_his_csv_row( row) #md_record.dump() if the_range is not None \ and md_record.t_day >= the_range.start \ and md_record.t_day <= the_range.end : continue db_operator.save_MD_to_db( dbcur, md_record) print "%s was imported" % filepath the_file.close()
def get_sec_info(dbcur): dbcur.execute(''' select code, name, dir from SecurityInfo ''') r = {} row = dbcur.fetchone() while row is not None: one_entry = data_struct.SecurityInfo() one_entry.code = row[0] one_entry.name = row[1] one_entry.dirpath = row[2] r[row[0]] = one_entry row = dbcur.fetchone() return r
def load_daily_md(filepath, yyyymmdd, dbcur, inventory_ranges ): with io.open( filepath, "r", encoding='utf-8') as the_file: reader = csv.DictReader( the_file, dialect = 'excel-tab') sec_info = db_operator.get_sec_info(dbcur) row_num = 0 for row in reader: row_num +=1 if 1== row_num: verify_daily_csv_format(filepath, row ) md_record = data_struct.MdRecord() r = md_record.load_from_daily_csv_row( filepath, row, yyyymmdd) if not r: continue # 如果某证券从未由‘历史’文件倒入过,那么不会在SecurityInfo里有记录,所以 # 不能 if md_record.code not in inventory_ranges: xxxx # if md_record not in sec_info: info = data_struct.SecurityInfo( ) info.code = md_record.code info.name = md_record.name #info.dump() db_operator.save_sec_info_to_db_if_not_exists(dbcur, info) db_operator.save_MD_to_db( dbcur, md_record) dbcur.connection.commit() print "%s was imported" % filepath