Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Fundamental algorithm error

I've tried to create a basic algorithm using roa, roe and pe ratio as filters. I used the structure of an algorithm from one of the pipeline tutorials but I think I messed the algo a bit. I believe that I have some logic errors as thee algorithm doesn't pace trades... Also, I receive an error regarding dividing float by 0 in this: picks_weight = context.long_leverage/ len(context.picks) --> so I think context.picks is by mistake 0 even though it should contain the number of stocks that respect the criteria.

Please help!

2 responses

It seems like there aren't any stocks matching your criteria

    ROA = morningstar.operation_ratios.roa.latest > 0.1  
    ROE = morningstar.operation_ratios.roe.latest >  0.2  
    PE = morningstar.valuation_ratios.pe_ratio.latest < 1.5

That is why it doesn't place any trades. In calculating the weight one should check for this condition (ie length equals 0) so to not divide by 0. Something maybe like this.

def compute_target_weights(context, data):  
    pick_qty = len(context.picks)  
    picks_weight = context.long_leverage/ pick_qty if pick_qty>0 else 0.0  
    return picks_weight

Attached is the algo with this change and the criteria opened up to return some stocks. Good luck.

Thank you very much for helping me Dan! By having this algorithm put in place now I can pay with the filters! Also, I think that the problem is that companies in QTradableStocksUS don't have this small pe ratio because they are blue chip companies. Thanks again!