当前位置: 移动技术网 > IT编程>脚本编程>Python > 量化分析之(四)股票暴涨行情不错过之买持股基金

量化分析之(四)股票暴涨行情不错过之买持股基金

2020年07月26日  | 移动技术网IT编程  | 我要评论

股票暴涨行情不错过之买持股基金。

有时在行情暴涨时,出现某些股票买进不了(天天一字板,看着非常的眼馋),怎么办呢?只能曲线买基金,那怎么买呢?

技术交流微信【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名,当然还可以以新基建或5G等等,都是可以的:

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

这时就可以上天天基金或者支付宝上买基金了。

本文地址:https://blog.csdn.net/xsophiax/article/details/107574020

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网