# fetch_historical_data { #shinybroker.fetch_historical_data }

```python
fetch_historical_data(
    contract,
    endDateTime='',
    durationStr='1 D',
    barSizeSetting='1 hour',
    whatToShow='Trades',
    useRTH=True,
    host='127.0.0.1',
    port=7497,
    client_id=9999,
    timeout=3,
)
```

Fetch historical data for a tradable asset

Creates a temporary IBKR client socket at the specified `host`, `port`, and
`client_id`, then makes a query for the historical data specified by the
input parameters, receives the response, closes the socket, formats the
response, and returns the result.

If `timeout` number of seconds elapse before receiving historical data,
then `fetch_historical_data` returns `None`.

## Parameters {.doc-section .doc-section-parameters}

| Name           | Type                                        | Description                                                                                                                                                                                                                                                                            | Default       |
|----------------|---------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------|
| contract       | [Contract](`shinybroker.obj_defs.Contract`) | The [Contract](`shinybroker.Contract`) object for which you want data                                                                                                                                                                                                                  | _required_    |
| endDateTime    |                                             | Ending datetime string formatted as “YYYYMMDD HH:mm:ss TMZ” specifying the end of the time period for which you want historical data. Leave it as the default "" to get historical data up to the current present moment.                                                              | `''`          |
| durationStr    |                                             | A [Duration String](https://www.interactivebrokers.com/campus/ibkr-api-page/twsapi-doc/#hist-duration) that specifies how far back in time you want to fetch data.                                                                                                                     | `'1 D'`       |
| barSizeSetting |                                             | A [Bar Size](https://www.interactivebrokers.com/campus/ibkr-api-page/twsapi-doc/#hist-bar-size) that specifies how fine-grained you want your historical data to be (daily, weekly, every 30 seconds, etc).                                                                            | `'1 hour'`    |
| whatToShow     |                                             | You may select from any of the [Historical Data Types](https://www.interactivebrokers.com/campus/ibkr-api-page/twsapi-doc/#historical-whattoshow) but for most cases you'll probably be happy with one of "BID_ASK", "MIDPOINT", or "TRADES".                                          | `'Trades'`    |
| useRTH         |                                             | "Use Regular Trading Hours". Set to `False` if you want the historical data to include after-hours/pre-market trading                                                                                                                                                                  | `True`        |
| host           |                                             | Address of a running IBKR client (such as TWS or IBG) that has been configured to accept API connections                                                                                                                                                                               | `'127.0.0.1'` |
| port           |                                             | Port of a running IBKR client                                                                                                                                                                                                                                                          | `7497`        |
| client_id      |                                             | Client ID you want to use for the request. If you are connecting to a system that is used by multiple users, then you may wish to set aside an  ID for this purpose; if you're the only one using the account then  you probably don't have to worry about it -- just use the default. | `9999`        |
| timeout        |                                             | Time in seconds to wait for a response.                                                                                                                                                                                                                                                | `3`           |

## Examples {.doc-section .doc-section-examples}

```
{{< include ../examples/fetch_historical_data.py >}}
```