# HLdensity()

### Class: `HLdensity`

#### Method: `run`

The `run` method is the primary entry point for executing the analysis. It fetches and processes the data, and optionally displays a chart or prints high/low data.

```python
HLdensity.run(client, ticker_symbol, days=100, chart=False, printdata=False)
```

#### Arguments

* **client** (`str`): The data provider to use for fetching historical data. Currently, the class supports `'yahoo'` as the client for Yahoo Finance.
* **ticker\_symbol** (`str`): The stock ticker symbol (e.g., `'AAPL'`, `'SPY'`). This specifies the stock for which the OHLC data will be fetched.
* **days** (`int`, optional): The number of past days to fetch the OHLC data for. Default is `100`.
* **chart** (`bool`, optional): Set to `True` to generate and display a density map chart of the price movements. Default is `False`.
* **printdata** (`bool`, optional): Set to `True` to print the high and low price order for each day. Default is `False`.

#### Returns

* **None**: This method does not return a value but prints the output or plots a chart depending on the `printdata` and `chart` parameters.

***

## Usage Guide

### Step 1: Initialize the Data Provider

Currently, the `HLdensity` class supports Yahoo Finance as the data provider. You must specify `'yahoo'` as the `client` argument to fetch data from Yahoo Finance.

```python
client = 'yahoo'  # Yahoo Finance data provider
```

### Step 2: Select the Stop Timeframe

Specify the stock ticker symbol and the number of days for which you want to analyze the stock. For example, if you want to analyze Apple stock (`AAPL`) over the past 100 days:

```python
ticker_symbol = 'AAPL'
days = 100
```

### Step 3: Run Price Movement Analysis

You can use the `HLdensity.run` method to fetch data, normalize it, and print high/low price information or plot a price movement density map. Depending on your requirements, you can choose to print data, generate a chart, or both.

***

### Examples

#### Example 1: Print High/Low Data to the Terminal

In this example, the high/low price order for the past 100 days of Apple (`AAPL`) stock will be printed to the terminal. No chart will be displayed.

```python
from marketquant.tools import HLdensity

HLdensity.run(
    client='yahoo', 
    ticker_symbol="AAPL", 
    days=100, 
    chart=False,        # No chart, just print the high/low data
    printdata=True      # Enable printing of high/low data
)
```

**Output:**

```
textC2024-01-02: Low was reached first (Low: 125.67), then High (High: 128.23).
2024-01-03: High was reached first (High: 130.45), then Low (Low: 129.01).
...
```

This will print a message for each trading day, indicating whether the day’s low or high was reached first.

***

#### Example 2: Generate and Plot a Price Movement Density Map

In this example, a price movement density map for Apple (`AAPL`) over the past 100 days will be generated and plotted.

```python
from marketquant.tools import HLdensity

HLdensity.run(
    client='yahoo', 
    ticker_symbol="AAPL", 
    days=100, 
    chart=True,         # Enable chart plotting
    printdata=False     # Do not print high/low data
)
```

**Expected Behavior:** A Matplotlib chart will be displayed, showing the price movements over normalized time. The X-axis will represent the normalized time (start of the day to the end), and the Y-axis will show the price movement relative to the opening price.

#### The chart will look like this:

<figure><img src="/files/FSeH8pPkDmAoFXWJwNwi" alt=""><figcaption><p>High/Low Range Density Map</p></figcaption></figure>

***

### Customization Options

The `HLdensity` class allows for flexible customization of outputs. Below are some of the key options:

#### Print High/Low Price Order (`printdata`)

By setting the `printdata` argument to `True`, you can print a daily log of whether the high or low price was reached first. This can help traders identify recurring patterns in price movements.

#### Plotting a Price Movement Density Map (`chart`)

When the `chart` argument is set to `True`, a density map will be plotted using normalized price movements. The chart shows how the stock's price moves relative to the opening price during the day.

***

### Output

#### When `printdata=True`

The output will be a printed log of daily high and low price information:

```
2024-01-02: Low was reached first (Low: 125.67), then High (High: 128.23).
2024-01-03: High was reached first (High: 130.45), then Low (Low: 129.01).
```

This allows you to see whether the price's low or high point was reached first in each session.

#### When `chart=True`

The output will be a Matplotlib-generated chart, displaying the normalized price movements. The chart will plot normalized price movements relative to the opening price throughout the day.

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://market-quant.gitbook.io/home/api-reference/tools/fractality/hldensity.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
