Skip to content
DeepForgeby svx
All problems
EasyTime Seriests-320

Moving Average Crossover Signal

Problem

Compare the latest observation with the simple moving average of the last k observations. Return 1 if the latest value is above the average, -1 if it is below, and 0 if they are equal.

Return 0 when k is not positive or k is larger than the series.

Hint

The latest value is part of its own moving average, so the signal needs a strict comparison.

Test cases

case 1
in: [[1,2,3,4,5],3]
expected: 1
case 2
in: [[5,4,3,2,1],2]
expected: -1
case 3
in: [[2,2,2],2]
expected: 0
case 4
in: [[1,2],5]
expected: 0
Starter codePython
def sma_crossover_signal(series, k):
    # Your code here
    pass

4 test cases · run your code against them in the editor