Upload web_server.py
Browse files- web_server.py +43 -16
web_server.py
CHANGED
@@ -799,6 +799,9 @@ def get_stock_data():
|
|
799 |
if not stock_code:
|
800 |
return custom_jsonify({'error': '请提供股票代码'}), 400
|
801 |
|
|
|
|
|
|
|
802 |
# 根据period计算start_date
|
803 |
end_date = datetime.now().strftime('%Y%m%d')
|
804 |
if period == '1m':
|
@@ -813,47 +816,71 @@ def get_stock_data():
|
|
813 |
start_date = (datetime.now() - timedelta(days=365)).strftime('%Y%m%d')
|
814 |
|
815 |
# 获取股票历史数据
|
816 |
-
app.logger.info(
|
817 |
-
|
818 |
-
|
819 |
-
|
820 |
-
|
821 |
-
|
822 |
-
|
|
|
823 |
|
824 |
# 检查数据是否为空
|
825 |
-
if df.empty:
|
826 |
app.logger.warning(f"股票 {stock_code} 的数据为空")
|
827 |
return custom_jsonify({'error': '未找到股票数据'}), 404
|
828 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
829 |
# 将DataFrame转为JSON格式
|
830 |
app.logger.info(f"将数据转换为JSON格式,行数: {len(df)}")
|
831 |
|
832 |
# 确保日期列是字符串格式 - 修复缓存问题
|
833 |
if 'date' in df.columns:
|
834 |
try:
|
|
|
835 |
if pd.api.types.is_datetime64_any_dtype(df['date']):
|
836 |
df['date'] = df['date'].dt.strftime('%Y-%m-%d')
|
837 |
else:
|
838 |
-
df = df.copy()
|
839 |
df['date'] = pd.to_datetime(df['date'], errors='coerce')
|
840 |
df['date'] = df['date'].dt.strftime('%Y-%m-%d')
|
|
|
841 |
except Exception as e:
|
842 |
app.logger.error(f"处理日期列时出错: {str(e)}")
|
|
|
|
|
843 |
df['date'] = df['date'].astype(str)
|
844 |
|
845 |
# 将NaN值替换为None
|
846 |
-
|
847 |
-
|
848 |
-
|
|
|
|
|
|
|
|
|
|
|
849 |
|
850 |
-
|
851 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
852 |
except Exception as e:
|
853 |
app.logger.error(f"获取股票数据时出错: {str(e)}")
|
854 |
app.logger.error(traceback.format_exc())
|
855 |
-
return custom_jsonify({'error': str(e)}), 500
|
856 |
-
|
857 |
|
858 |
# @app.route('/api/market_scan', methods=['POST'])
|
859 |
# def api_market_scan():
|
|
|
799 |
if not stock_code:
|
800 |
return custom_jsonify({'error': '请提供股票代码'}), 400
|
801 |
|
802 |
+
# 记录详细的请求参数
|
803 |
+
app.logger.info(f"请求参数: stock_code={stock_code}, market_type={market_type}, period={period}")
|
804 |
+
|
805 |
# 根据period计算start_date
|
806 |
end_date = datetime.now().strftime('%Y%m%d')
|
807 |
if period == '1m':
|
|
|
816 |
start_date = (datetime.now() - timedelta(days=365)).strftime('%Y%m%d')
|
817 |
|
818 |
# 获取股票历史数据
|
819 |
+
app.logger.info(f"获取股票 {stock_code} 的历史数据,市场: {market_type}, 起始日期: {start_date}, 结束日期: {end_date}")
|
820 |
+
try:
|
821 |
+
df = analyzer.get_stock_data(stock_code, market_type, start_date, end_date)
|
822 |
+
app.logger.info(f"成功获取股票数据,行数: {len(df) if df is not None and not df.empty else 0}")
|
823 |
+
except Exception as e:
|
824 |
+
app.logger.error(f"获取股票数据时出错: {str(e)}")
|
825 |
+
app.logger.error(traceback.format_exc())
|
826 |
+
return custom_jsonify({'error': f'获取股票数据失败: {str(e)}'}), 500
|
827 |
|
828 |
# 检查数据是否为空
|
829 |
+
if df is None or df.empty:
|
830 |
app.logger.warning(f"股票 {stock_code} 的数据为空")
|
831 |
return custom_jsonify({'error': '未找到股票数据'}), 404
|
832 |
|
833 |
+
# 计算技术指标
|
834 |
+
app.logger.info(f"计算股票 {stock_code} 的技术指标")
|
835 |
+
try:
|
836 |
+
df = analyzer.calculate_indicators(df)
|
837 |
+
app.logger.info(f"成功计算技术指标")
|
838 |
+
except Exception as e:
|
839 |
+
app.logger.error(f"计算技术指标时出错: {str(e)}")
|
840 |
+
app.logger.error(traceback.format_exc())
|
841 |
+
return custom_jsonify({'error': f'计算技术指标失败: {str(e)}'}), 500
|
842 |
+
|
843 |
# 将DataFrame转为JSON格式
|
844 |
app.logger.info(f"将数据转换为JSON格式,行数: {len(df)}")
|
845 |
|
846 |
# 确保日期列是字符串格式 - 修复缓存问题
|
847 |
if 'date' in df.columns:
|
848 |
try:
|
849 |
+
df = df.copy() # 创建副本避免SettingWithCopyWarning
|
850 |
if pd.api.types.is_datetime64_any_dtype(df['date']):
|
851 |
df['date'] = df['date'].dt.strftime('%Y-%m-%d')
|
852 |
else:
|
|
|
853 |
df['date'] = pd.to_datetime(df['date'], errors='coerce')
|
854 |
df['date'] = df['date'].dt.strftime('%Y-%m-%d')
|
855 |
+
app.logger.info("日期列处理成功")
|
856 |
except Exception as e:
|
857 |
app.logger.error(f"处理日期列时出错: {str(e)}")
|
858 |
+
app.logger.error(traceback.format_exc())
|
859 |
+
# 尝试简单转换为字符串
|
860 |
df['date'] = df['date'].astype(str)
|
861 |
|
862 |
# 将NaN值替换为None
|
863 |
+
try:
|
864 |
+
df = df.replace({np.nan: None, np.inf: None, -np.inf: None})
|
865 |
+
# 确保所有数值都是JSON可序列化的
|
866 |
+
for col in df.select_dtypes(include=['float64']).columns:
|
867 |
+
df[col] = df[col].astype(float)
|
868 |
+
except Exception as e:
|
869 |
+
app.logger.error(f"处理NaN值时出错: {str(e)}")
|
870 |
+
app.logger.error(traceback.format_exc())
|
871 |
|
872 |
+
try:
|
873 |
+
records = df.to_dict('records')
|
874 |
+
app.logger.info(f"数据处理完成,返回 {len(records)} 条记录")
|
875 |
+
return custom_jsonify({'data': records})
|
876 |
+
except Exception as e:
|
877 |
+
app.logger.error(f"转换为JSON时出错: {str(e)}")
|
878 |
+
app.logger.error(traceback.format_exc())
|
879 |
+
return custom_jsonify({'error': f'数据格式转换失败: {str(e)}'}), 500
|
880 |
except Exception as e:
|
881 |
app.logger.error(f"获取股票数据时出错: {str(e)}")
|
882 |
app.logger.error(traceback.format_exc())
|
883 |
+
return custom_jsonify({'error': f'服务器内部错误: {str(e)}'}), 500
|
|
|
884 |
|
885 |
# @app.route('/api/market_scan', methods=['POST'])
|
886 |
# def api_market_scan():
|