Despite using a clone as an example I'm getting and error when placing orders. The stocks must be in the universe to have passed through the if statement, so I'm at a loss as too why the error is being kicked up. Any help would be appreciated.

Will

Clone Algorithm

7

Loading...

There was an error loading this backtest.
Retry

Backtest from
to
with
initial capital

Cumulative performance:

Algorithm
Benchmark

Custom data:

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 |

from quantopian.algorithm import attach_pipeline, pipeline_output from quantopian.pipeline import Pipeline from quantopian.pipeline.data.builtin import USEquityPricing from quantopian.pipeline.factors import CustomFactor, SimpleMovingAverage from quantopian.pipeline.data import morningstar import numpy as np import pandas as pd from sklearn.decomposition import PCA from sklearn import linear_model from sklearn.feature_selection import RFECV, RFE class Factor(CustomFactor): window_length = 200 inputs = [USEquityPricing.close, # morningstar.earnings_ratios.equity_per_share_growth, # morningstar.valuation_ratios.book_value_per_share, # morningstar.valuation_ratios.cash_return, # morningstar.valuation_ratios.cf_yield, # morningstar.valuation_ratios.cfo_per_share, morningstar.valuation_ratios.ev_to_ebitda, # morningstar.valuation_ratios.fcf_per_share, morningstar.valuation_ratios.fcf_ratio, morningstar.valuation_ratios.pb_ratio, morningstar.valuation_ratios.pcf_ratio, morningstar.valuation_ratios.pe_ratio, # morningstar.valuation_ratios.peg_payback, # morningstar.valuation_ratios.peg_ratio, # morningstar.valuation_ratios.ps_ratio, # morningstar.valuation_ratios.sales_per_share, # morningstar.valuation_ratios.sustainable_growth_rate, # morningstar.valuation_ratios.tangible_book_value_per_share, # morningstar.valuation_ratios.working_capital_per_share ] def compute(self, today, assets, out, close, *inputs): close = pd.DataFrame(close) inputs = [pd.DataFrame(x) for x in inputs] returns = (close - close.shift(1))/close.shift(1) returns = returns.mean(axis = 1) returns = returns.to_frame() # print(returns) data = [self.preprocess(x) for x in inputs] data = self.ts_to_df(data) # print(data) pca= PCA() pca.fit(data) t_data = pd.DataFrame(pca.transform(data)) t_data.index = data.index # print(t_data) merged_data = pd.merge(returns[1:], t_data.shift(1)[1:], right_index=True, left_index=True, how='inner') reg = linear_model.SGDRegressor(fit_intercept=True) model = RFECV(reg, step=1) model.fit(merged_data.iloc[:,1:], merged_data.iloc[:,0]) # print(prediction) for i, asset in enumerate(assets): asset_data = [x.iloc[-1,i] for x in inputs] try: t_asset_data = pca.transform(asset_data) prediction = model.predict(t_asset_data) out[i] = prediction[0] except: out[i] = 0 def preprocess(self, df): df = df.fillna(0) df = df.mean(axis = 1, skipna = True) return (df-df.mean(skipna = True))/(df.max()-df.min()) def ts_to_df(self, ts_array): df = pd.DataFrame() for i, x in enumerate(ts_array): df[i] = x return df class MarketCap(CustomFactor): inputs = [USEquityPricing.close, morningstar.valuation.shares_outstanding] window_length = 1 def compute(self, today, assets, out, close, shares): out[:] = close[-1] * shares[-1] class AvgDailyDollarVolumeTraded(CustomFactor): inputs = [USEquityPricing.close, USEquityPricing.volume] window_length = 20 def compute(self, today, assets, out, close_price, volume): out[:] = np.mean(close_price * volume, axis=0) def before_trading_start(context, data): context.results = pipeline_output('PCA') context.longs = context.results[context.results["Factor"] > 0] context.shorts = context.results[context.results["Factor"] < 0] context.longs = context.longs.sort().tail(200) context.shorts = context.shorts.sort().head(200) context.shorts /= context.shorts.abs().sum() context.longs /= context.longs.sum() update_universe(context.shorts.index | context.longs.index) def initialize(context): context.spy = sid(8554) context.passes = 0 pipe = Pipeline() attach_pipeline(pipe, name = 'PCA') # mkt_cap = MarketCap() # top_n= mkt_cap.top(1) # pipe.set_screen(top_n) sma_200 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=200) dollar_volume = AvgDailyDollarVolumeTraded() pipe.set_screen((sma_200 > 5) & (dollar_volume > 10**7)) context.shorts = None context.longs = None pipe.add(Factor(), "Factor") schedule_function(rebalance, date_rules.every_day(), time_rules.market_open()) schedule_function(cancel_open_orders, date_rules.every_day(), time_rules.market_close()) def handle_data(context, data): record(lever = context.account.leverage, exposure = context.account.net_leverage, num_pos = len(context.portfolio.positions), oo = len(get_open_orders())) def rebalance(context, data): for security in context.shorts.index: if get_open_orders(security): continue if security in data: log.info("Shorting %s" % str(security.symbol)) order_target_percent(security, context.shorts[security]) for security in context.longs.index: if get_open_orders(security): continue if security in data: log.info("Buying %s" % str(security.symbol)) order_target_percent(security, context.longs[security]) for security in context.portfolio.positions: if get_open_orders(security): continue if security in data: if security not in (context.longs.index | context.shorts.index): order_target_percent(security, 0) def cancel_open_orders(context, data): for security in get_open_orders(): for order in get_open_orders(security): cancel_order(order)