Back to Community
Looking for minimum price during a period of time and it's datetime

Hi,

I'm a newb. I'm trying to find the minimum or maximum price that happened in a period of time as well as the datetime at which it occured. Is History dataframe the best way to do this, or is there some better way? An example would be much appreciated. TIA

5 responses

Hello Jeff,

Give this a go:

def initialize(context):  
    context.stocks =  [sid(19662),  # XLY Consumer Discrectionary SPDR Fund  
                       sid(19656),  # XLF Financial SPDR Fund  
                       sid(19658),  # XLK Technology SPDR Fund  
                       sid(19655),  # XLE Energy SPDR Fund  
                       sid(19661),  # XLV Health Care SPRD Fund  
                       sid(19657),  # XLI Industrial SPDR Fund  
                       sid(19659),  # XLP Consumer Staples SPDR Fund  
                       sid(19654),  # XLB Materials SPDR Fund  
                       sid(19660)]  # XLU Utilities SPRD Fund

def handle_data(context, data):  
    prices = history(390,'1m','low')  
    lows = prices.min()  
    lows_idx = prices.idxmin()  
    print lows  
    print lows_idx  

Grant

Clone Algorithm
10
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
def initialize(context):
    
    context.stocks =  [sid(19662),  # XLY Consumer Discrectionary SPDR Fund  
                       sid(19656),  # XLF Financial SPDR Fund  
                       sid(19658),  # XLK Technology SPDR Fund  
                       sid(19655),  # XLE Energy SPDR Fund  
                       sid(19661),  # XLV Health Care SPRD Fund  
                       sid(19657),  # XLI Industrial SPDR Fund  
                       sid(19659),  # XLP Consumer Staples SPDR Fund  
                       sid(19654),  # XLB Materials SPDR Fund  
                       sid(19660)]  # XLU Utilities SPRD Fund

def handle_data(context, data):
    
    prices = history(390,'1m','low')
    lows = prices.min()
    lows_idx = prices.idxmin()
    
    print lows
    print lows_idx
There was a runtime error.

This is nothing to do with the trend ! I just found out that Grant is currently second place on Quantopian Open. :) Best of Luck ! I hope you win .....

Thank you very much, that works. What if I want the 2 or 3 least values, how would the above example be modified or would a different technique advisable?

Actually, I think I figured out that I can exclude the previous low and look again:

price_history2 = price_history[price_history.index != price_history.idxmin()]  
low2 = price_history2.min()  
lowidx2 = price_history2.idxmin()

Not sure if it's the best way, but I think it works.
Thanks again for your help!

Jeff,

Looks like it'll work. You could also consider a sorting technique that would allow you to grab the N lowest values. For multiple securities, this would require some head-scratching, since you also need the datetime stamps, but I think it could be worked out.

Grant