Truy xuất và lưu dữ liệu realtime lên Google Sheet
Các ví dụ liên quan đến việc truy xuất dữ liệu realtime và lưu lên Google Sheet
Tạo và tải khóa tài khoản dịch vụ Google JSON
Đi đến Google Cloud Console: https://console.cloud.google.com/
Chọn hoặc tạo một project mới, nếu không có project nào click "New Project".

Đi đến mục APIs & Services > Library


Chọn mục Google Sheets API

Enable API

Truy cập vào mục IAM & Admin > Service Accounts, click "Create Service Account", thực hiện đặt tên chp Service Account rồi click "Create and Continue"

Chọn role cho Service Account (Editor) và click Continue

Sau khi tạo Service Account, click vào tên Service Account mới tạo, đi đến tab "Keys", click "Add Key" > "Create new key", chọn JSON và click "Create". Sau đó trình duyệt sẽ tải file JSON cấu hình để truy cập vào Google Sheet tự động. Sau đó hãy di chuyển file JSON vừa tải về đến cùng folder với file Python viết code Truy xuất data từ FiinQuant và lưu lên Google Sheets.
Sau đó thực hiện truy cập vào file Google Sheets cần load data lên, thực hiện add quyền edit cho email ở mục client_email của file JSON vừa tải về. Sau đó thực hiện đoạn code sau
import os
import gspread
import time
import pandas as pd
from FiinQuantX import FiinSession, RealTimeData
from gspread_dataframe import set_with_dataframe
from oauth2client.service_account import ServiceAccountCredentials
username = "REPLACE_WITH_YOUR_USERNAME"
password = "REPLACE_WITH_YOUR_PASSWORD"
client_fq = FiinSession(
username = username,
password = password
).login()
def OnTickerEvent(data: RealTimeData):
global full_df
df = data.to_dataFrame()
# Xác thực và kết nối đến Google Sheets
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("REPLACE_WITH_JSON_FILE_NAME")
client = gspread.authorize(creds)
# Mở Google Sheet và chọn worksheet
sheet = client.open("REPLACE_WITH_YOUR_GOOGLE_SHEET_FILE_NAME").worksheet("REPLACE_WITH_SHEET_NAME")
# Thêm data vào Google Sheet ---
existing_rows = len(sheet.get_all_values()) # Đếm số cột hiện có
# Nếu chưa có cột nào thêm data mới bao gồm cả tiêu đề cột, nếu không chỉ thêm data, không thêm tiêu đề cột
if existing_rows <= 1:
set_with_dataframe(sheet, df, row=existing_rows, include_column_header=True)
else:
set_with_dataframe(sheet, df, row=existing_rows + 1, include_column_header=False)
tickers = ['STB', 'FPT', 'MSN', 'HPG']
event = client_fq.Trading_Data_Stream(tickers=tickers, callback=OnTickerEvent)
event.start()
try:
while not event._stop:
time.sleep(1)
except KeyboardInterrupt:
print("KeyboardInterrupt received, stopping...")
event.stop()
Last updated