The path to Python quantization: Get a moment in history stock code and time to market of Shanghai and Shenzhen listed companies

Source: Internet
Author: User

Recently began to play stock quantization, because want to do a complete stock backtesting, so the stock listing and delisting information is essential. Because we have to go back to the test must know a certain date of the composition of the Shanghai-Shenzhen stock, right. So we have to put the Shanghai and Shenzhen all the stock market time, the time to go back to the city to climb down (save to the local search will be faster).

0.1. Confirm the main tool

The tools to use include:

(1) Python: Basic tools

(2) Pandas: formatted data processing

(3) Communication Data interface: http://www.datayes.com

(4) Communication interface api:https://api.wmcloud.com/docs/pages/viewpage.action?pageid=1867781

1. Start getting data

First of all, we have to get the listing time of all listed companies and the time of delisting (if any) of the list, using the interface of the data to find our task is very simple.

 fromPandasImportDataFrame fromDataapiclientImportClientImportjsonclient=Client () client.init ('cae5c4acc4ad4ccb93a8aaac4b8adb04363feaa9852c34d14ddd2248613b09b3') URL='/api/equity/getequ.json?field=ticker,secshortname,listdate,delistdate&liststatuscd=l,s,de,un&secid =&ticker=&equtypecd=a'code, result=client.getdata (URL) J=json.loads (Result.decode ()) d= DataFrame (j['Data']) d= D.set_index ('Ticker') d= d[['Secshortname','listdate','delistdate']]d.to_csv ('Data/ticker_and _day_of_ (DE) list_date.csv')

As a result, the required content is saved in the Ticker_and _day_of_ (DE) list_date.csv file. It is important to note that there is a special case in the data: DY600019

This is because the reorganization of the merger and acquisition led to changes in the subject, so the communications data in the stock code before the addition of DY prefix to show the difference.

Then, in order to conveniently obtain a stock ticker code that can be traded at a certain point in history, we define a function that uses local data by default: Get_a_stocks (Date=none, update=false), date default is the current date of the system, Update indicates whether local data needs to be updated. File name beefinance.py

 fromPandasImportDataFrame fromDatetimeImportdatetime fromDataapiclientImportClientImportPandasImportJSONImportOSImportTypesImportdatetimeImport TimedefGet_a_stocks (Date=none, update=False):ifDate isnone:date=Datetime.datetime.now ()ifisinstance (DATE,STR): Date= Datetime.datetime.strptime (date,"%y-%m-%d")    if  notisinstance (date,datetime.datetime):RaiseValueError ('Date does not accept this type')        if  notisinstance (UPDATE, BOOL):RaiseValueError ('Update does not accept this type') Data_dir= u'Data'Data_filename= Data_dir + u'/ticker_and _day_of_ (DE) list_date.csv'        if  notos.path.exists (Data_dir): Os.mkdir (Data_dir)if( notOs.path.exists (Data_filename))orupdate:client=Client () client.init ('cae5c4acc4ad4ccb93a8aaac4b8adb04363feaa9852c34d14ddd2248613b09b3') URL='/api/equity/getequ.json?field=ticker,secshortname,listdate,delistdate&liststatuscd=l,s,de,un&secid =&ticker=&equtypecd=a'code, result=client.getdata (URL) J=json.loads (Result.decode ()) d= DataFrame (j['Data']) d= D.set_index ('Ticker') d= d[['Secshortname','listdate','delistdate']] D.to_csv (data_filename, encoding='Utf-8') d['listdate'] = Pandas.to_datetime (d['listdate']) d['delistdate'] = Pandas.to_datetime (d['delistdate']) d= d[d['listdate']<=Date] D1= D[pandas.isnull (d['delistdate'])] D2= D[pandas.notnull (d['delistdate'])] D2= d2[d2['delistdate']>Date] D=D1.append (D2)returnDElse: D= Pandas.read_csv (Data_filename, index_col='Ticker', parse_dates=['listdate','delistdate'],encoding='Utf-8') d['listdate'] = Pandas.to_datetime (d['listdate']) d['delistdate'] = Pandas.to_datetime (d['delistdate']) d= d[d['listdate']<=Date] D1= D[pandas.isnull (d['delistdate'])] D2= D[pandas.notnull (d['delistdate'])] D2= d2[d2['delistdate']>Date] D=D1.append (D2)returnD

The following test results:

 from Import  = get_a_stocks ('2010-05-05')print(d)
data/Ticker_and _day_of_ (DE) list_date.csv secshortname listdate Delistdateticker 000001 Ping An bank 1991-04-03NaT20,000 Division A 1991-01-29NaT000004 National Agricultural Science and Technology 1991-01-14NaT000005 Century star Source 1990-12-10NaT000006 Deep Vibration Industry a 1992-04-27NaT000007 New Good 1992-04-13NaT000008 Shenzhou High-speed railway 1992-05-07NaT000009 China Bao ' an 1991-06-25NaT000010 Beautiful Ecology 1995-10-27NaT000011 Deep Property A 1992-03-30NaT000012 South Glass a 1992-02-28NaT000014 Shahe shares 1992-06-02NaT000016 Deep Konka a 1992-03-27NaT000017 Deep China A 1992-03-31NaT000018 Shenzhou Great Wall 1992-06-16NaT000019 Deep Treasure a 1992-10-12NaT000020 Deep Huafa a 1992-04-28NaT000021 Deep Science and technology 1994-02-02NaT000022 Shenche a 1993-05-05NaT000023 Deep World A 1993-04-29NaT000025 Special Force a 1993-06-21NaT000026 Feida A, 1993-06-03NaT000027 Shenzhen Energy 1993-09-03NaT000028 Sinopharm unanimously 1993-08-09NaT000029 Deep Room A 1993-09-15NaT000030 Fu AO shares 1993-09-29NaT000031 Cofco Real Estate 1993-10-08NaT000032 Deep Mulberry a 1993-10-28NaT000033 *st Xindu 1994-01-03NaT000034 Shenzhou Digital 1994-05-09NaT ... ...        ...        ...601899 Zijin Mining 2008-04-25NaT601918 *st Xinji 2007-12-19NaT601919 China Ocean 2007-06-26NaT601939 CCB 2007-09-25NaT601958 Gold Molybdenum shares 2008-04-17NaT601988 Bank of China 2006-07-05NaT601989 China Heavy Industry 2009-12-16NaT601991 Datang Power Generation 2006-12-20NaT601998 China Citic Bank 2007-04-27NaT601999 Publishing Media 2007-12-21NaT000024 China Merchants Real Estate 1993-06-07 2015-12-30000522 Baiyun Mountain a 1993-11-08 2013-04-26000527 mei electrical appliances 1993-11-12 2013-09-1 8000562 Hongyuan Securities 1994-02-02 2015-01-26 the578 Salt Lake Group 1995-03-03 2011-03-22 the594 China 1996-03-20 2015-07-13000602 Golden Horse Group 1996-08-19 2013-08-14 the787 *st 1997-06-26 2013-02-08 the805 *st Yan 1998-05-29 2013-03-27600087 Retreat Mayor Oil 1997-06-12 2014-06-05600102 Laiwu Steel shares 1997-08-28 2012-02-2860 0253 days prescription industry 2000-12-27 2013-07-15600263 Road and bridge construction 2000-07-25 2012-03-01600553 Taihang Cement 2002-08-22 2011-02-186 63,100 Joint Stock 1993-02-19 2011-08-23600656 Boyuan 1990-12-19 2016-05-13600832 Oriental Pearl 1994-02-24 2015-05-20 600991 GAC Changfeng 2004-06-14 2012-03-20601268 *st 2010-02-02 2015-05-21601299 China North car 2009-12-29 2015-05-2 0[1815 rows x 3 columns]

Not bad.

The path to Python quantization: Get a moment in history stock code and time to market of Shanghai and Shenzhen listed companies

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.