Skip to content

Commit

Permalink
Merge pull request ctpbee#134 from ctpbee/dev
Browse files Browse the repository at this point in the history
add use login in __init__ function
  • Loading branch information
somewheve authored Feb 2, 2021
2 parents a2b75b1 + 332c62b commit 4c28c8b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# ctpbee
bee bee .... for developer's trading ~~
bee bee .... for developer's trading ~

> tiny but strong
Expand Down
5 changes: 3 additions & 2 deletions ctpbee/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
for the future of life
"""
__version__ = '1.3.5'
__status__ = 'release level'
__status__ = 'develop level'

# About core
# Here are some import
from ctpbee.app import CtpBee
from ctpbee.signals import common_signals
from ctpbee.context import current_app, switch_app, get_app, del_app
Expand All @@ -18,3 +18,4 @@
from ctpbee.util import RiskLevel
from ctpbee.func import hickey, get_ctpbee_path
from ctpbee.data_handle.generator import HighKlineSupporter
from ctpbee.message import Mail
11 changes: 5 additions & 6 deletions ctpbee/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,10 @@ def start_all(self, app_func, info=True, interface="ctp", in_front=300):
* interface: 接口名字
* in_front: 相较于开盘提前多少秒进行运行登陆.单位: seconds
"""
print("""
Ctpbee 7*24 Manager started !
Warning: program will automatic start at trade time ....
Hope you will have a good profit ^_^
""")
print(
"Hi, ctpbee 7*24 Manager has been started!\nWarning: program will automatic start at trade time,"
"we recommend you to use pickle to save and reload variables....\n"
"Hope you will have a good profit ^_^")
if not isfunction(app_func):
raise TypeError(f"请检查你传入的app_func是否是创建app的函数, 而不是{type(app_func)}")
for i, v in self.open_trading[interface].items():
Expand All @@ -305,7 +304,7 @@ def start_all(self, app_func, info=True, interface="ctp", in_front=300):
status = self.auth_time(current)

if info:
print("ctpbee manager running ---> ^_^ ")
print(f"{current.strftime('%Y-%m-%d %H:%M:%S')} ctpbee manager running ---> ^_^ ")

if p is None and status:
p = Process(target=self.run_all_app, args=(app_func,))
Expand Down
30 changes: 16 additions & 14 deletions ctpbee/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ def send_trade(self, order: OrderData or TradeData):


class Mail(MessageHelper):
def __init__(self, config: dict):
self.to = config.get("TO", [])
self.passwd = config.get("PASSWD")
self.user_email = config.get("USER_EMAIL")
self.port = config.get("PORT", 465)
self.smtp = config.get("SERVER_URI")
def __init__(self, mail_config: dict):
self.to = mail_config.get("TO", [])
self.passwd = mail_config.get("PASSWD")
self.user_email = mail_config.get("USER_EMAIL")
self.port = mail_config.get("PORT", 465)
self.smtp = mail_config.get("SERVER_URI")

self.smtp_api = smtplib.SMTP_SSL(self.smtp, self.port)
self.smtp_api.login(self.user_email, self.passwd)

def send_message(self, title="ctpbee 日内邮件提醒", sub="日内邮件提醒", message: str = None):
email = MIMEMultipart()
Expand All @@ -32,10 +35,8 @@ def send_message(self, title="ctpbee 日内邮件提醒", sub="日内邮件提
email['To'] = self.to[0]
text = MIMEText(f"{datetime.now()} \n邮件类型: {sub}\n{message if message is not None else ''}")
email.attach(text)
smtp = smtplib.SMTP_SSL(self.smtp, self.port)
smtp.login(self.user_email, self.passwd)
try:
smtp.sendmail(self.user_email, self.to, email.as_string()) # 发送邮件
self.smtp.sendmail(self.user_email, self.to, email.as_string()) # 发送邮件
except smtplib.SMTPDataError as e:
print(f"{datetime.now()} 邮件发送失败 Reason: {e}")

Expand All @@ -49,13 +50,14 @@ def send_trade(self, order: OrderData or TradeData):
direction = '多' if order.direction == Direction.LONG else '空'
else:
direction = '空' if order.direction == Direction.LONG else '多'

offset = '开' if order.offset == Offset.OPEN else '平'
beh = offset + direction
text = MIMEText(f"报单价格: {order.price}\n报单手数: {order.volume} \n"
f"报单行为: {'开' if order.offset == Offset.OPEN else '平'}{direction} \n")
f"报单行为: {beh} \n")
email.attach(text)
smtp = smtplib.SMTP_SSL(self.smtp, self.port)
smtp.login(self.user_email, self.passwd)
try:
smtp.sendmail(self.user_email, self.to, email.as_string()) # 发送邮件
self.smtp_api.sendmail(self.user_email, self.to, email.as_string()) # 发送邮件
except smtplib.SMTPDataError as e:
print(f"{datetime.now()} 邮件发送失败 Reason: {e}")

Expand All @@ -73,5 +75,5 @@ class DingTalk(MessageHelper):
"PASSWD": "passwd",
"PORT": 465
}
mail = Mail(config=config)
mail = Mail(mail_config=config)
mail.send_message(message="你好呀")

0 comments on commit 4c28c8b

Please sign in to comment.