Back to Community
Fundamentals Portfolio

Hi Guys,

I need help please, I am trying to create a long-only portfolio of US stocks.

Criteria:
Top 20 companies with the highest cash reserves, equally weighted.

I am getting an error on line 72, not sure what I am doing wrong?

Notebook attached.

Thanks

Luke

Loading notebook preview...
Notebook previews are currently unavailable.
1 response

Try something like this:

from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline.data.factset import Fundamentals as fcf  
from quantopian.pipeline.filters import QTradableStocksUS  
from quantopian.pipeline import Pipeline  
import quantopian.optimize as opt  
import pandas as pd  
# ---------------------------------------------  
STK_SET, N, LEV = QTradableStocksUS(), 20, 1.00  
# ---------------------------------------------  
def initialize(context):  
    schedule_function(trade, date_rules.every_day(), time_rules.market_open())  
    cash_only = fcf.cash_only.latest  
    universe = cash_only.top(N) & STK_SET  
    pipe = Pipeline(columns = {'cash_only': cash_only}, screen = universe)  
    attach_pipeline(pipe, 'my_pipeline')  

def trade(context, data):  
    output = pipeline_output('my_pipeline')  
    securities_to_hold = output.query('cash_only > 10000000000.0').index  
    weight = 1.0 / len(securities_to_hold)  
    weights = pd.Series(index = securities_to_hold, data = weight)  
    order_optimal_portfolio(opt.TargetWeights(weights), [opt.MaxGrossExposure(LEV)])    

def before_trading_start(context, data):  
    record(leverage = context.account.leverage, num_positions = len(context.portfolio.positions))