def rotate( dbcur, secs, max_hold , start_day , end_day ): md_his_data = fetch_md_his_for_faster_horse2( dbcur, secs) make_indices_by_MA_delta( dbcur, secs, md_his_data, 0, 0 ) bt,trans_num = sim_rotate( md_his_data, max_hold , start_day, end_day) suffix = "" if "" != start_day: suffix = ".%s_%s" % (start_day, end_day) plotter.generate_htm_chart_for_faster_horse2( secs, bt , suffix) first_entry = bt[0] last_entry = bt[len(bt) - 1 ] #net_value = math.exp( last_entry [ len(secs) +1] ) net_value = last_entry [ len(secs) +1] / INITIAL_BALANCE legs = [] for i,sec in enumerate(secs): #print "%s " % str(sec) legs.append( math.exp( last_entry [ i + 1] - first_entry[ i + 1] )) #net_value = 0 #print "以上fh2\n" return ( net_value , len(bt) , trans_num, legs )
def faster_horse2( dbcur, secs, MA_Size1, switch_threshold , start_day , end_day ): md_his_data = fetch_md_his_for_faster_horse2( dbcur, secs) #make_indices_by_last_delta( dbcur, secs, md_his_data ) make_indices_by_MA_delta( dbcur, secs, md_his_data, MA_Size1, 0 ) #pp = pprint.PrettyPrinter(indent=4) #pp.pprint(md_his_data) #bt,trans_num = sim_faster_horse2( md_his_data, start_day, end_day) bt,trans_num = sim_faster_horse3( md_his_data, switch_threshold , start_day, end_day) suffix = "" if "" != start_day: suffix = ".%s_%s" % (start_day, end_day) plotter.generate_htm_chart_for_faster_horse2( secs, bt , suffix) first_entry = bt[0] last_entry = bt[len(bt) - 1 ] net_value = math.exp( last_entry [ len(secs) +1] ) legs = [] for i,sec in enumerate(secs): #print "%s " % str(sec) legs.append( math.exp( last_entry [ i + 1] - first_entry[ i + 1] )) #net_value = 0 #print "以上fh2\n" return ( net_value , len(bt) , trans_num, legs )
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)