import pandas as pd import numpy as np import openpyxl from openpyxl.utils.dataframe import dataframe_to_rows from openpyxl.styles import Font # Define the sample data for the updated dynamic Excel structure with adjustable SMA and EMA periods. sample_data = { "Timestamp": pd.date_range(start="2024-01-01", periods=10, freq="T"), "Open": np.random.uniform(30000, 40000, 10), "High": np.random.uniform(30000, 40000, 10), "Low": np.random.uniform(30000, 40000, 10), "Close": np.random.uniform(30000, 40000, 10), "Volume": np.random.uniform(100, 500, 10) } # Convert to DataFrame df_raw_data = pd.DataFrame(sample_data) # Calculate SMA and EMA with adjustable initial periods sma_period = 5 ema_period = 5 df_raw_data['SMA'] = df_raw_data['Close'].rolling(window=sma_period).mean() df_raw_data['EMA'] = df_raw_data['Close'].ewm(span=ema_period, adjust=False).mean() # Initialize workbook wb = openpyxl.Workbook() wb.remove(wb.active) # Add Raw Data sheet ws_raw = wb.create_sheet("Raw Data") for r in dataframe_to_rows(df_raw_data, index=False, header=True): ws_raw.append(r) # Create Feature Engineering sheet with formula placeholders ws_feature = wb.create_sheet("Feature Engineering") ws_feature["A1"].value = "Adjustable SMA and EMA" ws_feature["A1"].font = Font(bold=True) ws_feature["A2"].value = "SMA Period:" ws_feature["B2"].value = sma_period ws_feature["A3"].value = "EMA Period:" ws_feature["B3"].value = ema_period # Formula cells for recalculated SMA and EMA ws_feature["D2"].value = "Close Price" ws_feature["E2"].value = "SMA (Dynamic)" ws_feature["F2"].value = "EMA (Dynamic)" # Insert formulas in the feature sheet (assuming the raw data is in 'Raw Data' sheet starting from A2) for i in range(3, len(df_raw_data) + 3): ws_feature[f"D{i}"] = f"=Raw Data!E{i}" # Close price ws_feature[f"E{i}"] = f"=AVERAGE(OFFSET(D{i}-$B$2+1,0,0,$B$2,1))" # SMA formula using offset ws_feature[f"F{i}"] = f"=EXPONENTIALMOVINGAVERAGE(D{i}, $B$3)" # Placeholder for EMA dynamic # Add Synthetic Relationships sheet with parameters section ws_synthetic = wb.create_sheet("Synthetic Relationships") ws_synthetic["A1"].value = "Synthetic Relationships - Parameter Adjustments" ws_synthetic["A1"].font = Font(bold=True) # Populate example query columns for visualizing adjusted parameters ws_synthetic.append(["Query Date", "Found Close Price", "Adjusted SMA", "Adjusted EMA"]) for i in range(3, len(df_raw_data) + 3): ws_synthetic[f"A{i}"] = ws_raw[f"A{i}"].value # Query Date ws_synthetic[f"B{i}"] = f"=Raw Data!E{i}" # Found Close Price ws_synthetic[f"C{i}"] = f"=Feature Engineering!E{i}" # Adjusted SMA ws_synthetic[f"D{i}"] = f"=Feature Engineering!F{i}" # Adjusted EMA # Save the file file_path = "ActiveGraphTheory.xlsx" wb.save(file_path) file_path