fix fetch data in signal page and the backtest export excel issue

This commit is contained in:
2026-02-02 21:30:19 +08:00
parent 50f9734dcd
commit 891f0ea2b0
21 changed files with 8328 additions and 57803 deletions
+29 -25
View File
@@ -70,28 +70,32 @@ def index():
@app.route('/api/summary')
def get_summary():
engine_base = DataEngine()
instruments = engine_base.load_instruments_from_csv('instruments.csv')
engine = DataEngine()
instruments = engine.load_instruments_from_csv('instruments.csv')
results = []
for item in instruments:
engine = DataEngine(
symbol=item['symbol'],
url=item['url'],
provider=item['provider']
)
# Update engine target
engine.symbol = item['symbol'].strip().upper()
engine.file_path = os.path.join(engine.cache_dir, f"{engine.symbol}.csv")
metrics = engine.get_local_metrics()
display_name = item.get('name') or engine.symbol
if metrics and isinstance(metrics, dict):
metrics['symbol'] = item['symbol']
if metrics:
metrics.update({
'name': display_name,
'symbol': engine.symbol
})
results.append(metrics)
else:
# 🔥 FIX: Include the 'error' key so JavaScript hits the Gatekeeper
# IMPORTANT: If metrics are None, we still need to send
# an object so the frontend knows the row exists!
results.append({
"symbol": item['symbol'],
"last_close": None, # Ensure this is null, not the string "No Data"
"error": True
'name': display_name,
'symbol': engine.symbol,
'last_close': 'N/A',
'error': True
})
return jsonify(results)
@@ -107,25 +111,25 @@ def run_sync():
def api_backtest():
data = request.get_json() or {}
symbol = data.get('symbol', '').strip().upper()
engine = DataEngine(symbol=symbol)
if not symbol:
return jsonify({"error": "Symbol is required"}), 400
try:
# 1. Initialize the Engine
# (The Engine's __init__ should handle looking up the URL in instruments.csv)
# 1. Initialize Engine (Uses the absolute path logic we fixed)
data_eng = DataEngine(symbol=symbol)
# 2. Trigger Smart Fetch
# (Inside engine.py, this checks the 24h clock and updates if needed)
data_eng.fetch_data()
# 3. Verify data exists before proceeding
# 2. Smart Check: Request URL if file is missing
if not os.path.exists(data_eng.file_path):
return jsonify({"error": f"No data found for {symbol}"}), 404
print(f"--- ⚠️ {symbol} not found in cache. Triggering auto-fetch... ---")
# This will use the default Yahoo template if not in instruments.csv
data_eng.fetch_data()
# 3. Verify it worked before handing off to Strategy
if not os.path.exists(data_eng.file_path):
return jsonify({"error": f"Failed to retrieve data for {symbol}"}), 404
# 4. Run Strategy
# 4. Run Strategy Logic (Restored)
strat_eng = StrategyEngine(data_eng)
history = strat_eng.run_simulation(
start_date=data.get('startDate', '2024-01-01'),
@@ -140,8 +144,8 @@ def api_backtest():
except Exception as e:
app.logger.error(f"Backtest Error: {str(e)}")
return jsonify({"error": "Internal server error"}), 500
return jsonify({"error": f"Analysis failed: {str(e)}"}), 500
@app.route('/backtest') # This is the URL you will actually visit
def backtest_ui():
# This sends the HTML file to your browser