Baostock: Use Python's Baostock interface for trading day queries

Source: Internet
Author: User

In the quantitative analysis, sometimes use the stock trading day information, which can be obtained from the official website of SSE or SZSE, download the file from the official website is not a very good method, if you can download through the Python interface, you can put into the program, and Baostock Interface (official website:/http www.baostock.com) just provides this function (the following code is from the official website, Invasion and deletion).

According to the official website, the trading day query interface:query_trade_dates ().

Method Description: Query the stock trading day information information, can be set by the parameters to obtain the beginning and ending year data, provide 2014-2018 years of data. return type: The Dataframe type of pandas. (The author note: 2014 is recently added, originally only 2015-2018, the interface is someone in maintenance, you can see the version update from the homepage)

The sample code is as follows:

import baostock as bs  import pandas as pd    ####   Landing System  ####  lg = bs.login (user_id= "Anonymous",  password= "123456")    #  Display Login return information   print (' Login respond error_code: ' +lg.error_code)   print (' Login respond  error_msg: ' +lg.error_msg '     ####  get trading information  ####   rs = bs.query_trade_dates (start_date= "2017-01-01",  end_date= "2017-06-30")    print (' Query_trade_dates respond error_code: ' +rs.error_code)   print (' Query_trade_ Dates respond  error_msg: ' +rs.error_msg '     ####  print result set  ####   data_list = []  while  (rs.error_code ==  ' 0 ')  & rs.next ( ):      #  gets a record that merges the records together       data_ List.append (Rs.get_row_data ()) &nbsP; RESULT = PD. DataFrame (data_list, columns=rs.fields)     ####  result set Output to CSV file  ####      result.to_csv ("D:\\trade_datas.csv",  encoding= "GBK",  index=false)    print (Result)     ####  logout system  ####  bs.logout ()



Parameter meaning:

    • Start_date: Start date, empty when default is 2015-01-01.

    • End_date: End date, NULL when default is considered the current date.


The printing results are as follows:



Return Data Description
parameter name parameter description
calendar_date date
Is_trading_day Whether the trading day (0: Non-trading day; 1: Trading day)


As you can see, 1 is the trading day; 0 non-trading days.

Return type is dataframe type, very convenient to handle, like one, hope to help everyone.


Baostock: Use Python's Baostock interface for trading day queries

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.