November 2019
Beginner
394 pages
10h 31m
English
Now, let's have a look at the code that demonstrates the implementation of momentum:
time_period = 20 # how far to look back to find reference price to compute momentumhistory = [] # history of observed prices to use in momentum calculationmom_values = [] # track momentum values for visualization purposesfor close_price in close: history.append(close_price) if len(history) > time_period: # history is at most 'time_period' number of observations del (history[0]) mom = close_price - history[0] mom_values.append(mom)
This maintains a list history of past prices and, at each new observation, computes the momentum to be the difference between the current price and the price time_period days ago, which, in this case, ...