-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeframe.py
executable file
·173 lines (122 loc) · 4.39 KB
/
timeframe.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
.. module:: timeframe
:platform: Unix, Windows, Mac OS X
:synopsis: Module to manage timeframes \
(such as 'TF.M1', 'TF.M5', 'TF.H1', 'TF.D1'....).
.. moduleauthor:: Working4coins <[email protected]>
Copyright (C) 2013 "Working4coins" <[email protected]>
You can donate: https://sites.google.com/site/working4coins/donate
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
"""
import datetime
class TimeframeElement:
"""class to manage TimeframeElement such as 'M1', 'M5', 'H1', ..."""
def __init__(self, name='M1', minute=1):
self.__minute = minute
self.__name = name
def timedelta(self):
"""timedelta()
Returns datetime.timedelta of TimeframeElement.
Returns:
A datetime.timedelta of TimeframeElement
example for M15:
datetime.timedelta(0, 900)
"""
return(datetime.timedelta(minutes=self.__minute))
def minutes(self):
"""minutes()
Returns number of minutes for a TimeframeElement.
Returns:
integer
example for M15:
15
"""
return(self.__minute)
def name(self):
"""name()
Returns name of a TimeframeElement.
Returns:
example for M15:
M15
"""
return(self.__name)
def __repr__(self):
return(self.__name)
def __eq__(self, tf_elt):
return(self.__minute == tf_elt.minutes())
def __hash__(self):
return(hash(self.__name))
def floor(self, y):
"""floor(dt)
Return the floor of dt as a datetime, the largest datetime according to this timeframe less than or equal to dt.
NotImplemented
"""
raise(NotImplementedError)
def ceil(self, dt1):
"""ceil(dt)
Return the ceiling of dt as a datetime, the smallest datetime according to this timeframe greater than or equal to dt.
NotImplemented
"""
raise(NotImplementedError)
def round(self, dt1):
"""round(dt)
Return the datetime rounded to nearest timeframe.
NotImplemented
"""
class Timeframe:
"""class to manage Timeframes \
such as 'TF.M1', 'TF.M5', 'TF.H1', 'TF.D1'...."""
def __init__(self):
self.__periods_list = list()
self.append_tf('M1', 1)
self.append_tf('M5', 5)
self.append_tf('M15', 15)
self.append_tf('M30', 30)
self.append_tf('H1', 60)
self.append_tf('H2', 120) #?
self.append_tf('H4', 240)
self.append_tf('H6', 360) #?
self.append_tf('H12', 720) #?
self.append_tf('D1', 1440)
self.append_tf('W1', 10080)
self.append_tf('MN', 43200)
def append_tf(self, name, minutes):
"""Append TimeframeElement to Timeframe.
Args:
name (str): name of TimeframeElement.
minutes (int): delay (in minutes) of TimeframeElement.
"""
self.__dict__[name] = TimeframeElement(name, minutes)
self.__periods_list.append(name)
def timeframes(self):
"""TimeframeElement generator
Returns:
generator of TimeframeElement in ascending order (if append_tf was called with ascending TF)
"""
for timeframe in self.__periods_list:
yield(timeframe)
def from_string(self, name):
"""Append TimeframeElement to Timeframe.
Args:
name (str): name of TimeframeElement.
Returns:
TimeframeElement
Raises:
Exception when timeframe name doesn't exists
"""
if name in self.__dict__:
return(self.__dict__[name])
else:
raise(Exception("Timeframe doesn't exists"))
TF = Timeframe()