-
Notifications
You must be signed in to change notification settings - Fork 136
/
dataManager.py
95 lines (78 loc) · 3.28 KB
/
dataManager.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
import os,sys
import Singleton
import pandas as pd
from Singleton import Singleton
class DataManager(Singleton):
def DatetimeFormat(self, dataFilePath):
fileparts = os.path.split(dataFilePath)
datafile = fileparts[1]
# print(datafile[-4:])
if datafile[-4:]=='.csv':
df = pd.read_csv(dataFilePath, nrows=1)
timestring = df.iloc[0,0]
ncolon = timestring.count(':')
if ncolon==2:
return "%Y-%m-%d %H:%M:%S"
elif ncolon==1:
return "%Y-%m-%d %H:%M"
else:
nspace = timestring.count(' ')
if nspace==1:
return "%Y-%m-%d %H"
else:
return "%Y-%m-%d"
return ""
# Return True if loading is successfull & the error string if False
# dataPath is the full file path
def loadDataFrame(self, loadDataFile):
# Try importing data file
# We should code a widget that ask for options as : separators, date format, and so on...
try:
# Python contains
if pd.__version__<'2.0.0':
df = pd.read_csv(loadDataFile.filePath,
sep=loadDataFile.separator,
parse_dates=[0],
date_parser=lambda x: pd.to_datetime(x, format=loadDataFile.timeFormat),
skiprows=0,
header=0,
names=["Time", "Open", "High", "Low", "Close", "Volume"],
index_col=0)
else:
df = pd.read_csv(loadDataFile.filePath,
sep=loadDataFile.separator,
parse_dates=[0],
date_format=loadDataFile.timeFormat,
skiprows=0,
header=0,
names=["Time", "Open", "High", "Low", "Close", "Volume"],
index_col=0)
return df, ""
except ValueError as err:
return None, "ValueError error:" + str(err)
except AttributeError as err:
return None, "AttributeError error:" + str(err)
except IndexError as err:
return None, "IndexError error:" + str(err)
except :
return None, "Unexpected error:" + str(sys.exc_info()[0])
def findTimeFrame(self, df):
if len(df.index) > 2:
dtDiff = df.index[1] - df.index[0]
if dtDiff.total_seconds() == 60:
return "M1"
elif dtDiff.total_seconds() == 300:
return "M5"
elif dtDiff.total_seconds() == 900:
return "M15"
elif dtDiff.total_seconds() == 1800:
return "M30"
elif dtDiff.total_seconds() == 3600:
return "H1"
elif dtDiff.total_seconds() == 14400:
return "H4"
elif dtDiff.total_seconds() == 86400:
return "D"
elif dtDiff.total_seconds() == 604800:
return "W"
pass