def do_save_basics_pre(share_code, time_to_market): engine = DBUtils.db_connection() cur_date = Utils.get_current_date() change_time = time_to_market while change_time < cur_date: next_year = Utils.get_next_year(change_time) logger.debug('prepare to save next time range data....change_time=' + str(change_time) + 'next_year=' + str(next_year)) DBUtils.save_stock_h_data(engine, share_code, change_time, next_year) change_time = Utils.get_next_day(Utils.get_next_year(change_time))
def market_simulate(ware_house, share_code, start_date, end_date): result = [] mm = MoneyMaker.MoneyMaker(ware_house, share_code) engine = DBUtils.db_connection() fronter_date = Utils.get_front_day(start_date, 60) print('fronter_date=' + fronter_date) rs = DBUtils.db_read_k_history_des(share_code, engine, fronter_date, end_date) trade_cal = DBUtils.read_trad_cal(engine) # print("rs:::::::::::::::::::::::::") # print(rs) cur_date = start_date '''买入日期,如果前一天买入,第二天触发买入条件则不买入''' # sell_day = Utils.get_current_date() '''卖出日期,如果前一天卖出,第二天触发卖出条件则不卖出''' # buy_day = Utils.get_current_date() while cur_date <= end_date: is_today_open = trade_cal.loc[trade_cal[u'calendarDate'] == cur_date].iloc[0, 2] # is_yesterday_open=np.array(trade_cal.loc[trade_cal[u'calendarDate']==Utils.get_front_day(cur_date,1)][u'isOpen']).tolist()[0] logger.debug('begin:::::::::::::' + str(cur_date)) '''获取当前日期前的集合''' temp_rs = rs.loc[(rs[u'date'] < cur_date)] # print("temp_rs====================") # print(temp_rs) '''从当前日期前的集合获取排序前20个的集合''' days = Utils.BOLL_DAY filter_rs = temp_rs.iloc[0:days] # print("filter_rs==================") # print(filter_rs) (up, mi, dn) = get_boll_result(filter_rs) '''看当天日期,如果为休市状态,则跳到下一日''' if is_today_open == Utils.CLOSE_SALE: cur_date = Utils.get_next_day(cur_date) continue tmp_today = rs.loc[(rs[u'date'] == cur_date)] yest_tmp = Utils.get_front_day(cur_date, 1) # is_yesterday_open = \ # np.array(trade_cal.loc[trade_cal[u'calendarDate'] == yest_tmp][u'isOpen']).tolist()[ # 0] is_yesterday_open = trade_cal.loc[trade_cal[u'calendarDate'] == yest_tmp].iloc[0, 2] '''如果前一个交易日不开市,一直往前找到最近一个交易日''' while is_yesterday_open != Utils.OPEN_SALE: yest_tmp = Utils.get_front_day(yest_tmp, 1) is_yesterday_open = trade_cal.loc[trade_cal[u'calendarDate'] == yest_tmp].iloc[0, 2] tmp_yesterday = rs.loc[(rs[u'date'] == yest_tmp)] # today_info = np.array(tmp_today[u'close']).tolist()[0] # today_info = tmp_today.iloc[0,3] close_price = round(tmp_today.iloc[0, 3], 4) # yesterday_info = np.array(tmp_yesterday[u'close']).tolist()[0] close_yesterday = round(tmp_yesterday.iloc[0, 3], 4) # close_yesterday = round(yesterday_info, 4) logger.debug('close_yesterday::::::::::::' + str(close_yesterday)) logger.debug('close_price::::::::::::' + str(close_price)) logger.debug("MainProcess:::::up,mi,dn======" + str(up) + ' ' + str(mi) + ' ' + str(dn)) logger.debug(close_price) # print(temp_date) is_up_or_down = check_k_trend(close_yesterday, close_price) if is_up_or_down == Utils.TREND_DOWN: '''现根据当天close价格刷新市值''' mm.refresh_market_price(close_price) # cur_today=Utils.get_current_frontday(1) if 0 <= close_price - mi < 0.1: ware_house = mm.mm_buy_policy() buy_result = mm.buy_share(ware_house, close_price) # buy_day = Utils.get_current_date() if buy_result: logger.info("buy=========date:" + cur_date + "price:" + str(close_price) + "buy stock:" + str(ware_house)) logger.info("mm value:" + mm.print_moneymaker()) if 0 <= close_price - dn < 0.1: ware_house = mm.mm_buy_policy() buy_result = mm.buy_share(ware_house, close_price) if buy_result: logger.info("buy=========date:" + cur_date + "price:" + str(close_price) + "buy stock:" + str(ware_house)) logger.info("mm value:" + mm.print_moneymaker()) elif is_up_or_down == Utils.TREND_UP: """清仓时删除趋势判断""" if up - close_price <= 0.1 or close_price > up: sell_numbers = mm.mm_sell_policy() '''现根据当前价位刷新市值''' mm.refresh_market_price(close_price) mm.sell_share(sell_numbers, close_price) logger.info("sell=========date:" + cur_date + "price:" + str(close_price) + "sell stock:" + str(sell_numbers)) logger.info("mm value:" + mm.print_moneymaker()) if mi - close_price <= 0.1 or close_price > mi: sell_numbers = mm.mm_sell_policy() mm.refresh_market_price(close_price) mm.sell_share(sell_numbers, close_price) logger.info("sell=========date:" + cur_date + "price:" + str(close_price) + "sell stock:" + str(sell_numbers)) logger.info("mm value:" + mm.print_moneymaker()) mm.refresh_market_price(close_price) result.append(mm.share_value + mm.ware_house) cur_date = Utils.get_next_day(cur_date) logger.debug('end:::::::' + str(cur_date)) logger.debug("total result+" + str(result)) return result