Advertisement
cjc5013

momentum_example

Sep 19th, 2019
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. # Make a security list and grab the data
  2. # Notice the "field" arguement, we are only grabing the prices here (same as closing prices)
  3. # Check out the last 5 rows using .tail()
  4.  
  5. security_list = ['spy','xlk','xly','vnq','xlp','xlv','xli','xlf','xlb']
  6. df = get_pricing(security_list, start_date='2017-01-01', end_date='2019-09-18', frequency='daily', fields='price')
  7. df.tail()
  8.  
  9.  
  10.  
  11. # Calculate rolling 12 month percent changes and check out last 5 days
  12.  
  13. rolling_12_month_momentum = df.pct_change(252)
  14. rolling_12_month_momentum.tail()
  15.  
  16.  
  17.  
  18. # If you only want the most recent day (momentum as of 09/18/2019) use iloc[-1]:
  19.  
  20. most_recent_momentum_measures = df.pct_change(252).iloc[-1]
  21. most_recent_momentum_measures
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement