怎么科学合理的定投基金,使得自己的收益最高
在基金里面有一个定投的功能,可以选定日期让系统自动帮你在这个时间点自动购买基金,这样可以帮助我们节省大量的时间,非常适合我这种懒人或者没什么时间但是又想去理财的人,但是这样有一点坏处就是你无法得知这个是否是最低点,因为都是选定一天,所以有时候会出现很尴尬的局面就是它下跌的时候不买入,它上升的时候却偏偏买入,这样一来二去我们的基金就往往没什么钱赚,甚至还抱着不亏本就好心态,所以为了既让我们可以存钱下来又可以帮助我们进行理财,我就根据天天基金网的估算净值来写了一个脚本,希望能帮助大家省心又省力
首先我借用的天天基金自选的API来进行获取估算
然后程序会根据你给出来的基金代码来获取今天的净值,最新的估计,以及上涨幅度等
接下来程序会根据基金上涨的幅度来进行判断,是升还是降
并且做出相应的判断等,这里面的内容,可以自行修改
然后根据该判断去发送到你的邮箱里面,那么你就可以在3点之前获取到最新的估算的净值并且去做出相应的判断等
这样一来就避免了一直重复查看的重复性动作,可以节省更多的时间去做一些有意义的事情,让机器去帮我们完成
我们的程序写好之后,我们还得去做一个定时任务才行,window系统的话,可以自行百度看看怎么做一个定时任务,会Linux系统的话就好办了
直接远程登录Linux系统
进入你的定时任务列表
vi /etc/crontab
输入这条命令即可,45代表的是分钟,14代表的是小时,第一个cd进入的代码存放的路径
第二个是代码存入的具体位置
然后设置好之后,系统就会每天14点45准时发送一封基金的告警邮件给我们,提醒我们是该买入还是卖出还是不要动手,让更多的事情交给机器来完成这样我们就可以省下更多的时间去钻研我们的学术了
45 14 * * * root cd /usr/local/xxxx/xxxxx/ && /usr/bin/python3 /usr/local/xxx/xxx/main.py
看到最后如果觉得对你有用可以在点赞收藏加关注
以下是全部代码
import requests
import sys
from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Accept": "application/json, text/javascript, */*; q=0.01",
"Host": "api.fund.eastmoney.com",
"Origin": "https://favor.fund.eastmoney.com",
"Referer": "https://favor.fund.eastmoney.com/",
}
#在这里填写你的基金代码
data = {
"fcodes": "0000000,000000,00000,000000",
}
list_name = []
list_dwjz = []
list_gsz = []
list_gszzl = []
important_information = []
list_gszzl_1 = []
def parse_url():
url = "https://api.fund.eastmoney.com/favor/GetFundsInfo?"
requests.packages.urllib3.disable_warnings()
html = requests.post(url,headers=headers,data=data,verify=False)
if html.status_code == 200:
get_html(html)
else:
print(html.status_code)
def get_html(html):
global list_name,list_dwjz,list_gsz,list_gszzl
content = html.json()
data = content['Data']['KFS']
for d in data[:-2]:
name = d['SHORTNAME']
list_name.append(name)
#单位净值
dwjz = d['DWJZ']
list_dwjz.append(dwjz)
#净值估算
gsz = d['gsz']
list_gsz.append(gsz)
#估算涨幅
gszzl = d['gszzl']
list_gszzl.append(gszzl)
def get_data():
global important_information,list_gszzl_1
if float(list_gszzl[0]) < 0:
if -3.0 < float(list_gszzl[0]) <= 0:
a = '基金准备小幅下跌,无需关心'.format()
important_information.append(a)
elif -4.0 < float(list_gszzl[0]) <= -3.0:
s = xxx
a = '基金准备下跌可以适当补仓:{}'.format(s)
important_information.append(a)
elif -5.0 < float(list_gszzl[0]) <= -4.0:
s = xxx
a = '基金准备下跌可以适当补仓:{}'.format(s)
important_information.append(a)
elif -6.0 < float(list_gszzl[0]) <= -5.0:
s = xxx
a = '基金准备下跌可以适当补仓:{}'.format(s)
important_information.append(a)
else:
s = xxx
a = '基金准备下跌可以适当补仓:{}'.format(s)
important_information.append(a)
elif float(list_gszzl[0]) >= 5:
a = '基金准备大涨,在取得正收益的情况下,可以适当卖出部分基金'
important_information.append(a)
elif 0 <= float(list_gszzl[0]) <= 5:
a = '该基金目前处于上升阶段,状况比较良好,先继续观察观察,再考虑下一步决定'
important_information.append(a)
if float(list_gszzl[1]) < 0:
if -3.0 < float(list_gszzl[1]) <= 0:
a = '基金准备小幅下跌,无需关心'.format()
important_information.append(a)
elif -4.0 < float(list_gszzl[1]) <= -3.0:
s = xxx
a = '基金准备下跌可以适当补仓:{}'.format(s)
important_information.append(a)
elif -5.0 < float(list_gszzl[1]) <= -4.0:
s = xxx
a = '基金准备下跌可以适当补仓:{}'.format(s)
important_information.append(a)
elif -6.0 < float(list_gszzl[1]) <= -5.0:
s = xxx
a = '基金准备下跌可以适当补仓:{}'.format(s)
important_information.append(a)
else:
s = xxx
a = '基金准备下跌可以适当补仓:{}'.format(s)
important_information.append(a)
elif float(list_gszzl[1]) >= 5:
a = '基金准备大涨,在取得正收益的情况下,可以适当卖出部分基金'
important_information.append(a)
elif 0 <= float(list_gszzl[1]) <= 5:
a = '该基金目前处于上升阶段,状况比较良好,先继续观察观察,再考虑下一步决定'
important_information.append(a)
if float(list_gszzl[2]) < 0:
if -3.0 < float(list_gszzl[2]) <= 0:
a = '基金准备小幅下跌,无需关心'.format()
important_information.append(a)
elif -4.0 < float(list_gszzl[2]) <= -3.0:
s = xxx
a = '基金准备下跌可以适当补仓:{}'.format(s)
important_information.append(a)
elif -5.0 < float(list_gszzl[2]) <= -4.0:
s = xxx
a = '基金准备下跌可以适当补仓:{}'.format(s)
important_information.append(a)
elif -6.0 < float(list_gszzl[2]) <= -5.0:
s = xxx
a = '基金准备下跌可以适当补仓:{}'.format(s)
important_information.append(a)
else:
s = xxx
a = '基金准备下跌可以适当补仓:{}'.format(s)
important_information.append(a)
elif float(list_gszzl[2]) >= 5:
a = '基金准备大涨,在取得正收益的情况下,可以适当卖出部分基金'
important_information.append(a)
elif 0 <= float(list_gszzl[2]) <= 5:
a = '该基金目前处于上升阶段,状况比较良好,先继续观察观察,再考虑下一步决定'
important_information.append(a)
def text_to_html():
trigger_html_str = ''
list_gszzl[0] = '{}%'.format(list_gszzl[0])
list_gszzl[1] = '{}%'.format(list_gszzl[1])
list_gszzl[2] = '{}%'.format(list_gszzl[2])
for i in range(len(important_information)):
tail_html_str = '''<tr>
<td>{}</td>
<td>{}</td>
<td>{}</td>
<td>{}</td>
<td class="m_td">{}</td>
</tr>'''.format(
list_name[i], list_gsz[i], list_gszzl[i], list_dwjz[i], important_information[i])
trigger_html_str = trigger_html_str + tail_html_str
return trigger_html_str
def send_mail(receiver):
host_server = 'smtp.qq.com' # QQ邮箱的SMTP服务器
sender_qq = 'xxxxxxxxxx' # 发件人的QQ号码
pwd = 'xxxxxxxxxx' # QQ邮箱的授权码
sender_qq_mail = 'xxxxxxxxxx' # 发件人邮箱地址
table_html_code = '''
<table width="90%" border="1" cellspacing="0" cellpadding="4" bgcolor="#cccccc" class="tabtop13">
<tr>
<th colspan="5" class="btbg titfont">
</tr>
<tr class="btbg titfont">
<th>基金名称</th>
<th width="40px">净值估算</th>
<th>估算涨幅</th>
<th>单位净值</th>
<th width="40px">应对的做法</th>
</tr>
<!-- trigger -->'''
mail_html = open("table.html", "r", encoding="utf-8").read()
mail_html = mail_html.replace('<!-- imgstart -->', table_html_code)
mail_html = mail_html.replace('<!-- trigger -->', text_to_html())
mail_title = '每日基金汇报情况' # 设置邮件标题
smtp = SMTP_SSL(host_server) # SSL 登录
smtp.set_debuglevel(0) # set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式
smtp.ehlo(host_server) # 连接服务器
smtp.login(sender_qq, pwd) # 邮箱登录
msg = MIMEText(mail_html, "html", 'utf-8') # 填写正文内容
msg["Subject"] = Header(mail_title, 'utf-8') # 填写邮件标题
msg["From"] = sender_qq_mail # 发送者邮箱地址
msg["To"] = receiver # 接收者邮件地址
try:
smtp.sendmail(sender_qq_mail, receiver, msg.as_string()) # 发送邮件函数
smtp.quit() # 发送邮件结束
print("Successfully Send!") # 输出成功标志
except Exception as e:
print("The sever is busy,please continue later.",e)
if __name__ == '__main__':
parse_url()
get_data()
text_to_html()
try:
receiver = sys.argv[1]
except:
receiver = 'xxxxxxxxxxxx' # 收件人邮箱地址
send_mail(receiver) # 调用函数,发送邮件
HTML命名为table.html
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<meta name=Generator content="Microsoft Word 15 (filtered medium)">
<!--[if !mso]>
<style>v\: * {
behavior: url(#default#VML);
}
o\: * {
behavior: url(#default#VML);
}
w\: * {
behavior: url(#default#VML);
}
.shape {
behavior: url(#default#VML);
}
</style><![endif]-->
<style>
.tabtop13 {
}
.tabtop13 td {
background-color: #ffffff;
height: 25px;
line-height: 150%;
}
.btbg {
background: #0072c6 !important;
}
.m_td {
text-align: center;
}
.titfont {
font-size: 16px;
font-weight: bold;
color: #fff;
background-color: #e9faff;
}
<!--
/* Font Definitions */
@font-face {
font-family: 宋体;
panose-1: 2 1 6 0 3 1 1 1 1 1;
}
@font-face {
font-family: "Cambria Math";
panose-1: 2 4 5 3 5 4 6 3 2 4;
}
@font-face {
font-family: 等线;
panose-1: 2 1 6 0 3 1 1 1 1 1;
}
@font-face {
font-family: Calibri;
panose-1: 2 15 5 2 2 2 4 3 2 4;
}
@font-face {
font-family: "\@宋体";
panose-1: 2 1 6 0 3 1 1 1 1 1;
}
@font-face {
font-family: "\@等线";
panose-1: 2 1 6 0 3 1 1 1 1 1;
}
@font-face {
font-family: 微软雅黑;
panose-1: 2 11 5 3 2 2 4 2 2 4;
}
@font-face {
font-family: "\@微软雅黑";
}
/* Style Definitions */
p.MsoNormal,
li.MsoNormal,
div.MsoNormal {
margin: 0cm;
margin-bottom: .0001pt;
text-align: justify;
text-justify: inter-ideograph;
font-size: 10.5pt;
font-family: 等线;
}
a:link,
span.MsoHyperlink {
mso-style-priority: 99;
color: #0563C1;
text-decoration: underline;
}
a:visited,
span.MsoHyperlinkFollowed {
mso-style-priority: 99;
color: #954F72;
text-decoration: underline;
}
p.msonormal0,
li.msonormal0,
div.msonormal0 {
mso-style-name: msonormal;
mso-margin-top-alt: auto;
margin-right: 0cm;
mso-margin-bottom-alt: auto;
margin-left: 0cm;
font-size: 12.0pt;
font-family: 宋体;
}
span.EmailStyle18 {
mso-style-type: personal;
font-family: "Calibri", sans-serif;
color: black;
}
span.EmailStyle19 {
mso-style-type: personal;
font-family: 等线;
color: windowtext;
}
span.EmailStyle20 {
mso-style-type: personal;
font-family: 等线;
color: windowtext;
}
span.EmailStyle21 {
mso-style-type: personal;
font-family: 等线;
color: #1F497D;
}
span.EmailStyle22 {
mso-style-type: personal;
font-family: 等线;
color: #1F497D;
}
span.EmailStyle23 {
mso-style-type: personal;
font-family: 等线;
color: #1F497D;
}
span.EmailStyle24 {
mso-style-type: personal;
font-family: 等线;
color: #1F497D;
}
span.EmailStyle25 {
mso-style-type: personal;
font-family: 等线;
color: #1F497D;
}
span.EmailStyle26 {
mso-style-type: personal-reply;
font-family: 等线;
color: #1F497D;
}
.MsoChpDefault {
mso-style-type: export-only;
font-size: 10.0pt;
}
@page WordSection1 {
size: 612.0pt 792.0pt;
margin: 72.0pt 90.0pt 72.0pt 90.0pt;
}
div.WordSection1 {
page: WordSection1;
}
-->
</style>
<!--[if gte mso 9]>
<xml>
<o:shapedefaults v:ext="edit" spidmax="1026"/>
</xml><![endif]-->
<!--[if gte mso 9]>
<xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1"/>
</o:shapelayout>
</xml><![endif]-->
</head>
<body lang=ZH-CN link="#0563C1" vlink="#954F72" style='text-justify-trim:punctuation'>
<div class=WordSection1>
<p class=MsoNormal align=left style='text-align:left'><span
style='font-size:12.0pt;font-family:"微软雅黑",sans-serif'></span><span
style='font-size:12.0pt;font-family:"Calibri",sans-serif;color:#1F497D'> <span lang=EN-US>
<o:p></o:p>
</span></span></p>
<p class=MsoNormal align=left style='text-align:left'><span lang=EN-US
style='font-size:12.0pt;font-family:"Calibri",sans-serif'></span><span
style='font-size:12.0pt;font-family:"微软雅黑",sans-serif'></span><span
style='font-size:12.0pt;font-family:"Calibri",sans-serif'> <span lang=EN-US>
<o:p></o:p>
</span></span></p>
<p class=MsoNormal align=left style='text-align:left'><span
style='font-size:12.0pt;font-family:"微软雅黑",sans-serif'></span><span lang=EN-US
style='font-size:12.0pt;font-family:"Calibri",sans-serif'></span><span
style='font-size:12.0pt;font-family:"微软雅黑",sans-serif'></span><span
style='font-size:12.0pt;font-family:"Calibri",sans-serif'> <span lang=EN-US>
<o:p></o:p>
</span></span></p>
<p class=MsoNormal align=left style='text-align:left'><span
style='font-size:12.0pt;font-family:"微软雅黑",sans-serif'></span><span
style='font-size:12.0pt;font-family:"Calibri",sans-serif'> </span><span lang=EN-US
style='font-size:12.0pt;font-family:"Calibri",sans-serif'><a
href="mailto:gz_dsoc@macroview.com"></a></span><span lang=EN-US
style='font-size:12.0pt;font-family:"Calibri",sans-serif'>
<o:p></o:p>
</span></p>
<p class=MsoNormal align=left style='text-align:left'><span
style='font-size:12.0pt;font-family:"微软雅黑",sans-serif'></span><span lang=EN-US
style='font-size:12.0pt;font-family:"Calibri",sans-serif'><o:p></o:p></span>
</p>
<p class=MsoNormal align=left style='text-align:left'><span lang=EN-US
style='font-size:12.0pt;font-family:"Calibri",sans-serif'>
<o:p> </o:p>
</span></p>
<p class=MsoNormal><b><u><span lang=EN-US style='font-size:12.0pt;font-family:"Calibri",sans-serif'></span></u></b><b><span lang=EN-US
style='font-size:12.0pt;font-family:"Calibri",sans-serif'> </span></b><b><u><span
lang=EN-US
style='font-size:12.0pt;font-family:"Calibri",sans-serif'>
<o:p></o:p>
</span></u></b></p>
<p class=MsoNormal><b><span lang=EN-US style='font-size:12.0pt;font-family:"Calibri",sans-serif'>
</span></b><span lang=EN-US style='font-size:12.0pt;font-family:"Calibri",sans-serif'></span><span
lang=EN-US style='font-size:12.0pt;font-family:"Calibri",sans-serif'> </span><span lang=EN-US
style='font-size:12.0pt;font-family:"Calibri",sans-serif'>
<o:p></o:p>
</span></p>
<p class=MsoNormal><b><span lang=EN-US style='font-size:12.0pt;font-family:"Calibri",sans-serif'>
</span></b><span lang=EN-US style='font-size:12.0pt;font-family:"Calibri",sans-serif'> </span><span
lang=EN-US style='font-size:12.0pt;font-family:"Calibri",sans-serif'><o:p>
</o:p></span></p>
<p class=MsoNormal><b><span lang=EN-US style='font-size:12.0pt;font-family:"Calibri",sans-serif'>
</span></b><span lang=EN-US style='font-size:12.0pt;font-family:"Calibri",sans-serif'></span><span
lang=EN-US style='font-size:12.0pt;font-family:"Calibri",sans-serif'> </span><span lang=EN-US
style='font-size:12.0pt;font-family:"Calibri",sans-serif'>
<o:p></o:p>
</span></p>
<p class=MsoNormal align=center style='text-align:center'><b><span
style='font-size:12.0pt;font-family:"微软雅黑",sans-serif;color:black'></span></b><b><span
style='font-family:"微软雅黑",sans-serif'> <span lang=EN-US style='color:black'>
<o:p></o:p>
</span></span></b></p>
<!-- imgstart -->
<!-- imgend -->
</div>
</body>
</html>