-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretracement.py
278 lines (214 loc) · 8.62 KB
/
retracement.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
import sys
import forex as f
import logging
from enum import Enum
LOGGER = logging.getLogger(__name__)
BODY_THRESH = 0.6
FIBS = [0.236, 0.382, 0.5, 0.618, 0.705, 0.786, 1]
PIP_THRESH = 30
MIN_PRICE_MEMORY = 20
TOUCH_THRESH = 0.0001
class Retracement(Enum):
NOTHING = 0
FORWARD1 = 1
RETRACING = 2
FORWARD2 = 3
def retracement(trend, data_, dates):
print(trend)
buckets = {}
thresh = close = time = None
state = Retracement.NOTHING
swing_f = None
swing_r = None
price_memory = []
item_idx = 0
last_idx = 0
while True:
if state == Retracement.NOTHING and last_idx:
item_idx = last_idx
last_idx = 0
if item_idx >= len(data_):
break # from while
item = data_[item_idx]
item_idx += 1
price_memory.append(item)
if len(price_memory) > MIN_PRICE_MEMORY:
price_memory = price_memory[1:]
if state == Retracement.FORWARD2:
complete = (trend == f.BearBull.BEARISH and item.close < swing_f) or \
(trend == f.BearBull.BULLISH and item.close > swing_f)
violated = (trend == f.BearBull.BEARISH and item.close > close) or \
(trend == f.BearBull.BULLISH and item.close < close)
if complete or violated:
LOGGER.debug((swing_r, swing_f, close))
fib = abs(swing_r - swing_f) / abs(close - swing_f)
for level in FIBS:
if fib < level:
level_s = str(level)
c, v = buckets.get(level_s, ([], []))
if complete:
LOGGER.debug(("COMPLETE", abs(close - swing_f) * 10000))
c.append(time)
else:
LOGGER.debug(("VIOLATED", abs(close - swing_f) * 10000))
v.append(time)
buckets[level_s] = c, v
break
LOGGER.debug("Done with order block!")
state = Retracement.NOTHING
else:
LOGGER.debug("Still moving forward 2")
elif state == Retracement.RETRACING:
violated = (trend == f.BearBull.BEARISH and item.close > close) or \
(trend == f.BearBull.BULLISH and item.close < close)
complete = (trend == f.BearBull.BEARISH and item.close < swing_r) or \
(trend == f.BearBull.BULLISH and item.close > swing_r)
if violated:
level_s = "1.0+"
v = buckets.get(level_s, [])
v.append(time)
buckets[level_s] = v
LOGGER.debug(("VIOLATED", abs(close - swing_f) * 10000))
state = Retracement.NOTHING
LOGGER.debug("Retracement violation!")
elif complete:
state = Retracement.FORWARD2
LOGGER.debug("Starting moving forward 2 from swing_r: {swingr:.4f}".format(swingr=swing_r))
else:
swing_r = item.close
LOGGER.debug("Still retracing")
elif state == Retracement.FORWARD1:
complete = (trend == f.BearBull.BEARISH and item.close > swing_f) or \
(trend == f.BearBull.BULLISH and item.close < swing_f)
trashed = (trend == f.BearBull.BEARISH and item.close > thresh) or \
(trend == f.BearBull.BULLISH and item.close < thresh)
LOGGER.debug(complete)
LOGGER.debug(trend)
LOGGER.debug((item.close, swing_f))
if complete:
forward_pips = abs(close - swing_f) * 10000
LOGGER.debug(forward_pips)
if forward_pips > PIP_THRESH:
state = Retracement.RETRACING
swing_r = item.close
LOGGER.debug("Starting retracement from swing_f: {swingf:.4f}".format(swingf=swing_f))
elif trashed:
state = Retracement.NOTHING
else:
swing_f = item.close
LOGGER.debug("Still moving forward 1")
LOGGER.debug(state)
elif len(price_memory) < MIN_PRICE_MEMORY:
continue
else:
countertrend = (trend == f.BearBull.BEARISH and item.close > item.open) or \
(trend == f.BearBull.BULLISH and item.close < item.open)
LOGGER.debug((item.time, trend, countertrend, item.open, item.high, item.low, item.close))
if item.high == item.low or not (item.time.year, item.time.month, item.time.day) in dates:
continue
if countertrend:
oc = abs(item.open - item.close)
hl = item.high - item.low
LOGGER.debug((oc, hl))
body = oc / hl > BODY_THRESH
touches_ = [item.high for item in price_memory] if trend == f.BearBull.BEARISH else \
[item.low for item in price_memory]
touch = item.high if trend == f.BearBull.BEARISH else item.low
touches = [t for t in touches_ if abs(t - touch) <= TOUCH_THRESH]
LOGGER.debug((body, touches_, touch, touches))
if body and touches:
close = item.close
time = item.time
swing_f = item.close
thresh = item.low if trend == f.BearBull.BEARISH else item.high
state = Retracement.FORWARD1
last_idx = item_idx + 1
LOGGER.debug("Counter-trend order block: {time}: {open}, {high}, {low}, {close}".format(
open=item.open, close=item.close, high=item.high, low=item.low, time=time))
return buckets
def weekday(data):
return f.filter(data, f.filter_weekday)
def dates(data):
return [(item.time.year, item.time.month, item.time.day) for item in data]
def get_sample_sizes(buckets, complete):
samplesizes = {}
for key in buckets.keys():
entry = buckets[key]
if isinstance(entry, tuple):
if complete:
entry, _ = entry
else:
_, entry = entry
elif complete:
continue
for item in entry:
dayofweek = f.dayofweek(item)
samplesizes[dayofweek] = samplesizes.get(dayofweek, 0) + 1
return samplesizes
def print_buckets_(buckets, complete):
samplesizes = get_sample_sizes(buckets, complete)
len_buckets = 0
for key in buckets.keys():
entry = buckets[key]
if isinstance(entry, tuple):
if complete:
entry, _ = entry
else:
_, entry = entry
elif complete:
continue
if not entry:
continue
len_buckets += len(entry)
print("**** fib: {key} ****".format(key=key))
f.print_complete_summary(entry, [f.Summaries.DAYOFWEEK], samplesizes)
for item in entry:
print("---- {}".format(item))
print()
return len_buckets
def print_buckets(buckets):
print("COMPLETED: ")
len_compl = print_buckets_(buckets, True)
print()
print("VIOLATIONS: ")
len_viol = print_buckets_(buckets, False)
print()
len_total = len_compl + len_viol
if len_total:
pc = len_compl * 100. / len_total
print("--- Complete: {len_compl}/{len_total}, {pc:.2f}".format(
len_compl=len_compl, len_total=len_total, pc=pc))
else:
print("--- Complete: 0/0")
print()
if __name__ == '__main__':
logging.basicConfig(
filename="/tmp/retracement.log",
filemode='w',
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s' +
'%(message)s line:%(lineno)d',
datefmt='%H:%M:%S',
level=logging.DEBUG)
# years = [2016, 2017, 2018]
years = [2018]
# ltf data
datafile_ltf = sys.argv[1]
data_ltf = f.filter(
f.load_candles(datafile_ltf),
f.filter_year,
years)
# day data
datafile_d = sys.argv[2]
data_d = weekday(
f.filter(
f.load_candles(datafile_d),
f.filter_year,
years))
# profile bearish days
beardates = dates(f.filter(data_d, f.filter_bearish, True)) # , False)
bearbuckets = retracement(f.BearBull.BEARISH, data_ltf, beardates)
print_buckets(bearbuckets)
# profile bullish days
bulldates = dates(f.filter(data_d, f.filter_bullish, True)) # , False)
bullbuckets = retracement(f.BearBull.BULLISH, data_ltf, bulldates)
print_buckets(bullbuckets)