我一直在研究2个移动平均线交叉,这很简单。我想添加三分之一,然后尝试找出是否在4支蜡烛或更少的蜡烛内发生这种情况。
对于两个移动平均线,我使用以下内容:
// if shift5MA > shift0MA
if (shift5MAArray[1] > shift0MAArray[1]) {
//if shift5MA < shift0MA VALID SELL
if (shift5MAArray[2] < shift0MAArray[2]) {
signal = "sell";
}
}
if (shift5MAArray[1] < shift0MAArray[1]) {
//if shift5MA > shift0MA
if (shift5MAArray[2] > shift0MAArray[2]) {
signal = "buy";
}
}
如何检查3个移动均线何时在4根蜡烛或更少的蜡烛内彼此交叉,如图像中的三根交叉:
答案 0 :(得分:4)
此代码并未针对速度进行优化,但总体上说明了如何解决此问题。该答案仅回答您的问题,即发现三个移动平均线是否交叉。它不会给您卖出或买入信号,但是您可以通过检查diff阵列中的方向变化来轻松实现该信号。
注意:由于在4个烛台的范围内看,该代码当前可以给出“三个MA均已交叉”的重复。
import numpy as np
MA1 = np.asarray([0, 1, 4, 3, 4, 5, 6, 7, 8, 9, 10])
MA2 = np.asarray([0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
MA3 = np.asarray([0, 0, 6, 7, 8, 9, 10, 10, 11, 12, 13])
haveCrossed = False
for i in range(len(MA1)-3):
# These are the differences between the moving averages, if one MA
# crosses another the sign of the difference changes from positive to
# negative or vice versa.
diff1 = MA1[i:i+4] - MA2[i:i+4]
diff2 = MA1[i:i+4] - MA3[i:i+4]
diff3 = MA2[i:i+4] - MA3[i:i+4]
# Check if all signs are equal. If the signs are equal, the moving averages
# did not intersect.
# Check if MA1 and MA2 crossed.
if np.all(diff1 > 0) if diff1[0] > 0 else np.all(diff1 < 0):
cross1Flag = False
else:
cross1Flag = True
# Check if MA1 and MA3 crossed.
if np.all(diff2 > 0) if diff2[0] > 0 else np.all(diff2 < 0):
cross2Flag = False
else:
cross2Flag = True
# Check if MA2 and MA3 crossed.
if np.all(diff3 > 0) if diff3[0] > 0 else np.all(diff3 < 0):
cross3Flag = False
else:
cross3Flag = True
if cross1Flag and cross2Flag and cross3Flag:
haveCrossed = True
print(f"The three Moving Averages crossed at time: [{i}, {i+3}]")
if not haveCrossed:
print("The three Moving Averages have not crossed.")
输出:
The three Moving Averages crossed at time: [0, 3]
The three Moving Averages crossed at time: [1, 4]