Directional changes
23411 ワード
Directional changes
What is DC(Directional changes)?
How to?
Terminology
Extreme point / DCC point
DC event / OS event
Implementation in Python
THETA_bakhach=0.1
data_points = market_index['close_scaled'].tolist()
pext = data_points[0]
vals = []
breakpoints = []
yHat = []
for idx, pc in enumerate(data_points):
if idx == 0:
continue
val = (pc - pext)/pext
dif = pc - pext
if abs(val) > THETA_bakhach:
pext = pc
vals.append(val)
breakpoints.append(idx)
yHat.append(data_points[idx])
# endpoint
breakpoints.append(len(data_points) - 1)
vals.append((data_points[-1] - yHat[-1])/yHat[-1])
yHat.append(data_points[-1])
The right-end point is so ugly!! So, add some correction using amount of change .
THETA_bakhach=0.1
DELTA_bakhach=(max(market_index['close_scaled'])-min(market_index['close_scaled'])) * 0.08 #0.08
data_points = market_index['close_scaled'].tolist()
pext = data_points[0]
vals = []
breakpoints = []
yHat = []
for idx, pc in enumerate(data_points):
if idx == 0:
continue
# val = abs((pc - pext)/pext)
val = (pc - pext)/pext
dif = pc - pext
if abs(val) > THETA_bakhach:
pext = pc
vals.append(val)
breakpoints.append(idx)
yHat.append(data_points[idx])
# endpoint
breakpoints.append(len(data_points) - 1)
vals.append((data_points[-1] - yHat[-1])/yHat[-1])
yHat.append(data_points[-1])
rm_idx = []
for vi in range(len(vals))[1:]:
fs = np.sign(vals[vi - 1])
cs = np.sign(vals[vi])
if fs == cs:
rm_idx.append(vi-1)
vals_new = [j for i, j in enumerate(vals) if i not in rm_idx]
breakpoints_new = [j for i, j in enumerate(breakpoints) if i not in rm_idx]
yHat_new = [j for i, j in enumerate(yHat) if i not in rm_idx]
The problem in right-end part is solved now! However, it seems that a lot of unnecessary DC has been detected. For this, add a correction merging adjacent trends.
THETA_bakhach=0.1
DELTA_bakhach=(max(market_index['close_scaled'])-min(market_index['close_scaled'])) * 0.08
data_points = market_index['close_scaled'].tolist()
pext = data_points[0]
vals = []
breakpoints = []
yHat = []
for idx, pc in enumerate(data_points):
if idx == 0:
continue
# val = abs((pc - pext)/pext)
val = (pc - pext)/pext
dif = pc - pext
# if abs(dif) > DELTA_bakhach:
if abs(val) > THETA_bakhach or abs(dif) > DELTA_bakhach:
pext = pc
vals.append(val)
breakpoints.append(idx)
yHat.append(data_points[idx])
# endpoint
breakpoints.append(len(data_points) - 1)
vals.append((data_points[-1] - yHat[-1])/yHat[-1])
yHat.append(data_points[-1])
rm_idx = []
for vi in range(len(vals))[1:]:
fs = np.sign(vals[vi - 1])
cs = np.sign(vals[vi])
if fs == cs:
rm_idx.append(vi-1)
vals_new = [j for i, j in enumerate(vals) if i not in rm_idx]
breakpoints_new = [j for i, j in enumerate(breakpoints) if i not in rm_idx]
yHat_new = [j for i, j in enumerate(yHat) if i not in rm_idx]
Finally, the detected trends look great!
[References]
Reference
この問題について(Directional changes), 我々は、より多くの情報をここで見つけました https://velog.io/@hyangki0119/Directional-changesテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol