Skip to content

Referensi Kode

Auto-render Docstring (mkdocstrings)

src.jobs.main.fetch_stock_data(ticker, period=HISTORY_PERIOD)

Download daily OHLCV history for a ticker via yfinance.

Source code in src/jobs/main.py
def fetch_stock_data(ticker: str, period: str = HISTORY_PERIOD) -> pd.DataFrame | None:
    """Download daily OHLCV history for a ticker via yfinance."""
    try:
        df = yf.download(
            ticker, period=period, interval="1d", progress=False, auto_adjust=True,
            session=_yf_session(),
        )
    except Exception:
        logger.exception("Failed to download data for %s", ticker)
        return None

    if df is None or df.empty:
        logger.warning("No data returned for %s", ticker)
        return None

    if isinstance(df.columns, pd.MultiIndex):
        df.columns = df.columns.get_level_values(0)

    return df

Contoh Google Style Docstring

import pandas as pd
import yfinance as yf


def get_ticker_snapshot(ticker: str, period: str = "6mo") -> pd.DataFrame | None:
    """Ambil data historis OHLCV untuk satu ticker saham via yfinance.

    Args:
        ticker: Kode saham dengan suffix bursa, contoh "BBCA.JK".
        period: Rentang waktu histori yang diminta ke yfinance, contoh
            "6mo" untuk enam bulan atau "1y" untuk satu tahun.

    Returns:
        DataFrame pandas berisi kolom Open/High/Low/Close/Volume terindeks
        tanggal, atau None jika ticker tidak ditemukan atau yfinance gagal
        mengembalikan data.
    """
    try:
        df = yf.download(ticker, period=period, interval="1d", progress=False)
    except Exception:
        return None

    if df is None or df.empty:
        return None

    return df