Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Algorithm selling outside the set trading month

This is my algorithm:

import quantopian.algorithm as algo  
from quantopian.pipeline import Pipeline  
from quantopian.pipeline.data.builtin import USEquityPricing  
from quantopian.pipeline.filters import QTradableStocksUS  
from quantopian.pipeline.data.morningstar import Fundamentals  
from quantopian.pipeline import  CustomFactor  
from quantopian.pipeline.classifiers.morningstar import Sector  
import quantopian.optimize as opt  
import pandas as pd  
import numpy as np

N_ASSETS = 30  
SHORT = False  
MAX_GROSS_EXPOSURE = 1.0

def initialize(context):  
    """  
    Called once at the start of the algorithm.  
    """  
    algo.schedule_function(  
        rebalance,  
        algo.date_rules.month_start(days_offset=10),  
        algo.time_rules.market_open(hours=1),  
    )

    algo.schedule_function(  
        record_vars,  
        algo.date_rules.every_day(),  
        algo.time_rules.market_close(),  
    )

    algo.attach_pipeline(make_pipeline(), 'pipeline')  
    set_slippage(slippage.FixedSlippage(spread=0))  
    set_commission(commission.PerShare(cost=0))  
    context.rebalance = 0


def make_pipeline():  
    no_fin_no_utils = ((Sector != 103) & (Sector != 207)) # no financials, no utilites  
    large_cap = (Fundamentals.market_cap.latest>50e6)  
    no_low_pe_ratios = (Fundamentals.pe_ratio.latest > 0.5)  
    no_foreign_companies = ~Fundamentals.is_depositary_receipt.latest  
    my_universe = QTradableStocksUS() & large_cap & no_fin_no_utils & no_low_pe_ratios & no_foreign_companies

    rank = MyFactor().earnings_yield.rank(ascending=False) \  
           + MyFactor().return_on_capital.rank(ascending=False)  
    pipe = Pipeline(  
        columns={  
            'rank': rank  
        },  
        screen=my_universe  
    )  
    return pipe


class MyFactor(CustomFactor):  
    inputs = [Fundamentals.ebit.latest,  
              Fundamentals.working_capital.latest,  
              Fundamentals.net_ppe.latest,  
              Fundamentals.enterprise_value.latest]  
    outputs = ['return_on_capital', 'earnings_yield']  
    window_length = 1  
    def compute(self, today, asset_ids, out, ebit, net_working_capital, net_fixed_assets, enterprise_value):  
        out.return_on_capital[:] = ebit/(net_working_capital+net_fixed_assets)  
        out.earnings_yield[:] = ebit/enterprise_value

def before_trading_start(context, data):  
    context.output = algo.pipeline_output('pipeline')  
    context.security_list = context.output.index


def rebalance(context, data):  
    long_stocks = context.output.nlargest(N_ASSETS, 'rank').index.values  
    long_weights = pd.Series(index=long_stocks, data=0.3*np.ones(len(long_stocks)))  
    print(len(context.output.index.values))  
    if SHORT:  
        short_stocks = context.output.nsmallest(N_ASSETS, 'rank').index.values  
        short_weights = pd.Series(index=short_stocks, data=0.3*np.ones(len(short_stocks)))  
        target_weights = long_weights.append(short_weights)  
    else:  
        target_weights = long_weights  
    objective = opt.TargetWeights(target_weights)  
    constraints = []  
    constraints.append(opt.MaxGrossExposure(MAX_GROSS_EXPOSURE))  
    if get_datetime().month==1:  
        algo.order_optimal_portfolio(objective, constraints)  
        context.rebalance = 1  
    else:  
        context.rebalance = 0


def record_vars(context, data):  
    record('rebalance', context.rebalance)  
    pass  

I want to rebalance only every January, that is why I have this code:

    if get_datetime().month==1:  
        algo.order_optimal_portfolio(objective, constraints)  
        context.rebalance = 1  
    else:  
        context.rebalance = 0  

However, when I run a full backtest, the algorithm does the rebalancing every January as expected, but it also sells (typically only one stock) in some other month which is not January. I am not sure why?

3 responses

The backtest engine will automatically close any positions where the stock no longer trades. This is probably what you are seeing. If a stock is held but can no longer trade, it will be closed (ie sold if held long). The price used for this 'sale' is the last available closing price. In most cases, this is exactly what one would experience in real trading. The broker would unilaterally close the position and add any cash proceeds to the account.

Is that what you are seeing?

Also, it very much helps if, rather than pasting code into a post, one attaches a backtest. Use the dropdown menu in the upper right corner of the post entry text box. This makes it much easier for others to troubleshoot and you may get better responses.

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.

Thanks, this is probably what I am seeing, as these stocks do not trade anymore...
But when exactly the stock is sold? On the day of the delisting announcement? The day before the stock is delisted?
This is quite important as if the reason is bankruptcy the stock will start loosing value right after the delisting announcement.

The stock position will be closed when it can no longer trade which is typically the day it is delisted. The price will be the last available price which is typically the day before the delisting. There isn't a good data source currently on Quantopian which could check, for example, that a company 'announced' bankruptcy plans. Do note that there are many reasons a stock no longer trades. The biggest ones are a merger or acquisition. In those cases the stock price may be quite high through the last day of trading.