Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Feedback on price change code

Hi All - I'm new to python and algo trading and was hoping for some feedback on my notebook. Can you take a look and offer any suggestions for improvement? A few items that I'm curious about:

  • If the lookback date falls on a non-tradeable day (e.g. holiday), will price data be unavailable or will it grab the last available price? If NaaN, what's an efficient way of grabbing the last available day with price data?
#use these variables to determine price change from today until x period ago  
#comment out the period_start variables that are not in use  
#Example below is for checking 3 month performance  
#set_month = today.month - 3  
#period_start = today.replace(month = set_month)

set_month = today.month - 3 #today's month minus 3 months  
set_year = today.year  
set_day = today.day

#comment out the unused period_start objects -- e.g. if using month, comment out day and year  
period_start = today.replace(month = set_month)  
#period_start = today.replace(day = set_day)  
#period_start = today.replace(year = set_year)  
  • This seems sloppy -- is there a more efficient way to get the last value?
#get first and last price in the series  
value_count = 0  
for item in data.price.values:  
    value_count = value_count + 1

begin_price = data.price.values[0]  
end_price = data.price.values[value_count - 2]  
  • How do I reverse sort? I'm grabbing the last five instead of the first five because I couldn't figure that part out.
    price_change_matrix_sorted = sorted(price_change_matrix,key=lambda x: x[1]) purchase_list = price_change_matrix_sorted[-5:]

Thanks for any guidance and suggestions!

Brian

1 response

Hi Brian, for your three specific points:

  1. If the start date for get_pricing falls on a non-trading day it should move forward to the next trading day.
  2. A better way to get the length of data.price is len(data.price). A better way to get the last row is data.price.values[-1].
  3. Here is the documentation for sorted. There's a reverse argument you can specify to sort in reverse.

Let me know if you have other questions.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.