Can't convert the math equation to python code – Polychrom Momentum technical-indicator

Question:

I am having trouble calculating a technical indicator.

Below is the paper which explains the math equation for the technical indicator.
enter image description here
Here is my code which doesn’t work as planned.

def poly_mtm(length: int, prices: list[float]) -> float:
    poly = 0.0
    for i in range(1, length + 1):
        price = prices[-1]
        price_i = prices[-i]
        mtm_i = price - price_i
        poly += mtm_i / math.sqrt(i)
    return poly

My data is a list of floats(stock prices).
The expected results should be between -18 to 18.
my actual results vary between -100 to 100

Any suggestions would be welcome.

Asked By: Roy

||

Answers:

Not sure what part doesn’t work but try this? (Also consider edge cases)

def poly_mtm(prices: list[float]) -> float:
    if len(prices) == 1:
        return 0.0    
    poly = 0.0
    price = prices[-1]
    for index, value in enumerate(list, start = 2):
        price_i = prices[-1 * index]
        mtm_i = price - price_i
        poly += mtm_i / math.sqrt(i)
    return poly
Answered By: Marcus
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.