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): 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) 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']) 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): api_data = requests.get(api).json()['rows'] jsl_all_data = [] for i in api_data: 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)}】个!''')
|