Dear all,
How do I go about getting the previous close price for a particular stock? Given that handle_data
gets called at every tick, I can build a hash table to store this price for each stock. Is there any better way to do this?
Thank you
Dear all,
How do I go about getting the previous close price for a particular stock? Given that handle_data
gets called at every tick, I can build a hash table to store this price for each stock. Is there any better way to do this?
Thank you
You can use the history function to get historical prices and to compare prices between bars. To get yesterday's close for a particular stock you can do:
history(2, '1d', 'close_price')
This will return a datapanel with yesterday's close price and today's current price. Note that you'll need to run your algo in minute mode when you use the history function.
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.
Or in daily mode:
def initialize(context):
context.stocks = sid(2)
def handle_data(context, data):
closes = get_closes(data, context)
if closes is None:
return
# Yeterday's close
print closes[context.stocks][0]
@batch_transform(window_length=2, refresh_period=0)
def get_closes(data, context):
closes = data['close_price']
return closes
P.
Or you can do something like this where you initialize a dictionary containing the sid as the key with the prior closing price, for example, as the value.
Before finishing the main loop, you set the prior_close to the current close to update the dictionary.
Instead of using this structure for storing just a single value, it can be used to store information per security for items such as a list, whole class structure, or nested dictionary.
def initialize(context):
context.portfolio_ = [sid(8554), sid(19920)] # SPY, QQQ
context.prior_close = dict(zip(context.portfolio_,
[None] * len(context.portfolio_)))
def handle_data(context, data):
for stock in context.portfolio_:
close = data[stock].close_price
# for example...
last = context.prior_close[stock]
daily_return = close / float(last) - 1 if last is not None \
else None
log.info('stock: {0}, close: {1}, last: {2}, daily_return: {3}'\
.format(stock, close, last, daily_return))
context.prior_close[stock] = close
Here's a bit more detail on how to use history and pandas/numpy to extract the prices:
def initialize(context):
context.stocks = [sid(8554),sid(33652)]
def handle_data(context, data):
close_prices = history(2, '1d', 'close_price')
for stock in context.stocks:
print stock.symbol
print close_prices[stock].values[0]