程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to use Python to obtain fund market and analyze funds with excellent annual performance to unlock the secret of making money?

編輯:Python

Yesterday was 2022 The first day of the annual working day , It is also the first trading day of the domestic securities market , What are your investment goals and expectations for the new year ?

Before setting this year's investment goals and Strategies , Let's review and analyze 2021 The market situation and performance of the fund market in , Don't be a cut leek in the new year , Carry out investment activities scientifically and rationally , Unlock the secret of making money 、 The password of the fund !

Non fund Xiaobai can skip the first part directly .

1. Fund introduction

The word fund is familiar to everyone , What exactly is the fund ? How is it classified ? From which channels can you apply for ?

1.1. What is a fund

If you have a sum of money , Want to invest in money market instruments 、 bond 、 Stock these products to get income , But I'm not professional enough and I don't have enough time to study , There is not much money available . Then you can find a professional organization for help , This professional institution is the fund management company .

There are professionals in fund management companies , Help you operate your money 、 earn money . These professionals are fund managers . They made money , You can get a certain service fee and commission , The rest of the proceeds will be distributed to the customers who buy the fund in proportion .

Of course , We are here only for public funds .

1.2. Classification of funds

Generally speaking , From the broad category , The fund can be divided into public offering and private placement . Public funds are open to the public , A fund that anyone can buy . The investment threshold of general public funds is very low , Most funds can invest 1000 yuan or even hundreds of yuan .

And private equity funds , Xiaobai is usually not available . These private equity funds , First of all, the investment threshold is very high , Investors are generally required to have hundreds of thousands or even millions of assets . That is what we call qualified investors , Without so much money , You're not qualified , Not qualified for investment . You can simply understand that private equity funds are for the rich 、 A fund specially designed by high net worth individuals .

From the subdivision direction , According to different angles and different standards , Funds can be classified in different ways . for example , According to whether the fund unit can be increased or redeemed , It can be divided into open-end fund and closed-end fund ; According to the different organizational forms , It can be divided into company fund and contract fund ; According to the difference between investment risk and return , It can be divided into growth type 、 Income and balance funds . What we want to focus on is the following categories :

According to different investment directions and investment products , The more common funds in the market can be divided into : Money funds 、 Bond Fund 、 Equity funds 、 Hybrid funds and index funds .

1.2.1. Money funds

Monetary Fund is a fund that takes money market instruments as its main investment object . What is a money market tool ? simply , It is a financial product similar to money , For example, the cash itself is , In addition to cash , And short-term treasury bonds 、 buy-back 、 Time deposits within one year 、 Central bank bills within one year, etc . The characteristic of these financial instruments is that the term is very short , Liquidity is particularly good , It can be realized at any time , Very safe 、 reliable . Yu'e Bao is a typical money market fund .

In general , Money market funds are low risk 、 And low interest rates , Our change 、 Temporary funds, etc , Are particularly suitable for investing in money market funds , But it is not suitable for long-term investment , Because in the long run , Its yield is very limited .

1.2.2. Bond Fund

Bond funds are based on treasury bonds 、 Funds with fixed income financial instruments such as financial bonds as the main investment objects . When a fund , Its portfolio includes 80% All the above positions are invested in bonds , Then this fund belongs to the bond fund .

The risk and return of bond fund will be more stable than that of stock fund , Its characteristic is : Low risk 、 Low yield ; Steady returns ; Low cost .

1.2.3. Equity funds

Stock fund is a fund that takes equity financial instruments as its main investment object . When a fund , Its portfolio includes 80% The above positions are invested in securities , Then this fund belongs to the stock fund .

It is precisely because most of the objects invested in equity funds are stocks , Therefore, it also has the characteristics of high risk and high return . But buying a stock fund can effectively spread the investment risk , So compared with direct investment in stocks , Its risk will be reduced , And the return is compared with direct investment in the stock market , It's considerable . therefore , It is suitable for investors who want to invest in the stock market but lack professional investment knowledge .

1.2.4. Mixed fund

A hybrid fund is one that uses the money market at the same time 、 Fixed income 、 Funds with equity financial instruments as the main investment objects . According to the stock 、 Different bond investment proportion and investment strategy , Hybrid funds can be divided into partial debt funds 、 Biased funds 、 Allocation funds and other types .

The characteristics of hybrid funds are , By configuring many different types of assets , Realize the diversification of investment , So as to achieve the goal of balancing the benefits and risks .

1.2.5. Index Fund

Index fund is essentially a kind of passive stock fund . Passive type is a kind of investment idea , It is relative to the active type .

share index , It is used to reflect the overall level of various stock prices in the stock market , And the indicators of their changes . For example, Shanghai Stock Exchange 50 Index , From all the stocks on the Shanghai Stock Exchange , Select the 50 Only representative stocks , A price index compiled from the parity of price changes . For active investment , The goal of fund managers is to prove the above 50 Index as benchmark , Outperform this index over a period of time , So as to obtain more excess returns ; For passive investments , The goal of fund managers is to prove the above 50 Index as benchmark , Add them to the portfolio with similar weights , Gain a return that is basically consistent with the index .

1.2.6. FOF Type fund

In addition to the above fund categories , There is also a special fund , be called FOF, That is, the fund in the fund .FOF Type a fund is a fund that takes the fund as its main investment object . in other words , The manager of this fund , Screen various funds in the market , Choose some good funds , To invest .

FOF The fund is relatively less risky , For Xiaobai, who has just started , Facing thousands of funds in the market , The difficulty and risk of personal selection are not low , and FOF In fact, it is the fund manager who helps investors to buy at one time “ A basket of funds ”, Through experts' secondary fund screening , Effectively reduce risk .

1.2.7. Fund risk and return distribution

The above is the risk and return degree distribution chart of different types of funds , Investors should choose the most suitable fund type according to their own ability and investment purpose .

1.3. Subscription of the Fund

The act of buying a fund after its establishment is called subscription . Different types of funds , Subscription rates are also different . The subscription rate of the fund is from high to low : Stock type 、 mixed type 、 Exponential type 、 Bond type 、 Currency type . The specific values are as follows :

Stock type (2%~1.5%)≥ mixed type (2%~1.5%)> Exponential type (1%~1.5%)> Bond type (0.5%~1%)> Currency type ( about 0%)

The subscription fund has 4 Channels , Namely : Bank 、 securities company 、 Fund company 、 Third party fund consignment platform . Banks have the lowest cost performance , Not only are there fewer products , And the subscription rate is very high ; The products of securities companies and fund companies are relatively few ; The third-party fund sales platform not only has a large number of products , And the subscription rate is relatively low , Most of them are 1 fold .

2. Prepare before fund analysis

The fund data for this analysis comes from “ Omelet Fund ”, Its page is through Ajax To transmit data asynchronously , There is a special data interface , It needs to be analyzed .

in addition , This use pyecharts Visual output of data , It needs to be installed before use Python library , And set corresponding parameters .

2.1. URL And message analysis

2.1.1. URL analysis

Through the browser's developer tool , Can capture the fund list submitted by turning the page URL:

For its URL The parameters in :

https://danjuanapp.com/djapi/v3/filter/fund?type=1&order_by=1m&size=20&page=2

The parameter analysis is as follows :

  • type: Control fund type , It is the fund classification mentioned above
  • order_by: Control statistical time period
  • size: Control the number of records on a page
  • page: Control the number of pages

Fund type and time period parameter dictionary :

# Type of Fund
fund_type = {" Stock type ": 1, " mixed type ": 3, " Bond type ": 2, " Currency type ": 4, " Exponential type ": 5, "FOF type ": 6, "QDII type ": 11}
# Count the time
fund_time = {' Last week ': '1w', ' Last month ': '1m', ' Nearly three months ': '3m', ' Last June ': '6m', ' Nearly a year ': '1y', ' In the past two years ': '2y', ' Nearly three years ': '3y', ' Nearly five years ': '5y'}

2.1.2. Message analysis

Check the returned message content , And analyze :

The returned message result is json Format , So we need to introduce json The module processes the crawled data .

Net value of other transactions 、 Fund size 、 The analysis method of fund position and other information is similar .

2.2. Data visualization settings

This time, I used pyecharts Pie chart and histogram in data visualization tool , Configure related parameters :

# Draw the pie chart
def pie(name, value, savetitle, title):
c = (
# Initialize configuration items , Color can be set inside
Pie(
init_opts=opts.InitOpts(
# The background color
bg_color="#2c343c"
)
)
.add(
# Series name
series_name=" Fund up and down ",
# Series data items , The format is [(key1,value1),(key2,value2)]
data_pair=[list(z) for z in zip(name, value)],
# Size data by radius
rosetype="radius",
# The radius of the pie , Set to default percentage , Relative to half of the smaller item in the height and width of the container
radius="55%",
# The center of the pie chart , The first item is relative to the width of the container , The second item is the height relative to the container
center=["50%", "50%"],
# Tag configuration item
label_opts=opts.LabelOpts(is_show=False),
)
# Global settings
.set_global_opts(
# Set title
title_opts=opts.TitleOpts(
# name
title="" + str(title),
subtitle='hugowong draw , Official account : The way of several people ',
# The position of the assembly from the left side of the container
pos_left="center",
# The pixel value of the component above the container
pos_top="20",
# Set the title color
title_textstyle_opts=opts.TextStyleOpts(color="#ffffff"),
),
# Legend configuration item parameters , Whether to display the components in the figure
legend_opts=opts.LegendOpts(is_show=False),
)
# Series settings
.set_series_opts(
# Set prompt
tooltip_opts=opts.TooltipOpts(
trigger="item",
# Display format
formatter="{a} <br/>{b}: {c}% ( Occupy {d}%)"
),
# Set label color
label_opts=opts.LabelOpts(
color="rgba(255, 255, 255, 0.3)",
# Display format
formatter="{b}: {c}%"
),
)
.render(str(savetitle) + ".html")
)

Corresponding to different analysis results , Different parameter configurations are required , See the complete code for other configurations ( Official account 『 The way of several people 』, reply 2021 fund obtain ).

3. Fund performance analysis

Next, according to the analysis requirements , Crawl fund data 、 analysis 、 Handle 、 visualization 、 analysis .

3.1. 2021 Before annual yield 10 name

Before crawling the yield of each type of fund in the past year 10 The name of the data , Extract returned json Fund name and yield information in the data , Output the results visually in the form of pie chart .

【 Code 】

# Analysis of return rate of various funds in specified time period
def fund_analyze1():
for key in fund_type:
# Crawl through the data of various funds in the past year ( Before each 10 name )
res = requests.get(url=url, headers=headers)
res.encoding = "utf-8"
# Parse crawl data
fund = json.loads(res.text)
fund = fund["data"]["items"]
name = []
value = []
# Extract fund name and corresponding yield information
for i in range(0, len(fund)):
name.append(fund[i]["fd_name"])
value.append(fund[i]["yield"])
# Data visualization output ( The pie chart )
pie(name, value, str(key) + " fund 2021 Before annual income 10 name ", "[" + str(key) + "] fund 2021 Before annual income 10 name ")

【 result 】

【 analysis 】

The first place of stocks and index funds is far ahead , Mixing and QDII The top ten return rates of t-funds are relatively close ( The standard deviation is small ), The top ten bond funds performed very well .

3.2. Yield distribution analysis

Before crawling the yield of each type of fund in the past year 1000 The name of the data , Make statistics on the number of funds whose return rate is in different groups , Analyze the yield distribution of various types of funds , The statistical results are visually output in the form of histogram .

【 Code 】

# Analysis on the distribution of returns of various funds in a specified period of time
def fund_analyze2():
for key in fund_type:
# Crawl through the data of various funds in the past year ( various 1000 individual )
res = requests.get(url=url, headers=headers)
res.encoding = "utf-8"
# Parse crawl data
fund = json.loads(res.text)
fund = fund["data"]["items"]
name = []
value = []
# Extract fund name and corresponding yield information
for i in range(0, len(fund)):
name.append(fund[i]["fd_name"])
try:
value.append(fund[i]["yield"])
except:
value.append(0)
# Create data table , And the conversion yield type is float, Keep four decimal places
tb_fund = pd.DataFrame({"fund_name": name, "yield": value})
tb_fund["yield"] = round(tb_fund["yield"].astype(float), 4)
# The yield data of various funds are automatically divided into boxes , And count the number
count_yield = tb_fund["yield"].value_counts(bins=15).sort_index(ascending=True).to_frame()
count_yield.columns = ["count_num"]
name = list(count_yield.index.to_tuples())
value = count_yield["count_num"].to_list()
# Data visualization output
bars_non_slider2(name, value, str(key), str(key) + " Fund yield distribution ", "[" + str(key) + "] Distribution of fund return rate ")

【 result 】

【 analysis 】

Stock type 、 Exponential type 、QDII Most funds are in a loss state , And there are not a few who have lost much , At the same time, there are also a few very high returns , It can be seen that these funds are characterized by the coexistence of high risk and high return . The monetary fund is stable , But the profit is less . Bond type and FOF There are also a few abnormal performances in the stable performance of t-funds . Hybrid funds perform best , A fund that has not lost money , There is also a minimum return 11% above , And there is no lack of high-yield .

Among these types of funds, there are only hybrid funds 、 Bond type 、 Monetary funds do not suffer losses , Bonds and monetary funds are expected , And the hybrid fund also has such brilliant results, which is really a little unexpected .

3.3. Analysis of rise and fall in each stage

Crawl through the fund data that ranks first in the yield of all types of funds in recent one year , Obtain the rise and fall of its revenue in each time period , Analyze the performance of excellent funds at all stages , The results are visualized and output in the form of multi series histogram .

【 Code 】

# Analysis on the rise and fall of various top ranked funds in different time periods
def fund_analyze3():
name = [' Last week ', ' Last month ', ' Nearly three months ', ' Last June ', ' Nearly a year ', ' In the past two years ', ' Nearly three years ', ' Nearly five years ']
fund_value = {}
for key in fund_type:
# Crawl through the data of various funds in the past year
res = requests.get(url=url, headers=headers)
res.encoding = "utf-8"
fund = json.loads(res.text)
# Get the number one fund code
first_fd_code = fund["data"]["items"][0]["fd_code"]
# The rise and fall of the fund ranking first in each time period
res = requests.get(url=f_url, headers=headers)
res.encoding = "utf-8"
fund = json.loads(res.text)
fund = fund["data"]
f_values = []
# To avoid missing values , Plus exception handling ; And the conversion yield is float type , Take four decimal places
# Last week
try:
f_values.append('%.4f' % float(fund["nav_grl1w"]))
except:
f_values.append(0)
# Last month
try:
f_values.append('%.4f' % float(fund["nav_grl1m"]))
except:
f_values.append(0)
# Nearly three months
try:
f_values.append('%.4f' % float(fund["nav_grl3m"]))
except:
f_values.append(0)
# Last June
try:
f_values.append('%.4f' % float(fund["nav_grl6m"]))
except:
f_values.append(0)
# Nearly a year
try:
f_values.append('%.4f' % float(fund["nav_grl1y"]))
except:
f_values.append(0)
# In the past two years
try:
f_values.append('%.4f' % float(fund["nav_grl2y"]))
except:
f_values.append(0)
# Nearly three years
try:
f_values.append('%.4f' % float(fund["nav_grl3y"]))
except:
f_values.append(0)
# Nearly five years
try:
f_values.append('%.4f' % float(fund["nav_grl5y"]))
except:
f_values.append(0)
# Add... To the dictionary
fund_value[key] = f_values
# Data visualization output ( Histogram )
bars(name, fund_value, " The first funds of all kinds rose or fell in different periods ", " The first fund of all types rose or fell in the first stage ")

【 result 】

【 analysis 】

The performance of the top funds of all types in the last three months has been mediocre , There are even signs of falling back . Stocks 、 First place in index and hybrid funds , In recent years, the income has increased considerably .

3.4. near 60 The net value of a trading day

Crawl through the fund data that ranks first in the yield of all types of funds in recent one year , Get its near 60 Historical net worth of trading days ( Unit value ) data , Analyze the historical volatility of excellent funds , The results are visualized and output in the form of histogram of region stretching and scaling .

【 Code 】

# The number one funds of all kinds are nearly 60 Net worth analysis of trading days
def fund_analyze4():
for key in fund_type:
# Crawl through the data of various funds in the past year
res = requests.get(url=url, headers=headers)
res.encoding = "utf-8"
fund = json.loads(res.text)
# Get the number one fund code
first_fd_code = fund["data"]["items"][0]["fd_code"]
# Get the number one funds of all kinds 60 Net worth of trading days
res = requests.get(url=f_url, headers=headers)
res.encoding = "utf-8"
fund = json.loads(res.text)
fund = fund["data"]["items"]
name = []
value = []
for j in range(0, len(fund)):
name.append(fund[j]["date"])
value.append(fund[j]["nav"])
# Data visualization output ( Bar graph with stretching )
bars_slider(name, value, str(key), str(key) + " Fund near 60 Net value of trading days ", "[" + str(key) + "] Fund near 60 Net value of trading days ")

【 result 】

【 analysis 】

The first place of stock fund is nearly 60 A steady rise in trading days ; The first place of hybrid fund is nearly 60 A steady rise in trading days 、 Recent decline 、 There are occasional twists and turns ; The number one bond fund is close to 60 There is a peak in the last trading day , But the recent decline is obvious ; The first place of index fund is nearly 60 It's hard to deal with the problem after three trading days ;QDII The first place of the fund is nearly 60 Not enough stamina to climb in the last trading day .

3.5. Analysis of heavy position stocks

Climb to the top of the yield of equity and hybrid funds in recent one year 50 Fund data , Obtain the names of the top ten positions of each fund and the corresponding shares for summary statistics , Select the respective top positions of equity funds and hybrid funds 20 Positions in stocks , Analysis of the fund's favorite stocks , The results are visualized and output in the form of histogram .

【 Code 】

# Analysis of heavy positions of stock and hybrid funds
def fund_analyze5():
for key in fund_type:
# Crawling through the data of stock and hybrid funds in recent one year ( Before each 50 name )
if str(fund_type[key]) == '2':
break
res = requests.get(url=url, headers=headers)
res.encoding = "utf-8"
fund = json.loads(res.text)
fund = fund["data"]["items"]
fd_code = []
stock_name = []
percent = []
# Withdrawal fund code
for i in range(0, len(fund)):
fd_code.append(fund[i]["fd_code"])
# Obtain the name and share of the corresponding position shares of the fund
for j in range(0, len(fd_code)):
res = requests.get(url=f_url, headers=headers)
res.encoding = "utf-8"
stock = json.loads(res.text)
stock = stock["data"]["stock_list"]
for k in range(0, len(stock)):
stock_name.append(stock[k]["name"])
percent.append(stock[k]["percent"])
# Set the interval between each fund request
time.sleep(0.5)
# Create data table
tb_stocks = pd.DataFrame({"stock_name": stock_name, "hold_percent": percent})
# Aggregate position shares
sum_stocks = tb_stocks.groupby("stock_name", as_index=False).sum()
# Rank by share from high to low , And keep two decimal places after converting the share into format
sum_stocks = sum_stocks.sort_values("hold_percent", ascending=False)
sum_stocks["hold_percent"] = round(sum_stocks["hold_percent"].astype(float), 2)
# Before selection 20 Big positions
name = sum_stocks["stock_name"].head(20).to_list()
value = sum_stocks["hold_percent"].head(20).to_list()
# Data visualization output
bars_non_slider(name, value, str(key), str(key) + " Before the Fund 20 Large position stock ", "[" + str(key) + "] Before the Fund 20 Large position stock ")

【 result 】

【 analysis 】

In equity funds , The most popular star stock is “ Ningde era ”, The other top five are Tianqi lithium 、 Ganfeng lithium industry 、 Shanshan Co., Ltd 、 Huayou cobalt Co., Ltd . Four of the top five heavy positions of equity funds are enterprises in the new energy materials industry .

In hybrid funds , The most popular star stock is also “ Ningde era ”, The other top five are racing tyres 、 Trina Solar 、 Sun power 、 Tianqi lithium Co., Ltd . Four of the top five heavy positions of hybrid funds are also enterprises in the new energy materials industry , And there are two positions that coincide with stock funds .

It seems Investors and fund managers are all optimistic about the new energy materials industry .

4. Conclusion

Given the limited time and energy , This time, only partial perspective analysis has been made on crawling fund data , You can analyze it from different perspectives , Mining valuable information . for example , Asset scale 、 The comparison between the new fund and the old fund 、 Analysis of star fund managers and their managed funds, etc .

One last reminder : Investment is risky , Financial management needs to be cautious ! This paper does not constitute any investment proposal !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved