Back to Community
How to Retrieve a Day's Intraday Minute by Minute Data For a Specific Stock

How do you retrieve a day's intraday minute by minute data for a specific stock? I was planning on using this to study outliers.

Thank you.

5 responses

https://www.quantopian.com/help#ide-history
Examples would be found using a search like https://www.google.com/search?q="data.history"+site:quantopian.com

Fields are open, high, low, close, volume, and also price. I think there is a subtle difference between close and price. To clarify, when using '1m', close would be the close for each minute.

Basically, making use of the info ...

Last five minutes of prices with a single stock, returns a Series:

    prices = data.history(stock, 'price', 5, '1m')  

The second price would be prices[1].

Someone might do something like:

    prices = data.history(stock, 'price', 5, '1m')

    if prices[-3] > prices[-2] > prices[-1] * 1.05:    # price drop  
        order_target(stock, 0)    # sell  

More commonly the input is a list of stocks rather than a single stock.

Instead of a Series, a list for input returns a Dataframe so index into it using the stock.

    prices = data.history(context.portfolio.positions.keys(), 'price', 5, '1m')

    for stock in context.portfolio.positions:  
        price = prices[stock][-2]    # price before the current minute, or ...

        if prices[stock][-3] > prices[stock][-2] > prices[stock][-1] * 1.05:    # price drop  
            order_target(stock, 0)    # sell  

If the original were in a list by itself, that would also return a Dataframe instead of a Series. That's useful if starting out with one stock and planning to expand to a list later.

    prices = data.history([stock], 'price', 5, '1m')  

If the fields are more than just price, as a list, like ['price', 'volume'], then volume for example would be retrieved like this, and also an example for price ...

    history_info = data.history(context.portfolio.positions.keys, ['price', 'volume'], 5, '1m')

    for stock in context.portfolio.positions:  
        volumes_series = history_info.volume[stock]  
        second_latest_volume_value_for_this_stock = volumes_series[-2]  # or ...

        second_latest_price_value_for_this_stock  = history_info.price[stock][-2]  

The quick route is to start with some code, click in the margin on a line number to set a breakpoint, run, and try some things at the prompt that appears.

Hello Blue, I tried this code you posted, but data is actually showing all NaN if I am doing it for today. Does it mean that Quantopian does not supply current quotes?

Would love to know the answer to Alex Z's question above.

Per my understanding minute data is available only for the previous business day. Correct me if I am wrong. Thank you.

I used this
prices = data.history(stock, 'price', 5, '1m')
but I am getting after market data @ 8:30 AM.

Clone Algorithm
12
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
import datetime
import pytz
import numpy as np
import pandas as pd
import statsmodels.api as sm
from pytz import timezone  
import talib



def initialize(context):
    context.stocks = sid(8554)
    context.LOW_RSI = 25
    context.HIGH_RSI = 75
    context.ADX = 25
    schedule_function(func = close_trades,date_rule = date_rules.every_day(),time_rule = time_rules.market_close())

    set_commission(commission.PerTrade(cost=1.00))
    set_slippage(slippage.FixedSlippage(spread=0.00))
    
def handle_data(context, data):
    
    exchange_time = get_datetime().astimezone(pytz.timezone('US/Eastern'))
    period = 14
    
    H = data.history(context.stocks,'high', 2*period, '1m').dropna()
    L = data.history(context.stocks,'low', 2*period, '1m').dropna()   
    C = data.history(context.stocks,'price', 2*period, '1m').dropna()
    prices = data.history(context.stocks, 'price', 2*period, '1m')
    ta_ADX = talib.ADX(H, L, C, period)
    ta_RSI = talib.RSI(prices,14)
    ADX = ta_ADX[-1]
    rsi = ta_RSI[-1]
    current_position = context.portfolio.positions[context.stocks].amount
    #log.info('RSI is at {0},ADX is at {1}, current position {2},prices {3}'.format( rsi,ADX,current_position,prices))
    hour = exchange_time.hour
    minute = exchange_time.minute
    if rsi > context.HIGH_RSI and ADX>context.ADX and current_position == 0 and data.can_trade(context.stocks) and hour<14:
        
            order(context.stocks, 1)
            
            log.info('{0}: RSI is at {1},ADX is at {3}, buying {2} shares.,current position {4},prices {5}'.format(  
                context.stocks.symbol, rsi, 1,ADX,current_position,C[-3:]
            ))
    elif rsi < context.LOW_RSI and ADX>context.ADX and current_position == 0 and data.can_trade(context.stocks) and hour<14:
            order(context.stocks,-1)
            log.info('{0}: RSI is at {1}, selling {2} shares'.format(  
                context.stocks.symbol, rsi, -1 
            ))
            
            
    
def close_trades(context,data):
        current_position =context.portfolio.positions[context.stocks].amount
        if current_position>0:
            order_target(context.stocks, 0)
            '''log.info('{0}: RSI is at {1},ADX is at {3}, selling {2} shares. ,current position  {4}'.format(  
                context.stocks.symbol, rsi, 1,ADX,current_position
            ))'''
            
        if current_position<0:
            order_target(context.stocks, 0)
            '''log.info('{0}: RSI is at {1},ADX is at {3}, buying {2} shares.'.format(  
                context.stocks.symbol, rsi, 1,ADX
            ))'''
There was a runtime error.