Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to apply stochastic oscillator to all stocks in universe at once

Hi everybody,

In the past i did something like the next part of code to do an calculation to all stocks in my universe at once:

prices = history(context.returns_lookback, '1d', 'price')  
    # Calculate the past X days' returns for each security.  
    returns = (prices.iloc[-1] - prices.iloc[0]) / prices.iloc[0]  

Now i want to do the same with a stochastic oscillator, but with my code it gives a logical but iritating error that it can not do this calculation to all the stocks at once. Does anyone has some hints to how i can solve this? I would prefer not doing it with a loop to loop over all the stocks. This is my current not working code snap

def getStoch(H, L, C):  
    return talib.STOCH(H, L, C,  
                       fastk_period=14,  
                       slowk_period=3,  
                       slowd_period=3)

 H = history(25, '1d', 'high')  
 L = history(25, '1d', 'low')  
 C = history(25, '1d', 'close_price')  
    slowk, slowd  = getStoch(H, L, C)  
    k_val = slowk[-1]  
    d_val = slowd[-1]  
5 responses

Hi Dennis,
It can be tough to vectorize calculations that have several dimensions to them, talib.STOCH requires 3 inputs and outputs 2. I was able to come up with a simple way to get all the values at once though, it required making a series of the stocks and applying a function that accepts the stock and a panel of OHLCV data. It returns a tuple of the most recent slowk/slowd values for each stock (see the log output). Is this what you were looking for?

Hi David,

That's a realy nice trick! Thank you for your help. I'll try to implement it and let you know if it works ;)

Regards,
Dennis

I have another question with your solution. I have been trying to remove al leveraged ETF's and all stocks with open orders from the list of stocks using the following code:

stochs = stochs.dropna()  
    open_orders = get_open_orders()  
    eligible_secs = [sec for sec in data if sec not in security_lists.leveraged_etf_list]  
    stochs = stochs[eligible_secs]  
    if open_orders:  
        eligible_secs = [sec for sec in data if sec not in open_orders]  
        stochs = stochs[eligible_secs]  
    stochs.sort()  

the problem is however that if i do this extra check before ordering:
if stock in security_lists.leveraged_etf_list: # skip bar if in not to trade list return I see that still a not all stocks that need to be bought are buight because they are in the leveraged ETF list. I'm quite new to python so i cannot find the error i made in trying to filter out the leveraged ETF's from the stochs list.

Thanks in advance for the help!

Dennis

You were pretty close to having it down. Line 76 was your biggest issue, by using the keyword 'return' you are actually kicking out of the function entirely so the rest of the loop never executes. Instead use 'continue,' which skips the current element and goes back to the top of the loop.

I also removed the ineligible assets from the 'stocks' variable so the stoch function has fewer elements to evaluate. You could also iterate over the stocks array when doing your ordering since you know all of them are eligible.

Again thank you for the support. With this new understanding i hope to be able to finish my algorithm!