Use Python to analyze funds! Make money to win in the starting line!

Time:2020-5-23

You don’t manage money, money doesn’t care about you! Can Python help you with your finances?

Use Python to analyze funds! Make money to win in the starting line!

Effect preview

Trend chart of cumulative yield

Use Python to analyze funds! Make money to win in the starting line!

Basic information results

Use Python to analyze funds! Make money to win in the starting line!

How to use:

Python 3 + some third-party libraries

import requests
import pandas
import numpy
import matplotlib
import lxml

to configureconfig.jsoncodeAllocation fund code,useCacheWhether to use cache.

{
  "code":[
    "002736",
    "003328",
    "003547",
  ],
  "useCache":true
}

Run fund_ analysis.py

Implementation principle

Data acquisition:

Open a fund from the daily fund website, and observe the loaded files in the chrome developer tool. We found a JS file, which contains some basic information of the fund. This is a JS file.

Use Python to analyze funds! Make money to win in the starting line!

To obtain the cumulative yield information, you need to do some operations on the page. Click 3 years in the cumulative yield to observe the request of the developer tool, and it is easy to find out how the data source is obtained. This is a JSON data.

Use Python to analyze funds! Make money to win in the starting line!

The fund rate table is on another page. We can find the information source address several times. This is HTML data.

Use Python to analyze funds! Make money to win in the starting line!

Then through the analysis of the hearders, we use the request simulation browser to get the data (if it is not clear here, please refer to the previous article). Finally, it is saved locally as a buffer. Take the cumulative yield information JSON as an example. The main code is as follows.

filePath = f'./cache/{fundCode}.json'
requests_url='http://api.fund.eastmoney.com/pinzhong/LJSYLZS'
headers = {
  'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36',
  'Accept': 'application/json' ,
  'Referer': f'http://fund.eastmoney.com/{fundCode}.html',
}
params={
    'fundCode': f'{fundCode}',
    'indexcode': '000300',
    'type': 'try',
}
requests_page=requests.get(requests_url,headers=headers,params=params)
with open(filePath, 'w') as f:
  json.dump(requests_page.json(), f)

Data analysis:

For the JS file of basic information, read the file as a string, and get the required data through regular expression.

Use Python to analyze funds! Make money to win in the starting line!

For example, to obtain one-year yield, you can use the following code.

syl_1n=re.search(r'syl_1n\s?=\s?"([^\s]*)"',data).group(1);

For the cumulative yield JSON data, directly use JSON to analyze and find the data needed for filtering and processing.

Use Python to analyze funds! Make money to win in the starting line!

Adoptedall_ Data [fund code] [time] = cumulative yieldAnd then fill up the empty data through the dataframe of pandas.

df = DataFrame(all_data).sort_index().fillna(method='ffill')

For the HTML data of fund rate table, XPath is used. The XPath path can be obtained directly from chrome.

Use Python to analyze funds! Make money to win in the starting line!

For management rates, refer to the following codes.

selector = lxml.html.fromstring(data);
#Management rate
mg_rate=selector.xpath('/html/body/div[1]/div[8]/div[3]/div[2]/div[3]/div/div[4]/div/table/tbody/tr/td[2]/text()')[0]

Data storage:

Plot in dataframe can be used to draw pictures quickly, and to_ Excel is saved in Excel table. You can refer to the following code.

#Save data 
fig,axes = plt.subplots(2, 1)
#Process basic information
df2 = DataFrame(all_data_base)
df2.stack().unstack(0).to_excel(f'result_{time.time()}.xlsx',sheet_name='out')
df2.iloc[1:5,:].plot.barh(ax=axes[0],grid=True,fontsize=25)
#Disposal proceeds
df=DataFrame(all_data).sort_index().fillna(method='ffill')
df.plot(ax=axes[1],grid=True,fontsize=25)
fig.savefig(f'result_{time.time()}.png')

Summary

Data acquisition mainly uses the basic method of crawler, using the requests library. Regular expression, XPath parsing library and pandas data processing library are mainly used for data analysis and preservation.

The analysis of a fund is far more than these data (such as position distribution, fund manager information, etc.), here is just a guide, hoping to give you a thought, if you have an idea or do not understand the place, welcome to leave a message or private communication!


This article is only for personal learning and communication. Please do not use it for other purposes!


reference material

Recommended Today

Configure Apache to support PHP in the Apache main configuration file httpd.conf Include custom profile in

In Apache’s main configuration file / conf/ http.conf Add at the bottom Include “D:workspace_phpapache-php.conf” The file path can be any In D: workspace_ Create under PHP file apache- php.conf file Its specific content is [html] view plain copy PHP-Module setup LoadFile “D:/xampp/php/php5ts.dll” LoadModule php5_module “D:/xampp/php/php5apache2_2.dll” <FilesMatch “.php$”> SetHandler application/x-httpd-php </FilesMatch> <FilesMatch “.phps$”> SetHandler application/x-httpd-php-source </FilesMatch> […]