Skip to content

Commit

Permalink
v4 new files
Browse files Browse the repository at this point in the history
  • Loading branch information
ozdemirozcelik committed Jan 4, 2023
1 parent 6004a4e commit 8ecf51d
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
46 changes: 46 additions & 0 deletions config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# default users and passwords to be defined during database creation
[SECRET]
# change this after installation
WEBHOOK_PASSPHRASE : webhook
# below should be edited via API after the first creation
# changes do not apply after installation
ADMIN_USERNAME : admin
ADMIN_PASSWORD: password
USER1_USERNAME: user1
USER1_PASSWORD: password

# Used to calculate the pair price and moving averages
[EXCHANGE]
# Define start & end times of the traded exchange
SESSION_START : 09:30:00
SESSION_END : 16:00:00
EXCHANGE_TIMEZONE = US/Eastern
# SMA is calculated x min before and after session
SESSION_EXTENSION_MIN : 20

# configuraton for the email notifications
[EMAIL]
ENABLE_EMAIL_NOTIFICATIONS = True
# check for waiting/problematic orders in every x seconds
MAIL_CHECK_PERIOD = 90

MAIL_SERVER : smtp.youremailserver.com
MAIL_PORT : 465
MAIL_USERNAME : [email protected]
MAIL_PASSWORD : yourpassword
MAIL_USE_TLS : False
MAIL_USE_SSL : True

MAIL_SENDER : [email protected]
MAIL_RECIPIENT : [email protected]

MAIL_SUBJECT : Server Problem
MAIL_BODY : waiting orders-possible server problem

[SMA]
ENABLE_SMA_CALC = True
# calculate 20D moving average in every x minutes
# data is from yahoo finance, check for rate limitations: https://pypi.org/project/yfinance/
SMA_CALC_PERIOD = 5


81 changes: 81 additions & 0 deletions email_notifications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
### EMAIL NOTIFICATIONS ###

from flask_mail import Mail, Message
from app import app
from app import iftoday, timediff, configs
from models.signals import SignalModel

# Passphrase is required to register webhooks (& to update account positions & PNL)
PASSPHRASE = configs.get("SECRET", "WEBHOOK_PASSPHRASE")

# configuration of email
app.config["MAIL_SERVER"] = configs.get("EMAIL", "MAIL_SERVER")
app.config["MAIL_PORT"] = int(configs.get("EMAIL", "MAIL_PORT"))
app.config["MAIL_USERNAME"] = configs.get("EMAIL", "MAIL_USERNAME")
app.config["MAIL_PASSWORD"] = configs.get("EMAIL", "MAIL_PASSWORD")
app.config["MAIL_USE_TLS"] = configs.getboolean("EMAIL", "MAIL_USE_TLS")
app.config["MAIL_USE_SSL"] = configs.getboolean("EMAIL", "MAIL_USE_SSL")

email_subject = configs.get("EMAIL", "MAIL_SUBJECT")
email_body = configs.get("EMAIL", "MAIL_BODY")
email_sender = configs.get("EMAIL", "MAIL_SENDER")
email_recipient = configs.get("EMAIL", "MAIL_RECIPIENT")

mail = Mail(app) # instantiate the mail class


def get_waiting_time(signal_to_check):

send_email = False
warned = False
print("*** checking to warn ***")

if signal_to_check:
if iftoday(str(signal_to_check.timestamp)):
diff = timediff(str(signal_to_check.timestamp))

if signal_to_check.error_msg:
if "(warned)" in signal_to_check.error_msg:
warned = True

if int(diff) > 2 and not warned:
print("*** SENDING EMAIL ****")
send_email = True

else:
print("*** nothing to warn ***")

return send_email


def warning_email_context():

with app.app_context(): # being executed through a new thread outside the app context and the Mail object need this
try:

signal_to_check = SignalModel.check_timestamp()

send_email = get_waiting_time(signal_to_check)

if send_email:
msg = Message(
email_subject, sender=email_sender, recipients=[email_recipient]
)
msg.body = email_body
mail.send(msg)
print("*** warning email sent...***")

if signal_to_check.error_msg:
signal_to_check.error_msg = signal_to_check.error_msg + "(warned)"
else:
signal_to_check.error_msg = "(warned)"

signal_to_check.update(signal_to_check.rowid)

else:

print("*** no email ***")

except Exception as e:
print("*** Email Notification Error ***")
print(e)

0 comments on commit 8ecf51d

Please sign in to comment.