Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How can fetch_csv data be used in back testing?
  1. The help pages for Fetcher says: "Create a trailing window of data using history to make statistical models using your fetcher data". How do I actually make the connection between the fetched data and the history function? In my test script below, I don't seem to be able to make that connection, at least I don't get the prices['mean_ratio'] calculation to work (which I do for standard price data).

  2. Is it possible to put the fetched data (in my example Swedish Steel / SSAB stock prices from Quandl) the context for the algorithm? I need to add e.g. "context.stock = sid(3766)" in the script to get out any diagram data through the record function. Or is fetched data only supposed to complement the US stocks in the standard context? I think I'm asking if it is possible to only have fetched data in the context.stock?

  3. Related to the above (i.e. 2.), is it possible to back-test buy & sell orders of the fetched instrument (in my example below Swedish Steel preference and normal stocks / SSAB-A and SSAB-B)? And in that case, how should I formulate it? The order function wants a SID or SYMBOL, assuming from the standard US context.

import pandas as pd

def rename_col(df):  
    df = df.rename(columns={'Close': 'price'})  
    df = df.fillna(method='ffill')  
    df = df[['price', 'sid']]  
    log.info(' \n %s % df.head()')  
    return df  
def initialize(context):  
    fetch_csv('https://www.quandl.com/api/v1/datasets/GOOG/STO_SSAB_A.csv?trim_start=2013-11-18',  
        date_column='Date',  
        symbol='ssaba',  
        usecols=['Close'],  
        post_func=rename_col,  
        date_format='%Y-%m-%d'  
        )  
    fetch_csv('https://www.quandl.com/api/v1/datasets/GOOG/STO_SSAB_B.csv?trim_start=2013-11-18',  
        date_column='Date',  
        symbol='ssabb',  
        usecols=['Close'],  
        post_func=rename_col,  
        date_format='%Y-%m-%d'  
        )  
    context.stock = sid(3766)     #Why is this needed? How do I get my fetched data into the context

def handle_data(context, data):  
    stockA = data['ssaba']  
    stockB = data['ssabb']  
    prices = history(bar_count=50, frequency='1d', field='price')  
    prices['ratio'] = stockA.price / stockB.price      #This shows up nicely with record  
    prices['mean_ratio'] = pd.rolling_mean(prices['ratio'], 25)       #This shows up as identical as prices['ratio'], not as a rolling mean

    ratio=prices['ratio'][-1]  
    mean_ratio = prices['mean_ratio'][-1]  
    record(ratio=ratio)  
    record(mean_ratio=mean_ratio)