mns-scheduler 1.3.1.7__py3-none-any.whl → 1.3.1.9__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.9.dist-info}/METADATA +1 -1
  7. {mns_scheduler-1.3.1.7.dist-info → mns_scheduler-1.3.1.9.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.9.dist-info}/WHEEL +0 -0
  59. {mns_scheduler-1.3.1.7.dist-info → mns_scheduler-1.3.1.9.dist-info}/top_level.txt +0 -0
@@ -1,202 +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
-
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(2023)
192
- sync_by_year(2021)
193
- sync_by_year(2019)
194
- sync_by_year(2017)
195
- sync_by_year(2015)
196
- sync_by_year(2013)
197
- sync_by_year(2011)
198
- sync_by_year(2009)
199
- sync_by_year(2007)
200
- sync_by_year(2005)
201
- sync_by_year(2003)
202
- sync_by_year(2001)
@@ -1,144 +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
- import mns_scheduler.extraIncome.us.one_minute.api.stock_etf_info_api as stock_etf_info
18
-
19
- mongodb_util_27017 = MongodbUtil('27017')
20
- mongodbUtilV2_27019 = MongodbUtilV2('27019', extra_income_db_name.EXTRA_INCOME)
21
- from datetime import datetime
22
-
23
-
24
- def sync_us_stock_one_minute(now_year, now_month):
25
- real_time_quotes_all_us_stocks = stock_etf_info.get_us_stock_info()
26
- real_time_quotes_all_us_stocks = real_time_quotes_all_us_stocks.sort_values(by=['amount'], ascending=False)
27
-
28
- path = r'F:\us_stock\one_minute\{}'.format(now_year)
29
- if not os.path.exists(path):
30
- os.makedirs(path)
31
-
32
- path = path + '\{}'.format(now_month)
33
- if not os.path.exists(path):
34
- os.makedirs(path)
35
- stock_name_list = find_exist_file(path)
36
- real_time_quotes_all_us_stocks = real_time_quotes_all_us_stocks.loc[
37
- ~(real_time_quotes_all_us_stocks['symbol'].isin(stock_name_list))]
38
- real_time_quotes_all_us_stocks.dropna(subset=['list_date'], inplace=True)
39
- for stock_one in real_time_quotes_all_us_stocks.itertuples():
40
- symbol = stock_one.symbol
41
- flow_mv = stock_one.flow_mv
42
- try:
43
-
44
-
45
- # simple_symbol = int(stock_one.simple_symbol)
46
- # code = str(simple_symbol) + '.' + symbol
47
- list_date = str(stock_one.list_date)
48
- list_date_year = int(list_date[0:4])
49
- list_month = int(list_date[4:6])
50
- now_month_int = int(now_month[5:7])
51
- if (list_date_year > now_year) or ((list_date_year == now_year) and (list_month > now_month_int)):
52
- continue
53
- now_date = datetime.now()
54
- if net_work_check(now_date):
55
- # 休眠 6分钟
56
- time.sleep(5 * 60)
57
-
58
- df = alpha_vantage_api.sync_one_minute_data(symbol, now_month)
59
- df['time'] = df['time'].dt.strftime('%Y-%m-%d %H:%M:%S')
60
- df['str_day'] = df['time'].str.slice(0, 10)
61
- df['minute'] = df['time'].str.slice(11, 19)
62
- df['_id'] = symbol + "_" + df['time']
63
- df['symbol'] = symbol
64
- df_export_df = df.copy()
65
- export_original_data(df_export_df, symbol, path)
66
- except BaseException as e:
67
- time.sleep(1)
68
- fail_dict = {
69
- '_id': symbol + '_' + now_month,
70
- 'type': "stock",
71
- 'path': path,
72
- 'symbol': symbol,
73
- 'now_year': now_year,
74
- 'now_month': now_month,
75
- 'flow_mv': flow_mv,
76
- }
77
- fail_df = pd.DataFrame(fail_dict, index=[1])
78
-
79
- mongodb_util_27017.save_mongo(fail_df, 'us_stock_one_minute_down_load_fail')
80
- logger.error("同步股票分钟数据出现异常:,{},{},{}", e, symbol, now_month)
81
- logger.info("同步股票分钟票数据完成:{},{}", stock_one.symbol, stock_one.name)
82
-
83
-
84
- def export_original_data(df, symbol, path):
85
- file_name = path + '\{}.csv'.format(symbol)
86
- if data_frame_util.is_not_empty(df):
87
- df = df.dropna(subset=['_id'])
88
- del df['str_day']
89
- del df['minute']
90
- del df['_id']
91
- del df['symbol']
92
- df.to_csv(file_name, index=False, encoding='utf-8')
93
-
94
-
95
- def net_work_check(now_date):
96
- hour = now_date.hour
97
- minute = now_date.minute
98
- if hour == 7 and minute == 34:
99
- return True
100
- elif hour == 9 and minute == 59:
101
- return True
102
- elif hour == 10 and minute == 29:
103
- return True
104
- elif hour == 10 and minute == 59:
105
- return True
106
- elif hour == 12 and minute == 49:
107
- return True
108
- elif hour == 13 and minute == 28:
109
- return True
110
- elif hour == 13 and minute == 58:
111
- return True
112
- elif hour == 14 and minute == 28:
113
- return True
114
- elif hour == 15 and minute == 1:
115
- return True
116
- else:
117
- return False
118
-
119
-
120
- def sync_by_year(begin_year):
121
- begin_month = 4
122
- while begin_month > 0:
123
- if begin_month < 10:
124
- str_month = '0' + str(begin_month)
125
- else:
126
- str_month = str(begin_month)
127
- str_month = str(begin_year) + '-' + str_month
128
- sync_us_stock_one_minute(begin_year, str_month)
129
- begin_month = begin_month - 1
130
- logger.error("同步完成月份:{}", str_month)
131
-
132
-
133
- def find_exist_file(folder_path):
134
- if not os.path.exists(folder_path):
135
- logger.error("错误:目录不存在:{}", folder_path)
136
- else:
137
- folder_path = Path(folder_path)
138
- stock_names = [f.stem for f in folder_path.glob("*.csv")]
139
- return stock_names
140
-
141
-
142
- if __name__ == '__main__':
143
- # k_line_df = query_k_line('TSLA')
144
- sync_by_year(2025)
@@ -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,52 +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
- from mns_common.db.MongodbUtil import MongodbUtil
10
- from mns_common.db.v2.MongodbUtilV2 import MongodbUtilV2
11
- import mns_scheduler.extraIncome.a_stock.one_minute.common.db_create_index as db_create_index
12
- import mns_common.constant.extra_income_db_name as extra_income_db_name
13
- import mns_scheduler.extraIncome.us.one_minute.api.stock_etf_info_api as stock_etf_info_api
14
- import mns_scheduler.extraIncome.us.one_minute.api.y_finance_api as y_finance_api
15
-
16
- from datetime import datetime, timedelta
17
-
18
- mongodb_util_27017 = MongodbUtil('27017')
19
- mongodbUtilV2_27019 = MongodbUtilV2('27019', extra_income_db_name.EXTRA_INCOME)
20
-
21
-
22
- def sync_us_etf_one_minute():
23
- us_stock_df = stock_etf_info_api.get_us_etf_info()
24
-
25
- # 获取当前日期
26
- current_date = datetime.now()
27
- year = current_date.year
28
- col_name = extra_income_db_name.US_ETF_MINUTE_K_LINE_BFQ + "_" + str(year)
29
-
30
- # 创建索引
31
- db_create_index.create_index(mongodbUtilV2_27019, col_name)
32
-
33
- str_day = current_date.strftime('%Y-%m-%d')
34
- # 计算七天前的日期 todo 修改时间
35
- seven_days_ago = current_date - timedelta(days=8)
36
-
37
- str_seven_days_ago = seven_days_ago.strftime('%Y-%m-%d')
38
-
39
- for stock_one in us_stock_df.itertuples():
40
- symbol = stock_one.symbol
41
- name = stock_one.name
42
- try:
43
- us_one_minute_df = y_finance_api.get_us_one_minute(symbol, str_seven_days_ago, str_day, )
44
- us_one_minute_df['_id'] = symbol + '_' + us_one_minute_df['time']
45
- mongodbUtilV2_27019.insert_mongo(us_one_minute_df, col_name)
46
- logger.info("同步美国ETF分钟数据完成:{},{}", symbol, name)
47
- except BaseException as e:
48
- logger.error("同步美国股票ETF数据出现异常:{},{},{}", symbol, name, e)
49
-
50
-
51
- if __name__ == '__main__':
52
- sync_us_etf_one_minute()
@@ -1,141 +0,0 @@
1
- import os
2
- import sys
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 mns_common.utils.data_frame_util as data_frame_util
10
- from mns_common.db.MongodbUtil import MongodbUtil
11
- from mns_common.db.v2.MongodbUtilV2 import MongodbUtilV2
12
- import mns_scheduler.extraIncome.a_stock.one_minute.common.db_create_index as db_create_index
13
- import mns_common.constant.extra_income_db_name as extra_income_db_name
14
- import mns_scheduler.extraIncome.us.one_minute.api.stock_etf_info_api as stock_etf_info_api
15
- import mns_scheduler.extraIncome.us.one_minute.api.y_finance_api as y_finance_api
16
- import pandas as pd
17
- from datetime import datetime, timedelta
18
- import numpy as np
19
-
20
- mongodb_util_27017 = MongodbUtil('27017')
21
- mongodbUtilV2_27019 = MongodbUtilV2('27019', extra_income_db_name.EXTRA_INCOME)
22
-
23
-
24
- def sync_us_stock_one_minute():
25
- us_stock_df = stock_etf_info_api.get_us_stock_info()
26
- us_stock_df = us_stock_df.loc[us_stock_df['amount'] != 0]
27
-
28
- # 获取当前日期
29
- current_date = datetime.now()
30
- year = current_date.year
31
- col_name = extra_income_db_name.US_STOCK_MINUTE_K_LINE_BFQ + "_" + str(year)
32
-
33
- # 创建索引
34
- db_create_index.create_index(mongodbUtilV2_27019, col_name)
35
-
36
- str_day = current_date.strftime('%Y-%m-%d')
37
- # 计算七天前的日期 todo 修改时间
38
- seven_days_ago = current_date - timedelta(days=7)
39
-
40
- str_seven_days_ago = seven_days_ago.strftime('%Y-%m-%d')
41
-
42
- save_one_minute_data(us_stock_df, str_day, str_seven_days_ago, col_name, True)
43
- handle_fail_stocks()
44
-
45
-
46
- def save_one_minute_data(us_stock_df, str_day, str_seven_days_ago, col_name, save_flag):
47
- col_name_status = extra_income_db_name.US_STOCK_MINUTE_K_LINE_BFQ + "_status"
48
- for stock_one in us_stock_df.itertuples():
49
-
50
- symbol = stock_one.symbol
51
- name = stock_one.name
52
- try:
53
- list_date = stock_one.list_date
54
- if mongodbUtilV2_27019.exist_data_query(col_name_status, {"str_day": str_day,
55
- 'symbol': symbol, 'status': 'success'}):
56
- continue
57
-
58
- if not np.isnan(list_date):
59
- list_date = str(int(list_date))
60
-
61
- date_obj = datetime.strptime(list_date, '%Y%m%d')
62
- # 格式化为 "YYYY-MM-DD" 或其他格式
63
- list_date_str = date_obj.strftime('%Y-%m-%d')
64
-
65
- if list_date_str > str_day:
66
- continue
67
- elif list_date_str > str_seven_days_ago:
68
- str_seven_days_ago = list_date_str
69
-
70
- us_one_minute_df = y_finance_api.get_us_one_minute(symbol, str_seven_days_ago, str_day)
71
- us_one_minute_df = us_one_minute_df.fillna(0)
72
-
73
- us_one_minute_df['_id'] = symbol + '_' + us_one_minute_df['time']
74
- us_one_minute_df['symbol'] = symbol
75
- if save_flag:
76
- mongodbUtilV2_27019.insert_mongo(us_one_minute_df, col_name)
77
- else:
78
- mongodbUtilV2_27019.save_mongo(us_one_minute_df, col_name)
79
-
80
- result_dict = {
81
- '_id': symbol + "_" + str_day,
82
- "str_day": str_day,
83
- 'symbol': symbol,
84
- 'status': 'success',
85
-
86
- }
87
-
88
- result_dict_df = pd.DataFrame(result_dict, index=[1])
89
- mongodbUtilV2_27019.save_mongo(result_dict_df, col_name_status)
90
- logger.info("同步美股分钟数据完成:{},{}", symbol, name)
91
- except BaseException as e:
92
- result_dict = {
93
- '_id': symbol + "_" + str_day,
94
- "str_day": str_day,
95
- 'symbol': symbol,
96
- 'status': 'fail',
97
- }
98
- result_dict_df = pd.DataFrame(result_dict, index=[1])
99
- mongodbUtilV2_27019.save_mongo(result_dict_df, col_name_status)
100
-
101
- logger.error("同步美股分钟数据出现异常:{},{},{}", symbol, name, e)
102
-
103
-
104
- def handle_fail_stocks():
105
- us_stock_df = stock_etf_info_api.get_us_stock_info()
106
-
107
- # 获取当前日期
108
- current_date = datetime.now()
109
- str_current_date = current_date.strftime('%Y-%m-%d')
110
-
111
- query = {'status': 'fail', 'str_day': str_current_date}
112
- col_name_status = extra_income_db_name.US_STOCK_MINUTE_K_LINE_BFQ + "_status"
113
- fail_df = mongodbUtilV2_27019.find_query_data(col_name_status, query)
114
- if data_frame_util.is_empty(fail_df):
115
- return
116
- else:
117
- fail_us_df = us_stock_df.loc[us_stock_df['symbol'].isin(fail_df['symbol'])]
118
- year = current_date.year
119
- col_name = extra_income_db_name.US_STOCK_MINUTE_K_LINE_BFQ + "_" + str(year)
120
-
121
- # 循环7天,每天处理一次
122
- for days_ago in range(7):
123
- try:
124
- # 计算当前处理日期(从今天往前推days_ago天)
125
- target_date = current_date - timedelta(days=days_ago)
126
- str_target_date = target_date.strftime('%Y-%m-%d')
127
-
128
- # 计算前一天日期
129
- previous_date = target_date - timedelta(days=1)
130
- str_previous_date = previous_date.strftime('%Y-%m-%d')
131
-
132
- # 调用保存函数
133
- save_one_minute_data(fail_us_df, str_target_date, str_previous_date, col_name, False)
134
- except BaseException as e:
135
-
136
- logger.error("同步美股分钟数据补偿任务出现异常:{}", e)
137
-
138
-
139
- if __name__ == '__main__':
140
- sync_us_stock_one_minute()
141
-