Back to Community
curious phenomenon: an unknown stock is mixed in my portfolio

Today by backtesting I find a very curious thing: In my algo I just have the stock MDLZ. But from a date another stock KRFT is mixed in the portfolio. See my attached backtesting. I doubt if it has a little to do with the black hole? :-/

Formerly I have sometime quite mysterious error by backtesting. But I fail to find out the reason. Today I catch it.

Clone Algorithm
0
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
"""
This is a template algorithm on Quantopian for you to adapt and fill in.
"""
import quantopian.algorithm as algo
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.filters import QTradableStocksUS


def initialize(context):
    """
    Called once at the start of the algorithm.
    """
    context.stock = symbol('MDLZ')
    # Rebalance every day, 1 hour after market open.
    algo.schedule_function(
        rebalance,
        algo.date_rules.every_day(),
        algo.time_rules.market_open(hours=1),
    )

    # Record tracking variables at the end of each day.
    algo.schedule_function(
        record_vars,
        algo.date_rules.every_day(),
        algo.time_rules.market_close(),
    )

    # Create our dynamic stock selector.
    algo.attach_pipeline(make_pipeline(), 'pipeline')


def make_pipeline():
    """
    A function to create our dynamic stock selector (pipeline). Documentation
    on pipeline can be found here:
    https://www.quantopian.com/help#pipeline-title
    """

    # Base universe set to the QTradableStocksUS
    base_universe = QTradableStocksUS()

    # Factor of yesterday's close price.
    yesterday_close = USEquityPricing.close.latest

    pipe = Pipeline(
        columns={
            'close': yesterday_close,
        },
        screen=base_universe
    )
    return pipe


def before_trading_start(context, data):
    """
    Called every day before market open.
    """
    context.output = algo.pipeline_output('pipeline')

    # These are the securities that we are interested in trading each day.
    context.security_list = context.output.index


def rebalance(context, data):
    """
    Execute orders according to our schedule_function() timing.
    """
    if context.stock not in context.portfolio.positions:
        order_target_percent(context.stock, 0.5)

def record_vars(context, data):
    """
    Plot variables at the end of each day.
    """
    for stock in context.portfolio.positions:
        print("Stock:%s" %stock)
        
    record(Stocks = len(context.portfolio.positions))        


def handle_data(context, data):
    """
    Called every minute.
    """
    pass
There was a runtime error.
8 responses

Such a problem doesn't just happen to MDLZ, but also to some others.

One can see at the beginning there is only one stock MDLZ in the portfolio:
... 2012-08-20 21:59 PRINT Stock:Equity(22802 [MDLZ])
2012-08-21 21:59 PRINT Stock:Equity(22802 [MDLZ])
2012-08-22 21:59 PRINT Stock:Equity(22802 [MDLZ])
...

But from 2012-10-02, another unknown stock 'KRFT' is mixed in my portfolio. Terrible. :-)
... 2012-10-02 21:59 PRINT Stock:Equity(22802 [MDLZ])
2012-10-02 21:59 PRINT Stock:Equity(43405 [KRFT])
2012-10-03 21:59 PRINT Stock:Equity(22802 [MDLZ])
2012-10-03 21:59 PRINT Stock:Equity(43405 [KRFT])
2012-10-04 21:59 PRINT Stock:Equity(22802 [MDLZ])
2012-10-04 21:59 PRINT Stock:Equity(43405 [KRFT])

In 2012 there was a Mondelez/Kraft spin-off
See here

@Albert,

Thanks for the information. Do you know where can one find the general spin-off information? I think maybe there is spin-off not only by MDLZ but also by some others.

Besides, such spin-off will influence or even distroy the result of my algo. I wonder how to detect such spin-off when I run the backtesting, especially when I do live trade?

Thomas

I update my algo: as soon as the spin-off happens, I will sell it. One can see the RETURN is different than I haven't sold it.

I find this phenomenon occacionally. It seems I have backtesting all of my algos again under considering the spin-off.

backtesting

Clone Algorithm
0
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
"""
This is a template algorithm on Quantopian for you to adapt and fill in.
"""
import quantopian.algorithm as algo
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.filters import QTradableStocksUS


def initialize(context):
    """
    Called once at the start of the algorithm.
    """
    context.stock = symbol('MDLZ')
    # Rebalance every day, 1 hour after market open.
    algo.schedule_function(
        rebalance,
        algo.date_rules.every_day(),
        algo.time_rules.market_open(hours=1),
    )

    # Record tracking variables at the end of each day.
    algo.schedule_function(
        record_vars,
        algo.date_rules.every_day(),
        algo.time_rules.market_close(),
    )

    # Create our dynamic stock selector.
    algo.attach_pipeline(make_pipeline(), 'pipeline')


def make_pipeline():
    """
    A function to create our dynamic stock selector (pipeline). Documentation
    on pipeline can be found here:
    https://www.quantopian.com/help#pipeline-title
    """

    # Base universe set to the QTradableStocksUS
    base_universe = QTradableStocksUS()

    # Factor of yesterday's close price.
    yesterday_close = USEquityPricing.close.latest

    pipe = Pipeline(
        columns={
            'close': yesterday_close,
        },
        screen=base_universe
    )
    return pipe


def before_trading_start(context, data):
    """
    Called every day before market open.
    """
    context.output = algo.pipeline_output('pipeline')

    # These are the securities that we are interested in trading each day.
    context.security_list = context.output.index


def rebalance(context, data):
    """
    Execute orders according to our schedule_function() timing.
    """
    if context.stock not in context.portfolio.positions:
        order_target_percent(context.stock, 0.5)

def record_vars(context, data):
    """
    Plot variables at the end of each day.
    """
    for stock in context.portfolio.positions:
        price = data.current(stock, 'price')
        print("Stock:%s, Price:%.2f" %(stock, price))
        if stock is not context.stock:
            order_target_percent(stock, 0)
            log.info("Sell %s at %.2f" %(stock, price))
        
    record(Stocks = len(context.portfolio.positions))       

def handle_data(context, data):
    """
    Called every minute.
    """
    pass
There was a runtime error.

Yeah sure, you can subscribe to the spin-off datafeed:
https://www.quantopian.com/data/eventvestor/spin_offs
There is also one for M&A, which you probably also want to consider!
https://www.quantopian.com/data/eventvestor/mergers_and_acquisitions

Many thanks!