add weekly and bi-weekly frequently

This commit is contained in:
2026-01-28 12:09:19 +08:00
parent cf708d2466
commit d33b521b22
5 changed files with 431 additions and 206 deletions
+17 -12
View File
@@ -65,36 +65,40 @@ def api_backtest():
# 1. Extract and Sanitize Symbol
symbol = data.get('symbol', '').strip().upper()
print(f"DEBUG: Processing {symbol}")
print(f"DEBUG: Processing {symbol} with payload: {data}")
if not symbol:
return jsonify({"error": "Symbol is required"}), 400
try:
# 2. Extract Numerical and Logic Inputs (Matching updated JS keys)
# 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('start_date', '2024-01-01')
start_date = data.get('startDate') or data.get('start_date', '2024-01-01')
frequency = data.get('frequency', 'Monthly')
# Robust Boolean Check for toggles
# Robust Boolean Check
allow_sell = data.get('allow_sell') is True
allow_frac = data.get('allow_fractional') is True
# 3. Initialize Engines
# DataEngine __init__ now calls ensure_data() to download if missing
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)
# 4. Calculation
history = strat_eng.calculate_va_vs_dca(
initial_inv=initial,
monthly_target=monthly,
start_date=start_date,
allow_sell=allow_sell,
# 4. Calculation - Calling the correctly named method 'run_simulation'
# Ensure arguments match the signature in your engine.py
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
)
@@ -104,7 +108,8 @@ def api_backtest():
import traceback
print("CRITICAL ERROR in /api/backtest:")
print(traceback.format_exc())
return jsonify({"error": "Internal Server Error. Check terminal logs."}), 500
# Returning the actual error message helps debugging the '500' faster
return jsonify({"error": str(e)}), 500
@app.route('/backtest') # This is the URL you will actually visit
def backtest_ui():