# Real-time data function

Describe how to use the library after logging in. Detailed examples are provided at the end of this chapter.

```python
Events = client.Trading_Data_Stream(tickers = tickers, callback = callback)
```

**Parameter**

<table><thead><tr><th>Name</th><th width="187">Description</th><th>Data type</th><th>Required</th></tr></thead><tbody><tr><td>tickers</td><td>Code name, including stock codes, index codes, derivative codes, and covered warrant codes. These codes are written in uppercase.</td><td><p>list [str]</p><p><br></p></td><td>Yes</td></tr><tr><td>callback</td><td>A user-defined function for data manipulation</td><td>function</td><td>Yes</td></tr></tbody></table>

The data receiving class is **Trading\_Data\_Stream**, with the following two methods:

<figure><img src="/files/YAC6Nv3mqcMFLlwm4Sma" alt=""><figcaption></figcaption></figure>

**callback function**

It's a user-defined method for data manipulation, which will be passed as an argument when initializing the data receiving object. This method will have the following format:

```python
//pseudocode
//callback_function
def name_of_callback(data: RealTimeData):
    //do something

Events = client.Trading_Data_Stream(tickers = ['ticker_1','ticker_2',,,'ticker_n'], callback = name_of_callback)
Events.start()
# tickers can include ticker, coveredwarrant, index and derivative.
Events = client.Trading_Data_Stream(tickers = ['ticker_1','ticker_2',,,'ticker_n'], callback = name_of_callback)
Events.start()
```

**RealTimeData has the following methods and attributes:**

| Data object method name | Description                                                                         |
| ----------------------- | ----------------------------------------------------------------------------------- |
| to\_dataFrame( )        | Return all data attributes instead of individual ones, stored as a Pandas DataFrame |

**RealTimeData has the following attributes:**

| Attributes             | Description                                             | Data type |
| ---------------------- | ------------------------------------------------------- | --------- |
| Ticker                 | Code name                                               | str       |
| TotalMatchVolume       | Total volume                                            | int       |
| MarketStatus           | Market status                                           | str       |
| TradingDate            | Time                                                    | str       |
| ComGroupCode           | Indicator code                                          | str       |
| Reference              | Reference value                                         | float     |
| Open                   | Open price                                              | float     |
| Close                  | Close price                                             | float     |
| High                   | High price                                              | float     |
| Low                    | Low price                                               | float     |
| Change                 | Change relative to reference value                      | float     |
| ChangePercent          | Percentage change relative to reference value           | float     |
| MatchVolume            | Matching volume                                         | int       |
| MatchValue             | Matching value                                          | float     |
| TotalMatchValue        | Total matching value                                    | float     |
| TotalBuyTradeVolume    | Total buying volume                                     | int       |
| TotalSellTradeVolume   | Total selling volume                                    | int       |
| TotalDealVolume        | Total deal volume                                       | int       |
| TotalDealValue         | Total deal value                                        | float     |
| ForeignBuyVolumeTotal  | Total foreign buy volume from the beginning of the day  | int       |
| ForeignBuyValueTotal   | Total foreign buy value from the beginning of the day   | float     |
| ForeignSellVolumeTotal | Total foreign sell volume from the beginning of the day | int       |
| ForeignSellValueTotal  | Total foreign sell value from the beginning of the day  | float     |
| Bu                     | Active buy volume                                       | int       |
| Sd                     | Active sell volume                                      | int       |

**Example of order matching data (RealTimeData):**

```python
import FiinQuantX as fq
from FiinQuantX import RealTimeData
import pandas as pd
import time

client = fq.FiinSession(
    username='Enter your username',
    password='Enter your password'
)
client.login()

tickers = ['HPG','VNINDEX','VN30F1M']

def onTickerEvent(data: RealTimeData):
    print('----------------')
    print(data.to_dataFrame())
    # data.to_dataFrame().to_csv('callback.csv', mode='a', header=True)
Events = client.Trading_Data_Stream(tickers=tickers, callback = onTickerEvent)
Events.start()

try:
    while not Events._stop: 
        time.sleep(1)
except KeyboardInterrupt:
    print("KeyboardInterrupt received, stopping...")
    Events.stop()
```


---

# 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://docs.fiinquant.vn/fiinquant-en/function-and-formula/real-time-data-function.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.
