Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to include day's opening price?

Hey everyone, I am working on developing an algorithm to purchase shares of a stock if it falls 1% or so from the opening price. However, when coding, I am not really sure how to define this. I want to create a variable called "open_price" so that I can then define a variable "buy_price" as "open_price * 0.99", however I am not sure how to format that using data history when coding... Any help? My code so far is included below:

current_price = data.current('SPY', "price")  
open_price = data.history(context.security, "open", 1, "1d")  
current_positions = context.portfolio.positions[symbol('SPY')].amount  
cash = context.portfolio.cash  
buy_enter = (open_price * 0.99)


if (current_price <=buy_enter) and current_positions == 0:  
    number_of_shares = int(cash/current_price)  
    order(context.security, number_of_shares)  
1 response

Greg,
This might help get you started.
Calculates the open price for the Q500 and puts in a limit order at 99% of the open for 100 shares for all 500 stocks.
If the price falls below the limit price during the day the order would be filled at the market price.
The algo liquidates all holdings at the end of the day.

I think limit orders would be better for what you are trying to do, rather than trying to monitor the price yourself and constantly update.
Pipeline also allows you to have a much larger universe of stocks. I used the Q500 filter for simplicity, but you can create your own filter or select individual stocks with the pipeline as well.
Hopefully this gives you a start.

Best,
Cory