forked from vlex05/SMC-Algo-Trading
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vertex.py
282 lines (230 loc) · 8.12 KB
/
Vertex.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
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
from PySide2.QtWidgets import QGraphicsTextItem
from PySide2.QtGui import QBrush,QPen
from PySide2.QtCore import Qt
class Vertex():
def __init__(self,x=None,y=None):
self.x = x
self.y = y
self.type = None
self.last = None
self.next = None
self.breaks = False
self.is_first = False
self.is_last = False
self.protected_low = None
self.protected_high = None
self.is_choch = False
self.is_cos = False
def __repr__(self):
out = []
out.append(self.type)
out.append(self.x)
out.append(self.y)
if self.is_cos:
out.append("COS")
if self.is_choch:
out.append("CHOCH")
return str(out)
def draw(self,scene):
x = self.x
y = self.y
txt = self.type
# if self.is_choch:
# txt+= " + CHOCH"
if self.type == "HH":
text = QGraphicsTextItem(txt)
text.setX(x)
text.setY(y-25)
font = text.font()
font.setPointSize(12)
font.setBold(True)
pen = QPen(Qt.darkBlue)
pen.setWidth(4)
scene.addLine(x, y, x+20, y, pen)
scene.addItem(text)
elif self.type == "HL":
text = QGraphicsTextItem(txt)
text.setX(x)
text.setY(y+5)
font = text.font()
font.setPointSize(12)
font.setBold(True)
pen = QPen(Qt.darkBlue)
pen.setWidth(4)
scene.addLine(x, y, x+20, y, pen)
scene.addItem(text)
elif self.type == "LL":
text = QGraphicsTextItem(txt)
text.setX(x)
text.setY(y+5)
font = text.font()
font.setPointSize(12)
font.setBold(True)
pen = QPen(Qt.darkBlue)
pen.setWidth(4)
scene.addLine(x, y, x+20, y, pen)
scene.addItem(text)
elif self.type == "LH":
text = QGraphicsTextItem(txt)
text.setX(x)
text.setY(y-25)
font = text.font()
font.setPointSize(12)
font.setBold(True)
pen = QPen(Qt.darkBlue)
pen.setWidth(4)
scene.addLine(x, y, x+20, y, pen)
scene.addItem(text)
def locate(self):
print("------------------Vertex.locate() -----------------------")
# ---------------------- check if first -----------------------
if self.last is None:
self.is_first = True
print("is_first")
else:
self.is_first = False
# ------------------------ check if last ------------------------
if self.next is None:
self.is_last = True
print("is_last")
else:
self.is_last = False
# -------------------- check if high or low -----------------------
print("self: " + str(self))
if self.is_first:
print("next: " + str(self.next))
if self.next is not None:
if self.is_over(self.next):
print("self is over next")
self.type = "H"
elif self.is_under(self.next):
print("self is under next")
self.type = "L"
else:
self.type = "EQ"
else:
print("last: " + str(self.last))
if self.is_over(self.last):
print("self is over last")
self.type = "H"
print("self i H")
elif self.is_under(self.last):
print("self is under last")
self.type = "L"
print("self i L")
else:
self.type = "EQ"
# ------------------ if first movement, define first protected H/L--------------------
if self.is_first and self.is_H():
self.protected_high = self
self.protected_low = self.next
elif self.is_first and self.is_L():
self.protected_high = self.next
self.protected_low = self
elif self.last.is_first and self.is_H():
self.protected_high = self
self.protected_low = self.last
elif self.last.is_first and self.is_H():
self.protected_high = self.last
self.protected_low = self
# ------------------------ else check if this H/L break the last one -----------------
else:
# first define the protected high/low at the ones of this vertex
self.protected_low = self.last.protected_low
self.protected_high = self.last.protected_high
# ------------------ if vertex is L --------------------------------
if self.is_L:
if self.is_under(self.protected_low):
self.breaks = True
self.type = "LL"
print("self is LL")
print(self.last.type)
if self.last.type == "H":
print("last is LH")
self.last.type = 'LH'
self.protected_low = self
self.protected_high = self.last
self.last.protected_low = self
self.last.protected_high = self.last
else:
self.protected_high = self.last.protected_high
self.protected_low = self.last.protected_low
if self.breaks:
if self.last.protected_high.type == "HH":
self.is_choch = True
self.is_cos = False
elif self.last.protected_high.type == "LH":
self.is_choch = False
self.is_cos = True
# ---------------------------------- if vertex is H --------------------
if self.last.is_L:
if self.is_over(self.protected_high):
self.breaks = True
self.type = "HH"
print("self is HH")
print(self.last.type)
if self.last.type == "L":
print("last is HL")
self.last.type = 'HL'
self.protected_low = self.last
self.protected_high = self
self.last.protected_low = self.last
self.last.protected_high = self
else:
self.protected_high = self.last.protected_high
self.protected_low = self.last.protected_low
if self.breaks:
if self.last.protected_high.type == "LL" :
self.is_choch = True
self.is_cos = False
elif self.last.protected_high.type == "HL":
self.is_choch = False
self.is_cos = True
def is_HH(self):
if self.type == "HH":
return True
else:
return False
def is_HL(self):
if self.type == "HL":
return True
else:
return False
def is_LH(self):
if self.type == "LH":
return True
else:
return False
def is_LL(self):
if self.type == "LL":
return True
else:
return False
def is_H(self):
if self.type == "LH" or self.type == 'HH' or self.type == 'H':
return True
else:
return False
def is_L(self):
if self.type == "LL" or self.type == 'HL' or self.type == 'L':
return True
else:
return False
def set_next(self,n):
self.next = n
# self.trend()
def set_last(self,l):
self.last = l
# self.trend()
def break_by(self):
return self.breaker
def is_over(self,other):
if self.y > other.y:
return True
else:
return False
def is_under(self, other):
if self.y < other.y:
return True
else:
return False