【发布时间】:2021-07-07 22:08:09
【问题描述】:
我正在制定一个使用支撑位和阻力位的交易策略。我找到这些的方法之一是搜索最大值/最小值(高于/低于前一个和下一个 5 个价格的价格)。
我有一系列平滑的收盘价,我首先尝试使用 for 循环找到它们:
def find_max_min(smoothed_prices) # smoothed_prices = np.array([1.873,...])
avg_delta = np.diff(smoothed_prices).mean()
maximas = []
minimas = []
for index in range(len(smoothed_prices)):
if index < 5 or index > len(smoothed_prices) - 6:
continue
current_value = smoothed_prices[index]
previous_points = smoothed_prices[index - 5:index]
next_points = smoothed_prices [index+1:index+6]
previous_are_higher = all(x > current_value for x in previous_points)
next_are_higher = all(x > current_value for x in next_points)
previous_are_smaller = all(x < current_value for x in previous_points)
next_are_smaller = all(x < current_value for x in next_points)
previous_delta_is_enough = abs(previous[0] - current_value) > avg_delta
next_delta_is_enough = abs(next_points[-1] - current_value) > avg_delta
delta_is_enough = previous_delta_is_enough and next_delta_is_enough
if previous_are_higher and next_are_higher and delta_is_enough:
minimas.append(current_value)
elif previous_are_higher and next_are_higher and delta_is_enough:
maximas.append(current_value)
else:
continue
return maximas, minimas
(这不是我使用的实际代码,因为我删除了它,这可能不起作用,但就是这样)
所以这段代码可以找到最大值和最小值,但它太慢了,我需要在巨大的数组上每秒多次使用该函数。
我的问题是:是否有可能以类似的方式使用 numpy 掩码:
smoothed_prices = s
minimas = s[all(x > s[index] for x in s[index-5:index]) and all(x > s[index] for x in s[index+1:index+6])]
maximas = ...
或者你知道我可以用另一种有效的 numpy 方式来实现它吗?
【问题讨论】: