-
Notifications
You must be signed in to change notification settings - Fork 105
/
main.py
138 lines (84 loc) · 3.5 KB
/
main.py
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
# imports
import time
import logging
import os
from api_trader import ApiTrader
from tdameritrade import TDAmeritrade
from gmail import Gmail
from mongo import MongoDB
from assets.pushsafer import PushNotification
from assets.exception_handler import exception_handler
from assets.helper_functions import selectSleep
from assets.timeformatter import Formatter
from assets.multifilehandler import MultiFileHandler
class Main:
def connectAll(self):
""" METHOD INITIALIZES LOGGER, MONGO, GMAIL, PAPERTRADER.
"""
# INSTANTIATE LOGGER
file_handler = MultiFileHandler(
filename=f'{os.path.abspath(os.path.dirname(__file__))}/logs/error.log', mode='a')
formatter = Formatter('%(asctime)s [%(levelname)s] %(message)s')
file_handler.setFormatter(formatter)
ch = logging.StreamHandler()
ch.setLevel(level="INFO")
ch.setFormatter(formatter)
self.logger = logging.getLogger(__name__)
self.logger.setLevel(level="INFO")
self.logger.addHandler(file_handler)
self.logger.addHandler(ch)
# CONNECT TO MONGO
self.mongo = MongoDB(self.logger)
mongo_connected = self.mongo.connect()
# CONNECT TO GMAIL API
self.gmail = Gmail(self.logger)
gmail_connected = self.gmail.connect()
if mongo_connected and gmail_connected:
self.traders = {}
self.accounts = []
self.not_connected = []
return True
return False
@exception_handler
def setupTraders(self):
""" METHOD GETS ALL USERS ACCOUNTS FROM MONGO AND CREATES LIVE TRADER INSTANCES FOR THOSE ACCOUNTS.
IF ACCOUNT INSTANCE ALREADY IN SELF.TRADERS DICT, THEN ACCOUNT INSTANCE WILL NOT BE CREATED AGAIN.
"""
# GET ALL USERS ACCOUNTS
users = self.mongo.users.find({})
for user in users:
try:
for account_id in user["Accounts"].keys():
if account_id not in self.traders and account_id not in self.not_connected:
push_notification = PushNotification(
user["deviceID"], self.logger)
tdameritrade = TDAmeritrade(
self.mongo, user, account_id, self.logger, push_notification)
connected = tdameritrade.initialConnect()
if connected:
obj = ApiTrader(user, self.mongo, push_notification, self.logger, int(
account_id), tdameritrade)
self.traders[account_id] = obj
time.sleep(0.1)
else:
self.not_connected.append(account_id)
self.accounts.append(account_id)
except Exception as e:
logging.error(e)
@exception_handler
def run(self):
""" METHOD RUNS THE TWO METHODS ABOVE AND THEN RUNS LIVE TRADER METHOD RUNTRADER FOR EACH INSTANCE.
"""
self.setupTraders()
trade_data = self.gmail.getEmails()
for api_trader in self.traders.values():
api_trader.runTrader(trade_data)
if __name__ == "__main__":
""" START OF SCRIPT.
INITIALIZES MAIN CLASS AND STARTS RUN METHOD ON WHILE LOOP WITH A DYNAMIC SLEEP TIME.
"""
main = Main()
connected = main.connectAll()
while connected:
main.run()
time.sleep(selectSleep())