-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataPatternX.py
67 lines (56 loc) · 2.48 KB
/
DataPatternX.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
from argparse import RawDescriptionHelpFormatter
import pandas as pd
import argparse
from db.connection import DBConnection
from db.queries import QueryExecutor
from pattern_query.patterns import list_patterns
from plotter import plot_chart
def main(pattern_names, table, save_path: str = None, plot: bool = False):
db_connection = DBConnection()
try:
query_executor = QueryExecutor(db_connection)
if plot:
results, all_data = query_executor.execute_pattern_query(pattern_names, table, True)
plot_chart.plot(all_data, patterns=results)
else:
results = query_executor.execute_pattern_query(pattern_names, table, False)
if save_path:
try:
combined_df = pd.concat([res.get('df').assign(Pattern=res.get('name')) for res in results])
combined_df.to_csv(save_path, index=False)
print('Results saved to', save_path)
except Exception as e:
print('Error saving results:', e)
elif not plot:
for result in results:
print(result.get('name'), 'Patterns:')
print(result.get('df'))
finally:
db_connection.close_connection()
if __name__ == '__main__':
parser = argparse.ArgumentParser(
formatter_class=RawDescriptionHelpFormatter,
description='CandleQuery - Candlestick Pattern Query.'
)
# positional argument
parser.add_argument('pattern_name', nargs='*',
help='Name or names of the candlestick patterns to query')
parser.add_argument('-t', '--table', type=str,
help='Name of the table in database.')
parser.add_argument('-l', '--list', action='store_true',
help='List all available candlestick patterns')
parser.add_argument('-s', '--save', type=str, nargs='?', const='result.csv',
help='Path to save the results in a CSV file (default: result.csv)')
parser.add_argument('-p', '--plot', action='store_true',
help='Plot the results')
args = parser.parse_args()
if args.list:
print('Available patterns:')
for pattern in list_patterns():
print(f'- {pattern}')
elif args.pattern_name:
if not args.table:
parser.error('Table name is required when using pattern names')
main(args.pattern_name, args.table, save_path=args.save, plot=args.plot)
else:
parser.print_help()