# Get Historical Candlesticks

request_history_kline(code, start=None, end=None, ktype=KLType.K_DAY, autype=AuType.QFQ, fields=[KL_FIELD.ALL], max_count=1000, page_req_key=None, extended_time=False)

  • Description

    Get historical candlesticks

  • Parameters

    Parameter Type Description
    code str Stock code.
    start str Start time.
    end str End time.
    ktype KLType Candlestick type.
    autype AuType Type of adjustment.
    fields KL_FIELD List of fields to be returned.
    max_count int The maximum number of candlesticks returned in this request.
    page_req_key bytes The key of the page request. If the number of candlesticks between start and end is more than max_count, then None should be passed at the first time you call this interface, and the page_req_key returned by the last call must be passed in the subsequent pagerequests.
    extended_time bool Need pre-market and after-hours data for US stocks or not. False: not need, True: need.
    • The combination of start and end is as follows
      Start type End type Description
      str str start and end are the specified dates respectively.
      None str start is 365 days before end.
      str None end is 365 days after start.
      None None end is the current date, start is 365 days before.
  • Return

    Field Type Description
    ret RET_CODE Interface result.
    data pd.DataFrame If ret == RET_OK, historical candlestick data is returned.
    str If ret != RET_OK, error description is returned.
    page_req_key bytes The key of the next page request.
    • Historical candlestick data format as follows:
      Field Type Description
      code str Stock code.
      name str Stock name.
      time_key str Candlestick time.
      open float Open.
      close float Close.
      high float High.
      low float Low.
      pe_ratio float P/E ratio.
      turnover_rate float Turnover rate.
      volume int Volume.
      turnover float Turnover.
      change_rate float Change rate.
      last_close float Yesterday's close.
  • Example

from moomoo import *
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
ret, data, page_req_key = quote_ctx.request_history_kline('HK.00700', start='2019-09-11', end='2019-09-18', max_count=5) # 5 per page, request the first page
if ret == RET_OK:
    print(data)
    print(data['code'][0]) # Take the first stock code
    print(data['close'].values.tolist()) # The closing price of the first page is converted to a list
else:
    print('error:', data)
while page_req_key != None: # Request all results after
    print('*************************************')
    ret, data, page_req_key = quote_ctx.request_history_kline('HK.00700', start='2019-09-11', end='2019-09-18', max_count=5,page_req_key=page_req_key) # Request the page after turning data
    if ret == RET_OK:
        print(data)
    else:
        print('error:', data)
print('All pages are finished!')
quote_ctx.close() # After using the connection, remember to close it to prevent the number of connections from running out
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  • Output
    code     name             time_key       open      close       high        low  pe_ratio  turnover_rate    volume      turnover  change_rate  last_close
0   HK.00700  TENCENT  2019-09-11 00:00:00  307.85001  312.45926  314.11859  306.37505    36.405        0.00165  17133836  5.437023e+09     1.740968   307.11253
..       ...      ...                  ...        ...        ...        ...        ...       ...            ...       ...           ...          ...         ...
4   HK.00700  TENCENT  2019-09-17 00:00:00  309.87808  310.24682  311.90615  307.48127    36.153        0.00097  10007748  3.165508e+09    -0.883527   313.01237

[5 rows x 13 columns]
HK.00700
[312.45926, 315.59355, 315.77792, 313.01237, 310.24682]
*************************************
       code     name             time_key       open      close       high        low  pe_ratio  turnover_rate   volume      turnover  change_rate  last_close
0  HK.00700  TENCENT  2019-09-18 00:00:00  310.61556  309.69371  312.09052  308.40312     36.09        0.00077  7957229  2.516701e+09    -0.178281   310.24682
All pages are finished!
1
2
3
4
5
6
7
8
9
10
11
12

Interface Restrictions

  • Candlestick data with timeframes of 60 minutes and below, is only supported for the last 8 years. Data with timeframes of daily and above, is supported for the last 10 years.
  • We will issue historical candlestick quota based on your account assets and transaction conditions. Therefore, you can only obtain historical candlestick data for a limited number of stocks within 30 days. For specific rules, please refer to Subscription Quota & Historical Candlestick Quota. The historical candlestick quota you consume on that day will be automatically released after 30 days.
  • A maximum of 60 requests per 30 seconds. Note: If you obtain data by page, this frequency limit rule is only applicable to the first time calling the interface, and subsequent pages request frequency is unlimited.
  • Change rate, only supports timeframes of daily and above.
  • Options related candlestick data, only supports 1 day, 1 minute, 5 minutes, 15 minutes and 60 minutes.
  • The pre-market and after-hours candlestick of US stocks, only supports timeframes of 60 minutes and below. Since the pre-market and after-hours of the US stock market are irregular trading hours, the candlestick data for this period may be less than 2 years.
  • Turnover of US stocks, only supports data after 2015-10-12.