Bitcoin history data-use Python to get data from the trading platform __python

Source: Internet
Author: User

In accordance with the relevant policy provisions, domestic Bitcoin trading will be closed by the end of September 2017, but in recent years the historical data on Bitcoin trading may have significant value for future research on economics, finance and quantitative trading strategies, so this article is mainly about how to use Python to deliver from the trading platform API to get the data and save it as a CSV file for later use.

The main contents of this article are as follows: Bitcoin, the history market of the Wright coins use Python to get okcoin.cn bit history data using Python to get huobi.com bit-currency history data Bitcoin, Wright currency historical quotes Data

Bitcoin and the Wright currency and several other digital currency of the historical market data has been obtained and saved in CSV format, need to use the data can be downloaded directly from the GITHUB, data acquisition of the source code has also been provided on the GitHub, to understand the source of data can read the source. Note: The following data from the network, access to the source code has been made public, do not guarantee the accuracy of the data, please use according to the actual situation.

number marked by Exchange Data Download Address
1 Btc_cny Okcoin Daily_price_btc_cny.csv
2 Ltc_cny Okcoin Daily_price_ltc_cny.csv
3 Eth_cny Okcoin Daily_price_eth_cny.csv
4 Etc_cny Okcoin Daily_price_etc_cny.csv
5 Bcc_cny Okcoin Daily_price_bcc_cny.csv
6 Btc_cny Huobi Daily_price_btc_cny.csv
7 Ltc_cny Huobi Daily_price_ltc_cny.csv
using Python to get bitcoin history data from okcoin.cn

We can refer to the API manuals provided by Okcoin to get historical quotes data.
Get historical data There are two main steps: The connection API gets the data using pandas to process the data and saves it as a CSV file
The following is the detailed code for obtaining data from okcoin.cn:

Import http.client Import JSON import pandas as PD Request_url = ' www.okcoin.cn ' kline_tt_cols = [' Date ', ' open ', ' high ', ' Low ', ' Close ', ' volume '] def http_get (URL, resource, params= '): conn = http.client.HTTPSConnection (URL, timeout=1 0 conn.request ("get", Resource + '? ' + params) response = Conn.getresponse () data = Response.read (). Decode (' U Tf-8 ') return Json.loads (data) def ticker (symbol= ', data_type= ' 1day ', since= '): Ticker_resource = "/api/v1/kli Ne.do "params = ' if Symbol:params = ' symbol=% (symbol) s&type=% (type) s '% {' symbol ': symbol, ' type ': Data_type} if Since:params + = ' &since=% (since) s '% {' since ': since} k_data = Http_get (Request_url, TI
    Cker_resource, params) If Len (k_data) = = 0:raise ValueError (' Can not obtain the data. ') ELSE:DF = PD. Dataframe (K_data, Columns=kline_tt_cols) df[' date '] = Pd.to_datetime (df[' Date '), unit= ' Ms ') return DF if __n ame__ = ' __main__ ': # okcoin.cn BITCOIN-CNY since 2013-6-11 ~ Now, daily price history DAILY_PRICE_BTC_CNY = Ticker (' BTC_CNY ') Daily_price_btc_cny.to_csv ('./data/okcoin/daily_price_btc_cny.csv ') # okcoin.cn LITECOIN-CNY since 2013-9-11 ~ now Daily price History DAILY_PRICE_LTC_CNY = Ticker (' LTC_CNY ') daily_price_ltc_cny.to_csv ('./data/okcoin/daily_price_ Ltc_cny.csv ') # okcoin.cn ETH-CNY since 2017-5-31 ~ Now the price history DAILY_PRICE_ETH_CNY = Ticker (' ETH_CN Y ') daily_price_ltc_cny.to_csv ('./data/okcoin/daily_price_eth_cny.csv ') # okcoin.cn ETC-CNY since 2017-7-16 ~ No W daily price History DAILY_PRICE_ETC_CNY = Ticker (' ETC_CNY ') daily_price_etc_cny.to_csv ('./data/okcoin/daily_pric E_etc_cny.csv ') # okcoin.cn BCC-CNY since 2017-7-16 ~ Now the price history DAILY_PRICE_BCC_CNY = Ticker (' ETC_ CNY ') daily_price_bcc_cny.to_csv ('./data/okcoin/daily_price_bcc_cny.csv ') # okcoin.cn BITCOIN-CNY since 2013-6-1 1 ~ Now, hour price histOry HOUR_PRICE_BTC_CNY = Ticker (symbol= ' BTC_CNY ', data_type= ' 1hour ') hour_price_btc_cny.to_csv ('./data/okcoin/hour _price_btc_cny.csv ') # okcoin.cn LITECOIN-CNY since 2013-6-11 ~ Now, hour price history HOUR_PRICE_LTC_CNY = Tic Ker (symbol= ' ltc_cny ', data_type= ' 1hour ') hour_price_ltc_cny.to_csv ('./data/okcoin/hour_price_ltc_cny.csv ')
using Python to get bitcoin history data from huobi.com

Similar to obtaining historical data from okcoin.cn, refer to the API manuals provided by huobi.com, with detailed code as follows:

Import http.client Import JSON import pandas as PD Request_url = ' api.huobi.com ' kline_tt_cols = [' Date ', ' open ', ' high ', ' Low ', ' Close ', ' volume '] def http_get (URL, resource, params= '): conn = http.client.HTTPSConnection (URL, timeout=1 0 conn.request ("get", Resource + '? ' + params) response = Conn.getresponse () data = Response.read (). Decode (' U Tf-8 ') return Json.loads (data) def ticker (symbol= '): Ticker_resource = "/staticmarket/% (symbol) S_kline_100_json  . js "% {' symbol ': symbol} params = ' if Symbol:params = ' length=2000 ' k_data = Http_get (Request_url,
    Ticker_resource, params) If Len (k_data) = = 0:raise ValueError (' Can not obtain the data. ') ELSE:DF = PD. Dataframe (K_data, Columns=kline_tt_cols) df[' date '] = Pd.to_datetime (df[' Date '), format= "%y%m%d%h%m%s%f") Retu RN DF If __name__ = = ' __main__ ': # huobi.com BITCOIN-CNY since 2013-9-1 ~ Now, daily price history daily_price_ BTC_CNY = Ticker(' BTC ') daily_price_btc_cny.to_csv ('./data/huobi/daily_price_btc_cny.csv ') # huobi.com LITECOIN-CNY since 2014-3 -9 ~ Now the price history DAILY_PRICE_LTC_CNY = ticker (' LTC ') daily_price_ltc_cny.to_csv ('./DATA/HUOBI/DAILY_PR Ice_ltc_cny.csv ')
reference materials

Okcoin API
Huobi API
GitHub

Related Article

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.