14
14
import pytz
15
15
import json
16
16
import os
17
+ import sys
17
18
18
19
19
20
# setup for coloured output
70
71
PACK_UP = datetime .time (hour = 15 , minute = 15 , second = 0 )
71
72
72
73
##############################################################
73
- today = datetime .date .today ().strftime ("%d-%m-%Y" )
74
- if not os .path .exists (f"database/{ today } " ):
75
- os .mkdir (f"database/{ today } " )
76
- ml = master_logger (f'database/{ today } /master.log' )
74
+ TODAY = datetime .date .today ().strftime ("%d-%m-%Y" )
75
+ if not os .path .exists (f"database/{ TODAY } " ):
76
+ os .mkdir (f"database/{ TODAY } " )
77
+ ml = master_logger (f'database/{ TODAY } /master.log' )
77
78
ml .info ("-" * 76 )
78
79
ml .info ("-" * 27 + " NEW SESSION DETECTED " + "-" * 27 )
80
+ sys .stderr = open (f"database/{ TODAY } /errorStream.txt" , "a" )
81
+
79
82
##############################################################
80
83
81
84
try :
86
89
quit (0 )
87
90
ml .info ("Successfully loaded user_info.json" )
88
91
ml .info ("-" * 76 )
92
+
89
93
##############################################################
90
94
91
95
HEADERS = {
121
125
quit (0 )
122
126
else :
123
127
IDLE_DELAY = 0
124
- ml .warning ("Running in no delay mode " )
128
+ ml .warning ("[ MODE ] Zero delay " )
125
129
else :
126
130
IDLE_DELAY = args .delay
127
131
ml .info (f"Idle delay set to { IDLE_DELAY } " )
128
132
129
133
if args .np :
130
134
PERIOD_INTERVAL = 0
131
- ml .warning ("Running with zero period interval ! " )
135
+ ml .warning ("[ MODE ] Zero period interval" )
132
136
133
137
if args .t :
134
138
IDLE_DELAY = 1
135
139
PERIOD_INTERVAL = 0
136
- ml .warning ("Running in test mode" )
140
+ Notify .warn ("Running in Test Mode, meant for debugging and demonstration purposes only." )
141
+ ml .warning ("[ MODE ] TEST" )
142
+ print ("" )
137
143
138
144
# developer mode
139
145
DEV_MODE = args .nd and args .np
140
146
if DEV_MODE :
141
147
PENNY_STOCK_THRESHOLD = 0
142
- ml .warning ("Running in developer mode " )
148
+ ml .warning ("[ MODE ] DEVELOPER " )
143
149
144
150
##############################################################
145
151
@@ -199,12 +205,21 @@ def fetch_stocks():
199
205
Deque of tickers of relevant stocks
200
206
201
207
"""
208
+
209
+ global ml
210
+
202
211
# url to grab data from
203
212
url = f'https://in.finance.yahoo.com/gainers?count={ NUM_OF_STOCKS_TO_SEARCH } '
204
213
# request header
205
214
headers = {
206
215
'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36' }
207
- src = requests .get (url = url , headers = headers ).content
216
+ try :
217
+ src = requests .get (url = url , headers = headers ).content
218
+ except Exception as e :
219
+ src = None
220
+ Notify .fatal ("Trade abort due to unexpected error. Check activity log for details" )
221
+ ml .critical ("Encountered error : " , e )
222
+ quit (0 )
208
223
# soup object of source code
209
224
soup = BeautifulSoup (src , "html.parser" )
210
225
rows = soup .find ('table' ).tbody .find_all ('tr' )
@@ -298,6 +313,9 @@ def __init__(self, number, ticker):
298
313
self .IN_SHORT_TRADE = True
299
314
self .price_for_buffer = price
300
315
self .logger = trader_logger (self .ticker )
316
+ self .logger .info ("-" * 76 )
317
+ self .logger .info ("-" * 27 + " NEW SESSION DETECTED " + "-" * 27 )
318
+ self .logger .info ("-" * 76 )
301
319
302
320
def get_initial_data (self ):
303
321
try :
@@ -467,8 +485,10 @@ def lineup_traders(self, tickers):
467
485
count = 1
468
486
for ticker in tickers :
469
487
self .traders .append (Trader (count , ticker ))
488
+ Notify .info (f"Successfully connected Trader #{ count } to { ticker } " , delay = 0.01 )
470
489
count += 1
471
490
ml .info ("Trader lineup complete" )
491
+ print ("" )
472
492
473
493
# initialise traders
474
494
def init_traders (self , Tmode = False ):
@@ -564,15 +584,21 @@ def main():
564
584
"""
565
585
# make sure that market is open
566
586
if not DEV_MODE :
567
- if is_open ():
587
+ if args .t :
588
+ Notify .for_input ("Check Market? (y/n) : " )
589
+ confirm = input ().strip ().lower ()
590
+ print ("" )
591
+ else :
592
+ confirm = "y"
593
+ if is_open () or confirm == "n" :
568
594
pass
569
595
else :
570
596
Notify .fatal ("Market is closed at the moment, aborting." )
571
597
print ("" )
572
598
quit (0 )
573
599
else :
574
600
Notify .warn ("You are in developer mode, if not intended, please quit." )
575
- Notify .info ("Press ENTER to continue, Ctrl + C to quit" )
601
+ Notify .info ("Press ENTER to continue, Ctrl+ C to quit" )
576
602
input ()
577
603
578
604
# allow market to settle to launch Ichimoku strategy
@@ -590,16 +616,14 @@ def main():
590
616
Notify .info ("Finding stocks to focus on ....." )
591
617
try :
592
618
stocks_to_focus = fetch_stocks ()
593
- except Exception as e :
619
+ except :
594
620
stocks_to_focus = []
595
- ml .critical ("Could not fetch relevant stocks : " , e )
621
+ Notify .fatal ("Could not fetch relevant stocks. Verify Network connection and check logs for details." )
622
+ ml .critical ("Could not fetch relevant stocks, Most possibly due to network error" )
596
623
quit (0 )
597
624
Notify .info ("\t Status : Complete" )
598
625
ml .info ("Successfully found relevant stocks" )
599
626
print ("" )
600
- print (stocks_to_focus )
601
- print ("" )
602
- print ("" )
603
627
604
628
# setup traders and begin trade
605
629
master = Master ()
@@ -624,3 +648,6 @@ def main():
624
648
Notify .fatal ("Operation cancelled by user." )
625
649
ml .critical ("Operation cancelled by user" )
626
650
quit (0 )
651
+ except Exception as err :
652
+ Notify .fatal ("Encountered fatal error. Check log for details. Aborting" )
653
+ ml .critical ("Trade abort due to unexpected error : " , err )
0 commit comments