-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata.py
110 lines (88 loc) · 3.85 KB
/
data.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
import os
import json
import datetime
from dataLoad import loadDataFromInfluxdb, loadDataFromFile, DataFrameClient
class dataHandler():
"""Class that is a universal handler for data processing"""
loadMethod = None
loadDataOthersArgs = dict()
patternFile = None
noCandles = None
@classmethod
def init(cls, pars):
"""Initiate arguments necessary to perform data operations"""
# Reset arguments
cls.loadMethod = None
cls.loadDataOthersArgs = dict()
cls.patternFile = None
cls.noCandles = None
# specify path for pattern file
cls.patternFile = './static/data/'+pars['patternFile']
# specify number of preloaded candles
cls.noCandles = 1000
# specify arguments for data loading
if pars['loadMethod'] == 'influxdb':
# Specify method for actual data loading
cls.loadMethod = loadDataFromInfluxdb
# influxdb client
cls.loadDataOthersArgs['client'] = DataFrameClient(host='127.0.0.1',
port=8086,
database=pars['dbName'])
elif pars['loadMethod'] == 'file':
# Specify method for actual data loading
cls.loadMethod = loadDataFromFile
# filename
cls.loadDataOthersArgs['filePath'] = './static/data/'+pars['fileUploadVisible']
else:
raise Exception('Data loading method %s unknown during initialization!' %
pars['loadMethod'])
@classmethod
def load(cls, dtLimit, direction):
"""Call method for actual data loading
Returns loaded data"""
if direction not in ('left', 'right'):
raise Exception('Direction unknown during loading new data!')
return cls.loadMethod(dtLimit,
direction,
cls.noCandles,
**cls.loadDataOthersArgs)
@classmethod
def savePattern(cls, startDt, stopDt, direction):
"""Save new pattern to json file of sorted patterns"""
if os.path.isfile(cls.patternFile):
# load pattern file
if os.stat(cls.patternFile).st_size == 0:
patterns = list()
else:
try:
with open(cls.patternFile, 'r') as patternFile:
patterns = json.load(patternFile)
except Exception as e:
raise Exception('Error during loading pattern file: %s' % e)
# append new pattern
try:
patterns.append({'startDt': startDt,
'stopDt': stopDt,
'dir': direction})
# sort updated list by startDt
patterns = sorted(patterns,
key=lambda p: datetime.datetime.strptime(p['startDt'],'%Y-%m-%dT%H:%M:%S.000Z'))
except Exception as e:
raise Exception('Error during updating patterns: %s' % e)
# dumps updated patterns into file
try:
with open(cls.patternFile, 'w') as patternFile:
patternFile.write(json.dumps(patterns, indent=4))
except Exception as e:
raise Exception('Error during saving pattern file: %e' % e)
else:
raise Exception('Pattern file does not exist!')
@classmethod
def loadPatterns(cls):
"""Load and return patterns from json file"""
try:
with open(cls.patternFile, 'r') as patternFile:
patterns = json.load(patternFile)
except:
patterns = []
return patterns