Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to get the low price, closing price, and high price of a given week?

Hello,

I have an algorithm that I trade futures manually with but am having difficulty getting it into code. Any help getting the closing price, high and low of an entire week?

  1. At the end of each week I gather the low, close, and high of the week for a given asset or stock. (i.e Soybean futures)

  2. For example, if at the end of this current week, Soybeans trade higher than the high of last week, and close below the close of last week. I will short them.

Any help? Thank you so much! Any help would be greatly appreciated.

5 responses

We don't have futures on the platform, but we're working to include them: https://www.quantopian.com/posts/futures-are-coming-to-quantopian

You can try your idea using US equities. To make it easier, use schedule_function to get the OHLCV prices for the week. Here is a basic example:

def initialize(context):  
   schedule_function( ohlcv,  
                                           date_rules.week_end()  
                                          )  
    context.stock = sid(24)  #AAPL

def ohlcv(context,data):  
    weekly_high = max(history(5, '1d', 'high'))  
    weekly_low = min(history(5, '1d', 'low'))  
    close_prices = history(5, '1d', 'price')  #gives close price for the last 5 days for all stocks in the algo  

Take a look at this documentation of the history function and tutorial. These will be helpful for you to get started.

And here is a similar thread to your question, maybe you can leverage some of the code: https://www.quantopian.com/posts/help-intraday-backtest-how-to-get-highest-high-of-today-and-yesterday

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.

Thank you so much! However, it's saying "Error Algorithm must implement handle_data(context, data) method."

Also, when I print weekly_high. It just prints, "2015-07-06PRINTEquity(24 [AAPL])" no value.

Ah yes, each algo on Quantopian is required to have an initialize() and handle_data() method. (You can see our help doc for more details).

Here is the udpated skeleton (note that it doesn't have any orders or advanced logic, it's merely getting the weekly high, weekly low, and 5 day close prices)

def initialize(context):  
   schedule_function(ohlcv, date_rules.week_end())  
   context.stock = sid(24)  #AAPL  
def handle_data(context,data):  
    pass

def ohlcv(context,data):  
    weekly_high = max(history(5, '1d', 'high'))  
    weekly_low = min(history(5, '1d', 'low'))  
    close_prices = history(5, '1d', 'price')  #gives close price for the last 5 days for all stocks in the algo  

Thanks again. If I wanted to print out the weekly_high and weekly_low, how would I do that?

Using
print weekly_high doesn't work. It prints out 2015-07-10 PRINT Equity(24 [AAPL])

Hi Lincoln,

Give this a shot:

def initialize(context):  
   schedule_function(ohlcv, date_rules.week_end())  
   context.stock = sid(24)  #AAPL  
def handle_data(context,data):  
    pass

def ohlcv(context,data):  
    weekly_high = history(5, '1d', 'high')  
    weekly_low = history(5, '1d', 'low')  
    close_prices = history(5, '1d', 'price')  #gives close price for the last 5 days for all stocks in the algo  

    # gives the date of the weekly high  
    print weekly_high.idxmax()[0]  
    #give the value (price) of weekly high  
    print weekly_high.max()[0]  

To learn more about the structure of the data, try googling "pandas dataframe max values" or other terms about pandas manipulations. It's a powerful tool!