Check rule individually?

Hello Q,

how can I check a rule individually for every ETF in my specified universe of 11 stocks? Without doing the same thing 11 times...
After that I would need to count for how many of the ETF's its true and then divide 1 by that number, to get my weight to order stocks.
In the attached also you can see where I'm struggling. The current problem is, that it just combines all True and False and gives me 0 or 1. That won't work though since I need a number between 0 and 11.

Thanks for the help.

2
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
 Returns 1 Month 3 Month 6 Month 12 Month
 Alpha 1 Month 3 Month 6 Month 12 Month
 Beta 1 Month 3 Month 6 Month 12 Month
 Sharpe 1 Month 3 Month 6 Month 12 Month
 Sortino 1 Month 3 Month 6 Month 12 Month
 Volatility 1 Month 3 Month 6 Month 12 Month
 Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 595d3cef2bafce4ddc365a62
There was a runtime error.
2 responses

Maybe something like this?

    # data.history returns a pandas dataframe. rows are dates. columns are securities.
hist = data.history(context.alle, "price", 200, "1d")

# pandas dataframes can easily perform math over all securities in a single statement
# the result is a pandas series which is magically now indexed by security
sma_200 = hist.mean(axis=0)
sma_50 = hist.iloc[-50:].mean(axis=0)

# put the two series together into a single dataframe for easy manipulation
my_data = pd.concat([sma_200, sma_50], axis=1)

# name the columns to be something readible
my_data.columns = ['sma_200', 'sma_50']

# use the pandas query method to perform whatever logic one wishes on the columns.
# it will return a list with only those securities that pass

# to get the number of securities which pass use len()
# set the weight but consider the situation where len = 0
try:
weight =  1.0 / len(ps)
except:
weight = 0.0

# order the target value of all stocks which pass your criteria
for stock in ps:
order_target_percent(stock, weight)

# close out all current positions which don't meet the criteria
for stock in context.portfolio.positions:
if stock not in ps and data.can_trade(stock):
order_target_percent(stock, 0.0)



Since you are using daily data only, pipeline should maybe be used rather than the 'data.history' method. It will be faster and the 'rules' may be easier by using the factor methods. Just an idea. See attached algorithm which is a modified version of the original.

0
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
 Returns 1 Month 3 Month 6 Month 12 Month
 Alpha 1 Month 3 Month 6 Month 12 Month
 Beta 1 Month 3 Month 6 Month 12 Month
 Sharpe 1 Month 3 Month 6 Month 12 Month
 Sortino 1 Month 3 Month 6 Month 12 Month
 Volatility 1 Month 3 Month 6 Month 12 Month
 Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 595d65cb9577044ddbd2326a
There was a runtime error.

Alright, thanks Dan. That was exactly what I was looking for. Probably pipeline would be better, but with specified stocks it is always a struggle for me to get it done.