Back to Community
Unable to Order

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...
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
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)                
    
There was a runtime error.
3 responses

Hi William,

I took a look at your code and it seems that the value for the order_target_percent() function in line 173 (and also in 180) was not evaluating to a float. Note that to get this value out of your dataframe, you'll want to do something like:

context.shorts[context.shorts.index==security]['Factor'][0]  

I've attached a functioning version to this post.

I can't seem to find where you cloned this algo from, do you think you could point it out to me so I can take a look at it as well?

Clone Algorithm
3
Loading...
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
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))
            print context.shorts
            order_target_percent(security, context.shorts[context.shorts.index==security]['Factor'][0])
            
    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[context.longs.index==security]['Factor'][0])
            
    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)                
    
There was a runtime error.
Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Hi Jamie,

Thanks a lot, that's brilliant! The factor itself was my own concoction (quite a clunky one at that) , but the framework I based it around were the algos here: https://www.quantopian.com/posts/long-short-pipeline-multi-factor.

Will

update_universe(context.longs.index | context.shorts.index) shows error for me.