Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Get the Time of the day ( a time object of any form) when a stock attained its minimum on a given trading day.

Hi All,

I am trying to Get the Time of the day ( a time object of any form) when a stock attained its minimum on a given trading day.

How do I code this using Quantopian APIs

Thanks
Ronak

6 responses

Hello Ronak,

Do you want it for the current trading day, or a specific day in the past, or for all days over a trailing window of days?

Grant

Hi Ronak,

Here's my first crack at it. I suggest checking the code and output to make sure I did it correctly.

[EDIT] Woops! You'll need to use idxmin instead of idxmax.

Grant

import pandas as pd

def initialize(context):  
    context.spy = sid(8554)  
    context.prices = pd.DataFrame()  
    context.index_max = []  
    context.prior_day = None

def handle_data(context, data):  
    if context.prior_day != get_datetime().day and context.prior_day != None:  
        context.index_max.append(context.prices[context.spy].idxmax())  
        print context.index_max  
        context.prices = pd.DataFrame()  
    prices = history(1,'1d','price')  
    context.prices = context.prices.append(prices)  
    context.prior_day = get_datetime().day  

Hello Grant,

Thanks for writing out the code. I really appreciate your quick response. I will be running the algorithm tomorrow and let you know if face problem somewhere. Once again Grant thanks for the taking the time...

Best Regards
Ronak

Ronak,

Here's an update (hopefully improved). I have some ideas for further improvement, but would like to hear if this is what you are looking for.

Grant

import pandas as pd

def initialize(context):  
    context.spy = sid(8554)  
    context.prices = pd.DataFrame()  
    context.index_min = []  
    context.prior_day = None

def handle_data(context, data):  
    if context.prior_day != get_datetime().day and context.prior_day != None:  
        context.index_min.append(context.prices[context.spy].idxmin())  
        print context.index_min  
        context.prices = pd.DataFrame()  
    prices = history(1,'1d','low')  
    context.prices = context.prices.append(prices)  
    context.prior_day = get_datetime().day  

Grant,

Purely from a readability perspective. I have altered your code very slightly. Changes are
i. Convert to Eastern Time ( Included a Module 'pytz' )
ii. Instead of printing the entire index_min[] , I am printing the last element of the list for a given date. That way importing the log becomes easier for further analysis.

    context.index_min.append(context.prices[context.spy].idxmin().astimezone(pytz.timezone('US/Eastern')))  
    print context.index_min[-1]

Thanks once again.

Best Regards
Ronak

Good. Note that if you don't need to print to the log, I suggest suppressing it, as it will improve performance a bit. --Grant