Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

回测时“买方机构次数”有问题,并附上修改代码 #52

Open
myhhub opened this issue Mar 16, 2023 · 0 comments
Open

回测时“买方机构次数”有问题,并附上修改代码 #52

myhhub opened this issue Mar 16, 2023 · 0 comments

Comments

@myhhub
Copy link

myhhub commented Mar 16, 2023

有问题可以参考:https://github.com/myhhub/InStock
抓取股票每日关键数据,计算股票各种指标,识别K线各种形态,内置多种选股策略,支持选股验证回测,支持自动交易,支持批量时间,运行高效

问题1:
“近三月”是死时间,为当前的“近三月”,不支持历史选股回测
#
  df = ak.stock_lhb_stock_statistic_em(symbol="近三月")
  mask = (df['买方机构次数'] > 1)  # 机构买入次数大于1
  df = df.loc[mask]
  top_list = df['代码'].tolist()

上面代码建议修改为:

--------
# 股票近三月上龙虎榜且必须有2次以上机构参与的
def fetch_stock_top_entity_data(date):
    if date is None:
        date_date = datetime.datetime.now().date()
    else:
        tmp_year, tmp_month, tmp_day = date.split("-")
        date_date = datetime.datetime(int(tmp_year), int(tmp_month), int(tmp_day)).date()
        
    run_date = date_date + datetime.timedelta(days=-90)
    start_date = run_date.strftime("%Y%m%d")
    end_date = date_date.strftime("%Y%m%d")
    
    code_name = '代码'
    entity_amount_name = '买方机构数'
    
    try:
        data = ak.stock_lhb_jgmmtj_em(start_date, end_date)
        if data is None or len(data.index) == 0:
            return None

        # 机构买入次数大于1计算方法,首先:每次要有买方机构数(>0),然后:这段时间买方机构数求和大于1
        mask = (data[entity_amount_name] > 0)  # 首先:每次要有买方机构数(>0)
        data = data.loc[mask]
    
        if len(data.index) == 0:
            return None
    
        grouped = data.groupby(by=data[code_name])
        data_series = grouped[entity_amount_name].sum()
        data_code = set(data_series[data_series > 1].index.values)  # 然后:这段时间买方机构数求和大于1
    
        if not data_code:
            return None
    
        return data_code
    except Exception as e:
        logging.debug("{}处理异常:{}".format('stockfetch.fetch_stock_top_entity_data', e))

问题2:
top_list = df['代码'].tolist(),top_list不宜为全局变量,否则不支持按时间段选股,例如:(2023-01-01至2023-03-01)

上面代码建议修改为:

# 高而窄的旗形
def check_high_tight(code_name, data, end_date=None, threshold=60, istop=False):
    # 龙虎榜上必须有机构
    if not istop:
        return False
    .............


def check_enter(end_date=None, strategy_fun=enter.check_volume):
    is_check_high_tight = False
    if strategy_fun.__name__ == 'check_high_tight':
        stock_tops = fetch_stock_top_entity_data(end_date)
        if stock_tops is not None:
            is_check_high_tight = True

def end_date_filter(stock_data):
    if end_date is not None:
        if end_date < stock_data[1].iloc[0].日期:  # 该股票在end_date时还未上市
            logging.debug("{}在{}时还未上市".format(stock_data[0], end_date))
            return False
    if is_check_high_tight:
        return strategy_fun(stock_data[0], stock_data[1], end_date=end_date, istop=(data[0][0] in stock_tops))
    else:
        return strategy_fun(stock_data[0], stock_data[1], end_date=end_date)
    .............
@myhhub myhhub changed the title 历史数据“买方机构次数”有问题,并附上修改代码 回测时“买方机构次数”有问题,并附上修改代码 Mar 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant