forked from ejp-zz/upstox_python_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
89 lines (71 loc) · 2.77 KB
/
utils.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
from datetime import datetime, date
import logging
import os
EXPIRIES = ['04-01-2018', '11-01-2018', '18-01-2018', '25-01-2018', '01-02-2018',
'01-02-2018', '08-02-2018', '15-02-2018', '22-02-2018', '01-03-2018',
'08-03-2018', '15-03-2018', '22-03-2018', '29-03-2018', '05-04-2018',
'12-04-2018', '19-04-2018', '26-04-2018', '03-05-2018', '10-05-2018',
'17-05-2018', '24-05-2018', '31-05-2018', '31-05-2018', '07-06-2018',
'14-06-2018', '21-06-2018', '28-06-2018', '28-06-2018', '05-07-2018',
'12-07-2018', '19-07-2018', '26-07-2018', '02-08-2018', '02-08-2018',
'09-08-2018', '16-08-2018', '23-08-2018', '30-08-2018', '06-09-2018',
'13-09-2018', '20-09-2018', '27-09-2018', '04-10-2018', '11-10-2018',
'18-10-2018', '25-10-2018', '01-11-2018', '08-11-2018', '15-11-2018',
'22-11-2018', '29-11-2018', '06-12-2018', '13-12-2018', '20-12-2018',
'27-12-2018', '03-01-2019']
MAX_LOGIN_TRIES = 10
BUY = 'B'
SELL = 'S'
TIMEOUT = 10
def round_off(num, div=0.1):
x = div * round(num / div)
return float(x)
def get_trade_hours(date):
o = datetime.strptime(date.strftime('%d-%m-%y') + '-09:15', '%d-%m-%y-%H:%M')
c = datetime.strptime(date.strftime('%d-%m-%y') + '-15:15', '%d-%m-%y-%H:%M')
return o, c
def ts_to_datetime(ts=None):
if ts is None:
return ts
return datetime.fromtimestamp(ts / 1000)
def thursdays():
from calendar import Calendar
c = Calendar()
days = []
thursday = 3
for i in range(1, 13):
for d in c.itermonthdates(2018, i):
if d.weekday() == thursday:
days.append(d)
with open('thursdays.txt', 'w') as f:
for day in days:
f.write(day.strftime('%d-%m-%Y\n'))
def get_expiry_dates(month=1):
dates = []
for e in EXPIRIES:
d = int(e[0:2])
m = int(e[3:5])
Y = int(e[6:])
if m == month:
dates.append(date(day=d, month=m, year=Y))
return dates
def create_logger(name, console=False, level=logging.INFO):
log_dir = os.path.join(os.getcwd(), 'logs')
if not os.path.exists(log_dir):
os.makedirs(log_dir)
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
name = name + date.today().strftime(' %m-%d-%Y') + '.log'
fmt = logging.Formatter('[{asctime} - {levelname}] {name} - {message}',
datefmt='%H:%M:%S',
style='{')
if console:
ch = logging.StreamHandler()
ch.setFormatter(fmt)
ch.setLevel(level)
logger.addHandler(ch)
fh = logging.FileHandler(os.path.join(log_dir, name))
fh.setFormatter(fmt)
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
return logger