量子化分析の(四)株価の急騰相場は持ち株ファンドを買わなければならない.


株価高騰相場は持ち株ファンドを買わなければならない.
時には相場が急騰した時、いくつかの株が買えないことがあります(毎日一字板で、とても食いしん坊を見ています)、どうしますか?曲線で基金を買うしかありませんが、どうやって買いますか?
技術交流微信【xicebloodx】は、交流のみ、嫌がらせはブラックアウト、微信は【技術交流】と明記してください.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @license : (C) Copyright 2017-2020.
# @Time    : 2020/6/14 15:20
# @File    : etf.py
# @Software: PyCharm
# @desc    :

import requests
from bs4 import BeautifulSoup
import re
import pandas as pd
import json

FUND_COLUMNS = ['SCode', 'SName', 'RDate', 'SHCode', 'SHName', 'IndtCode',
                'InstSName', 'TypeCode', 'Type', 'ShareHDNum', 'Vposition',
                'TabRate', 'TabProRate', 'BuyState']

REQUEST_HEADER = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36'}


def read_csv(path):
    """
    read data frame
    :param path: file path
    :return: data frame
    """
    with open(path, 'r') as f:
        p = re.search(r'\[.+\]', f.read().replace(r"\/", ""))
        if p:
            text = p.group(0)
            jstr = json.dumps(text)
            js = json.loads(jstr)
            df = pd.DataFrame(pd.read_json(js, dtype={'code': object}),
                              columns=FUND_COLUMNS)

            return df

def get_fund_list(symbol, date):
    """
    get fund list
    :param symbol: code
    :param date: date
    :return: data frame
    """

    stock_url = 'http://data.eastmoney.com/zlsj/detail.aspx?type=ajax&sr=-1&p=1&ps=1000&stat=0&code=%s&date=%s&rt=52763434'
    try:
        url = stock_url % (symbol, date)
        web_source = requests.get(url, headers=REQUEST_HEADER, timeout=5)

        path = f'./data/{symbol}.txt'
        open(path, 'wb').write(web_source.content)

        df = read_csv(path)
        df['SHCode'] = df['SHCode'].astype('str')
    except Exception as e:
        raise e

    return df

def get_stock_fundlist(symbol, date='2020-03-31'):
    """
                    
    :param symbol:     ,  : SZ000001
    :param date:     ,  :2019-12-31,2019-03-31
    :return:
    """
    fund_url = 'http://fund.eastmoney.com/f10/FundArchivesDatas.aspx?type=jjcc&code=%s&topline=15'

    df = get_fund_list(symbol, date)
    etf_list = [x for x in list(df.SHCode) if len(x)==6]

    results = {}
    for code in etf_list[:]:
        web_source = requests.get(fund_url % code, headers=REQUEST_HEADER, timeout=5)
        html_source = web_source.content.decode()
        html_source = html_source.split("\"")[1]
        soup = BeautifulSoup(html_source, 'lxml')
        items = soup.select(".tzxq")[0].select('tr')
        for item in items[1:]:
            if symbol[2:] not in item.text:
                continue
            fields = item.select('td')
            record = [field.text.strip() for field in fields if
                      field.text.strip() != '' and '  ' not in field.text]
            results[code] = record[3][:-1]
            break
    return sorted(results.items(), key=lambda kv: kv[1], reverse=True)

テストコード:
「三七互娯楽」を例にとると、上位10位を選ぶのは、もちろん新しい基礎建設や5 Gなどでもいいです.
if __name__ == '__main__':
    etf = get_stock_fundlist('002555', '2020-03-31')

    for code, ratio in etf[:10]:
        print(code, ratio)

データの取得:
512980 9.43
164818 9.04
160629 9.03
160522 8.91
160512 7.75
161036 7.51
163805 7.18
240009 6.85
161605 6.63
160519 6.56

この時、毎日基金や支付宝で基金を買うことができます.