Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Reverse and inverse backtesting

hi,
This is just in my head for a couple of days, I am wondering if it would make sense to backtest an algo with the same data but reversed, that is from now back into the time, and to inverse the data by mirroring it, so a bull will become a bear and visaversa. Since 2009 the stock market has increased significantly...so maybe the data we use to backtest is too positive. At the moment we see a more side wards pattern. are the algos we make still viable? I see remarks in forums like "runs well till 2009, or since last years no returns.".

If others find it interesting to reverse and inverse data these might be nice features for Quantopian. Other idea is to have a random start. Maybe controllable byown code.

J..

1 response

J, testing on simulated data is always a good idea, and reversing real data is a simple way to get simulated prices. It makes sense, if the algorithm should make money regardless of market conditions, it should work with the data reversed. It can't be done in Quantopian, but zipline will run on whatever you throw at it, you could give that a shot.

Here's the generic zipline example with a function to reverse the index of the data it uses.

import pytz  
import pandas as pd  
from datetime import datetime  
from zipline.utils.factory import load_from_yahoo  
from zipline import TradingAlgorithm  
from zipline.api import *


def initialize(context):  
    context.stock = 'AAPL'


def handle_date(context, data):  
    order(context.stock, 10)  

def reversed_data(data):  
    ''' Reverses the index of a pandas timeseries '''  
    type_ = type(data)  
    rdata = type_(data.values, index=data.index[-1::-1])  
    if type_ == pd.DataFrame:  
        rdata.columns = data.columns  
    return rdata.sort_index()


start = datetime(2005, 12, 22, 0, 0, 0, 0, pytz.utc)  
end = datetime(2012, 6, 3, 0, 0, 0, 0, pytz.utc)

data = load_from_yahoo(stocks=['AAPL'], indexes={}, start=start,  
                       end=end)  
data = data.dropna()  
data = reversed_data(data)  
algo = TradingAlgorithm(initialize=initialize,  
                        handle_data=handle_date)  
results = algo.run(data)  
results.portfolio_value.plot()