Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Custom Factor Input Question

In this custom factor I input all the USEquityPrices but I want to input just one stock instead of 8000+ stocks how do I do that??

class Last_Close_30(CustomFactor):
"""
Gets the latest prices for each asset
"""
inputs = [USEquityPricing.close]
window_length = 30

outputs = ['yesterday','projection','difference','standard']  
def compute(self, today, assets, out, close):  

    a = -0  
    b = -1  
    look_back = 0  
    sample = []  

    # gets log return from last 30 close prices and puts in array  
    for look_back in range(0,30):  
        logs = np.log(close[a]/close[b])  
        sample.append(logs)  
        a = a-1  
        b = b-1  
3 responses

CustomFactor has an optional mask parameter, little bit on this here : https://www.quantopian.com/tutorials/pipeline#lesson10

Example of calling a custom factor for one stock :

symbol = StaticAssets(symbols(['MSFT']))  
someValue = myCustomFactor(mask = symbol) 

Thanks for the response Chris! That worked perfectly did not know about the masking function until now!
Another question? So in my factor I am trying to get the std of the stocks projection price but it keeps taking the std of the row which is useless to me. My output is an array that looks something like this
A B C A B C
[[121.431, 153.65, 34.65], [120.54, 160.63, 35.76] where those are three different stocks so how would i get the std of A and B ext.???

class Last_Close_30(CustomFactor):
"""
Gets the latest prices for each asset
"""
inputs = [USEquityPricing.close]
window_length = 30

outputs = ['yesterday','projection','difference','standard']  
def compute(self, today, assets, out, close):  

    a = -0  
    b = -1  
    look_back = 0  
    sample = []  

    # gets log return from last 30 close prices and puts in array  
    for look_back in range(0,30):  
        logs = np.log(close[a]/close[b])  
        sample.append(logs)  
        a = a-1  
        b = b-1  

    # project 10 time steps ahead and make projected price 10 days out  
    # that process done 1000 times (monte carlo)  
    monte_carlo = []  

    for num in range(0,1000):  
        tomorrow_projection = close[-1]  
        for count in range(0, 10):  
            random_number = random.choice(sample)  
            tomorrow_projection = tomorrow_projection * np.exp(random_number)  
        monte_carlo.append(tomorrow_projection)  

    # average close prices for each stock  
    average = (sum(monte_carlo))/1000  

    # difference from average  
    difference = average - close[-1]  

    std_mc = np.nanstd(monte_carlo) # NOT WORKING  


    # output (yesterdays price, average projection of tomorrows price, difference of yesterdays and average, standard deviation)  
    out.yesterday[:] = close[-1]  
    out.projection[:] = average  
    out.difference[:] = difference  
    out.standard[:] = std_mc  

Hi gashbaugh, I think I'm a similar stage in the learning curve as you - I've placed a bunch of debug print statements in Notebooks to try and understand what is going on, and what is expected for return values of CustomFactors / CustomFilters.

Attached is what I've been toying with - The main bit is the close.T, where it is transposing the array of close prices to work at an individual stock level. (Might not be a good python approach though, I'm from a Microsoft .NET background and am not really used to the vectorized numpy/pandas stuff).

Anyway, hope this helps!