Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
397 lines (287 loc) · 10.4 KB
/
Copy pathutils.py
File metadata and controls
397 lines (287 loc) · 10.4 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import os
import sys
import re
import datetime
import inspect
import logging
from logging.handlers import TimedRotatingFileHandler
from rich.logging import RichHandler
from collections import namedtuple
from email.message import EmailMessage
from markdown2 import Markdown
import pandas as pd
from jinja2 import Environment, FileSystemLoader
import os, sys, re
import locale
import logging
from enum import Enum
import traceback
from pandas import DataFrame, DatetimeIndex, merge, to_datetime, to_timedelta
from trading.common import mailer
from cryptography.fernet import Fernet
locale.setlocale(locale.LC_ALL, 'en_CA.UTF-8')
LOG_DIR = os.path.join(os.getcwd(), 'logs')
GROUP_NAMES = namedtuple('GroupName',
['all', 'common', 'new', 'removed', 'name'])
class Markets(str, Enum):
NIFTY = 'nifty'
CRYPTO = 'crypto'
SPX = 'spx'
class HLOC:
Col_ADJ_CLOSE = 'Adj Close'
Col_CLOSE = 'Close'
Col_High = 'High'
Col_Low = 'Low'
Col_Open = 'Open'
Col_Volume = 'Volume'
TICKER_INTERVAL_MINUTES = {
"1m": 1,
"5m": 5,
"15m": 15,
"30m": 30,
"1h": 60,
"60m": 60,
"2h": 120,
"4h": 240,
"6h": 360,
"12h": 720,
"1d": 1440,
"1w": 10080,
"m": 1,
"h": 60,
"d": 1440,
"w": 10080,
}
def set_highlighted_excepthook():
import sys, traceback
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import TerminalFormatter
lexer = get_lexer_by_name("pytb" if sys.version_info.major < 3 else "py3tb")
formatter = TerminalFormatter()
def myexcepthook(type, value, tb):
tbtext = ''.join(traceback.format_exception(type, value, tb))
sys.stderr.write(highlight(tbtext, lexer, formatter))
sys.excepthook = myexcepthook
def set_pandas_options():
pd.options.display.float_format = '{:.2f}'.format
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
def check_and_create_directory(dir):
if not os.path.isdir(dir):
os.makedirs(dir)
def get_token_name_without_quote_currency(full_token_name):
return full_token_name.split('-')[0].split('_')[0].split('/')[0]
def print_stack_trace():
"""
Prints the current call stack trace using the logging module,
without raising an exception.
"""
stack_trace = traceback.extract_stack()
# Log the full stack trace
logger.info("Current Call Stack Trace:")
for frame in stack_trace[:
-1]: # Exclude the last frame (this function itself)
logger.info(
f"File: {frame.filename}, Line: {frame.lineno}, Function: {frame.name}"
)
logger.info(f" Code: {frame.line.strip()}")
def get_logger(log_name='cpr-btc', should_add_ts=False, use_rich=False):
logger = logging.getLogger('root')
if not len(logger.handlers):
logger.setLevel(logging.INFO)
formatter = logging.Formatter(
'%(asctime)s, %(levelname)s : [%(filename)s:%(lineno)s - %(funcName)20s() ] - %(message)s'
)
if use_rich:
rich_handler = RichHandler(rich_tracebacks=True, )
rich_handler.setLevel(logging.DEBUG)
logger.addHandler(rich_handler)
else:
# Stream handler for printing to console
stdout_handler = logging.StreamHandler()
stdout_handler.setLevel(logging.INFO)
stdout_handler.setFormatter(formatter)
logger.addHandler(stdout_handler)
check_and_create_directory(LOG_DIR)
if should_add_ts:
log_name += datetime.datetime.now().strftime("__%Y-%m-%d__%H-%M")
# Rotating file handler for saving logs to file
logger_path = os.path.join(LOG_DIR, f'{log_name}.log')
file_handler = TimedRotatingFileHandler(filename=logger_path,
when='midnight',
backupCount=10)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.info(f'Logger path = {logger_path}')
print_stack_trace()
return logger
logger = logging.getLogger('root')
class TimeFrame(str, Enum):
monthly = 'm'
weekly = 'w'
daily = 'd'
half = '4h'
hourly = '1h'
minute_5 = '5m'
# write a function that takes in a date time and returns whether it is 60 minutes of the current time
def is_within_minutes_of_the_current_time(date_time: datetime.datetime,
interval,
offset=datetime.timedelta(hours=9)):
minutes_to_use = TICKER_INTERVAL_MINUTES[interval]
current_time = datetime.datetime.now()
given_time_offset = date_time + offset
is_it_ok = (current_time - given_time_offset
).total_seconds() / minutes_to_use < minutes_to_use
total_secs = abs((current_time - given_time_offset).total_seconds())
return total_secs / minutes_to_use < minutes_to_use
def resample_to_interval(dataframe: DataFrame, interval):
"""
Resamples the given dataframe to the desired interval.
Please be aware you need to use resampled_merge to merge to another dataframe to
avoid lookahead bias
:param dataframe: dataframe containing close/high/low/open/volume
:param interval: to which ticker value in minutes would you like to resample it
:return:
"""
if isinstance(interval, str):
interval = TICKER_INTERVAL_MINUTES[interval]
df = dataframe.copy()
df = df.set_index(DatetimeIndex(df["Date"]))
ohlc_dict = {
"Open": "first",
"High": "max",
"Low": "min",
"Close": "last",
"Volume": "sum",
"Adj Close": "last"
}
# Resample to "left" border as dates are candle open dates
df = df.resample(str(interval) + "min", label="left").agg(ohlc_dict).dropna()
df.reset_index(inplace=True)
return df
def show_numbers(value):
# return "{:,}".format(value)
return locale.currency(value, symbol=False, grouping=True)
def varname(p):
for line in inspect.getframeinfo(inspect.currentframe().f_back)[3]:
m = re.search(r'\bvarname\s*\(\s*([A-Za-z_][A-Za-z0-9_]*)\s*\)', line)
if m:
return m.group(1)
def unix_to_time(unix_ts):
dt = datetime.datetime.fromtimestamp(unix_ts / 1000000000)
return dt
def compare_lists(lst_today, lst_prev):
today = set(lst_today)
prev = set(lst_prev)
common = today.intersection(prev)
new = today - common
removed = prev - common
return common, new, removed
def find_days_since_given_date(far_date):
now = datetime.datetime.now()
days_since_far_date = (now - far_date).days
return days_since_far_date
def markdown_to_html(text):
markdowner = Markdown()
return markdowner.convert(text)
def create_markdown_table(ticker_list, header):
header_text = f'''| {header} |
| -- |
'''
row_fmt = '''| {} |
'''
rows = ''.join([row_fmt.format(ticker) for ticker in ticker_list])
return header_text + rows
# Create an offset of 10 Business days and 10 hours
def get_business_days_offset(days=-56):
today = datetime.datetime.today()
ts = pd.Timestamp(today).date()
bd = pd.tseries.offsets.BusinessDay(offset=datetime.timedelta(days=days))
return (bd + ts).to_pydatetime()
def get_crypto_days_offset(days=-56):
today = datetime.datetime.today().date()
bd = today + datetime.timedelta(days=days)
# convert bd into pd datatime
return pd.to_datetime(bd)
def disable_third_party_logging():
logging.getLogger('yahoo_fin').setLevel(logging.ERROR)
logging.getLogger('requests').setLevel(logging.ERROR)
logging.getLogger('urllib3').setLevel(logging.ERROR)
def compare_lists_to_get_detailed_info(tickers, backup_file):
tickers = sorted(tickers)
prev_tickers = []
if os.path.exists(backup_file):
with open(backup_file, 'r') as f:
lines = tmp_tickers = f.readlines()
if len(lines):
tmp_tickers = lines[0].split(',')
print(f'backup_file={backup_file}; tickers orig= {tmp_tickers}')
prev_tickers = sorted([ticker.strip() for ticker in tmp_tickers])
logger.info(f'Comparing lists: {tickers}; \n\t prev_tickers={prev_tickers}')
common, new, removed = compare_lists(tickers, prev_tickers)
logger.info(f'Comparing lists: {common}; \n\t '
f'new={new} \n\t removed={removed}')
with open(backup_file, 'w') as f:
f.writelines(','.join(tickers))
return common, new, removed
def names_to_tradingview_naming(tickers, exchange='NSE'):
if exchange == 'NSE':
return ','.join([
'NSE:' +
x.replace('.NS', '').replace('-', '_').replace('M&MF', 'M_MFIN')
for x in tickers
])
else:
return ','.join(['KUCOIN:' + x.replace('/USDT', 'USDT') for x in tickers])
def get_html_for_groups(groups):
# Set up the Jinja2 environment to load templates from the "templates" directory
env = Environment(loader=FileSystemLoader('data/templates'))
# Load the "tickers_table_template.html" template from the "templates" directory
template = env.get_template('tickers_table_template.html')
# Render the template with the tickers variable
html_output = template.render(GROUPS=groups)
print(f'html_output = {html_output}')
return html_output
def create_jinja2_data_for_relative_strength(pairs, output_json_file):
# Create a Jinja2 environment
env = Environment(loader=FileSystemLoader('trading/resources'))
# Load the template file
template = env.get_template("pairlists.template")
# Render the template with the list of pairs
output = template.render(pairs=pairs)
# Write the output to a file
with open(output_json_file, "w") as f:
f.write(output)
def send_mail_with_html(html_output, subject):
mail_util = mailer.Mailer()
mail_util.send_message(subject, html_output)
def get_date(offset=datetime.timedelta(days=0)):
dt = datetime.datetime.today() + offset
return dt
def date_get_short(given_dt=None):
if not given_dt:
return datetime.datetime.now().strftime('%Y-%m-%d')
else:
return given_dt.strftime('%Y-%m-%d')
# tickers = ['AAPL', 'GOOGL']
# compare_and_update_list(tickers, 'data/tickers_tabled.ticks')
def generate_key():
"""Generates and returns a new encryption key."""
return Fernet.generate_key()
def encrypt_string(key, plaintext):
"""Encrypts a string using the provided key."""
fernet = Fernet(key)
encrypted = fernet.encrypt(plaintext.encode())
return encrypted
def decrypt_string(key, encrypted):
"""Decrypts an encrypted string using the provided key."""
fernet = Fernet(key)
decrypted = fernet.decrypt(encrypted).decode()
return decrypted
if __name__ == '__main__':
# dt = date_get_short()
# print(f'dt = {dt}')
pass