Skip to content
DeepForgeby svx
All problems
EasyTime Seriests-049

Momentum Indicator

Problem

Compute the momentum indicator over k periods: prices[-1] - prices[-1-k].

Return 0.0 if k is not positive or k is at least the length of the price series.

Hint

Negative momentum means the series has fallen over the lookback window.

Test cases

case 1
in: [[1,2,3,4,5],1]
expected: 1
case 2
in: [[1,3,6,10],2]
expected: 7
case 3
in: [[5,4,3],1]
expected: -1
case 4
in: [[1,2],5]
expected: 0
Starter codePython
def momentum(prices, k):
    # Your code here
    pass

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