"""
load_indicator_export.py
Loads a CSV file produced by IndicatorBufferExporter.mq5 into pandas,
with correct dtypes and warm-up period rows containing NaN.

Requirements: Python 3.7+, pandas
Install:      pip install pandas
Usage:        python load_indicator_export.py indicator_export.csv
"""

import sys
import pandas as pd


def load_indicator_export(path: str) -> pd.DataFrame:
    """
    Load an indicator export CSV into a time-indexed DataFrame.

    Parameters
    ----------
    path : str
        Path to the CSV file written by IndicatorBufferExporter.mq5.

    Returns
    -------
    pd.DataFrame
        DataFrame indexed by bar time, with OHLCV columns and every
        exported indicator buffer column as float64. Warm-up period
        rows contain NaN in the indicator columns.
    """
    df = pd.read_csv(path, parse_dates=["time"])
    df.set_index("time", inplace=True)

    # Ensure OHLC and any indicator buffer columns are float64
    for col in df.columns:
        if col != "tick_volume":
            df[col] = df[col].astype("float64")

    if "tick_volume" in df.columns:
        df["tick_volume"] = df["tick_volume"].astype("int64")

    return df


if __name__ == "__main__":
    path = sys.argv[1] if len(sys.argv) > 1 else "indicator_export.csv"

    df = load_indicator_export(path)

    print(f"Loaded {len(df)} rows from {path}")
    print(f"Columns: {list(df.columns)}")
    print()
    print("First 25 rows (warm-up period should contain NaN in indicator columns):")
    print(df.head(25))
    print()
    print("Last 10 rows:")
    print(df.tail(10))

    # Identify the indicator columns (everything after tick_volume)
    fixed_cols = {"open", "high", "low", "close", "tick_volume"}
    indicator_cols = [c for c in df.columns if c not in fixed_cols]

    if indicator_cols:
        print()
        print(f"Indicator columns detected: {indicator_cols}")
        nan_counts = df[indicator_cols].isna().sum()
        print(f"NaN count per indicator column (warm-up period length):")
        print(nan_counts)
