def btest_period_on_pct(strategy_code, ts_code, freq='D'): try: if ts_code is None: listed_companies = StockNameCodeMap.objects.filter() else: ts_code_list = ts_code.split(',') if ts_code_list is not None and len(ts_code_list) >= 1: listed_companies = StockNameCodeMap.objects.filter( ts_code__in=ts_code_list) for listed_company in listed_companies: tasks = get_analysis_task(listed_company.ts_code, 'EXP_PCT_TEST', strategy_code, freq) if tasks is not None and len(tasks): print(listed_company.ts_code + ' for strategy ' + strategy_code + ' pct has started') for task in tasks: # if ready2btest(listed_company.ts_code, 'MARK_CP', strategy_code, task.start_date, task.end_date, freq): if days_between(task.start_date, task.end_date) >= 365 * 3: #分析至少需要三年的数据 test_exp_pct(strategy_code, listed_company.ts_code, task.start_date, task.end_date, freq) set_task_completed(listed_company.ts_code, 'EXP_PCT_TEST', freq, strategy_code, task.start_date, task.end_date) # generate_task(listed_company.ts_code, # test_freq, task.start_date, task.end_date, event_list=['TGT_PCT_QTN'], strategy_list=[strategy_code]) else: print(listed_company.ts_code + ' for strategy ' + strategy_code + ' pct has tested already / no task') except Exception as e: print(e)
def process_jiuzhuan_cp(ts_code, freq='D'): ''' 同步策略在交易中的使用情况 ''' btest_event_list = ['EXP_PCT_TEST', 'PERIOD_TEST'] strategy_list = ['jiuzhuan_count_b', 'jiuzhuan_count_s'] start_date = None end_date = None today = date.today() period = 4 try: if ts_code is None: listed_companies = StockNameCodeMap.objects.filter().order_by( '-ts_code') else: ts_code_list = ts_code.split(',') if ts_code_list is not None and len(ts_code_list) >= 1: listed_companies = StockNameCodeMap.objects.filter( ts_code__in=ts_code_list) for listed_company in listed_companies: # 需要优化 tasks = get_analysis_task(listed_company.ts_code, 'MARK_CP', 'jiuzhuan_bs', freq) if tasks is not None and len(tasks) > 0: atype = '1' # 标记更新的股票历史记录 # 如何差额取之前的历史记录?9 for task in tasks: if task.start_date == listed_company.list_date: print(listed_company.ts_code + '第一次处理,从上市日开始。。。') atype = '0' # 从上市日开始标记 start_date = task.start_date else: print(listed_company.ts_code + ' 更新处理,从上一次更新时间-4d - 开盘日 开始...') if days_between(task.start_date, listed_company.list_date) <= period: # fix issue - 计算period=4的有效交易日器 start_date = listed_company.list_date else: start_date = task.start_date - \ timedelta(days=get_trade_cal_diff( listed_company.ts_code, task.start_date, listed_company.asset)) mark_jiuzhuan(listed_company.ts_code, listed_company.asset, freq, start_date, task.end_date, atype) # print(task.start_date) # print(task.end_date) set_task_completed(listed_company.ts_code, 'MARK_CP', freq, 'jiuzhuan_bs', task.start_date, task.end_date) # generate_task(listed_company.ts_code, # freq, task.start_date, task.end_date, event_list=btest_event_list, strategy_list=strategy_list) else: print(listed_company.ts_code + ' no jiuzhuan mark cp task') except Exception as e: print(e)
def get_pct_days_between(df, b, b_date, pct_incr): try: closest_date = df[(df['close'] >= b['close'] * pct_incr) & (df['close'] <= b['close'] * (pct_incr + 0.05)) & (df['trade_date'] > b['trade_date'])].head( 1).trade_date.values # ??方法是否对? # pct_date = closest_date[0].strptime('%Y%m%d') pct_days = days_between(closest_date[0], b_date) except Exception as e: # logger.error(e) pct_days = -1 return pct_days
def updown_pct_quantiles_stat(strategy_code, ts_code, stock_name, freq='D'): # test_period_list = [10,20,30,50,80,130,210,340,550] 正确的序列应该为340, 550 test_period_list = [10, 20, 30, 50, 80, 130, 210, 340, 550] # strategy_codes = ['jiuzhuan_b', 'jiuzhuan_s', 'dibu_b', 'dingbu_s', 'w_di', 'm_ding', 'tupo_yali_b', # 'diepo_zhicheng_s', 'ma25_zhicheng_b', 'ma25_tupo_b', 'ma25_diepo_s', 'ma25_yali_s'] test_type = ['up_pct', 'down_pct'] try: tasks = get_analysis_task(ts_code, 'UPDN_PCT_QTN', strategy_code, freq) # if not is_analyzed(ts_code, 'TGT_PCT_QTN', strategy_code, freq): if tasks is not None and len(tasks): # if not is_analyzed(ts_code, 'UPDN_PCT_QTN', strategy_code, freq): task = tasks[0] if ready2btest(ts_code, 'PERIOD_TEST', strategy_code, task.start_date, task.end_date, freq): if days_between(task.start_date, task.end_date) >= 365 * 3: #分析至少需要三年的数据 print('updown pct quantile on start - ' + strategy_code + '/' + ts_code + ',' + datetime.now().strftime('%Y-%m-%d %H:%M:%S')) updown_qtiles_list = [] low_quantile_list = [] high_quantile_list = [] for test_period in test_period_list: results = StrategyTestLowHigh.objects.filter( strategy_code=strategy_code, ts_code=ts_code, test_period=test_period) if results is not None and len(results) > 0: df = pd.DataFrame( results.values('stage_high_pct', 'stage_low_pct')) high_qtiles = df.stage_high_pct.quantile( [0.1, 0.25, 0.5, 0.75, 0.9]) low_qtiles = df.stage_low_pct.quantile( [0.1, 0.25, 0.5, 0.75, 0.9]) for qtile in high_qtiles: high_quantile_list.append(round(qtile, 3)) high_quantile_list.append( round(df.stage_high_pct.max(), 3)) high_quantile_list.append( round(df.stage_high_pct.min(), 3)) high_quantile_list.append( round(df.stage_high_pct.mean(), 3)) for qtile in low_qtiles: low_quantile_list.append(round(qtile, 3)) low_quantile_list.append( round(df.stage_low_pct.max(), 3)) low_quantile_list.append( round(df.stage_low_pct.min(), 3)) low_quantile_list.append( round(df.stage_low_pct.mean(), 3)) strategy_up_qtiles = StrategyUpDownTestQuantiles( strategy_code=strategy_code, test_type=test_type[0], ts_code=ts_code, stock_name=stock_name, test_period=test_period, qt_10pct=high_quantile_list[0], qt_25pct=high_quantile_list[1], qt_50pct=high_quantile_list[2], qt_75pct=high_quantile_list[3], qt_90pct=high_quantile_list[4], max_val=high_quantile_list[5], min_val=high_quantile_list[6], mean_val=high_quantile_list[7]) strategy_down_qtiles = StrategyUpDownTestQuantiles( strategy_code=strategy_code, stock_name=stock_name, test_type=test_type[1], ts_code=ts_code, test_period=test_period, qt_10pct=low_quantile_list[0], qt_25pct=low_quantile_list[1], qt_50pct=low_quantile_list[2], qt_75pct=low_quantile_list[3], qt_90pct=low_quantile_list[4], max_val=low_quantile_list[5], min_val=low_quantile_list[6], mean_val=low_quantile_list[7]) updown_qtiles_list.append(strategy_up_qtiles) updown_qtiles_list.append(strategy_down_qtiles) high_quantile_list.clear() low_quantile_list.clear() if len(updown_qtiles_list) > 0: StrategyUpDownTestQuantiles.objects.bulk_create( updown_qtiles_list) # log_test_status(ts_code, # 'UPDN_PCT_QTN', freq, [strategy_code]) set_task_completed(ts_code, 'UPDN_PCT_QTN', freq, strategy_code, task.start_date, task.end_date) print('updown pct quantile on end - ' + strategy_code + '/' + ts_code + ',' + datetime.now().strftime('%Y-%m-%d %H:%M:%S')) else: print('no record for updown pct quantile on - ' + strategy_code + '/' + ts_code + ',' + datetime.now().strftime('%Y-%m-%d %H:%M:%S')) else: print('already exist updown pct quantile on end - ' + strategy_code + '/' + ts_code + ',' + datetime.now().strftime('%Y-%m-%d %H:%M:%S')) except Exception as err: logger.error(err)
def target_pct_quantiles_stat(strategy_code, ts_code, stock_name, freq='D'): # freq = 'D' target_pct_list = [ 'pct10_period', 'pct20_period', 'pct30_period', 'pct50_period', 'pct80_period', 'pct100_period', 'pct130_period' ] try: tasks = get_analysis_task(ts_code, 'TGT_PCT_QTN', strategy_code, freq) # if not is_analyzed(ts_code, 'TGT_PCT_QTN', strategy_code, freq): if tasks is not None and len(tasks): task = tasks[0] # if ready2btest(ts_code, 'EXP_PCT_TEST', strategy_code, task.start_date, task.end_date, freq): if days_between(task.start_date, task.end_date) >= 365 * 3: #分析至少需要三年的数据 print('target pct quantile on start - ' + strategy_code + '/' + ts_code + ',' + datetime.now().strftime('%Y-%m-%d %H:%M:%S')) quantile_list = [] target_pct_qtiles_list = [] # for task in tasks: # for target_pct in target_pct_list: results = BStrategyOnFixedPctTest.objects.filter( strategy_code=strategy_code, ts_code=ts_code, test_freq=freq).order_by( 'trade_date') # [:int(freq_count)] if results is not None and len(results) > 0: df = pd.DataFrame(results.values()) for target_pct in target_pct_list: # print(target_pct) df = df[df[target_pct] != -1] qtiles = df[target_pct].quantile( [0.1, 0.25, 0.5, 0.75, 0.9]) # for qtile in qtiles.values(): for idx, value in qtiles.items(): quantile_list.append(round(value, 3)) # print(value) quantile_list.append(round(df[target_pct].max(), 3)) quantile_list.append(round(df[target_pct].min(), 3)) quantile_list.append(round(df[target_pct].mean(), 3)) # print(quantile_list) target_pct_qtiles = StrategyTargetPctTestQuantiles( strategy_code=strategy_code, target_pct=target_pct, ts_code=ts_code, stock_name=stock_name, qt_10pct=quantile_list[0], qt_25pct=quantile_list[1], qt_50pct=quantile_list[2], qt_75pct=quantile_list[3], qt_90pct=quantile_list[4], max_val=quantile_list[5], min_val=quantile_list[6], mean_val=quantile_list[7]) target_pct_qtiles_list.append(target_pct_qtiles) quantile_list.clear() if len(target_pct_qtiles_list) > 0: StrategyTargetPctTestQuantiles.objects.bulk_create( target_pct_qtiles_list) set_task_completed(ts_code, 'TGT_PCT_QTN', freq, strategy_code, task.start_date, task.end_date) # log_test_status(ts_code, # 'TGT_PCT_QTN', freq, [strategy_code]) print('target pct quantile on end - ' + strategy_code + '/' + ts_code + ',' + datetime.now().strftime('%Y-%m-%d %H:%M:%S')) else: print('no record for target pct quantile on - ' + strategy_code + '/' + ts_code + ',' + datetime.now().strftime('%Y-%m-%d %H:%M:%S')) else: print('already exist target pct quantile on - ' + strategy_code + '/' + ts_code + ',' + datetime.now().strftime('%Y-%m-%d %H:%M:%S')) except Exception as err: print(err) logging.error(err)
def generate_task( listed_company, start_date, end_date, freq='D', ): threshold_hist_fx = 365 * 3 threshold_hist_25 = 25 threshold_hist_60 = 60 threshold_hist_200 = 200 analysis_start_date = listed_company.last_analyze_date if listed_company.last_analyze_date is not None else listed_company.list_date mark_start_date = listed_company.list_date analysis_event_list = [ 'EXP_PCT_TEST', 'PERIOD_TEST', 'TGT_PCT_QTN', 'UPDN_PCT_QTN', 'UPDN_PCT_RK', 'TGT_PCT_RK' ] analysis_strategy_list = [ 'ma25_zhicheng', 'ma25_diepo', 'ma25_yali', 'ma25_tupo', 'ma60_zhicheng', 'ma60_diepo', 'ma60_yali', 'ma60_tupo', 'jiuzhuan_count_b', 'jiuzhuan_count_s', 'dingbu_s', 'dibu_b', 'tupo_b', 'diepo_s', 'm_ding', 'w_di', 'ma200_zhicheng', 'ma200_diepo', 'ma200_yali', 'ma200_tupo' ] mark_cp_event_list = ['MARK_CP'] dl_daily_event_list = ['DAILY_DOWNLOAD'] mark_strategy_set = {} mark_strategy_dict = { '25': {'junxian25_bs', 'jiuzhuan_bs'}, '60': { 'junxian60_bs', 'dingdi', 'tupo_yali_b', 'diepo_zhicheng_s', 'wm_dingdi_bs', }, '200': {'junxian200_bs'} } # analysis_hist = StockHistoryDaily.objects.filter( # ts_code=listed_company.ts_code, trade_date__gte=analysis_start_date, trade_date__lte=end_date) # mark_hist = StockHistoryDaily.objects.filter( # ts_code=listed_company.ts_code, trade_date__gte=mark_start_date, trade_date__lte=end_date) # 如果hist length > 600,生成分析事件 # tasks = get_analysis_task(ts_code, event, ) for event in analysis_event_list: for strategy in analysis_strategy_list: try: task = StockStrategyTestLog.objects.get( ts_code=listed_company.ts_code, analysis_code=strategy, event_type=event, freq=freq, end_date=start_date - timedelta(days=1), is_done=False) task.end_date = end_date listed_company.last_analyze_date = end_date task.save() except Exception as e: # print(e) # 未找到运行记录 if days_between(mark_start_date, end_date) >= threshold_hist_fx: # mark_strategy_set = set.union( # mark_strategy_dict['200'], mark_strategy_dict['60'], mark_strategy_dict['25']) task = StockStrategyTestLog(ts_code=listed_company.ts_code, analysis_code=strategy, event_type=event, freq=freq, start_date=start_date, end_date=end_date) listed_company.last_analyze_date = end_date task.save() # listed_company.save() # else: # # 如果hist length < 600, >= 25, 生成MA25 标记事件 # # if len(hist) < 600 and len(hist) >= 25: # # pass # # 如果hist length < 600, >= 60, 生成MA60,突破,WM底,跌破,标记事件 # # if len(hist) < 600 and len(hist) >= 60: # # pass # # 如果hist length < 600, >= 200, 生成MA200 标记事件 # # if len(hist) < 600 and len(hist) >= 200: # # pass # if days_between(mark_start_date, end_date) >= threshold_hist_200: # mark_strategy_set = set.union( # mark_strategy_dict['200'], mark_strategy_dict['60'], mark_strategy_dict['25']) # else: # if days_between(mark_start_date, end_date) >= threshold_hist_60: # mark_strategy_set = set.union( # mark_strategy_dict['25'], mark_strategy_dict['60']) # else: # if days_between(mark_start_date, end_date) >= threshold_hist_25: # mark_strategy_set = mark_strategy_dict['25'] mark_strategy_set = set.union(mark_strategy_dict['200'], mark_strategy_dict['60'], mark_strategy_dict['25']) for event in mark_cp_event_list: for strategy in mark_strategy_set: try: # tasks = get_analysis_task(ts_code, event, ) task = StockStrategyTestLog.objects.get( ts_code=listed_company.ts_code, analysis_code=strategy, event_type=event, freq=freq, end_date=start_date - timedelta(days=1), is_done=False) task.end_date = end_date # mark_log.save() except Exception as e: # 未找到运行记录 # print(e) # 未找到运行记录 task = StockStrategyTestLog(ts_code=listed_company.ts_code, analysis_code=strategy, event_type=event, freq=freq, start_date=start_date, end_date=end_date) task.save() for event in dl_daily_event_list: try: # tasks = get_analysis_task(ts_code, event, ) task = StockStrategyTestLog.objects.get( ts_code=listed_company.ts_code, event_type=event, freq=freq, end_date=start_date - timedelta(days=1), is_done=False) task.end_date = end_date # mark_log.save() except Exception as e: # 未找到运行记录 # print(e) # 未找到运行记录 task = StockStrategyTestLog(ts_code=listed_company.ts_code, event_type=event, freq=freq, start_date=start_date, end_date=end_date) task.save()