diff --git a/stock-html/routes/analysis.py b/stock-html/routes/analysis.py index b588f2d..65b0bdd 100644 --- a/stock-html/routes/analysis.py +++ b/stock-html/routes/analysis.py @@ -601,9 +601,10 @@ def batch_technical_signals(): scan_map[row['code']] = row cur.close() - put_db(conn) except Exception as e: print(f"批量扫描获取数据失败: {e}") + finally: + put_db(conn) results = [] errors = [] @@ -919,7 +920,6 @@ def get_scan_results(): recommend_counts[disp] = recommend_counts.get(disp, 0) + 1 cur.close() - put_db(conn) return jsonify({ 'success': True, @@ -939,6 +939,8 @@ def get_scan_results(): }) except Exception as e: return jsonify({'success': False, 'error': str(e)}), 500 + finally: + put_db(conn) def _compute_recommend(signal_status, indicators, triggered_count, is_holding): @@ -994,7 +996,6 @@ def signal_alerts(): WHERE code IN ({placeholders}) """, stock_codes) price_rows = cur.fetchall() - put_db(conn) price_map = {} change_map = {} @@ -1076,6 +1077,8 @@ def signal_alerts(): import traceback traceback.print_exc() return jsonify({'success': False, 'error': str(e)}), 500 + finally: + put_db(conn) def _is_scan_running(): @@ -1140,7 +1143,6 @@ def get_scan_status(): triggered = cur.fetchone()[0] cur.close() - put_db(conn) return jsonify({ 'success': True, @@ -1154,6 +1156,8 @@ def get_scan_status(): }) except Exception as e: return jsonify({'success': False, 'error': str(e)}), 500 + finally: + put_db(conn) @bp.route('/start_full_scan', methods=['POST']) @@ -1225,7 +1229,6 @@ def get_scan_strategy(): """, (scan_date,)) rows = cur.fetchall() cur.close() - put_db(conn) tier1, tier2, tier3, tier4 = [], [], [], [] for r in rows: @@ -1306,6 +1309,8 @@ def get_scan_strategy(): }) except Exception as e: return jsonify({'success': False, 'error': str(e)}), 500 + finally: + put_db(conn) def _get_kline_data(stock_code, days=120): @@ -1357,8 +1362,6 @@ def get_bull_stocks(): for p in cur.fetchall(): price_map[p['code']] = {'price': float(p['price']), 'change_pct': float(p.get('change_pct') or 0)} - put_db(conn) - # ---- 批量计算综合评分 ---- scores_map = None try: @@ -1420,6 +1423,8 @@ def get_bull_stocks(): import traceback traceback.print_exc() return jsonify({'success': False, 'error': str(e)}), 500 + finally: + put_db(conn) def _llm_polish_summary(stock_name, stock_code, ai_summary, score, verdict): diff --git a/stock-html/routes/trades.py b/stock-html/routes/trades.py index 25d6a26..3085c51 100644 --- a/stock-html/routes/trades.py +++ b/stock-html/routes/trades.py @@ -130,7 +130,6 @@ def update_trade(trade_id): """, (trade_id, user_id)) old_trade = cur.fetchone() if not old_trade: - put_db(conn) return jsonify({'error': '交易记录不存在'}), 404 cur.execute(""" @@ -152,7 +151,6 @@ def update_trade(trade_id): trade = cur.fetchone() if not trade: conn.rollback() - put_db(conn) return jsonify({'error': '交易记录不存在'}), 404 old_delta = _calc_cash_delta(old_trade.get('trade_type'), old_trade.get('price'), old_trade.get('quantity')) @@ -194,7 +192,6 @@ def delete_trade(trade_id): """, (trade_id, user_id)) old_trade = cur.fetchone() if not old_trade: - put_db(conn) return jsonify({'success': False, 'error': '交易记录不存在'}), 404 cur.execute("DELETE FROM trades WHERE id = %s AND user_id = %s", (trade_id, user_id)) @@ -306,7 +303,7 @@ def check_stoploss(): current_price = 0 try: import requests as _rq - _tc = ('sh' if code.startswith('6') else 'sz') + code + _tc = ('sh' if code.startswith('6') else 'bj' if code.startswith(('8', '9')) else 'sz') + code _rr = _rq.get(f'http://qt.gtimg.cn/q={_tc}', timeout=5, headers={'Referer': 'https://finance.qq.com'}) if _rr.status_code == 200 and '\"' in _rr.text: