Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Rockyzsu committed Feb 11, 2020
1 parent 6a8e332 commit 50f0940
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 149 deletions.
93 changes: 83 additions & 10 deletions StockAnalyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import tushare as ts
import datetime
import os
import matplotlib.pyplot as plt
# import matplotlib.pyplot as plt
import numpy as np

pd.set_option('display.max_rows', None)

Expand Down Expand Up @@ -113,27 +114,33 @@ def stock_profit(code, start, end):
:param code: 股票代码
:param start: 开始时间
:param end: 结束时间
:return:
:return: 收益率
'''

k_data = ts.get_k_data(start=start, end=end, code=code)

if len(k_data)==0:
return np.nan

start_price = k_data['close'].values[0]
print("Start price: ", start_price)

end_price = k_data['close'].values[-1]

print("End price: ", end_price)

earn_profit = (end_price - start_price) / start_price * 100
print("Profit: ", round(earn_profit, 2))
return round(earn_profit, 2)


def exclude_kcb(df):
'''
:help: 去除科创板
:param df:
:return:
'''
non_kcb = df[~df['code'].map(lambda x:True if x.startswith('688') else False)]
non_kcb = df[~df['code'].map(lambda x: True if x.startswith('688') else False)]
return non_kcb


Expand All @@ -146,7 +153,7 @@ def plot_percent_distribution(date):

total = []
engine = get_engine('db_daily')
df = pd.read_sql(date,con=engine)
df = pd.read_sql(date, con=engine)
df = exclude_kcb(df)

count = len(df[(df['changepercent'] >= -11) & (df['changepercent'] <= -9.5)])
Expand All @@ -160,18 +167,78 @@ def plot_percent_distribution(date):
total.append(count)
# print(total)
df_figure = pd.Series(total)
plt.figure(figsize=(16,10))
plt.figure(figsize=(16, 10))
X = range(-10, 10)
plt.bar(X, height=total,color='y')
plt.bar(X, height=total, color='y')
for x, y in zip(X, total):
plt.text(x, y + 0.05, y, ha='center', va='bottom')
plt.grid()
plt.xticks(range(-10,11))
plt.xticks(range(-10, 11))
plt.show()


def main():
def year_price_change(year,ignore_new_stock=False):
'''
:year: 年份
:ignore_new_stock: 排除当年上市的新股
计算某年个股的涨幅排名
:return: None 生成excel
'''

year = int(year)

basic = ts.get_stock_basics()
pro = []

name=''
# basic['timeToMarket']=pd.to_datetime(basic['timeToMarket'],format='%Y%m%d')

# 去除当年的新股
if ignore_new_stock:
basic=basic[basic['timeToMarket']< int('{}0101'.format(year))]
name = '_ignore_new_stock'

filename='{}_all_price_change{}.xls'.format(year,name)

for code in basic.index.values:
p = stock_profit(code, '{}-01-01'.format(year), '{}-01-01'.format(year+1))
pro.append(p)

basic['p_change_year'] = pro
basic=basic.sort_values(by='p_change_year', ascending=False)
basic.to_excel(filename, encoding='gbk')


def stock_analysis(filename):
'''
# 分析年度的数据
:return:
'''

df=pd.read_excel(filename,encoding='gbk')
print('mean:\n',df['p_change_year'].mean())
print('max:\n',df['p_change_year'].max())
print('min:\n',df['p_change_year'].min())
print('middle\n',df['p_change_year'].median())
# plt.figure()
# df['p_change_year'].plot.hist()
# plt.show()


def cb_stock_year():
'''
上一年可转债正股的涨跌幅排名
:return:
'''
engine = get_engine('db_stock')
df_cb = pd.read_sql('tb_bond_jisilu', engine)
filename='2019_all_price_change_ignore_new_stock.xls'
df_all=pd.read_excel(filename,encoding='gbk')
zg_codes = list(df_cb['正股代码'].values)
df = df_all[df_all['code'].isin(zg_codes)]
df.to_excel('2019_cb_zg.xls',encoding='gbk')

def main():
## 某个股票某个时间段的成交量 ####
# code = '000069'
# v, ratio = volume_calculation(code,'09:30:00', '10:00:00')
Expand All @@ -193,8 +260,14 @@ def main():
# stock_profit('300333','2019-01-01','2020-02-03')

## 显示价格分布
date='2020-02-07'
plot_percent_distribution(date)
# date = '2020-02-07'
# plot_percent_distribution(date)

# 某年个股涨幅
# year_price_change(2019,True)
# stock_analysis('2019_all_price_change_ignore_new_stock.xls')

cb_stock_year()

if __name__ == '__main__':
main()
107 changes: 103 additions & 4 deletions alert_me.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
# -*-coding=utf-8-*-
# 估价达到自己的设定值,发邮件通知, 每天2.45发邮件
import requests
import tushare as ts
from settings import get_engine, trading_time, llogger, is_holiday, get_mysql_conn
import datetime
import time
import pandas as pd
import numpy as np
import os
import tkinter
import tkinter
import tkinter.messagebox #弹窗库
import threading

def show_box(msg):
tkinter.messagebox.showinfo('注意',msg)


dirname = os.path.dirname(__file__)
full_name = os.path.join(dirname, 'log/alert_me_{}.log'.format(datetime.date.today()))
Expand All @@ -18,8 +27,8 @@
MARKET_OPENING = 0
# ALERT_PERCENTAGE = 3
DELTA_TIME = 30
ZG_ALERT_PERCENT = 5
ZZ_ALERT_PERCENT = 4
ZG_ALERT_PERCENT = 8
ZZ_ALERT_PERCENT = 8
CW_ALERT_PERCENT=-5
DIFF_DELTA_TIME=30
# ALERT_PERCENT_POOL = 3
Expand Down Expand Up @@ -293,6 +302,90 @@ def get_price_diff(self, codes, has_sent_, types,kzz_stocks,kzz_stocks_yjl):
logger.info('发送微信失败')
logger.info(e)

# 使用jsl作为数据源
class ReachTargetJSL():
def __init__(self):
self.session = requests.Session()
self.cookies = {
'auto_reload': 'true',
'kbzw_r_uname': '%E9%87%8F%E5%8C%96%E8%87%AA%E7%94%B1%E4%B9%8B%E8%B7%AF',
'kbz_newcookie': '1',
'kbzw__Session': '1kmak8h8v6pscf5brjllb9hfk3',
'Hm_lvt_164fe01b1433a19b507595a43bf58262': '1578275141,1578879529',
'Hm_lpvt_164fe01b1433a19b507595a43bf58262': '1579488732',
}

self.headers = {
'Sec-Fetch-Mode': 'cors',
'Origin': 'https://www.jisilu.cn',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh,en;q=0.9,en-US;q=0.8',
'X-Requested-With': 'XMLHttpRequest',
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Cache-Control': 'no-cache',
'Referer': 'https://www.jisilu.cn/data/cbnew/',
'Sec-Fetch-Site': 'same-origin',
}

self.params = (
('___jsl', 'LST___t=1579488785838'),
)

self.data = {
'fprice': '',
'tprice': '',
'volume': '',
'svolume': '',
'premium_rt': '',
'ytm_rt': '',
'rating_cd': '',
'is_search': 'Y',
'btype': '',
'listed': 'Y',
'sw_cd': '',
'bond_ids': '',
'rp': '50'
}

def get_info(self):

try:
response = self.session.post('https://www.jisilu.cn/data/cbnew/cb_list/', headers=self.headers, params=self.params,
cookies=self.cookies, data=self.data, timeout=30)
except Exception as e:
print(e)
print('网络超时')
else:
ret = response.json()

for body_dict in ret.get('rows', []):
# print(item)
item = body_dict.get('cell', {})
bond_nm = item.get('bond_nm', '').strip()
full_price = item.get('full_price')
if full_price:
full_price = float(full_price)

remium_rt = item.get('premium_rt')

increase_rt = item.get('increase_rt')
if increase_rt:
increase_rt = increase_rt.replace('%','')
increase_rt = float(increase_rt)

if abs(increase_rt)> ZZ_ALERT_PERCENT:
'''
发送消息
t = threading.Thread(target=show_box, args=(bond_nm,))
t.start()
'''




if __name__ == '__main__':

Expand All @@ -306,5 +399,11 @@ def get_price_diff(self, codes, has_sent_, types,kzz_stocks,kzz_stocks_yjl):
#
wechat = WechatSend('wei')
logger.info('{} 开始实时行情爬取'.format(datetime.date.today()))
obj = ReachTarget()
obj.monitor()
jsl = True
if jsl:
obj = ReachTargetJSL()
obj.get_info()

else:
obj = ReachTarget()
obj.monitor()
102 changes: 102 additions & 0 deletions fund_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import random
import re

import datetime
import demjson
import requests
import time

from settings import get_mysql_conn
# 基金数据爬虫

today = datetime.datetime.now().strftime('%Y-%m-%d')
headers = {
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36',
'Accept': '*/*',
'Referer': 'http://stockapp.finance.qq.com/mstats/?id=fund_close',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh,en;q=0.9,en-US;q=0.8',
}

conn = get_mysql_conn('db_fund', local='local')
cursor = conn.cursor()

create_table = 'create table if not EXISTS `{}` (`基金代码` varchar(20) PRIMARY KEY,`基金简称` varchar(100),`最新规模-万` float,`实时价格` float,`涨跌幅` float,`成交额-万` float,`单位净值` float,`累计净值` float,`折溢价率` float ,`申购状态` VARCHAR(20),`申赎状态` varchar(20),`基金经理` VARCHAR(200),`成立日期` VARCHAR(20), `管理人名称` VARCHAR(200));'.format(today)

cursor.execute(create_table)
conn.commit()

def fetch_fund_data():
'''
第一次运行
:return:
'''
for p in range(1,114):
print('page ',p)
params = (
('appn', 'rank'),
('t', 'ranklof/chr'),
('p', p),
('o', '0'),
('l', '40'),
('v', 'list_data'),
)
session =requests.Session()
response = session.get('http://stock.gtimg.cn/data/index.php', headers=headers, params=params, verify=False)
ls_data = re.search('var list_data=(.*?);',response.text,re.S)
ret = ls_data.group(1)
js=demjson.decode(ret)
detail_url = 'http://gu.qq.com/{}'
query_string = js.get('data')
time.sleep(5*random.random())

insert_data = 'insert into `{}` VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'.format(today)
check_code_exists = 'select count(*) from `{}` WHERE `基金代码`=%s'.format(today)

for code in query_string.split(','):

cursor.execute(check_code_exists,(code[2:]))
ret = cursor.fetchone()
if ret[0]>0:
continue

r = session.get(detail_url.format(code), headers=headers)
search_str = re.search('<script>SSR\["hqpanel"\]=(.*?)</script>',r.text)
# time.sleep(5*random.random())

if search_str:
s = search_str.group(1)
js_ = demjson.decode(s)

sub_js = js_.get('data').get('data').get('data')
zxjg= sub_js.get('zxjg')
jgzffd= sub_js.get('jgzffd')
cj_total_amount= sub_js.get('cj_total_amount')

zyjl= float(sub_js.get('zyjl',0))*100

info =js_.get('data').get('data').get('info')
jjdm=info.get('jjdm')
jjjc=info.get('jjjc')
zxgm=info.get('zxgm')
dwjz=info.get('dwjz')
ljjz=info.get('ljjz')
sgzt=info.get('sgzt')
shzt=info.get('shzt')
jjjl=info.get('jjjl')
clrq=info.get('clrq')
glrmc=info.get('glrmc')

try:
cursor.execute(insert_data,(jjdm,jjjc,float(zxgm),float(zxjg),float(jgzffd),float(cj_total_amount),float(dwjz),float(ljjz),float(zyjl),sgzt,shzt,jjjl,clrq,glrmc))
except Exception as e:
print(e)
conn.rollback()
else:
conn.commit()

if __name__=='__main__':
fetch_fund_data()
Loading

0 comments on commit 50f0940

Please sign in to comment.