Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Newbie dilemma: from Notebook to IDE

Hi all!

Kinda new here and to coding in general. Sorry for any silly questions and/or mistakes.

Shortly, I am trying to build a few factor strategies but I am having a hard time figuring how to implement the code that I wrote in the Notebook into an algorithm. For example, for a Value strategy I wrote the following code in the Notebook (see attached Notebook below). As for the algorithm, the main issues I am facing are:

1) how to apply the winsorisation procedure;
2) mapping scores to positive values;
3) defining market-cap weights.

For example, to overcome the third issue, I tried to define a new CustomFactor. That didn't work out though as even in the Notebook I get wrong market-cap weights:

# Market-Cap w  
class Market_Cap_w(CustomFactor):  
    inputs = [morningstar.valuation.market_cap]  
    window_length = 1  
    def compute(self, today, assets, out, mcap):  
        df = pd.DataFrame(index = assets)  
        df['mcap'] = mcap[-1]  
        df['mcap_w'] = df['mcap'] / df['mcap'].sum()  
        out[:] = df['mcap_w']

As for the algo, this is my pipeline so far:

def make_pipeline():

    universe = Q500US()  
    sector = Sector()

    pipe = Pipeline()

    book_to_price = Book_to_Price().zscore(mask = universe, groupby = sector)  
    pipe.add(book_to_price, 'B/P')  
    earnings_to_price = Earnings_to_Price().zscore(mask = universe, groupby = sector)  
    pipe.add(earnings_to_price, 'E/P')  
    value = ((book_to_price + earnings_to_price) / 2).zscore(mask = universe, groupby = sector)  
    pipe.add(value, 'Value')  
    market_cap = Market_Cap()  
    pipe.add(market_cap, 'Mkt Cap')  
    filter1 = value.isfinite()  
    pipe.set_screen(universe & filter1)

    return pipe

As you can see, I am still missing the 3 points above (winsorisation, mapping, mkt cap weights), so I can define the final weights as I did in the Notebook below. Any help will be much appreciated!