Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Backtest once/year custom signal

Hello,
I want to test a csutom back signal that comes one time each year an October. I want the backtester to trade on start=2014-10-27 and hold assets till the next signal on 2015-10-26 and so on for the next years.

The problem is the backtester gives me this error when i try building

Runtime exception: NoDataAvailable: Backtest began on 2014-10-27 and ended on 2015-10-26, but some of the requested datasets do not have data for this time. The datasets are: user_5c8907a847f10d52c00167b0.signal_overall: start=2014-10-27, end=2015-10-26. Your backtest must begin on or after: 2014-10-27 and end on or before: 2015-10-26.

I understand that this error is because the dataset doesn't have data for the days between the start and end of the test but i don't know how to fix it.
here is my code


# Import the libraries we will use here  
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 AverageDollarVolume, Returns  
from quantopian.pipeline.data.user_5c8907a847f10d52c00167b0 import signal_overall  
"""
The initialize function is the place to create your pipeline (stock selector),  
and set trading conditions such as commission and slippage. It is called once  
at the start of the simulation and also where context variables can be set.  
"""
def initialize(context):  
    # Define context variables that can be accessed in other methods of  
    # the algorithm.  
    context.long_leverage = 0.5  
    context.short_leverage = -0.5  
    context.returns_lookback = 5  
    # Rebalance every Monday (or the first trading day if it's a holiday).  
    # At 11AM ET, which is 1 hour and 30 minutes after market open.  
    now = datetime.datetime.now()  
    if (now.year == 2014 and now.month == 10 and now.day == 27) or (now.year == 2015 and now.month == 10 and now.day == 26):  
        schedule_function(my_rebalance, date_rules.month_end(), time_rules.market_close(minutes=30))  
    '''  
    schedule_function(rebalance,  
                      date_rules.every_day(),  
                      time_rules.market_open(hours = 1, minutes = 30))  
    '''  
    # Record tracking variables at the end of each day.  
    schedule_function(record_vars,  
                      date_rules.every_day(),  
                      time_rules.market_close(minutes=1))  
    # Create and attach our pipeline (dynamic stock selector), defined below.  
    attach_pipeline(make_pipeline(context), 'signal_example')  
def make_pipeline(context):  
    # Create a pipeline object.  
    pipe = Pipeline()  


    signal=signal_overall.signal.latest  
    pipe.add(signal, 'signal')  
    signal_assets = signal_overall.signal.latest.notnull()  
    low_signal = signal.percentile_between(0,10,mask=signal_assets)  
    high_signal = signal.percentile_between(90,100,mask=signal_assets)  

    pipe.add(signal.rank(mask=signal_assets), 'signal_rank')  
    pipe.set_screen((low_signal | high_signal) & signal_assets)  
    pipe.add(low_signal, 'low_returns')  
    pipe.add(high_signal, 'high_returns')  
    return pipe

"""
Called every day before market open. This is where we get the securities  
that made it through the pipeline.  
"""
def before_trading_start(context, data):  
    # Pipeline_output returns a pandas DataFrame with the results of our factors  
    # and filters.  
    context.output = pipeline_output('signal_example')  
    # Sets the list of securities we want to long as the securities with a 'True'  
    # value in the low_returns column.  
    context.long_secs = context.output[context.output['low_signal']]  
    # Sets the list of securities we want to short as the securities with a 'True'  
    # value in the high_returns column.  
    context.short_secs = context.output[context.output['high_signal']]  
    # Update our universe to contain the securities that we would like to long and short.  
    update_universe(context.long_secs.index.union(context.short_secs.index))

"""
This rebalancing function is called according to our schedule_function settings.  
"""
def rebalance(context,data):  
    # Get any open orders that we may have, to prevent double ordering.  
    open_orders = get_open_orders()  
    # Set the allocations to even weights in each portfolio.  
    long_weight = context.long_leverage / (len(context.long_secs) + len(open_orders)/2)  
    short_weight = context.short_leverage / (len(context.short_secs) + len(open_orders)/2)  
    # For each security in our universe, order long or short positions according  
    # to our context.long_secs and context.short_secs lists, and sell all previously  
    # held positions not in either list.  
    for stock in data:  
        # Guard against ordering too much of a given security if a previous order  
        # is still unfilled.  
        if stock not in open_orders:  
            if stock in context.long_secs.index:  
                order_target_percent(stock, long_weight)  
            elif stock in context.short_secs.index:  
                order_target_percent(stock, short_weight)  
            else:  
                order_target_percent(stock, 0)  
    # Log the long and short orders each week.  
    log.info("This week's longs: "+", ".join([long_.symbol for long_ in context.long_secs.index]))  
    log.info("This week's shorts: "  +", ".join([short_.symbol for short_ in context.short_secs.index]))

"""
This function is called at the end of each day and plots certain variables.  
"""
def record_vars(context, data):  
    # Record and plot the leverage of our portfolio over time. Even in minute  
    # mode, only the end-of-day leverage is plotted.  
    record(leverage = context.account.leverage)

    # We also want to monitor the number of long and short positions  
    # in our portfolio over time. This loop will check our positition sizes  
    # and add the count of longs and shorts to our plot.  
    longs = shorts = 0  
    for position in context.portfolio.positions.itervalues():  
        if position.amount > 0:  
            longs += 1  
        if position.amount < 0:  
            shorts += 1  
    record(long_count=longs, short_count=shorts)

"""
The handle_data function is called every minute. There is nothing that we want  
to do every minute in this algorithm.  
"""
def handle_data(context,data):  
    pass  

Thanks in advance,

1 response

any help ?