电脑版

1
2
3
4
5
6
7
8
9
10
# 千亿公司
qygs_api = 'http://fund.eastmoney.com/company/default.html'

# 集思录
jsl_api_etf = 'https://www.jisilu.cn/data/etf/etf_list/?___jsl=LST___t=1663138938775&rp=25&page=1'
jsl_api_yz = 'https://www.jisilu.cn/data/qdii/qdii_list/A?___jsl=LST___t=1665370506235&rp=22&page=1'
jsl_api_om = 'https://www.jisilu.cn/data/qdii/qdii_list/E?___jsl=LST___t=1665371145110&rp=22&page=1'

# 天天基金网历史净值
ttjjw_api_lsjz = 'https://fundf10.eastmoney.com/F10DataApi.aspx?type=lsjz&code=510500&sdate=2001-12-18&edate=2022-10-07&per=20&page=1'
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
import API
import requests
from bs4 import BeautifulSoup


def com():
"""
千亿市值
"""
# 空列表存储获取到的公司名
coms = []
# 调用API接口获取数据
res = requests.get(API.qygs_api)
# 定义编码格式
res.encoding = 'utf-8'
# 转换为字符串数据
company = res.text
# 解析数据
datas = BeautifulSoup(company, 'html.parser')

# 提取数据
names = datas.find_all('td', class_='td-align-left')
values = datas.find_all('td', class_="scale number")

for i in range(len(names)):
# 获取每个公司的市值
value = values[i].attrs['data-sortvalue']
# 判断市值不为空同时大于1000亿
if value != "" and float(value) > 1000:
name = names[i].text
# 添加到空列表中
coms.append(name)
return coms


# 以写入的模式打开了文件"coms.txt"。
save_file = open('coms.txt', 'a', encoding='utf-8')
# 往文件中写入内容,由于write里面必须是str类型,必须强制转换成str
save_file.write(str(com()))
# 关闭文件
save_file.close()





qyszgs = ['易方达基金管理有限公司', '广发基金管理有限公司', '天弘基金管理有限公司', '华夏基金管理有限公司', '博时基金管理有限公司', '南方基金管理股份有限公司', '富国基金管理有限公司', '鹏华基金管理有限公司', '汇添富基金管理股份有限公司', '工银瑞信基金管理有限公司', '招商基金管理有限公司', '嘉实基金管理有限公司', '建信基金管理有限责任公司', '银华基金管理股份有限公司', '兴证全球基金管理有限公司', '华安基金管理有限公司', '交银施罗德基金管理有限公司', '国泰基金管理有限公司', '景顺长城基金管理有限公司', '平安基金管理有限公司', '中欧基金管理有限公司', '中银基金管理有限公司', '华宝基金管理有限公司', '万家基金管理有限公司', '浦银安盛基金管理有限公司', '兴业基金管理有限公司', '华泰柏瑞基金管理有限公司', '永赢基金管理有限公司', '大成基金管理有限公司', '国寿安保基金管理有限公司', '上海东方证券资产管理有限公司', '长城基金管理有限公司', '国投瑞银基金管理有限公司', '农银汇理基金管理有限公司', '上投摩根基金管理有限公司', '前海开源基金管理有限公司', '海富通基金管理有限公司', '诺安基金管理有限公司', '财通证券资产管理有限公司', '民生加银基金管理有限公司', '安信基金管理有限责任公司', '中银国际证券股份有限公司', '中融基金管理有限公司', '中加基金管理有限公司', '中信保诚基金管理有限公司', '银河基金管理有限公司', '融通基金管理有限公司', '上银基金管理有限公司', '长信基金管理有限责任公司', '泰康资产管理有限责任公司', '创金合信基金管理有限公司']
qyszgsjc = ['易方达', '广发基金', '天弘基金', '华夏基金', '博时基金', '南方基金', '富国基金', '鹏华基金', '汇添富', '工银瑞信', '招商基金', '嘉实基金', '建信基金', '银华基金', '兴证全球基金', '华安基金', '交银施罗德', '国泰基金', '景顺长城', '平安基金', '中欧基金', '中银基金', '华宝基金', '万家基金', '浦银安盛', '兴业基金', '华泰柏瑞', '永赢基金', '大成基金', '国寿安保', '上海东方证券资产', '长城基金', '国投瑞银', '农银汇理基金', '上投摩根', '前海开源', '海富通', '诺安基金', '财通证券资产', '民生加银', '安信基金管理有限责任公司', '中银国际', '中融基金', '中加基金', '中信保诚基金', '银河基金', '融通基金', '上银基金', '长信基金管理有限责任公司', '泰康资产', '创金合信基金']
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from zhdate import ZhDate


def date():
import time
# 获取今天日期
day = time.strftime('%Y-%m-%d')
# 获取今天星期
zhou = time.strftime('%w', time.localtime())
# 获取现在时间
time = time.strftime('%H:%M:%S')
# 获取农历日期
nl = ZhDate.today()
dates = f''' time:[{day}]【星期{zhou}】[{time}]
农历:{nl.chinese()}'''
return dates


print(date())
1
2
3
4
5
6
codes = [
'510050', '510300', '510500', '159949', # 大小盘
'510880', '159905', # 策略
'515050', '159995', '512980', '515790', '512660', # 主题
'159928', '512170', '512800', '512690', '512000', '512010', '513050' # 行业
]
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
import pandas as pd
import requests
import API
import etf_codes

# 代码
codes = etf_codes.codes


def jsl_data(code):
# """
# 集思录数据
# """
# 集思录接口
def jsl_api(api):
# 提取数据并转换成json格式
api_data = requests.get(api).json()['rows']
# 创建空列表存储集思录全部数据
jsl_all_data = []
for i in api_data:
# 循环遍历从api接口中获取所有数据
jsl_all_data.append(i['cell'])
# 数据转换
df = pd.DataFrame(jsl_all_data)
# 筛选指定列
df = df[['fund_id', 'fund_nm', 'price', 'increase_rt', 'discount_rt']]
# 修改列名
df.columns = ['code', 'fund_nm', 'price', 'increase_rt', 'discount_rt']
# 将代码作为列索引
df = df.set_index('code')
# 显示所有列
pd.set_option('display.max_columns', None)
# 显示所有行
pd.set_option('display.max_rows', None)
# 返回全部数据
return df

# 调用集思录的方法
data = pd.concat([jsl_api(API.jsl_api_etf), jsl_api(API.jsl_api_yz), jsl_api(API.jsl_api_om)], sort=False)

# 筛选列索引指定代码的数据
data = data.loc[code]
# 删除%号
data = data.replace('\\%', '', regex=True)

def zdyhszh(val):
"""
自定义函数转换
移除%
移除-
"""
new_val = val.replace('%', '').replace('-', '0')
return new_val

# 通过自定义函数清洗数据
data[['price', 'increase_rt', 'discount_rt']] = data[['price', 'increase_rt', 'discount_rt']].apply(zdyhszh)
# 索引重置
data.reset_index(inplace=True)
# 转换数据类型 【现价、涨幅、溢价率】
data['code'] = data['code'].astype('int')
data[['price', 'increase_rt', 'discount_rt']] = data[['price', 'increase_rt', 'discount_rt']].astype('float')

# 返回集思录数据
return data

data =jsl_data(codes)
# data.to_csv('jsl.csv', index=False)
# print(data)
# print(data.info())
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
import requests
import pandas as pd
from bs4 import BeautifulSoup

print("程序运行中...")


def ttjjw():
# 单个代码的历史净值
def lsjz(code):
# 单页历史净值
def get_html(code, page, sdate='2001-01-01', edate='day', per=49):
# 爬虫API接口
res = requests.get(
f'https://fundf10.eastmoney.com/F10DataApi.aspx?type=lsjz&code={code}&sdate={sdate}&edate={edate}&per={per}&page={page}').text
# 转换数据格式
datas = BeautifulSoup(res, 'html.parser').find_all('tr')[1:]
# 单页历史净值列表
lsjzs = []
for i in range(len(datas)):
dict = {}
dict['code'] = code
dict['date'] = datas[i].find('td').text
dict['data'] = datas[i].find(class_="tor bold").text
lsjzs.append(dict)
return lsjzs

# 创建空列表存储所有历史净值
lsjz_list = []
for i in range(1, 9):
lsjz_list.extend(get_html(code, i))
# 转换数据格式
df = pd.DataFrame(lsjz_list)
# 将代码作为列索引
df = df.set_index('code')
# 将数据类型转换为日期类型
df['date'] = pd.to_datetime(df['date'])
# 日期倒序排列
df = df.sort_values(by='date', ascending=True)
# 显示所有列
pd.set_option('display.max_columns', None)
# 显示所有行
pd.set_option('display.max_rows', None)
return df

# 移动平均线
def MA(code):
# 拿到索引历史净值
df = lsjz(code)
# 计算移动平均线 5,10,30,60,120
df['ma_5'] = df.data.rolling(window=5).mean()
df['ma_10'] = df.data.rolling(window=10).mean()
df['ma_30'] = df.data.rolling(window=30).mean()
df['ma_60'] = df.data.rolling(window=60).mean()
df['ma_120'] = df.data.rolling(window=120).mean()
# 选取最后一天移动平均线
ydpjz = df.iloc[[-1]]
# 筛选数据
ydpjz = ydpjz[['ma_5', 'ma_10', 'ma_30', 'ma_60', 'ma_120']]
return ydpjz

# 计算历史平均值
def avg(code, n=20):
# 拿到索引历史净值
df = lsjz(code)
# 选取最近几天的
df = df.tail(n)
# 转换数据类型
df['data'] = df['data'].astype('float')

# 调用移动平均线
olds = MA(code)
# 创建空列表储存数据
old = []
# 循环遍历获取到大前天、前天、昨天的历史净值
for i in df.tail(3)['data']:
old.append(i)
# 昨天
olds['z'] = old[2]
# 前天
olds['q'] = old[1]
# 大前天
olds['d'] = old[0]
# 最大值
olds['big'] = max(df['data'])
# 最小值
olds['small'] = min(df['data'])
# 求平均值(保留4位小数)
olds['avg'] = float('%.4f' % (sum(df['data']) / len(df['data'])))
return olds

'''
追加数据
'''

data = [avg(510050), avg(510300), avg(510500), avg(159949), avg(510880), avg(159905), avg(515050), avg(159995),
avg(512980), avg(515790), avg(512660), avg(159928), avg(512170), avg(512800), avg(512690),
avg(512000), avg(512010), avg(513050)]

ttjjw = pd.concat(data)
return ttjjw

data = ttjjw()
# 索引重置
data.reset_index(inplace=True)
# 类型强制转换
data["code"] = data["code"].astype(int)
# 数据保存到本地
data.to_csv('ttjjw.csv', index=False)
print(data)
print(data.info())
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
from zhdate import ZhDate
import pandas as pd
import jsl
# 集思录数据
jsl = jsl.data
# 读取本地数据
ttjjw = pd.read_csv('ttjjw.csv')
# 添加温度每周更新一次
c = [
20.3, 16.4, 9.7, 28.21,
0.76, 36.01,
0.71, 0.85, 3.55, 44, 41.96,
0, 25.46, 0, 64.25, 4.65, 0, 0.05]
codes = [
'510050', '510300', '510500', '159949', # 大小盘
'510880', '159905', # 策略
'515050', '159995', '512980', '515790', '512660', # 主题
'159928', '512170', '512800', '512690', '512000', '512010', '513050' # 行业
]
# 添加温度
ttjjw['c'] = c
# 数据拼接
data = pd.merge(jsl, ttjjw, left_on="code", right_on="code", how="inner")

print("程序开始运行,请稍等...")


'''
时间
'''
def date():
import time
# 获取今天日期
day = time.strftime('%Y-%m-%d')
# 获取今天星期
zhou = time.strftime('%w', time.localtime())
# 获取现在时间
time = time.strftime('%H:%M:%S')
# 获取农历日期
nl = ZhDate.today()
dates = f''' time:[{day}]【星期{zhou}】[{time}]
农历:{nl.chinese()}'''
return dates

# 打印时间
print(date())

'''

1、现价<均价 且 现价<昨 且 昨<前 且 前<大
2、现价小于所有均线
3、溢价率小于0,或涨幅大于0
'''
buy = data.query('price<avg and price<z and z<q and q<d')
buy = buy.query('price<ma_120 and price<ma_60 and price<ma_30 and price<ma_10 and price<ma_5')
buy = buy.query('discount_rt <0 or increase_rt >0')
# 筛选数据
buy = buy[['code', 'fund_nm', 'increase_rt', 'avg', 'price', 'small', 'c']]
# 转换类型后再添加单位
buy['increase_rt'] = buy['increase_rt'].astype('str') + '%'
buy['c'] = buy['c'].astype('str') + '°C'
# 暴力修改列名
buy.columns = ['代码', '名称', '涨幅', '均价', '现价', '最小', '温度']

print(f'--------买【{len(buy)}】--------')
print(buy)




'''

1、现价>均价 且 现价>昨 且 昨>前 且 前>大
2、现价大于所有均线
3、溢价率大于0,或涨幅小于0
'''
shell = data.query('price>avg and price>z and z>q and q>d')
shell = shell.query('price>ma_120 and price>ma_60 and price>ma_30 and price>ma_10 and price>ma_5')
shell = shell.query('discount_rt >0 or increase_rt <0')
# 筛选数据
shell = shell[['code', 'fund_nm', 'increase_rt', 'avg', 'price', 'big', 'c']]
# 转换类型后再添加单位
shell['increase_rt'] = shell['increase_rt'].astype('str') + '%'
shell['c'] = shell['c'].astype('str') + '°C'
# 暴力修改列名
shell.columns = ['代码', '名称', '涨幅', '均价', '现价', '最大', '温度']

print(f'--------卖【{len(shell)}】--------')
print(shell)

print(f'''
-------------------------------------------------
程序全部运行完成,可买的有【{len(buy)}】个,可卖的有【{len(shell)}】个!''')

手机版

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
from zhdate import ZhDate
import requests
import pandas as pd
from bs4 import BeautifulSoup

print("程序开始运行,请稍等...")

'''
时间
'''
def date():
import time
# 获取今天日期
day = time.strftime('%Y-%m-%d')
# 获取今天星期
zhou = time.strftime('%w', time.localtime())
# 获取现在时间
time = time.strftime('%H:%M:%S')
# 获取农历日期
nl = ZhDate.today()
dates = f''' time:[{day}]【星期{zhou}】[{time}]
农历:{nl.chinese()}'''
return dates

# 打印时间
print(date())


'''
API接口
'''
# 千亿公司
qygs_api = 'http://fund.eastmoney.com/company/default.html'

# 集思录
jsl_api_etf = 'https://www.jisilu.cn/data/etf/etf_list/?___jsl=LST___t=1663138938775&rp=25&page=1'
jsl_api_yz = 'https://www.jisilu.cn/data/qdii/qdii_list/A?___jsl=LST___t=1665370506235&rp=22&page=1'
jsl_api_om = 'https://www.jisilu.cn/data/qdii/qdii_list/E?___jsl=LST___t=1665371145110&rp=22&page=1'

# 天天基金网历史净值
ttjjw_api_lsjz = 'https://fundf10.eastmoney.com/F10DataApi.aspx?type=lsjz&code=510500&sdate=2001-12-18&edate=2022-10-07&per=20&page=1'


'''
代码
'''
codes = [
'510050', '510300', '510500', '159949', # 大小盘
'510880', '159905', # 策略
'515050', '159995', '512980', '515790', '512660', # 主题
'159928', '512170', '512800', '512690', '512000', '512010', '513050' # 行业
]

'''
历史数据
'''
def ttjjw():
# 单个代码的历史净值
def lsjz(code):
# 单页历史净值
def get_html(code, page, sdate='2001-01-01', edate='day', per=49):
# 爬虫API接口
res = requests.get(
f'https://fundf10.eastmoney.com/F10DataApi.aspx?type=lsjz&code={code}&sdate={sdate}&edate={edate}&per={per}&page={page}').text
# 转换数据格式
datas = BeautifulSoup(res, 'html.parser').find_all('tr')[1:]
# 单页历史净值列表
lsjzs = []
for i in range(len(datas)):
dict = {}
dict['code'] = code
dict['date'] = datas[i].find('td').text
dict['data'] = datas[i].find(class_="tor bold").text
lsjzs.append(dict)
return lsjzs

# 创建空列表存储所有历史净值
lsjz_list = []
for i in range(1, 2):
lsjz_list.extend(get_html(code, i))
# 转换数据格式
df = pd.DataFrame(lsjz_list)
# 将代码作为列索引
df = df.set_index('code')
# 将数据类型转换为日期类型
df['date'] = pd.to_datetime(df['date'])
# 日期倒序排列
df = df.sort_values(by='date', ascending=True)
# 显示所有列
pd.set_option('display.max_columns', None)
# 显示所有行
pd.set_option('display.max_rows', None)
return df

# 移动平均线
def MA(code):
# 拿到索引历史净值
df = lsjz(code)
# 计算移动平均线 5,10,30
df['ma_5'] = df.data.rolling(window=5).mean()
df['ma_10'] = df.data.rolling(window=10).mean()
df['ma_30'] = df.data.rolling(window=30).mean()
# 选取最后一天移动平均线
ydpjz = df.iloc[[-1]]
# 筛选数据
ydpjz = ydpjz[['ma_5', 'ma_10', 'ma_30']]
return ydpjz

# 计算历史平均值
def avg(code, n=20):
# 拿到索引历史净值
df = lsjz(code)
# 选取最近几天的
df = df.tail(n)
# 转换数据类型
df['data'] = df['data'].astype('float')

# 调用移动平均线
olds = MA(code)
# 创建空列表储存数据
old = []
# 循环遍历获取到大前天、前天、昨天的历史净值
for i in df.tail(3)['data']:
old.append(i)
# 昨天
olds['z'] = old[2]
# 前天
olds['q'] = old[1]
# 大前天
olds['d'] = old[0]
# 最大值
olds['big'] = max(df['data'])
# 最小值
olds['small'] = min(df['data'])
# 求平均值(保留4位小数)
olds['avg'] = float('%.4f' % (sum(df['data']) / len(df['data'])))
return olds

'''
追加数据
'''

data = [avg(510050), avg(510300), avg(510500), avg(159949), avg(510880), avg(159905), avg(515050), avg(159995),
avg(512980), avg(515790), avg(512660), avg(159928), avg(512170), avg(512800), avg(512690),
avg(512000), avg(512010), avg(513050)]

ttjjw = pd.concat(data)
return ttjjw

lssj = ttjjw()
# 索引重置
lssj.reset_index(inplace=True)
# 类型强制转换
lssj["code"] = lssj["code"].astype(int)
# 数据保存到本地
lssj.to_csv('ttjjw.csv', index=False)
# 添加温度每周更新一次
c = [
20.3, 16.4, 9.7, 28.21,
0.76, 36.01,
0.71, 0.85, 3.55, 44, 41.96,
0, 25.46, 0, 64.25, 4.65, 0, 0.05]
# 添加温度
lssj['c'] = c

'''
实时数据
'''
def jsl_data(code):
# """
# 集思录数据
# """
# 集思录接口
def jsl_api(api):
# 提取数据并转换成json格式
api_data = requests.get(api).json()['rows']
# 创建空列表存储集思录全部数据
jsl_all_data = []
for i in api_data:
# 循环遍历从api接口中获取所有数据
jsl_all_data.append(i['cell'])
# 数据转换
df = pd.DataFrame(jsl_all_data)
# 筛选指定列
df = df[['fund_id', 'fund_nm', 'price', 'increase_rt', 'discount_rt']]
# 修改列名
df.columns = ['code', 'fund_nm', 'price', 'increase_rt', 'discount_rt']
# 将代码作为列索引
df = df.set_index('code')
# 显示所有列
pd.set_option('display.max_columns', None)
# 显示所有行
pd.set_option('display.max_rows', None)
# 返回全部数据
return df
# 调用集思录的方法
data = pd.concat([jsl_api(jsl_api_etf), jsl_api(jsl_api_yz), jsl_api(jsl_api_om)], sort=False)
# 筛选列索引指定代码的数据
data = data.loc[codes]
# 删除%号
data = data.replace('\\%', '', regex=True)
def zdyhszh(val):
"""
自定义函数转换
移除%
移除-
"""
new_val = val.replace('%', '').replace('-', '0')
return new_val
# 通过自定义函数清洗数据
data[['price', 'increase_rt', 'discount_rt']] = data[['price', 'increase_rt', 'discount_rt']].apply(zdyhszh)
# 索引重置
data.reset_index(inplace=True)
# 转换数据类型 【现价、涨幅、溢价率】
data['code'] = data['code'].astype('int')
data[['price', 'increase_rt', 'discount_rt']] = data[['price', 'increase_rt', 'discount_rt']].astype('float')

# 返回集思录数据
return data

jsl = jsl_data(codes)


'''
数据合并
'''
# 数据拼接
data = pd.merge(jsl, lssj, left_on="code", right_on="code", how="inner")



'''

1、现价<均价 且 现价<昨 且 昨<前 且 前<大
2、现价小于所有均线
3、溢价率小于0,或涨幅大于0
'''
buy = data.query('price<avg and price<z and z<q and q<d')
buy = buy.query('price<ma_30 and price<ma_10 and price<ma_5')
buy = buy.query('discount_rt <0 or increase_rt >0')
# 筛选数据
buy = buy[['code', 'fund_nm', 'increase_rt', 'avg', 'price', 'small', 'c']]
# 转换类型后再添加单位
buy['increase_rt'] = buy['increase_rt'].astype('str') + '%'
buy['c'] = buy['c'].astype('str') + '°C'
# 暴力修改列名
buy.columns = ['代码', '名称', '涨幅', '均价', '现价', '最小', '温度']

print(f'--------买【{len(buy)}】--------')
print(buy)




'''

1、现价>均价 且 现价>昨 且 昨>前 且 前>大
2、现价大于所有均线
3、溢价率大于0,或涨幅小于0
'''
shell = data.query('price>avg and price>z and z>q and q>d')
shell = shell.query('price>ma_30 and price>ma_10 and price>ma_5')
shell = shell.query('discount_rt >0 or increase_rt <0')
# 筛选数据
shell = shell[['code', 'fund_nm', 'increase_rt', 'avg', 'price', 'big', 'c']]
# 转换类型后再添加单位
shell['increase_rt'] = shell['increase_rt'].astype('str') + '%'
shell['c'] = shell['c'].astype('str') + '°C'
# 暴力修改列名
shell.columns = ['代码', '名称', '涨幅', '均价', '现价', '最大', '温度']

print(f'--------卖【{len(shell)}】--------')
print(shell)

print(f'''
-------------------------------------------------
程序全部运行完成,可买的有【{len(buy)}】个,可卖的有【{len(shell)}】个!''')
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
from zhdate import ZhDate
import requests
import pandas as pd
from bs4 import BeautifulSoup

print("程序开始运行,请稍等...")

'''
时间
'''
def date():
import time
# 获取今天日期
day = time.strftime('%Y-%m-%d')
# 获取今天星期
zhou = time.strftime('%w', time.localtime())
# 获取现在时间
time = time.strftime('%H:%M:%S')
# 获取农历日期
nl = ZhDate.today()
dates = f''' time:[{day}]【星期{zhou}】[{time}]
农历:{nl.chinese()}'''
return dates

# 打印时间
print(date())


'''
API接口
'''
# 千亿公司
qygs_api = 'http://fund.eastmoney.com/company/default.html'

# 集思录
jsl_api_etf = 'https://www.jisilu.cn/data/etf/etf_list/?___jsl=LST___t=1663138938775&rp=25&page=1'
jsl_api_yz = 'https://www.jisilu.cn/data/qdii/qdii_list/A?___jsl=LST___t=1665370506235&rp=22&page=1'
jsl_api_om = 'https://www.jisilu.cn/data/qdii/qdii_list/E?___jsl=LST___t=1665371145110&rp=22&page=1'

# 天天基金网历史净值
ttjjw_api_lsjz = 'https://fundf10.eastmoney.com/F10DataApi.aspx?type=lsjz&code=510500&sdate=2001-12-18&edate=2022-10-07&per=20&page=1'


'''
代码
'''
codes = [
'510050', '510300', '510500', '159949', # 大小盘
'510880', '159905', # 策略
'515050', '159995', '512980', '515790', '512660', # 主题
'159928', '512170', '512800', '512690', '512000', '512010', '513050' # 行业
]

'''
历史数据
'''
lssj = pd.read_csv('ttjjw.csv')

# 添加温度每周更新一次
c = [
20.3, 16.4, 9.7, 28.21,
0.76, 36.01,
0.71, 0.85, 3.55, 44, 41.96,
0, 25.46, 0, 64.25, 4.65, 0, 0.05]
# 添加温度
lssj['c'] = c

# 添加场内代码
cn = [
'001051', '160706', '160119', '007464', # 大小盘
'005561', '481012', # 策略
'008086', '008887', '004752', '011102', '161024', # 主题
'000248', '162412', '001594', '160632', '004069', '001344', '006327' # 行业
]
lssj['cn'] = cn
'''
实时数据
'''
def jsl_data(code):
# """
# 集思录数据
# """
# 集思录接口
def jsl_api(api):
# 提取数据并转换成json格式
api_data = requests.get(api).json()['rows']
# 创建空列表存储集思录全部数据
jsl_all_data = []
for i in api_data:
# 循环遍历从api接口中获取所有数据
jsl_all_data.append(i['cell'])
# 数据转换
df = pd.DataFrame(jsl_all_data)
# 筛选指定列
df = df[['fund_id', 'fund_nm', 'price', 'increase_rt', 'discount_rt']]
# 修改列名
df.columns = ['code', 'fund_nm', 'price', 'increase_rt', 'discount_rt']
# 将代码作为列索引
df = df.set_index('code')
# 显示所有列
pd.set_option('display.max_columns', None)
# 显示所有行
pd.set_option('display.max_rows', None)
# 返回全部数据
return df
# 调用集思录的方法
data = pd.concat([jsl_api(jsl_api_etf), jsl_api(jsl_api_yz), jsl_api(jsl_api_om)], sort=False)
# 筛选列索引指定代码的数据
data = data.loc[codes]
# 删除%号
data = data.replace('\\%', '', regex=True)
def zdyhszh(val):
"""
自定义函数转换
移除%
移除-
"""
new_val = val.replace('%', '').replace('-', '0')
return new_val
# 通过自定义函数清洗数据
data[['price', 'increase_rt', 'discount_rt']] = data[['price', 'increase_rt', 'discount_rt']].apply(zdyhszh)
# 索引重置
data.reset_index(inplace=True)
# 转换数据类型 【现价、涨幅、溢价率】
data['code'] = data['code'].astype('int')
data[['price', 'increase_rt', 'discount_rt']] = data[['price', 'increase_rt', 'discount_rt']].astype('float')

# 返回集思录数据
return data

jsl = jsl_data(codes)


'''
数据合并
'''
# 数据拼接
data = pd.merge(jsl, lssj, left_on="code", right_on="code", how="inner")



'''

1、现价<均价 且 现价<昨 且 昨<前 且 前<大
2、现价小于所有均线
3、溢价率小于0,或涨幅大于0
'''
buy = data.query('price<avg and price<z and z<q and q<d')
buy = buy.query('price<ma_30 and price<ma_10 and price<ma_5')
buy = buy.query('discount_rt <0 or increase_rt >0')
# 筛选数据
buy = buy[['cn', 'increase_rt', 'price', 'c']]
# 转换类型后再添加单位
buy['increase_rt'] = buy['increase_rt'].astype('str') + '%'
buy['c'] = buy['c'].astype('str') + '°C'
# 暴力修改列名
buy.columns = ['代码', '涨幅', '现价', '温度']
# 将代码作为列索引
buy = buy.set_index('代码')
print(f'--------买【{len(buy)}】--------')
print(buy)




'''

1、现价>均价 且 现价>昨 且 昨>前 且 前>大
2、现价大于所有均线
3、溢价率大于0,或涨幅小于0
'''
shell = data.query('price>avg and price>z and z>q and q>d')
shell = shell.query('price>ma_30 and price>ma_10 and price>ma_5')
shell = shell.query('discount_rt >0 or increase_rt <0')
# 筛选数据
shell = shell[['cn', 'increase_rt', 'price', 'c']]
# 转换类型后再添加单位
shell['increase_rt'] = shell['increase_rt'].astype('str') + '%'
shell['c'] = shell['c'].astype('str') + '°C'
# 暴力修改列名
shell.columns = ['代码', '涨幅', '现价', '温度']
# 将代码作为列索引
shell = shell.set_index('代码')
print(f'--------卖【{len(shell)}】--------')
print(shell)

print(f'''
----------------------
程序全部运行完成,可买的有【{len(buy)}】个,可卖的有【{len(shell)}】个!''')