Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to just hold a list of stocks for a month?

Newbie Question. I want to do a two-step screening because more TTM data are readily available from Portfolio 123.
Now I have a list of around 200 stocks after 1st stage of ranking. And I want to add more filters to it to control my portfolio size.
The first step is to backtest the 200 stocks portfolio as my benchmark to see if I can improve upon that later.
I tried the following code:

def initialize(context):

    set_symbol_lookup_date('2015-11-28')  
    context.stock_list = symbols(context.base_universe = symbols('UIHC','NPTN','MFLX','ZAGG','UVE','ODC','EBF','EXTR','HALL','TRR','FSB','BUSE','EMCI','IIIN','TCBK','GNCMA','HCKT','AEPI','ISLE','SBCF','ICFI','ROCK','SHOR','TBK','FORR','HRTG','RTEC','OME','FBNC','SSNI','FNHC','PPBI','MG','SRI','HOFT','GCAP','QCRH','CSS','SNOW','RYAM','EVRI','NOVT','MRCY','NLS','AVD','TSYS','EFSC','JMG','CACB','GABC','AEGN','ANCX','CASH','LYTS','HMTV','FRP','FISI','NX','CCRN','GBNK','SGMS','PGC','ZIXI','VRTV','RPXC','SIGM','HTLF','CRD.B','MBVT','TIPT','OSBC','AMNB','PSTB','NBBC','GARS','WIN','CETV','PCBK','CCNE','OSUR','CWST','CAMP','CUNB','DGII','PATK','HCOM','AT','VRNS','LAWS','MSFG','FOXF','MCBC','CPF','BBRG','ORC','SENEA','AMBC','KAI','BHBK','ABCD','RYI','GRBK','BH','ONDK','MBWM','BEAT','HSII','CNBKA','IBP','HWAY','CORR','PLAB','VEC','BGC','TSQ','SQI','ATLO','FRSH','ROIAK','BLMT','GSBD','TAST','PTX','SREV','CVO','CSFL','NUTR','NEFF','ERI','METR','RMAX','RM','PEBO','MTSN','PFBC','AHH','FSAM','HCCI','MBUU','AMSWA','UEIC','ATEN','LIOX','ABCW','NRIM','ROX','SGBK','APTS','BSET','DXLG','RECN','AGX','MGPI','DX','FUEL','USLM','FARM','ARC','CCG','PERY','ANIK','CTRE','NPK','IMH','UCFC','RST','EPIQ','CBZ','ECHO','HTBK','EVC','FVE','BBSI','RUTH','BCOV','SCVL','UMH','SRDX','FC','FBIZ','NXRT','LDL','CAC','ANGI','GNBC','TRK','ALCO','BNK','EBTC','WEYS','REIS','ESIO','CHEF','UBA','WIFI','TTGT','TTMI')  
    schedule_function(rebalance,  
                      date_rules.month_start(),  
                      time_rules.market_open(hours=1))  
    schedule_function(recording,  
                     date_rules.every_day(),  
                     time_rules.market_close())  
def rebalance(context):  
    for stock in context.stock_list:  
        if data.can_trade(stock):  
            order_target_percent(stock, 1.0/len(context.stock_list))

def recording(context):  
    longs = len(context.portfolio.positions)  
    record(long_position=longs, cash=context.portfolio.cash)  

but was encountered with SyntaxError: invalid syntax in line 6 which is the 'schedule_function(rebalance,' every time.
Don't know what's happening. Any ides?

2 responses

The error you're getting is a little misleading. The problem actually occurs in the previous assignment statement. But, after that, there are two other problems too. Fix these and you're good to go. See attached backtest/algorithm.

  1. remove 'context.base_universe = symbols('. When setting 'context.stock_list' just use 'symbols(and then a list of symbols)'. See the documentation https://www.quantopian.com/help#api-symbols)
  2. the format of the schedule function is in the form 'date_rules = ', and time_rules ='. You omitted that. See the documentation https://www.quantopian.com/help#ide-schedulefunction)
  3. the functions which you schedule need to have as parameters 'context' AND 'data'. You only had 'context'.
# Updated code

def initialize(context):

    set_symbol_lookup_date('2015-11-28')  
    context.stock_list = symbols('UIHC','NPTN','MFLX','ZAGG','UVE','ODC','EBF','EXTR','HALL','TRR','FSB','BUSE','EMCI','IIIN','TCBK','GNCMA','HCKT','AEPI','ISLE','SBCF','ICFI','ROCK','SHOR','TBK','FORR','HRTG','RTEC','OME','FBNC','SSNI','FNHC','PPBI','MG','SRI','HOFT','GCAP','QCRH','CSS','SNOW','RYAM','EVRI','NOVT','MRCY','NLS','AVD','TSYS','EFSC','JMG','CACB','GABC','AEGN','ANCX','CASH','LYTS','HMTV','FRP','FISI','NX','CCRN','GBNK','SGMS','PGC','ZIXI','VRTV','RPXC','SIGM','HTLF','CRD.B','MBVT','TIPT','OSBC','AMNB','PSTB','NBBC','GARS','WIN','CETV','PCBK','CCNE','OSUR','CWST','CAMP','CUNB','DGII','PATK','HCOM','AT','VRNS','LAWS','MSFG','FOXF','MCBC','CPF','BBRG','ORC','SENEA','AMBC','KAI','BHBK','ABCD','RYI','GRBK','BH','ONDK','MBWM','BEAT','HSII','CNBKA','IBP','HWAY','CORR','PLAB','VEC','BGC','TSQ','SQI','ATLO','FRSH','ROIAK','BLMT','GSBD','TAST','PTX','SREV','CVO','CSFL','NUTR','NEFF','ERI','METR','RMAX','RM','PEBO','MTSN','PFBC','AHH','FSAM','HCCI','MBUU','AMSWA','UEIC','ATEN','LIOX','ABCW','NRIM','ROX','SGBK','APTS','BSET','DXLG','RECN','AGX','MGPI','DX','FUEL','USLM','FARM','ARC','CCG','PERY','ANIK','CTRE','NPK','IMH','UCFC','RST','EPIQ','CBZ','ECHO','HTBK','EVC','FVE','BBSI','RUTH','BCOV','SCVL','UMH','SRDX','FC','FBIZ','NXRT','LDL','CAC','ANGI','GNBC','TRK','ALCO','BNK','EBTC','WEYS','REIS','ESIO','CHEF','UBA','WIFI','TTGT','TTMI')  
    schedule_function(rebalance,  
                      date_rule = date_rules.month_start(),  
                      time_rule = time_rules.market_open(hours=1))  
    schedule_function(recording,  
                     date_rule = date_rules.every_day(),  
                     time_rule = time_rules.market_close())  
def rebalance(context, data):  
    for stock in context.stock_list:  
        if data.can_trade(stock):  
            order_target_percent(stock, 1.0/len(context.stock_list))

def recording(context, data):  
    longs = len(context.portfolio.positions)  
    record(long_position=longs, cash=context.portfolio.cash)  

Note the logs of the backtest. There are a lot of unfilled orders. You can see this also in your recording of portfolio cash. These must be pretty thinly traded stocks. You can change the behavior of the backtest engine to allow ordering a larger percent of actual volume (default is 2.5%). This will get your orders to fill and provide you a better picture of returns. See the documentation https://www.quantopian.com/help#ide-slippage.

Thanks Dan! That helps a lot XD