Python crawls all kinds of fund data and displays the rise and fall of funds in the form of "motion graph visualization"

Preface

The text and pictures in this article are from the Internet and are for learning and communication purposes only, and do not have any commercial use. If you have any questions, please contact us for processing.

PS: If you need Python learning materials, you can click on the link below to get it by yourself

Python free learning materials, codes and exchange answers click to join


Hello, everyone, the fund has made people stupid recently

 

 

But I took a look at the managers of the funds I invested in. They are all excellent, so my heart is stabilized again. Today I will share with you an article about crawling fund data for data visualization. I hope it will help everyone learn technology and Raise awareness.
Be cautious when entering the market, and financial management is risky.

1. Fund data visualization

I contacted the fund last year and realized the fragrance of the fund (true fragrance). Today, I crawled the data of the "Eggroll Fund" and displayed the fund's rise and fall through the pyecharts dynamic chart visualization.
This article will crawl data around these three points, and display the data visually by moving pictures:

  • Top 10 increase and decrease in the past month
  • The fund's various stages of rise and fall
  • Net worth in the past 30 trading days

2. Data acquisition

Data Sources

Data source for this article: "Eggroll Fund"


https://danjuanapp.com/

data analysis

The next crawled data involves five categories (five types of funds)
1. Equity funds
2. Hybrid funds
3. Bond funds
4. Index funds
5. QDII funds


Analyze the law of ajax asynchronous interactive links through packet capture :

 

  • type is the code of the corresponding five funds
  • order_by is the order corresponding to how long the fund has risen and fallen
'近一周':'1w'
'近一月':'1m'
'近三月':'3m'
'近六月':'6m'
'近1年':'1y'
'近2年':'2y'
'近3年':'3y'
'近5年':'5y'
  • page is the corresponding number of pages, starting from page 1

     

     

    Remarks: "Eggroll Fund" this website is not anti-climbing! ! ! , The request does not require cookies! ! !
    Ok, after all these are clear, you can start crawling data!

3. Data visualization

Because the "Eggroll Fund" website has no anti-climbing! ! ! , So data crawling and visual analysis are put together (visualize directly after crawling the data!)

Analysis 1: The top 10 rises and falls in the past month

Crawler code


###基金类型
dict_type={"股票型":1,"混合型":3,"债券型":2,"指数型":5,"QDII型":11}
###时间
dict_time={'近一周':'1w','近一月':'1m','近三月':'3m','近六月':'6m','近1年':'1y','近2年':'2y','近3年':'3y','近5年':'5y'}

for key in dict_type:
    url = "https://danjuanapp.com/djapi/v3/filter/fund?type="+str(dict_type[key])+"&order_by=1w&size=10&page=1"
    res = requests.get(url, headers=headers)
    res.encoding = 'utf-8'
    s = json.loads(res.text)
    s = s['data']['items']
    name = []
    value = []
    for i in range(0,len(s)):
        print(s[i]['fd_name']+":"+s[i]['yield'])
        name.append(s[i]['fd_name'])
        value.append(s[i]['yield'])
    ###开始绘图
    pie(name, value, str(key)+"基金涨跌幅", "["+str(key)+"]基金近一月涨跌幅前10名")

Pie chart visualization code


###饼状图
def pie(name,value,picname,tips):
    c = (
        Pie()
            .add(
            "",
            [list(z) for z in zip(name, value)],
            # 饼图的中心(圆心)坐标,数组的第一项是横坐标,第二项是纵坐标
            # 默认设置成百分比,设置成百分比时第一项是相对于容器宽度,第二项是相对于容器高度
            center=["35%", "50%"],
        )
            .set_colors(["blue", "green", "yellow", "red", "pink", "orange", "purple"])  # 设置颜色
            .set_global_opts(
            title_opts=opts.TitleOpts(title=""+str(tips)),
            legend_opts=opts.LegendOpts(type_="scroll", pos_left="70%", orient="vertical"),  # 调整图例位置
        )
            .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
            .render(str(picname)+".html")
    )

Here the pie chart visualization code is encapsulated into a function, and you only need to call this function to draw the pie chart of five types of funds.


###开始绘图
pie(name, value, str(key)+"基金涨跌幅", "["+str(key)+"]基金近一月涨跌幅前10名")

1. Stock funds

2. Hybrid funds

3. Bond funds

4. Index funds

5.QDII funds

analysis

The above figure shows the top 10 funds with the highest rise and fall in the past month from five major types of funds for mapping.

In the same way, you can also use this code to draw in the past week, three months, or one year. You only need to modify the parameter order_by.


'近一周':'1w'
'近一月':'1m'
'近三月':'3m'
'近六月':'6m'
'近1年':'1y'
'近2年':'2y'
'近3年':'3y'
'近5年':'5y'

Analysis 2: The rise and fall of the fund at various stages

From the above analysis, it is clear that the five types of funds have the highest rise and fall rankings in the past month. Below, select the first fund from the rankings (the first in each of the five categories) to show the rise and fall of the fund in each stage.

Stage situation:


'近一周':'1w'
'近一月':'1m'
'近三月':'3m'
'近六月':'6m'
'近1年':'1y'
'近2年':'2y'
'近3年':'3y'
'近5年':'5y'

Crawler code


####分析2:基金各个阶段涨跌幅
def analysis2():
    name =['近1周','近1月','近3月','近6月','近1年','近3年','近5年']
    ##五类基金
    dict_value={}

    for key in dict_type:
        #### 获取排名第一名基金代号
        url = "https://danjuanapp.com/djapi/v3/filter/fund?type="+str(dict_type[key])+"&order_by=1w&size=10&page=1"
        res = requests.get(url, headers=headers)
        res.encoding = 'utf-8'
        s = json.loads(res.text)
        ###取第一名
        fd_code = s['data']['items'][0]['fd_code']

        #### 获取排名第一名基金各个阶段情况
        fu_url = "https://danjuanapp.com/djapi/fund/derived/"+str(fd_code)
        res = requests.get(fu_url, headers=headers)
        res.encoding = 'utf-8'
        s = json.loads(res.text)
        data = s['data']

        valuess=[]

        ####防止基金最长时间不够1年、2年、5年的情况报错,用0填充
        ##近1周
        try:
            valuess.append(data['nav_grl1w'])
        except:
            valuess.append(0)
        ##近1月
        try:
            valuess.append(data['nav_grl1m'])
        except:
            valuess.append(0)
        ##近3月
        try:
            valuess.append(data['nav_grl3m'])
        except:
            valuess.append(0)
        ##近6月
        try:
            valuess.append(data['nav_grl6m'])
        except:
            valuess.append(0)
        ##近1年
        try:
            valuess.append(data['nav_grl1y'])
        except:
            valuess.append(0)
        ##近3年
        try:
            valuess.append(data['nav_grl3y'])
        except:
            valuess.append(0)
        ##近5年
        try:
            valuess.append(data['nav_grl5y'])
        except:
            valuess.append(0)
        ###添加到集合中
        dict_value[key]=valuess
    bars(name,dict_value)

Visualization code


###柱形图
def bars(name,dict_values):

    # 链式调用
    c = (
        Bar(
            init_opts=opts.InitOpts(  # 初始配置项
                theme=ThemeType.MACARONS,
                animation_opts=opts.AnimationOpts(
                    animation_delay=1000, animation_easing="cubicOut"  # 初始动画延迟和缓动效果
                ))
        )
            .add_xaxis(xaxis_data=name)  # x轴
            .add_yaxis(series_name="股票型", yaxis_data=dict_values['股票型'])  # y轴
            .add_yaxis(series_name="混合型", yaxis_data=dict_values['混合型'])  # y轴
            .add_yaxis(series_name="债券型", yaxis_data=dict_values['债券型'])  # y轴
            .add_yaxis(series_name="指数型", yaxis_data=dict_values['指数型'])  # y轴
            .add_yaxis(series_name="QDII型", yaxis_data=dict_values['QDII型'])  # y轴
            .set_global_opts(
            title_opts=opts.TitleOpts(title='涨跌幅', subtitle='李运辰绘制',  # 标题配置和调整位置
                                      title_textstyle_opts=opts.TextStyleOpts(
                                          font_family='SimHei', font_size=25, font_weight='bold', color='red',
                                      ), pos_left="90%", pos_top="10",
                                      ),
            xaxis_opts=opts.AxisOpts(name='阶段', axislabel_opts=opts.LabelOpts(rotate=45)),
            # 设置x名称和Label rotate解决标签名字过长使用
            yaxis_opts=opts.AxisOpts(name='涨跌点'),

        )
            .render("基金各个阶段涨跌幅.html")
    )

analysis

From the above animation, you can clearly understand the rise and fall of the first fund of these five types of funds in each stage.

Some funds have not reached 3 or 5 years for the longest time, so fill 0 is used here.

Analysis 3: The net value of the past 30 trading days

Similarly, in the above analysis, it is clear that these five types of funds have the highest rise and fall rankings in the past month. Below, select the first fund from the rankings (the first in each of the five categories) to show the nearly 30 transactions of the fund. Daily net worth situation.

Crawler code


####分析3:近30个交易日净值情况
def analysis3():
    for key in dict_type:
        #### 获取排名第一名基金代号
        url = "https://danjuanapp.com/djapi/v3/filter/fund?type=" + str(
            dict_type[key]) + "&order_by=1w&size=10&page=1"
        res = requests.get(url, headers=headers)
        res.encoding = 'utf-8'
        s = json.loads(res.text)
        ###取第一名
        fd_code = s['data']['items'][0]['fd_code']

        #### 获取排名第一名基金近30个交易日净值情况
        fu_url = "https://danjuanapp.com/djapi/fund/nav/history/"+str(fd_code)+"?size=30&page=1"
        res = requests.get(fu_url, headers=headers)
        res.encoding = 'utf-8'
        s = json.loads(res.text)
        data = s['data']['items']
        name=[]
        value=[]
        for k in range(0,len(data)):
            name.append(data[k]['date'])
            value.append(data[k]['nav'])

        silder(name, value,key)

Visualization code


###拉伸图
def silder(name,value,tips):
    c = (
        Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK))
            .add_xaxis(xaxis_data=name)
            .add_yaxis(tips, yaxis_data=value)
            .set_global_opts(
            title_opts=opts.TitleOpts(title=str(tips)+"近30个交易日净值情况"),
            datazoom_opts=[opts.DataZoomOpts(), opts.DataZoomOpts(type_="inside")],
        )
            .render(str(tips)+"近30个交易日净值情况.html")
    )

1. Stock type

2. Hybrid

3. Bond type

4. Exponential

5.QDII type

analysis

From the above animation, it is clear that the net value of the first fund of these five types of funds in the past 30 trading days.

4. Summary

The above is to crawl fund data and visualize the fund's rise and fall through pyecharts animation.

Crawl data around these three points, and display the data visually by moving pictures:

  • Top 10 increase and decrease in the past month
  • The fund's various stages of rise and fall
  • Net worth in the past 30 trading days

Guess you like

Origin blog.csdn.net/pythonxuexi123/article/details/114833379