How to tell when 3 Moving Averages cross within a duration of 4 candles or less
Asked Answered
I

1

6

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:

enter image description here

Ive answered 7/9, 2020 at 14:2 Comment(1)
First of all, what is your definition of "crossing lines" for more than 2, for example, with 3 lines, do you consider as crossed when the order goes from 1-2-3 to 2-3-1, or only when the order is totally inverted at the end of the observed time. I'm pretty sure the solution is by comparing the sort order of averages between the first and the last candle.Mosaic
O
3

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]
Ossein answered 11/9, 2020 at 14:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.