mns-scheduler 1.3.1.7__py3-none-any.whl → 1.3.1.8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of mns-scheduler might be problematic. Click here for more details.

Files changed (59) hide show
  1. mns_scheduler/company_info/em_stock_info/sync_em_stock_info_sync.py +5 -4
  2. mns_scheduler/trade/auto_login/trader_auto_service.py +1 -1
  3. mns_scheduler/trade/sync_position_api.py +2 -2
  4. mns_scheduler/zt/zt_pool/em_zt_pool_sync_api.py +1 -1
  5. mns_scheduler/zz_task/data_sync_task.py +2 -43
  6. {mns_scheduler-1.3.1.7.dist-info → mns_scheduler-1.3.1.8.dist-info}/METADATA +1 -1
  7. {mns_scheduler-1.3.1.7.dist-info → mns_scheduler-1.3.1.8.dist-info}/RECORD +9 -59
  8. mns_scheduler/baidu/__init__.py +0 -7
  9. mns_scheduler/baidu/baidu_yun_pan_handle_service.py +0 -122
  10. mns_scheduler/extraIncome/__init__.py +0 -7
  11. mns_scheduler/extraIncome/a_stock/__init__.py +0 -7
  12. mns_scheduler/extraIncome/a_stock/one_minute/__init__.py +0 -7
  13. mns_scheduler/extraIncome/a_stock/one_minute/common/__init__.py +0 -7
  14. mns_scheduler/extraIncome/a_stock/one_minute/common/db_create_index.py +0 -9
  15. mns_scheduler/extraIncome/a_stock/one_minute/common/symbol_handle_util.py +0 -41
  16. mns_scheduler/extraIncome/a_stock/one_minute/etf/__init__.py +0 -7
  17. mns_scheduler/extraIncome/a_stock/one_minute/etf/etf_one_minute_sync_task.py +0 -87
  18. mns_scheduler/extraIncome/a_stock/one_minute/index/__init__.py +0 -7
  19. mns_scheduler/extraIncome/a_stock/one_minute/index/main_index_sync_task.py +0 -74
  20. mns_scheduler/extraIncome/a_stock/one_minute/kzz/__init__.py +0 -7
  21. mns_scheduler/extraIncome/a_stock/one_minute/kzz/kzz_one_minute_sync_task.py +0 -87
  22. mns_scheduler/extraIncome/a_stock/one_minute/one_minute_sync_task.py +0 -34
  23. mns_scheduler/extraIncome/a_stock/one_minute/stock/__init__.py +0 -7
  24. mns_scheduler/extraIncome/a_stock/one_minute/stock/stock_one_minute_sync_task.py +0 -89
  25. mns_scheduler/extraIncome/a_stock/one_minute/upload/__init__.py +0 -7
  26. mns_scheduler/extraIncome/a_stock/one_minute/upload/etf_upload_to_baidu_task.py +0 -95
  27. mns_scheduler/extraIncome/a_stock/one_minute/upload/index_upload_to_baidu_task.py +0 -87
  28. mns_scheduler/extraIncome/a_stock/one_minute/upload/kzz_upload_to_baidu_task.py +0 -95
  29. mns_scheduler/extraIncome/a_stock/one_minute/upload/stock_upload_to_baidu_task.py +0 -85
  30. mns_scheduler/extraIncome/hk/__init__.py +0 -7
  31. mns_scheduler/extraIncome/hk/hk_stock_qfq_daily_k_line.py +0 -102
  32. mns_scheduler/extraIncome/us/__init__.py +0 -7
  33. mns_scheduler/extraIncome/us/daily/__init__.py +0 -7
  34. mns_scheduler/extraIncome/us/daily/us_stock_qfq_daily_k_line.py +0 -103
  35. mns_scheduler/extraIncome/us/one_minute/__init__.py +0 -7
  36. mns_scheduler/extraIncome/us/one_minute/api/__init__.py +0 -0
  37. mns_scheduler/extraIncome/us/one_minute/api/alpha_vantage_api.py +0 -34
  38. mns_scheduler/extraIncome/us/one_minute/api/em_us_one_minute_api.py +0 -139
  39. mns_scheduler/extraIncome/us/one_minute/api/stock_etf_info_api.py +0 -68
  40. mns_scheduler/extraIncome/us/one_minute/api/y_finance_api.py +0 -63
  41. mns_scheduler/extraIncome/us/one_minute/etf/__init__.py +0 -7
  42. mns_scheduler/extraIncome/us/one_minute/stock/__init__.py +0 -7
  43. mns_scheduler/extraIncome/us/one_minute/stock/down_load/__init__.py +0 -7
  44. mns_scheduler/extraIncome/us/one_minute/stock/down_load/etf/__init__.py +0 -11
  45. mns_scheduler/extraIncome/us/one_minute/stock/down_load/etf/down_load_ETF_his_2024.py +0 -152
  46. mns_scheduler/extraIncome/us/one_minute/stock/down_load/etf/handle_down_load_fail_ETF.py +0 -67
  47. mns_scheduler/extraIncome/us/one_minute/stock/down_load/fail/__init__.py +0 -7
  48. mns_scheduler/extraIncome/us/one_minute/stock/down_load/fail/handle_fail_data.py +0 -161
  49. mns_scheduler/extraIncome/us/one_minute/stock/down_load/rename/__init__.py +0 -7
  50. mns_scheduler/extraIncome/us/one_minute/stock/down_load/rename/rename_stock.py +0 -18
  51. mns_scheduler/extraIncome/us/one_minute/stock/down_load/stock/__init__.py +0 -7
  52. mns_scheduler/extraIncome/us/one_minute/stock/down_load/stock/down_load_stock_his_01.py +0 -203
  53. mns_scheduler/extraIncome/us/one_minute/stock/down_load/stock/down_load_stock_his_02.py +0 -202
  54. mns_scheduler/extraIncome/us/one_minute/stock/down_load/stock/down_load_stock_his_2025.py +0 -144
  55. mns_scheduler/extraIncome/us/one_minute/stock/now/__init__.py +0 -7
  56. mns_scheduler/extraIncome/us/one_minute/stock/now/us_etf_one_minute_sync.py +0 -52
  57. mns_scheduler/extraIncome/us/one_minute/stock/now/us_stock_one_minute_sync.py +0 -141
  58. {mns_scheduler-1.3.1.7.dist-info → mns_scheduler-1.3.1.8.dist-info}/WHEEL +0 -0
  59. {mns_scheduler-1.3.1.7.dist-info → mns_scheduler-1.3.1.8.dist-info}/top_level.txt +0 -0
@@ -1,152 +0,0 @@
1
- import sys
2
- import os
3
-
4
- file_path = os.path.abspath(__file__)
5
- end = file_path.index('mns') + 17
6
- project_path = file_path[0:end]
7
- sys.path.append(project_path)
8
- import mns_common.component.em.em_stock_info_api as em_stock_info_api
9
- from loguru import logger
10
- import time
11
- import mns_common.utils.data_frame_util as data_frame_util
12
- from mns_common.db.MongodbUtil import MongodbUtil
13
- from mns_common.db.v2.MongodbUtilV2 import MongodbUtilV2
14
- import mns_scheduler.extraIncome.a_stock.one_minute.common.db_create_index as db_create_index
15
- import mns_common.constant.extra_income_db_name as extra_income_db_name
16
- import mns_scheduler.extraIncome.us.one_minute.api.alpha_vantage_api as alpha_vantage_api
17
- import pandas as pd
18
- import math
19
- from pathlib import Path
20
-
21
- mongodb_util_27017 = MongodbUtil('27017')
22
- mongodbUtilV2_27019 = MongodbUtilV2('27019', extra_income_db_name.EXTRA_INCOME)
23
- from datetime import datetime
24
-
25
- no_choose_symbol = ['FNGA', 'MSTU', 'SPYU']
26
-
27
-
28
- def sync_us_stock_one_minute(now_year, now_month):
29
- real_time_quotes_all_us = em_stock_info_api.get_us_stock_info()
30
- real_time_quotes_all_us_stocks = real_time_quotes_all_us.loc[real_time_quotes_all_us['flow_mv'] == 0]
31
- real_time_quotes_all_us_stocks = real_time_quotes_all_us_stocks.sort_values(by=['amount'], ascending=False)
32
- real_time_quotes_all_us_stocks = real_time_quotes_all_us_stocks.loc[~real_time_quotes_all_us_stocks['symbol'].isin(no_choose_symbol)]
33
- real_time_quotes_all_us_stocks = real_time_quotes_all_us_stocks.loc[
34
- real_time_quotes_all_us_stocks['amount'] >= 50000000]
35
-
36
- path = r'F:\us_etf\one_minute\{}'.format(now_year)
37
- if not os.path.exists(path):
38
- os.makedirs(path)
39
-
40
- path = path + '\{}'.format(now_month)
41
- if not os.path.exists(path):
42
- os.makedirs(path)
43
- stock_name_list = find_exist_file(path)
44
- real_time_quotes_all_us_stocks = real_time_quotes_all_us_stocks.loc[
45
- ~(real_time_quotes_all_us_stocks['symbol'].isin(stock_name_list))]
46
- for stock_one in real_time_quotes_all_us_stocks.itertuples():
47
-
48
- try:
49
- symbol = stock_one.symbol
50
- # simple_symbol = int(stock_one.simple_symbol)
51
- # code = str(simple_symbol) + '.' + symbol
52
- list_date = stock_one.list_date
53
-
54
- if not math.isnan(list_date):
55
- list_date = str(stock_one.list_date)
56
- list_date_year = int(list_date[0:4])
57
- list_month = int(list_date[4:6])
58
- now_month_int = int(now_month[5:7])
59
- if (list_date_year > now_year) or ((list_date_year == now_year) and (list_month > now_month_int)):
60
- continue
61
- now_date = datetime.now()
62
- if net_work_check(now_date):
63
- # 休眠 6分钟
64
- time.sleep(5 * 60)
65
-
66
- df = alpha_vantage_api.sync_one_minute_data(symbol, now_month)
67
- df = df.fillna(0)
68
- df['time'] = df['time'].dt.strftime('%Y-%m-%d %H:%M:%S')
69
- df['str_day'] = df['time'].str.slice(0, 10)
70
- df['minute'] = df['time'].str.slice(11, 19)
71
- df['_id'] = symbol + "_" + df['time']
72
- df['symbol'] = symbol
73
- df_export_df = df.copy()
74
- export_original_data(df_export_df, symbol, path)
75
- except BaseException as e:
76
- time.sleep(1)
77
- fail_dict = {
78
- '_id': symbol + '_' + now_month,
79
- 'type': "ETF",
80
- 'path': path,
81
- 'symbol': symbol,
82
- 'now_year': now_year,
83
- 'now_month': now_month
84
- }
85
- fail_df = pd.DataFrame(fail_dict, index=[1])
86
-
87
- mongodb_util_27017.save_mongo(fail_df, 'us_stock_one_minute_down_load_fail')
88
- logger.error("同步股票分钟数据出现异常:,{},{},{}", e, symbol, now_month)
89
- logger.info("同步股票分钟票数据完成:{},{}", stock_one.symbol, stock_one.name)
90
-
91
-
92
- def export_original_data(df, symbol, path):
93
- file_name = path + '\{}.csv'.format(symbol)
94
- if data_frame_util.is_not_empty(df):
95
- df = df.dropna(subset=['_id'])
96
- del df['str_day']
97
- del df['minute']
98
- del df['_id']
99
- del df['symbol']
100
- df.to_csv(file_name, index=False, encoding='utf-8')
101
-
102
-
103
- def net_work_check(now_date):
104
- hour = now_date.hour
105
- minute = now_date.minute
106
- if hour == 7 and minute == 34:
107
- return True
108
- elif hour == 9 and minute == 59:
109
- return True
110
- elif hour == 10 and minute == 29:
111
- return True
112
- elif hour == 10 and minute == 59:
113
- return True
114
- elif hour == 12 and minute == 49:
115
- return True
116
- elif hour == 13 and minute == 28:
117
- return True
118
- elif hour == 13 and minute == 58:
119
- return True
120
- elif hour == 14 and minute == 28:
121
- return True
122
- elif hour == 15 and minute == 1:
123
- return True
124
- else:
125
- return False
126
-
127
-
128
- def sync_by_year(begin_year):
129
- begin_month = 12
130
- while begin_month > 0:
131
- if begin_month < 10:
132
- str_month = '0' + str(begin_month)
133
- else:
134
- str_month = str(begin_month)
135
- str_month = str(begin_year) + '-' + str_month
136
- sync_us_stock_one_minute(begin_year, str_month)
137
- begin_month = begin_month - 1
138
- logger.error("同步完成月份:{}", str_month)
139
-
140
-
141
- def find_exist_file(folder_path):
142
- if not os.path.exists(folder_path):
143
- logger.error("错误:目录不存在:{}", folder_path)
144
- else:
145
- folder_path = Path(folder_path)
146
- stock_names = [f.stem for f in folder_path.glob("*.csv")]
147
- return stock_names
148
-
149
-
150
- if __name__ == '__main__':
151
- # k_line_df = query_k_line('TSLA')
152
- sync_by_year(2024)
@@ -1,67 +0,0 @@
1
- import sys
2
- import os
3
-
4
- file_path = os.path.abspath(__file__)
5
- end = file_path.index('mns') + 17
6
- project_path = file_path[0:end]
7
- sys.path.append(project_path)
8
-
9
- from loguru import logger
10
- import mns_common.utils.data_frame_util as data_frame_util
11
- from mns_common.db.MongodbUtil import MongodbUtil
12
- from mns_common.db.v2.MongodbUtilV2 import MongodbUtilV2
13
- import mns_common.constant.extra_income_db_name as extra_income_db_name
14
- import mns_scheduler.extraIncome.us.one_minute.api.alpha_vantage_api as alpha_vantage_api
15
-
16
- mongodb_util_27017 = MongodbUtil('27017')
17
- mongodbUtilV2_27019 = MongodbUtilV2('27019', extra_income_db_name.EXTRA_INCOME)
18
-
19
-
20
- def handle_fail_data():
21
- query = {"type": "ETF", }
22
- fail_df = mongodb_util_27017.find_query_data('us_stock_one_minute_down_load_fail', query)
23
-
24
- for stock_one in fail_df.itertuples():
25
- try:
26
-
27
- now_year = stock_one.now_year
28
- now_month = stock_one.now_month
29
- symbol = stock_one.symbol
30
- id_key = symbol + '_' + now_month
31
- path = r'F:\us_etf\one_minute\{}'.format(now_year)
32
- if not os.path.exists(path):
33
- os.makedirs(path)
34
-
35
- path = path + '\{}'.format(now_month)
36
- if not os.path.exists(path):
37
- os.makedirs(path)
38
-
39
- df = alpha_vantage_api.sync_one_minute_data(symbol, now_month)
40
- df = df.fillna(0)
41
- df['time'] = df['time'].dt.strftime('%Y-%m-%d %H:%M:%S')
42
- df['str_day'] = df['time'].str.slice(0, 10)
43
- df['minute'] = df['time'].str.slice(11, 19)
44
- df['_id'] = symbol + "_" + df['time']
45
- df['symbol'] = symbol
46
- df_export_df = df.copy()
47
- export_original_data(df_export_df, symbol, path)
48
- logger.info("同步股票分钟票数据完成:{}", stock_one.symbol)
49
- query = {"_id": id_key}
50
- mongodb_util_27017.remove_data(query, 'us_stock_one_minute_down_load_fail')
51
- except BaseException as e:
52
- logger.error("同步股票分钟数据出现异常:,{},{},{}", e, symbol, now_month)
53
-
54
-
55
- def export_original_data(df, symbol, path):
56
- file_name = path + '\{}.csv'.format(symbol)
57
- if data_frame_util.is_not_empty(df):
58
- df = df.dropna(subset=['_id'])
59
- del df['str_day']
60
- del df['minute']
61
- del df['_id']
62
- del df['symbol']
63
- df.to_csv(file_name, index=False, encoding='utf-8')
64
-
65
-
66
- if __name__ == '__main__':
67
- handle_fail_data()
@@ -1,7 +0,0 @@
1
- import sys
2
- import os
3
-
4
- file_path = os.path.abspath(__file__)
5
- end = file_path.index('mns') + 16
6
- project_path = file_path[0:end]
7
- sys.path.append(project_path)
@@ -1,161 +0,0 @@
1
- import sys
2
- import os
3
-
4
- file_path = os.path.abspath(__file__)
5
- end = file_path.index('mns') + 16
6
- project_path = file_path[0:end]
7
- sys.path.append(project_path)
8
- import mns_scheduler.extraIncome.us.one_minute.stock.down_load.rename.rename_stock as rename_stock
9
- from loguru import logger
10
- import time
11
- import mns_common.utils.data_frame_util as data_frame_util
12
- from mns_common.db.MongodbUtil import MongodbUtil
13
- from mns_common.db.v2.MongodbUtilV2 import MongodbUtilV2
14
- import mns_common.constant.extra_income_db_name as extra_income_db_name
15
- import mns_scheduler.extraIncome.us.one_minute.api.alpha_vantage_api as alpha_vantage_api
16
- import pandas as pd
17
-
18
- mongodb_util_27017 = MongodbUtil('27017')
19
- mongodbUtilV2_27019 = MongodbUtilV2('27019', extra_income_db_name.EXTRA_INCOME)
20
- import threading
21
-
22
-
23
- def handle_fail_sync():
24
- begin_year = 2025
25
- end_year = 2020
26
- while begin_year > end_year:
27
- query = {"now_year": {"$in": [begin_year, str(begin_year)]}}
28
- us_fail_df = mongodb_util_27017.find_query_data('us_stock_one_minute_down_load_fail', query)
29
- us_fail_df = us_fail_df.sort_values(by=['now_month'], ascending=False)
30
- fix_name_df = rename_stock.read_csv_df()
31
- for stock_one in us_fail_df.itertuples():
32
- symbol = stock_one.symbol
33
- now_month = stock_one.now_month
34
- path = stock_one.path
35
- fix_name_df_one = fix_name_df.loc[fix_name_df['current_code'] == symbol]
36
- try:
37
-
38
- if data_frame_util.is_not_empty(fix_name_df_one):
39
- current_code = list(fix_name_df_one['current_code'])[0]
40
- former_code = list(fix_name_df_one['former_code'])[0]
41
-
42
- change_date = list(fix_name_df_one['change_date'])[0]
43
-
44
- changed = list(fix_name_df_one['changed'])[0]
45
- if changed:
46
- change_month = change_date.strftime('%Y-%m')
47
- if change_month == now_month:
48
- df_01 = call_with_timeout(sync_one_minute_data,
49
- current_code,
50
- now_month,
51
- timeout=60)
52
- df_02 = call_with_timeout(sync_one_minute_data,
53
- former_code,
54
- now_month,
55
- timeout=60)
56
-
57
- export_original_data(df_01, current_code, str(begin_year), now_month)
58
- export_original_data(df_02, former_code, str(begin_year), now_month)
59
- elif change_month > now_month:
60
- df_02 = call_with_timeout(sync_one_minute_data,
61
- former_code,
62
- now_month,
63
- timeout=60)
64
- export_original_data(df_02, former_code, str(begin_year), now_month)
65
-
66
- elif change_month < now_month:
67
- df_01 = call_with_timeout(sync_one_minute_data,
68
- current_code,
69
- now_month,
70
- timeout=60)
71
- export_original_data(df_01, current_code, str(begin_year), now_month)
72
- else:
73
- df = call_with_timeout(sync_one_minute_data,
74
- symbol,
75
- now_month,
76
- timeout=60)
77
- export_original_data(df, symbol, str(begin_year), now_month)
78
- else:
79
- df = call_with_timeout(sync_one_minute_data,
80
- symbol,
81
- now_month,
82
- timeout=60)
83
- export_original_data(df, symbol, str(begin_year), now_month)
84
- remove_query = {'symbol': symbol, 'now_month': now_month}
85
- mongodb_util_27017.remove_data(remove_query, 'us_stock_one_minute_down_load_fail')
86
- logger.info("同步股票分钟数据完成:,{},{}", symbol, now_month)
87
- except BaseException as e:
88
- time.sleep(1)
89
- fail_dict = {
90
- '_id': symbol + '_' + now_month,
91
- 'type': "stock",
92
- 'path': path,
93
- 'symbol': symbol,
94
- 'now_year': begin_year,
95
- 'now_month': now_month,
96
- }
97
- fail_df = pd.DataFrame(fail_dict, index=[1])
98
-
99
- mongodb_util_27017.save_mongo(fail_df, 'us_stock_one_minute_down_load_fail')
100
- logger.error("同步股票分钟数据出现异常:,{},{},{}", e, symbol, now_month)
101
-
102
- begin_year = begin_year - 1
103
-
104
-
105
- def export_original_data(df, symbol, year, now_month):
106
- path = r'F:\us_stock\one_minute\{}'.format(year)
107
- if not os.path.exists(path):
108
- os.makedirs(path)
109
-
110
- path = path + '\{}'.format(now_month)
111
- if not os.path.exists(path):
112
- os.makedirs(path)
113
-
114
- file_name = path + '\{}.csv'.format(symbol)
115
- if data_frame_util.is_not_empty(df):
116
- # df = df.dropna(subset=['_id'])
117
- # del df['str_day']
118
- # del df['minute']
119
- # del df['_id']
120
- # del df['symbol']
121
- df.to_csv(file_name, index=False, encoding='utf-8')
122
-
123
-
124
- # 定义一个带超时的函数调用
125
- def call_with_timeout(func, *args, timeout=60, **kwargs):
126
- # 用于存储函数执行结果
127
- result = None
128
- exception = None
129
-
130
- # 定义一个线程目标函数
131
- def target():
132
- nonlocal result, exception
133
- try:
134
- result = func(*args, **kwargs)
135
- except Exception as e:
136
- exception = e
137
-
138
- # 创建线程并启动
139
- thread = threading.Thread(target=target)
140
- thread.start()
141
-
142
- # 等待线程完成,最多等待 timeout 秒
143
- thread.join(timeout)
144
-
145
- # 如果线程仍然存活,说明函数超时了
146
- if thread.is_alive():
147
- raise TimeoutError(f"Function exceeded timeout of {timeout} seconds")
148
-
149
- # 如果函数抛出了异常,重新抛出
150
- if exception is not None:
151
- raise exception
152
- return result
153
-
154
-
155
- def sync_one_minute_data(symbol, now_month):
156
- df = alpha_vantage_api.sync_one_minute_data(symbol, now_month)
157
- return df
158
-
159
-
160
- if __name__ == '__main__':
161
- handle_fail_sync()
@@ -1,7 +0,0 @@
1
- import sys
2
- import os
3
-
4
- file_path = os.path.abspath(__file__)
5
- end = file_path.index('mns') + 16
6
- project_path = file_path[0:end]
7
- sys.path.append(project_path)
@@ -1,18 +0,0 @@
1
- import sys
2
- import os
3
-
4
- file_path = os.path.abspath(__file__)
5
- end = file_path.index('mns') + 16
6
- project_path = file_path[0:end]
7
- sys.path.append(project_path)
8
- import pandas as pd
9
-
10
-
11
- def read_csv_df():
12
- data = pd.read_excel(
13
- fr"D:\mns\mns-scheduler\mns_scheduler\extraIncome\us\one_minute\stock\down_load\rename\stock_code_changes.xlsx")
14
- return data
15
-
16
-
17
- if __name__ == '__main__':
18
- read_csv_df()
@@ -1,7 +0,0 @@
1
- import sys
2
- import os
3
-
4
- file_path = os.path.abspath(__file__)
5
- end = file_path.index('mns') + 17
6
- project_path = file_path[0:end]
7
- sys.path.append(project_path)
@@ -1,203 +0,0 @@
1
- import sys
2
- import os
3
-
4
- file_path = os.path.abspath(__file__)
5
- end = file_path.index('mns') + 17
6
- project_path = file_path[0:end]
7
- sys.path.append(project_path)
8
- from loguru import logger
9
- import time
10
- import mns_common.utils.data_frame_util as data_frame_util
11
- from mns_common.db.MongodbUtil import MongodbUtil
12
- from mns_common.db.v2.MongodbUtilV2 import MongodbUtilV2
13
- import mns_common.constant.extra_income_db_name as extra_income_db_name
14
- import mns_scheduler.extraIncome.us.one_minute.api.alpha_vantage_api as alpha_vantage_api
15
- import pandas as pd
16
- from pathlib import Path
17
-
18
- mongodb_util_27017 = MongodbUtil('27017')
19
- mongodbUtilV2_27019 = MongodbUtilV2('27019', extra_income_db_name.EXTRA_INCOME)
20
- from datetime import datetime
21
- import mns_scheduler.extraIncome.us.one_minute.api.stock_etf_info_api as stock_etf_info_api
22
-
23
- import threading
24
-
25
-
26
- # 定义一个带超时的函数调用
27
- def call_with_timeout(func, *args, timeout=60, **kwargs):
28
- # 用于存储函数执行结果
29
- result = None
30
- exception = None
31
-
32
- # 定义一个线程目标函数
33
- def target():
34
- nonlocal result, exception
35
- try:
36
- result = func(*args, **kwargs)
37
- except Exception as e:
38
- exception = e
39
-
40
- # 创建线程并启动
41
- thread = threading.Thread(target=target)
42
- thread.start()
43
-
44
- # 等待线程完成,最多等待 timeout 秒
45
- thread.join(timeout)
46
-
47
- # 如果线程仍然存活,说明函数超时了
48
- if thread.is_alive():
49
- raise TimeoutError(f"Function exceeded timeout of {timeout} seconds")
50
-
51
- # 如果函数抛出了异常,重新抛出
52
- if exception is not None:
53
- raise exception
54
- return result
55
-
56
-
57
- def sync_one_minute_data(symbol, now_month):
58
- df = alpha_vantage_api.sync_one_minute_data(symbol, now_month)
59
- return df
60
-
61
-
62
- def sync_us_stock_one_minute(now_year, now_month):
63
- real_time_quotes_all_us_stocks = stock_etf_info_api.get_us_stock_info()
64
-
65
- path = r'F:\us_stock\one_minute\{}'.format(now_year)
66
- if not os.path.exists(path):
67
- os.makedirs(path)
68
-
69
- path = path + '\{}'.format(now_month)
70
- if not os.path.exists(path):
71
- os.makedirs(path)
72
- stock_name_list = find_exist_file(path)
73
- real_time_quotes_all_us_stocks = real_time_quotes_all_us_stocks.loc[
74
- ~(real_time_quotes_all_us_stocks['symbol'].isin(stock_name_list))]
75
- real_time_quotes_all_us_stocks.dropna(subset=['list_date'], inplace=True)
76
- for stock_one in real_time_quotes_all_us_stocks.itertuples():
77
- flow_mv = stock_one.flow_mv
78
- symbol = stock_one.symbol
79
- try:
80
- symbol = stock_one.symbol
81
- # simple_symbol = int(stock_one.simple_symbol)
82
- # code = str(simple_symbol) + '.' + symbol
83
- list_date = str(stock_one.list_date)
84
- list_date_year = int(list_date[0:4])
85
- list_month = int(list_date[4:6])
86
- now_month_int = int(now_month[5:7])
87
- if (list_date_year > now_year) or ((list_date_year == now_year) and (list_month > now_month_int)):
88
- continue
89
- now_date = datetime.now()
90
- if net_work_check(now_date):
91
- # 休眠 6分钟
92
- time.sleep(5 * 60)
93
-
94
- df = call_with_timeout(sync_one_minute_data,
95
- symbol,
96
- now_month,
97
- timeout=60)
98
- df['time'] = df['time'].dt.strftime('%Y-%m-%d %H:%M:%S')
99
- df['str_day'] = df['time'].str.slice(0, 10)
100
- df['minute'] = df['time'].str.slice(11, 19)
101
- df['_id'] = symbol + "_" + df['time']
102
- df['symbol'] = symbol
103
- df_export_df = df.copy()
104
- export_original_data(df_export_df, symbol, now_year, now_month)
105
- except BaseException as e:
106
- time.sleep(1)
107
- fail_dict = {
108
- '_id': symbol + '_' + now_month,
109
- 'type': "stock",
110
- 'path': path,
111
- 'symbol': symbol,
112
- 'now_year': now_year,
113
- 'now_month': now_month,
114
- 'flow_mv': flow_mv,
115
- }
116
- fail_df = pd.DataFrame(fail_dict, index=[1])
117
-
118
- mongodb_util_27017.save_mongo(fail_df, 'us_stock_one_minute_down_load_fail')
119
- logger.error("同步股票分钟数据出现异常:,{},{},{}", e, symbol, now_month)
120
- logger.info("同步股票分钟票数据完成:{},{}", stock_one.symbol, stock_one.name)
121
-
122
-
123
- def export_original_data(df, symbol, year, now_month):
124
- path = r'F:\us_stock\one_minute\{}'.format(year)
125
- if not os.path.exists(path):
126
- os.makedirs(path)
127
-
128
- path = path + '\{}'.format(now_month)
129
- if not os.path.exists(path):
130
- os.makedirs(path)
131
-
132
- file_name = path + '\{}.csv'.format(symbol)
133
- if data_frame_util.is_not_empty(df):
134
- df = df.dropna(subset=['_id'])
135
- del df['str_day']
136
- del df['minute']
137
- del df['_id']
138
- del df['symbol']
139
- df.to_csv(file_name, index=False, encoding='utf-8')
140
-
141
-
142
- def net_work_check(now_date):
143
- hour = now_date.hour
144
- minute = now_date.minute
145
- if hour == 7 and minute == 34:
146
- return True
147
- elif hour == 9 and minute == 59:
148
- return True
149
- elif hour == 10 and minute == 29:
150
- return True
151
- elif hour == 10 and minute == 59:
152
- return True
153
- elif hour == 12 and minute == 49:
154
- return True
155
- elif hour == 13 and minute == 28:
156
- return True
157
- elif hour == 13 and minute == 58:
158
- return True
159
- elif hour == 14 and minute == 28:
160
- return True
161
- elif hour == 15 and minute == 1:
162
- return True
163
- else:
164
- return False
165
-
166
-
167
- def sync_by_year(begin_year):
168
- begin_month = 12
169
- while begin_month > 0:
170
- if begin_month < 10:
171
- str_month = '0' + str(begin_month)
172
- else:
173
- str_month = str(begin_month)
174
- str_month = str(begin_year) + '-' + str_month
175
- sync_us_stock_one_minute(begin_year, str_month)
176
- begin_month = begin_month - 1
177
- logger.error("同步完成月份:{}", str_month)
178
-
179
-
180
- def find_exist_file(folder_path):
181
- if not os.path.exists(folder_path):
182
- logger.error("错误:目录不存在:{}", folder_path)
183
- else:
184
- folder_path = Path(folder_path)
185
- stock_names = [f.stem for f in folder_path.glob("*.csv")]
186
- return stock_names
187
-
188
-
189
- if __name__ == '__main__':
190
- # k_line_df = query_k_line('TSLA')
191
- # sync_by_year(2024)
192
- # sync_by_year(2022)
193
- # sync_by_year(2020)
194
- sync_by_year(2018)
195
- sync_by_year(2016)
196
- sync_by_year(2014)
197
- sync_by_year(2012)
198
- sync_by_year(2010)
199
- sync_by_year(2008)
200
- sync_by_year(2006)
201
- sync_by_year(2004)
202
- sync_by_year(2002)
203
- sync_by_year(2000)