I've been working with 2 Moving averages crossing which is quite straight forward. I want to add a third to the mix and I'm trying to figure out to check for this occurring within 4 candles or less.
For two moving averages I was using the following:
// 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";
}
}
How can I check when the 3 moving averages cross each other within 4 candles or less as in the image three crossed:
All you have to do is plop on a couple of moving averages on your chart, and wait for a crossover. If the moving averages cross over one another, it could signal that the trend is about to change soon, thereby giving you the chance to get a better entry.
The triple exponential moving average (TEMA) uses multiple EMA calculations and subtracts out the lag to create a trend following indicator that reacts quickly to price changes. The TEMA can help identify trend direction, signal potential short-term trend changes or pullbacks, and provide support or resistance.
Trading EMA (or SMA) crossovers is a foundational strategy for trend trading. EMA crossover occurs when a short-term EMA (e.g. 12 day) crosses the long-term EMA (e.g. 50 day), either above (bullish, uptrend) or below (bearish, downtrend).
Simple Moving Average Calculation For example, a four-period SMA with prices of 1.2640, 1.2641, 1.2642, and 1.2641 gives a moving average of 1.2641 using the calculation (1.2640 + 1.2641 + 1.2642 + 1.2641) / 4 = 1.2641.
This code is not optimized for speed, but shows in general how you could solve this problem. This answer only answers your question of finding if the three moving averages have crossed. It does not give you a sell or buy signal, but you could easily implemented it by checking in which direction the signs change in the diff arrays.
Note: The code currently can give duplicates of "The three MA's have crossed", because of looking within a range of 4 candlesticks.
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.")
Output:
The three Moving Averages crossed at time: [0, 3]
The three Moving Averages crossed at time: [1, 4]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With