Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to get "current" price in a scheduled function

Hi,

I have a function to be scheduled at 2 hours after market open. I'm wondering if it's possible to get its current price in that function instead of the close price of that day in backtest? The value I get from data[stock].price seems to be the close price.

Thanks

8 responses

Do I have to run my algo at minute level in order to get this price?

Call the following code in the function that you have "scheduled" for 2 hours after market open.

current_price = history(1, '1d', 'price').iloc[0]  

Yes, you have to run a minute-level backtest to get any minute-level data.

Thanks Simon, that seems working but it's kind of wasting resources since my algo only needs to run at a specific moment in a day now it's running for every minute.

If your function is run with schedule_function, and you don't do anything in handle_data (ie just pass), it should be quite quick.

Yeah, I'm okay with that, it's Quantopian's computing resource anyway :)

When you use current_price = history(1, '1d', 'price').iloc[0], what is the .iloc[0]?

history(1, '1d', 'price') returns a dataframe with 1 row, the last seen close price. The columns are all the stocks you have in your universe. If you ran history(5, '1d', 'price') this would return a dataframe with 5 rows, the last 4 day's close prices and the price last minute for all your stocks.

You can then index into this dataframe to get a specific value, via iloc. The call .iloc[0] will retrieve the first row of the dataframe. For more info: http://pandas.pydata.org/pandas-docs/stable/indexing.html

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.