Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help(Intraday backtest): How to get Highest high of today and yesterday ?

Hi
I am new to python and Quantopian.
I just want to know how can I get Highest high of today and yesterday.
Thanks.

9 responses

Hi,

Clone attached algorithm, press 'Build Algorithm' button and you'll see today's and yesterday's highs printed in logs window.

Regards,
Ed

Thank you Ed. It's my bad didn't explain the detail.
What I mean is to get Today's High in minutes data backtesting mode , for example I want to get Today's Highest high at 3:30 PM
I guess in minutes backtesting mode use " data[context.stock].high " can only return High of this minute bar. Not Highest high of today.
Am I right ?
Thanks

no prob. Here you go:

Thank you Ed. You are so generous.

Hi Ed,

I copied your source code and ran it with AAPL. I think there's a few error or may be bugs?

The log printed as below.
2014-01-17handle_data:8INFOYesterday high: 556.850000
2014-01-17handle_data:12INFOtoday high: 552.080000
2014-01-21handle_data:8INFOYesterday high: 552.080000
2014-01-21handle_data:12INFOtoday high: 550.070000
2014-01-22handle_data:8INFOYesterday high: 550.070000
2014-01-22handle_data:12INFOtoday high: 557.300000
2014-01-23handle_data:8INFOYesterday high: 557.300000
2014-01-23handle_data:12INFOtoday high: 556.500000
2014-01-24handle_data:8INFOYesterday high: 556.500000
2014-01-24handle_data:12INFOtoday high: 556.180000
2014-01-27handle_data:8INFOYesterday high: 556.180000
2014-01-27handle_data:12INFOtoday high: 554.800000
2014-01-28handle_data:8INFOYesterday high: 554.800000
2014-01-28handle_data:12INFOtoday high: 515.000000
2014-01-29handle_data:8INFOYesterday high: 515.000000
2014-01-29handle_data:12INFOtoday high: 507.380000

I downloaded the historical prices (high) from Yahoo Finance and see below.

Jan 29, 2014, 507.37
Jan 28, 2014, 515.00
Jan 27, 2014, 554.80
Jan 24, 2014, 555.62
Jan 23, 2014, 556.50
Jan 22, 2014, 557.29
Jan 21, 2014, 550.07
Jan 17, 2014, 552.07

1.As you can see, the high price on 1/24 is different (556.18 (log) vs 555.62 (Yahoo)).
2.Is this the only way to get the yesterday's price data? It's a little hard to see the logs.

Can you help me?
Thanks.

Hi Kyu,

Algo is correct. high 556.18 comes from 09:35 bar:
Open: 554.97
High: 556.18
Low: 554.57
Close: 554.87

Data can differ depending on data provider. That's normal.
Look:
According to 2 minute chart on finance.google.com high 555.62 was on 09:36 bar.
According to weekly 20 min chart from IB high was 556.18 on 09:35 bar
and according to chart from ThinkOrSwim (TD Ameritrade) it was 556.17 on 09:00 bar (premarket)

It looks like yahoo and google ignore premarket prices.

Regards,
Ed

Hi guys,

Here's an attempt using pandas. It should output the high closing minutely price for the current day to the log for a portfolio of stocks, on a rolling basis updated every minute.

Grant

I wanted to share an example of how you can use history() to return yesterday and today's high prices directly, without needing to create and store a new variable day over day.

The returned data for daily history, for each day, is:

close_price: the close of the last minute bar for that day. For the current day, the most recent close is returned.
price: same as close_price.
open_price: the open of the first minute bar of the given day.
volume: the sum of all minute volumes. For the current day, the sum of volume thus far.
high: the max of the minute highs for the given day. For the current day it returns the max of the minute highs 'today'.
low: the min of the minute bar lows for the given day. For the current day it returns the max of the minute highs 'today'.

To access the highest prices from yesterday and today, and then compute the delta between them, you can use to following logic:

price_history = history(bar_count=2, frequency='1d', field='high')  

yesterdays_high = float(price_history.iloc[-2])  
todays_high     = float(price_history.iloc[-1])

delta = 100 * (todays_high - yesterdays_high) / yesterdays_high
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.

Good point, Jess,

I'd missed looking over the "Returned Data" definitions for the history API on the help page. One reason I jumped to playing around with accumulating minutely bars is that then statistics other than finding the maximum for the current day could be applied. Presumably, once the full history API is released, this won't be necessary, since a longer (5 day?) trailing window of minutely bars will be available in a pandas dataframe.

Grant