I’ve been using the debugger in the IDE to dig down into what my algorithm is actually doing and found some strange results. The general form of my filter is like this (where I substitute values for x and y):
def before_trading_start(context):
fundamental_df = get_fundamentals(
query(
fundamentals.operation_ratios.roa,
fundamentals.valuation_ratios.pe_ratio,
fundamentals.valuation.market_cap
)
.filter(fundamentals.valuation.market_cap > x)
.filter(fundamentals.valuation_ratios.pe_ratio > y)
.filter(fundamentals.operation_ratios.roa > 0)
.order_by(fundamentals.valuation_ratios.pe_ratio)
.limit(50)
)
context.fundamental_df = fundamental_df
update_universe(context.fundamental_df.columns.values)
I looked at the dataframe returned from three separate queries using different filter values and found something weird.
- P/E > 20, market cap > 50000000, February 2002
Of the fifty companies returned the ROA values were as follows:
5 had 2.04%
7 had 0.95%
13 had 7.062%
25 had 1.87%
- P/E > 5, market cap > 50000000, February 2002
Of the fifty companies returned the ROA values were as follows:
5 had 2.09%
7 had 3.10%
13 had 0.29%
25 had 25.56%
- P/E > 5, any market cap, October 2014
Of the fifty companies returned the ROA values were as follows:
5 had 3.61%
7 had 0.14%
13 had 0.74%
25 had 1.86%
Each query returns this pattern of 5,7,13,25 ROA’s that are the same. The same pattern is also found in the returned P/E and Market cap metrics (with different numbers).
I guess the problem is this could have implications for ranking stocks.
I went and used an online stock scanner and didn't see anything like this pattern (for stocks currently).
Anybody else found this when ranking stocks by fundamental metrics?
Edit:
I am using context.fundamental_df.irow(z) (where z is 0 = ROA, 1 = P/E and 2 = Market cap) in the debugger console to get at the raw values. Could this possibly be introducing some type of error?