41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
import pandas as pd
|
|
import os
|
|
|
|
def import_history(source_file, symbol):
|
|
"""
|
|
source_file: path to your historical data (Excel or CSV)
|
|
symbol: The symbol name matching your instruments.csv (e.g., 'AGI_USD')
|
|
"""
|
|
cache_dir = "data_cache"
|
|
os.makedirs(cache_dir, exist_ok=True)
|
|
target_path = os.path.join(cache_dir, f"{symbol}.csv")
|
|
|
|
try:
|
|
# Load your data
|
|
if source_file.endswith('.xlsx'):
|
|
df = pd.read_excel(source_file)
|
|
else:
|
|
df = pd.read_csv(source_file)
|
|
|
|
# 1. Standardize columns: We need 'date' and 'close'
|
|
# Adjust these strings if your Excel uses different names like 'Price' or 'Date'
|
|
df.columns = [c.lower().strip() for c in df.columns]
|
|
|
|
# If your Excel has columns named 'nav' or 'price', rename them
|
|
rename_map = {'price': 'close', 'nav': 'close', 'valuation date': 'date'}
|
|
df = df.rename(columns=rename_map)
|
|
|
|
# 2. Convert to proper format
|
|
df['date'] = pd.to_datetime(df['date']).dt.strftime('%Y-%m-%d')
|
|
df = df[['date', 'close']].dropna().sort_values('date')
|
|
|
|
# 3. Save to the cache folder
|
|
df.to_csv(target_path, index=False)
|
|
print(f"✅ Successfully created cache for {symbol} at {target_path}")
|
|
print(f"📊 Rows imported: {len(df)}")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Error importing {symbol}: {e}")
|
|
|
|
# EXAMPLE USAGE:
|
|
# import_history('my_old_data.xlsx', 'AGI_USD') |