fetch data bug fix for both index and DVA/DCA calculation

This commit is contained in:
2026-02-02 06:48:49 +08:00
parent d33b521b22
commit 6506932042
15 changed files with 35346 additions and 16918 deletions
+21 -34
View File
@@ -62,54 +62,41 @@ def run_sync():
@app.route('/api/backtest', methods=['POST'])
def api_backtest():
data = request.get_json() or {}
# 1. Extract and Sanitize Symbol
symbol = data.get('symbol', '').strip().upper()
print(f"DEBUG: Processing {symbol} with payload: {data}")
engine = DataEngine(symbol=symbol)
if not symbol:
return jsonify({"error": "Symbol is required"}), 400
try:
# 2. Extract and Cast Inputs (Ensuring types match engine requirements)
# Note: JS uses 'startDate' (camelCase), Python often uses 'start_date'
initial = float(data.get('initial_inv', 0))
monthly = float(data.get('monthly_target', 0))
start_date = data.get('startDate') or data.get('start_date', '2024-01-01')
frequency = data.get('frequency', 'Monthly')
# Robust Boolean Check
allow_sell = data.get('allow_sell') is True
allow_frac = data.get('allow_fractional') is True
# 3. Initialize Engines
# 1. Initialize the Engine
# (The Engine's __init__ should handle looking up the URL in instruments.csv)
data_eng = DataEngine(symbol=symbol)
# Verify file exists after DataEngine logic
if not os.path.exists(data_eng.file_path):
return jsonify({"error": f"Data for {symbol} could not be retrieved."}), 404
strat_eng = StrategyEngine(data_eng)
# 2. Trigger Smart Fetch
# (Inside engine.py, this checks the 24h clock and updates if needed)
data_eng.fetch_data()
# 4. Calculation - Calling the correctly named method 'run_simulation'
# Ensure arguments match the signature in your engine.py
# 3. Verify data exists before proceeding
if not os.path.exists(data_eng.file_path):
return jsonify({"error": f"No data found for {symbol}"}), 404
# 4. Run Strategy
strat_eng = StrategyEngine(data_eng)
history = strat_eng.run_simulation(
start_date=start_date,
monthly_goal=monthly,
initial_inv=initial,
frequency=frequency,
allow_sell=allow_sell,
allow_fractional=allow_frac
start_date=data.get('startDate', '2024-01-01'),
monthly_goal=float(data.get('monthly_target', 0)),
initial_inv=float(data.get('initial_inv', 0)),
frequency=data.get('frequency', 'Monthly'),
allow_sell=data.get('allow_sell') is True,
allow_fractional=data.get('allow_fractional') is True
)
return jsonify(history)
except Exception as e:
import traceback
print("CRITICAL ERROR in /api/backtest:")
print(traceback.format_exc())
# Returning the actual error message helps debugging the '500' faster
return jsonify({"error": str(e)}), 500
app.logger.error(f"Backtest Error: {str(e)}")
return jsonify({"error": "Internal server error"}), 500
@app.route('/backtest') # This is the URL you will actually visit
def backtest_ui():