-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
921 lines (803 loc) · 35.6 KB
/
app.py
File metadata and controls
921 lines (803 loc) · 35.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
# backend/app.py
from flask import Flask, request, jsonify
from flask_cors import CORS
import json
from signals.tech_sig import TechnicalSignals
from signals.pattern_sig import PatternRecognizer
from backtester import backtest_signal_strategy, plot_backtest_results
from util.stock.load_stock import load_stock_eodprice_from_tiingo, load_stock_realtime_price
from util.crypto.load_crypto import load_crypto_price_from_tiingo, load_crypto_realtime_price, load_crypto_eodprice_from_tiingo
import numpy as np
import pandas as pd
import feedparser
from textblob import TextBlob
import statistics
from datetime import datetime
from multi_factor import train_and_backtest_multifactormodel
import pytz # pip install pytz
import re
import traceback
import os
from util.news.news_analyzer import fetch_company_news_batched, add_finbert_sentiment_to_df, calculate_ticker_overall_sentiment,summarize_news
app = Flask(__name__)
CORS(app) # 允许跨域请求
db_params = {
'host': 'localhost',
'database': 'postgres',
'user': 'postgres',
'password': 'password',
'port': 5432
}
api_token='YOUR_TIINGO_API_TOKEN'
# Helper function to make anything JSON-safe
def make_json_serializable(obj):
if isinstance(obj, (np.floating, float)):
return float(obj)
if isinstance(obj, (np.integer, int)):
return int(obj)
if isinstance(obj, (np.ndarray, list)):
return [make_json_serializable(item) for item in obj]
if isinstance(obj, dict):
return {key: make_json_serializable(value) for key, value in obj.items()}
if hasattr(obj, 'isoformat'): # datetime, date, etc.
return obj.isoformat()
if obj is None or isinstance(obj, (str, bool)):
return obj
return str(obj) # fallback
from flask import jsonify
from datetime import datetime
@app.route('/api/health', methods=['GET'])
def health_check():
"""
健康检查接口
前端用于检测后端服务是否正常运行
"""
return jsonify({
"status": "healthy",
"message": "Backend service is running normally",
"timestamp": datetime.utcnow().isoformat() + "Z",
"service": "quant-backtest-api",
"version": "1.0.0" # 可选:你可以改成实际版本号
}), 200
@app.route('/api/backtest', methods=['GET','POST'])
def run_backtest():
try:
data = request.get_json()
print(data)
ticker = data.get('ticker', 'AMZN')
initial_capital = float(data.get('initial_capital', 100000))
start_date = data.get('start_date', '2021-01-01')
end_date = data.get('end_date', '2025-01-01')
commission_rate = float(data.get('commission_rate', 0.002))
slippage = float(data.get('slippage', 0.001))
risk_free_rate = float(data.get('risk_free_rate', 0.025))
asset_type = data.get('asset_type', 'stock')
print(f"📊 收到回测请求: {ticker}, 资金: ${initial_capital}")
if asset_type == 'stock':
eodprice = load_stock_eodprice_from_tiingo(ticker, start_date, end_date, db_params)
else:
eodprice = load_crypto_eodprice_from_tiingo(ticker, start_date, end_date, chunk_days=100,delay=0, db_connection_params=db_params, api_token=api_token)
recognizer = PatternRecognizer(ticker, eodprice)
patterns_df = recognizer.analyze_all()
#patterns_df = recognizer.get_patterns_entry(found_patterns)
df_with_signals = eodprice.join(patterns_df, how='left')
print(df_with_signals)
df_backtest, summary, chart_data = backtest_signal_strategy(
df=df_with_signals,
positive_signals_column=['positive_signal'],
negative_signals_column=['negative_signal'],
positive_signals_threshold=1,
negative_signals_threshold=1,
initial_capital=initial_capital,
commission_rate=commission_rate,
slippage=slippage
)
summary_dict = {
"ticker": ticker,
"startDate": start_date,
"endDate": end_date,
"initialCapital": summary['初始资金'],
"finalEquity": summary['最终资产'],
"totalReturn": round(summary['总收益率'] * 100, 2),
"annualReturn": round(summary['年化收益率'] * 100, 2),
"maxDrawdown": round(summary['最大回撤'] * 100, 2),
"totalTrades": summary['交易次数'],
"holdingDays": summary['持仓天数'],
"dataDays": summary['数据天数'],
"winRate": round(len([r for r in chart_data['tradeReturns'] if r > 0]) / len(chart_data['tradeReturns']) * 100, 1) if chart_data['tradeReturns'] else 0,
"sharpeRatio": summary['夏普比率'], # 示例,实际计算 (年化收益 - 无风险) / 年化波动率
"volatility": round(summary['波动率'] * 100, 2),
"commissionRate": commission_rate,
"slippage": slippage,
"riskFreeRate": risk_free_rate
}
response = {
"success": True,
"data": make_json_serializable({
**summary_dict,
**chart_data # 加入图表数据
}),
"timestamp": "2024-01-01T00:00:00Z",
"calculation_time": 1.5
}
return jsonify(response)
except Exception as e:
return jsonify({
"success": False,
"error": str(e)
}), 400
# 假設你的模型儲存結構如下:
# models/
# ├── rf/
# │ ├── rf_model_20260118_223000.pkl
# │ └── rf_model_20260119_010000.joblib
# └── xgb/
# ├── xgb_model_20260118_223000.json
# └── xgb_model_20260120_143000.json
MODEL_BASE_DIR = 'models' # 建議從 config 讀取
@app.route('/api/models/types', methods=['GET'])
def list_model_types():
"""
返回 model_basedir 下的所有子文件夹(模型类型)
如:['rf', 'xgb', 'lgb', 'catboost']
"""
try:
base_dir = 'models' # 或从 config 读取 model_basedir
if not os.path.exists(base_dir):
return jsonify({
"status": "error",
"message": f"模型基础目录不存在: {base_dir}"
}), 404
# 只返回目录(排除文件)
types = [
d for d in os.listdir(base_dir)
if os.path.isdir(os.path.join(base_dir, d)) and not d.startswith('.')
]
# 可选:排序或过滤无效目录
types.sort()
return jsonify({
"status": "success",
"types": types,
"count": len(types)
})
except Exception as e:
return jsonify({
"status": "error",
"message": str(e)
}), 500
@app.route('/api/models/weights', methods=['GET'])
def list_model_weights():
"""
根據 model_type 返回對應模型的權重檔案列表
Query Param:
- model_type: rf, xgb, lgb, catboost 等(必填)
返回格式:
{
"status": "success",
"models": [
{
"name": "xgb_model_20260118_223000.json",
"label": "2026-01-18 22:30:00",
"timestamp": "20260118_223000",
"file_size": 123456,
"created_at": "2026-01-18T22:30:00+08:00"
},
...
],
"count": 5
}
"""
model_type = request.args.get('model_type')
factor_type = request.args.get('factor_type')
if not model_type:
return jsonify({
"status": "error",
"message": "缺少 model_type 參數(例如:rf, xgb)"
}), 400
# 模型子目錄
model_dir = os.path.join(MODEL_BASE_DIR, model_type.lower(), factor_type)
if not os.path.exists(model_dir):
return jsonify({
"status": "success",
"models": [],
"count": 0,
"message": f"目錄不存在或無權重檔案: {model_dir}"
})
models = []
# 不同模型類型可能使用不同副檔名,定義模式
extensions = {
'rf': r'\.pkl$|\.joblib$',
'xgb': r'\.json$',
'lgb': r'\.txt$|\.model$',
'cat': r'\.cbm$',
# 可繼續擴充
}
pattern_ext = extensions.get(model_type.lower())
pattern = rf'^{re.escape(model_type)}_model_' + '(\d{8}_\d{6})' + pattern_ext
for filename in os.listdir(model_dir):
match = re.match(pattern, filename)
if match:
timestamp_str = match.group(1) # 如 20260118_223000
try:
dt = datetime.strptime(timestamp_str, "%Y%m%d_%H%M%S")
label = dt.strftime("%Y-%m-%d %H:%M:%S")
iso_time = dt.isoformat() + "+08:00"
file_path = os.path.join(model_dir, filename)
file_size = os.path.getsize(file_path)
models.append({
"name": filename,
"label": label,
"timestamp": timestamp_str,
"file_size": file_size,
"created_at": iso_time
})
except ValueError:
continue
# 按時間降序(最新在上)
models.sort(key=lambda x: x['timestamp'], reverse=True)
return jsonify({
"status": "success",
"models": models,
"count": len(models)
})
@app.route('/api/models/weights', methods=['DELETE'])
def delete_model_weight():
"""
删除指定的模型权重文件
Request Body:
- model_type: rf, xgb, lgb, catboost 等(必填)
- weight_name: 模型文件名(必填)
- factor_type: 因子类型(必填)
返回格式:
{
"status": "success",
"message": "模型权重文件已成功删除",
"deleted_file": "xgb_model_20260118_223000.json"
}
"""
try:
data = request.get_json()
if not data:
return jsonify({
"status": "error",
"message": "缺少请求数据"
}), 400
model_type = data.get('model_type')
weight_name = data.get('weight_name')
factor_type = data.get('factor_type')
# 验证必填参数
if not model_type:
return jsonify({
"status": "error",
"message": "缺少 model_type 參數"
}), 400
if not weight_name:
return jsonify({
"status": "error",
"message": "缺少 weight_name 參數"
}), 400
if not factor_type:
return jsonify({
"status": "error",
"message": "缺少 factor_type 參數"
}), 400
# 构建文件路径
model_dir = os.path.join(MODEL_BASE_DIR, model_type.lower(), factor_type)
file_path = os.path.join(model_dir, weight_name)
# 安全检查:确保文件在预期目录内(防止路径遍历攻击)
if not os.path.abspath(file_path).startswith(os.path.abspath(MODEL_BASE_DIR)):
return jsonify({
"status": "error",
"message": "无效的文件路径"
}), 400
# 检查文件是否存在
if not os.path.exists(file_path):
return jsonify({
"status": "error",
"message": f"文件不存在: {weight_name}"
}), 404
# 检查是否为文件(而非目录)
if not os.path.isfile(file_path):
return jsonify({
"status": "error",
"message": f"路径不是文件: {weight_name}"
}), 400
# 删除文件
try:
os.remove(file_path)
return jsonify({
"status": "success",
"message": "模型权重文件已成功删除",
"deleted_file": weight_name,
"model_type": model_type,
"factor_type": factor_type
})
except OSError as e:
return jsonify({
"status": "error",
"message": f"删除文件失败: {str(e)}"
}), 500
except Exception as e:
return jsonify({
"status": "error",
"message": str(e)
}), 500
@app.route('/api/latest_patterns')
def get_latest_patterns():
try:
# 美股預設列表
DEFAULT_STOCK_TICKERS = [
'NVDA', 'AAPL', 'GOOGL', 'MSFT', 'AMZN', 'META', 'TSLA', 'AVGO',
'BRK-B', 'LLY', 'WMT', 'JPM', 'V', 'ORCL', 'MA', 'JNJ', 'XOM',
'PLTR', 'BAC', 'NFLX'
]
# 加密貨幣主流 USDT 交易對(2026年常見高流動性對,可根據實際情況調整)
DEFAULT_CRYPTO_PAIRS = [
'BTCUSD', 'ETHUSD', 'SOLUSD', 'BNBUSD', 'XRPUSD',
'ADAUSD', 'DOGEUSD', 'TRXUSD', 'SUIUSD', 'LINKUSD',
'AVAXUSD', 'TONUSD', 'PEPEUSD', 'BCHUSD', 'DOTUSD'
]
# 1. 獲取參數
tickers_param = request.args.get('tickers')
asset_type = request.args.get('asset_type', 'stock').lower()
realtime_mode = request.args.get('realtime_mode', 'false')
# 決定使用哪個預設列表
if tickers_param:
tickers = [t.strip().upper() for t in tickers_param.split(',') if t.strip()]
else:
if asset_type == 'crypto':
tickers = DEFAULT_CRYPTO_PAIRS
else:
tickers = DEFAULT_STOCK_TICKERS
# 2. 設定日期範圍(加密貨幣通常交易天數更多,可適當延長)
now = pd.Timestamp.now(tz='UTC').tz_convert('US/Eastern').normalize().tz_localize(None)
if asset_type == 'crypto':
# 加密貨幣建議取更長時間(很多幣種歷史較短,但可取最大可用)
days_back = 720 # 約5.5年
else:
days_back = 1500 # 原有約4年多
START_DATE = (now - pd.Timedelta(days=days_back)).strftime('%Y-%m-%d')
END_DATE = now.strftime('%Y-%m-%d')
recent_patterns = []
# 3. 根據 asset_type 選擇不同的價格加載函數
for TICKER in tickers:
try:
if asset_type == 'crypto':
# 假設你已經有這個函數,且返回格式與 stock 一致(OHLCV DataFrame)
price_df = load_crypto_eodprice_from_tiingo(TICKER, START_DATE, END_DATE, chunk_days=100,delay=0, db_connection_params=db_params, api_token=api_token)
else:
price_df = load_stock_eodprice_from_tiingo(TICKER, START_DATE, END_DATE, db_params)
if price_df.empty:
print(f"No data for {TICKER} ({asset_type})")
continue
price_df = price_df[['close']]
# 4. 形態識別(假設 PatternRecognizer 能處理兩種格式)
if realtime_mode == 'true':
if asset_type == 'stock':
realtime_price = load_stock_realtime_price(TICKER)
price_df = pd.concat([price_df, realtime_price], axis=0)
recognizer = PatternRecognizer(TICKER, price_df)
found_patterns = recognizer.analyze_all()
if not found_patterns:
continue
patterns_df = recognizer.get_patterns_entry(found_patterns)
df_with_signals = price_df.join(patterns_df, how='left')
# 只取最近20天有信號的
last_20_signals = df_with_signals[['close', 'pattern', 'signal']].tail(20).dropna()
if not last_20_signals.empty:
last_20_signals['ticker'] = TICKER
last_20_signals['asset_type'] = asset_type # 可選:回傳資產類型
recent_patterns.append(last_20_signals)
except Exception as e:
print(f"處理 {TICKER} 時出錯: {str(e)}")
continue
# 5. 合併結果
if recent_patterns:
result_df = pd.concat(recent_patterns, axis=0).sort_index().reset_index()
if asset_type == 'crypto':
result_df['date'] = result_df['date'].dt.strftime('%Y-%m-%d')
result_df['index'] = result_df['date']
else:
result_df['date'] = result_df['index'].dt.strftime('%Y-%m-%d') # 統一日期格式
return jsonify(result_df.to_dict(orient='records'))
else:
return jsonify([])
except Exception as e:
return jsonify({
"success": False,
"error": str(e)
}), 400
@app.route('/api/patterns/<ticker>')
def get_stock_patterns(ticker):
END_DATE = pd.Timestamp.now(tz='UTC').tz_convert('US/Eastern').normalize().tz_localize(None)
START_DATE = END_DATE - pd.Timedelta(days=200)
END_DATE = END_DATE.strftime('%Y-%m-%d')
START_DATE = START_DATE.strftime('%Y-%m-%d')
recent_patterns = []
stock_eodprice = load_stock_eodprice_from_tiingo(ticker, START_DATE, END_DATE,db_params)
recognizer = PatternRecognizer(ticker, stock_eodprice)
found_patterns = recognizer.analyze_all()
patterns_df = recognizer.get_patterns_entry(found_patterns)
df_with_signals = stock_eodprice.join(patterns_df, how='left')
last_20_detected_signals = df_with_signals[['pattern', 'signal']].tail(20).dropna()
if last_20_detected_signals is not None:
last_20_detected_signals['ticker'] = ticker
recent_patterns.append(last_20_detected_signals)
recent_patterns = pd.concat(recent_patterns, axis=0).sort_index().reset_index()
return jsonify(recent_patterns.to_dict(orient='records'))
@app.route('/api/stock-news-sentiment',methods=['POST'])
def stock_news_sentiment():
data = request.get_json()
ticker = data.get('ticker')
start_date = data.get('start_date')
if not ticker:
return jsonify({"error": "缺少股票代码参数"}), 400
try:
# 调用你的新闻获取 + 情绪分析函数
df = fetch_company_news_batched(
symbol=ticker,
start_date=start_date,
batch_days=5, # 每30天一批
delay=1.2 # 免费账号安全延迟
)
df = add_finbert_sentiment_to_df(df)
result = calculate_ticker_overall_sentiment(df)
'''news = result['news_list']
extreme = news[news['sentiment_finbert'].abs() > 0.9].sort_values('days_ago').iloc[:50]
extreme = summarize_news(extreme)
extreme = extreme.to_dict(orient='records')
result['extreme_news'] = extreme'''
response = {
"success": True,
"data": make_json_serializable({**result}),
"timestamp": datetime.now(pytz.UTC).isoformat(),
"calculation_time": 1.5
}
return jsonify(response)
except Exception as e:
return jsonify({"error": str(e)}), 500
def analyze_sentiment_from_rss(rss_urls, num_entries_per_feed=100, top_n_titles=5):
"""
从多个 RSS 源抓取新闻,进行情感分析,并返回结果和最近新闻标题。
参数:
rss_urls (list): RSS 链接列表
num_entries_per_feed (int): 每个 RSS 源最多抓取的条目数
top_n_titles (int): 返回的最近新闻标题数量(默认 5 条)
返回:
dict: 包含情感指标和最近新闻的字典
"""
all_sentiments = [] # 存储所有文章的情感极性
recent_entries = [] # 存储最新的 top_n_titles 条新闻(带标题、链接、时间)
total_analyzed = 0
for url in rss_urls:
print(f"Fetching RSS: {url}")
feed = feedparser.parse(url)
if not hasattr(feed, 'entries') or len(feed.entries) == 0:
print(f" → No entries or failed to parse: {url}")
continue
print(f" → Available entries: {len(feed.entries)}")
entries = feed.entries[:num_entries_per_feed]
for entry in entries:
if total_analyzed >= 500: # 安全上限,防止过载
break
# 构建分析文本:标题 + 摘要/描述
text = entry.title.strip()
if 'description' in entry and entry.description:
text += " " + entry.description
elif 'summary' in entry and entry.summary:
text += " " + entry.summary
# 清理 HTML 标签
text = re.sub('<[^<]+?>', '', text).strip()
if not text:
continue
# 情感分析
blob = TextBlob(text)
polarity = blob.sentiment.polarity # -1.0 ~ +1.0
all_sentiments.append(polarity)
# 收集最近的新闻条目(只取前 top_n_titles 条,全局最早的最新文章)
if len(recent_entries) < top_n_titles:
link = entry.get('link', '').strip()
published = entry.get('published', entry.get('updated', '未知时间'))
recent_entries.append({
'title': entry.title.strip(),
'link': link if link else None,
'published': published
})
total_analyzed += 1
if total_analyzed >= 500:
break
# 如果没有抓到任何文章
if total_analyzed == 0 or len(all_sentiments) == 0:
return {
'average_polarity': 0.0,
'sentiment_label': 'Neutral',
'analyzed_entries': 0,
'positive_percentage': 0.0,
'negative_percentage': 0.0,
'recent_titles': []
}
# 计算平均极性
average_polarity = statistics.mean(all_sentiments)
# 统计正负面文章比例(阈值可调)
positive_count = sum(1 for p in all_sentiments if p > 0.05)
negative_count = sum(1 for p in all_sentiments if p < -0.05)
positive_percentage = (positive_count / total_analyzed) * 100
negative_percentage = (negative_count / total_analyzed) * 100
# 情感强度标签
abs_polarity = abs(average_polarity)
if abs_polarity > 0.3:
strength = 'Strongly'
elif abs_polarity > 0.15:
strength = 'Moderately'
elif abs_polarity > 0.05:
strength = 'Mildly'
else:
strength = ''
if average_polarity > 0.05:
label = f"{strength} Positive".strip()
elif average_polarity < -0.05:
label = f"{strength} Negative".strip()
else:
label = 'Neutral'
return {
'average_polarity': round(average_polarity, 6),
'sentiment_label': label,
'analyzed_entries': total_analyzed,
'positive_percentage': round(positive_percentage, 2),
'negative_percentage': round(negative_percentage, 2),
'recent_titles': recent_entries # 列表,每项含 title, link, published
}
@app.route('/api/sentiment/stock')
def analyze_us_stock_sentiment():
try:
stock_rss_urls = [
'https://www.cnbc.com/id/100003114/device/rss/rss.html', # CNBC Top News
'https://seekingalpha.com/feed.xml', # Seeking Alpha
'https://feeds.reuters.com/reuters/businessNews', # Reuters Business
'https://finance.yahoo.com/news/rssindex', # Yahoo Finance
'https://rss.nytimes.com/services/xml/rss/nyt/Business.xml', # NY Times Business
'https://www.marketwatch.com/rss/topstories' # MarketWatch
]
print("=== US Stock Market Sentiment Analysis ===\n")
stock_sentiment = analyze_sentiment_from_rss(stock_rss_urls, num_entries_per_feed=100)
print(f"Overall Sentiment: {stock_sentiment['sentiment_label']}")
print(f"Average Polarity: {stock_sentiment['average_polarity']:.4f} (-1 very negative → +1 very positive)")
print(f"Positive articles: {stock_sentiment['positive_percentage']:.1f}%")
print(f"Negative articles: {stock_sentiment['negative_percentage']:.1f}%")
print(f"Total Entries Analyzed: {stock_sentiment['analyzed_entries']}\n")
sentiment_dict = {
"type" : "US stock",
"average_polarity" : stock_sentiment['average_polarity'],
"positive_articles_num" : stock_sentiment['positive_percentage'],
"negative_articles_num" : stock_sentiment['negative_percentage'],
"total_articles_num" : stock_sentiment['analyzed_entries']
}
response = {
"success": True,
"data": sentiment_dict,
"timestamp": datetime.now(pytz.UTC).isoformat(),
"calculation_time": 1.5
}
return response
except Exception as e:
return jsonify({
"success": False,
"error": str(e)
}), 400
@app.route('/api/sentiment/crypto')
def analyze_crypto_sentiment():
try:
crypto_rss_urls = [
'https://cointelegraph.com/rss', # Cointelegraph (high volume)
'https://www.coindesk.com/arc/outboundfeeds/rss/', # CoinDesk
'https://news.bitcoin.com/feed/', # Bitcoin.com News
'https://www.newsbtc.com/feed/', # NewsBTC
'https://cryptopotato.com/feed/' # CryptoPotato
]
print("=== Cryptocurrency Market Sentiment Analysis ===\n")
stock_sentiment = analyze_sentiment_from_rss(crypto_rss_urls, num_entries_per_feed=100)
print(f"Overall Sentiment: {stock_sentiment['sentiment_label']}")
print(f"Average Polarity: {stock_sentiment['average_polarity']:.4f} (-1 very negative → +1 very positive)")
print(f"Positive articles: {stock_sentiment['positive_percentage']:.1f}%")
print(f"Negative articles: {stock_sentiment['negative_percentage']:.1f}%")
print(f"Total Entries Analyzed: {stock_sentiment['analyzed_entries']}\n")
sentiment_dict = {
"type" : "Crypto",
"average_polarity" : stock_sentiment['average_polarity'],
"positive_articles_num" : stock_sentiment['positive_percentage'],
"negative_articles_num" : stock_sentiment['negative_percentage'],
"total_articles_num" : stock_sentiment['analyzed_entries']
}
response = {
"success": True,
"data": sentiment_dict,
"timestamp": datetime.now(pytz.UTC).isoformat(),
"calculation_time": 1.5
}
return response
except Exception as e:
return jsonify({
"success": False,
"error": str(e)
}), 400
# Note: Add these new routes to your existing Flask app.py or API file.
# Assume the analyze_sentiment_from_rss function and other dependencies (feedparser, TextBlob, etc.) are already defined.
# I've selected English-language RSS feeds for better compatibility with TextBlob (which works best with English).
# If you need Chinese-language analysis, consider switching to a library like SnowNLP for sentiment.
from flask import jsonify # If not already imported
@app.route('/api/sentiment/hkstock')
def analyze_hk_stock_sentiment():
try:
hk_rss_urls = [
'https://www.scmp.com/rss/35/feed', # SCMP Business
'https://www.scmp.com/rss/37/feed', # SCMP Economy
'https://www.hkex.com.hk/Services/RSS-Feeds/News-Releases?sc_lang=en', # HKEX News Releases
'https://www.aastocks.com/en/stocks/news/aafn-news/rss' # AAStocks News
# Add more if needed, e.g., 'https://www.thestandard.com.hk/rss/finance.xml' (if valid)
]
print("=== Hong Kong Stock Market Sentiment Analysis ===\n")
hk_sentiment = analyze_sentiment_from_rss(hk_rss_urls, num_entries_per_feed=100)
print(f"Overall Sentiment: {hk_sentiment['sentiment_label']}")
print(f"Average Polarity: {hk_sentiment['average_polarity']:.4f} (-1 very negative → +1 very positive)")
print(f"Positive articles: {hk_sentiment['positive_percentage']:.1f}%")
print(f"Negative articles: {hk_sentiment['negative_percentage']:.1f}%")
print(f"Total Entries Analyzed: {hk_sentiment['analyzed_entries']}\n")
sentiment_dict = {
"type": "HK stock",
"average_polarity": hk_sentiment['average_polarity'],
"positive_articles_num": hk_sentiment['positive_percentage'],
"negative_articles_num": hk_sentiment['negative_percentage'],
"total_articles_num": hk_sentiment['analyzed_entries']
}
response = {
"success": True,
"data": sentiment_dict,
"timestamp": datetime.now(pytz.UTC).isoformat(), # Update to current time if desired
"calculation_time": 1.5 # Placeholder; calculate actual time if needed
}
return response
except Exception as e:
return jsonify({
"success": False,
"error": str(e)
}), 400
@app.route('/api/sentiment/ashare')
def analyze_a_share_sentiment():
try:
a_rss_urls = [
'http://www.chinadaily.com.cn/rss/business_rss.xml', # China Daily Business
'https://www.caixinglobal.com/rss/', # Caixin Global
'https://www.china-briefing.com/feed/', # China Briefing
'https://www.chinabankingnews.com/feed', # China Banking News
# Add more if needed, e.g., 'https://www.scmp.com/rss/91/feed' for broader China news
]
print("=== A-Share Market Sentiment Analysis ===\n")
a_sentiment = analyze_sentiment_from_rss(a_rss_urls, num_entries_per_feed=100)
print(f"Overall Sentiment: {a_sentiment['sentiment_label']}")
print(f"Average Polarity: {a_sentiment['average_polarity']:.4f} (-1 very negative → +1 very positive)")
print(f"Positive articles: {a_sentiment['positive_percentage']:.1f}%")
print(f"Negative articles: {a_sentiment['negative_percentage']:.1f}%")
print(f"Total Entries Analyzed: {a_sentiment['analyzed_entries']}\n")
sentiment_dict = {
"type": "A share",
"average_polarity": a_sentiment['average_polarity'],
"positive_articles_num": a_sentiment['positive_percentage'],
"negative_articles_num": a_sentiment['negative_percentage'],
"total_articles_num": a_sentiment['analyzed_entries']
}
response = {
"success": True,
"data": sentiment_dict,
"timestamp": datetime.now(pytz.UTC).isoformat(), # Update to current time if desired
"calculation_time": 1.5 # Placeholder
}
return response
except Exception as e:
return jsonify({
"success": False,
"error": str(e)
}), 400
@app.route('/api/multifactor/run', methods=['POST'])
def multifactor_run():
try:
data = request.get_json()
if not data:
return jsonify({"success": False, "message": "No JSON data received"}), 400
# 参数提取与默认值
load_fromcsv = data.get('load_fromcsv', True)
train_model = data.get('train_model', True)
train_startdate = data.get('train_startdate')
test_startdate = data.get('test_startdate')
train_enddate = data.get('train_enddate') # 可为 None
backtest_startdate = data.get('backtest_startdate')
backtest_enddate = data.get('backtest_enddate')
pick_stockmode = data.get('pick_stockmode', False)
disable_factor_process = data.get('disable_factor_process', True)
rebalance_period = int(data.get('rebalance_period', 5))
top_n = int(data.get('top_n', 30))
selected_factor = data.get('selected_factor')
selected_model = data.get('selected_model')
selected_model_weight = data.get('selected_model_weight')
balance_weight = data.get('balance_weight')
pick_startdate = data.get('pick_startdate')
pick_stock = data.get('pick_stock')
train_selected_model = data.get('train_selected_model')
print(f"Received request: {data}")
# 调用你的核心函数(关闭 verbose,避免日志刷屏)
result = train_and_backtest_multifactormodel(
load_fromcsv=load_fromcsv,
disable_factor_process=disable_factor_process,
train_model=train_model,
train_startdate=train_startdate,
test_startdate=test_startdate,
train_enddate=train_enddate or '2019-01-01', # 如果为空给个旧日期
backtest_startdate=backtest_startdate,
backtest_enddate=backtest_enddate,
balance_weight=balance_weight,
pick_stockmode=pick_stockmode,
pick_startdate = pick_startdate,
pick_stock = pick_stock,
rebalance_period=rebalance_period,
top_n=top_n,
selected_model=selected_model,
selected_model_weight=selected_model_weight,
selected_factor=selected_factor,
train_selected_model=train_selected_model,
verbose=False # API 中不打印太多日志
)
# 根据模式返回不同结构的数据
if pick_stockmode:
# 选股模式:返回股票列表
top_stocks_list = result['stocks'].to_dict(orient='records')
response = {
"success": True,
"mode": "pick_stock",
"date": result['date'],
"top_stocks": top_stocks_list
}
else:
summary = {}
for k, v in result.items():
if k in ['selected', 'turnover', 'daily_returns', 'cumulative_returns']:
continue
if isinstance(v, (int, float, str, bool)):
summary[k] = v
else:
try:
summary[k] = float(v) if isinstance(v, (np.integer, np.floating)) else str(v)
except:
summary[k] = str(v)
# 处理换手率
if 'turnover' in result and not result['turnover'].empty:
avg_turnover = result['turnover'].iloc[1:]['One_Way_Turnover_%'].mean()
summary['average_one_way_turnover_%'] = round(float(avg_turnover), 2)
# 处理净值曲线(关键!)
equity_curve = []
if 'cumulative_returns' in result:
cum_df = result['cumulative_returns'].copy()
cum_df = cum_df.reset_index()
cum_df['date'] = cum_df['date'].astype(str) # 确保日期是字符串
# 假设有 'portfolio' 和可选的 'benchmark' 列
for _, row in cum_df.iterrows():
equity_curve.append({
"date": row['date'],
"portfolio": round(float(row.get('portfolio', row.get('cumulative_return', 0))), 6)
# 如果有基准,可加 "benchmark": ...
})
response = {
"success": True,
"mode": "backtest",
"summary": summary,
"equity_curve": equity_curve if 'equity_curve' in locals() else None,
"selected": result['selected'],
}
return jsonify(response)
except Exception as e:
print("Error:", str(e))
traceback.print_exc()
return jsonify({
"success": False,
"message": str(e)
}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)