Hello,
I am trying to run an algorithm with zipeline involving SP500, however I have got the following error whether I use the sid of symbol function.
My backtest begins on January 2008 until December 2016.
Do you have any idea what is wrong ?
Thank you for your help.
Symbol 'SPY' was not found.
SidsNotFound: No asset found for sid: 8554.
Following is my code:
# Companies file directory Wikipedia
companies_file_path='https://docs.google.com/spreadsheets/d/12u_q25eaTF455JOcg8kIh4g6YQzK0cfjc_W5jANxc5U/pub?output=csv'
#economic termns file directory
economic_file_path='https://docs.google.com/spreadsheets/d/1sx6CQlWBuRmqz2X7dnwOI95JWq8aLsgrbaUQ-1Vjow0/pub?output=csv'
from zipline.api import (symbol,sid, order, record, fetch_csv,set_benchmark,schedule_function,
date_rules,time_rules,order_target_percent,order_target,get_datetime,set_commission,commission )
from zipline import run_algorithm
import numpy as np
import datetime
import matplotlib.pyplot as plt
import pytz
from datetime import datetime
import pandas as pd
#from quantopian.pipeline.data.sentdex import sentiment_free
def drop_na(df):
return df.fillna(0)
def backtest(ticker,delta_t1,delta_t2):
def initialize(context):
# This is the search query we are using, this is tied to the csv file.
context.query = str(ticker) #column name for each stock in our universe
# User fetcher to get data. I uploaded this csv file manually.
# Note that this data is daily averages.
fetch_csv(companies_file_path,
date_format='%Y-%m-%d',
pre_func =drop_na,
symbol='wiki',
skiprows=0
)
set_commission(commission.PerTrade(cost=0.000 ))
#Define our securities, global variables, and schedule when to trade.
context.security = sid(8554)
#set_benchmark(symbol(ticker)) # S&P5000 # stock refering to context.query
schedule_function(rebalance,
date_rule=date_rules.every_day(), #day or week
time_rule=time_rules.market_open())
context.past_queries_t1 = []
context.past_queries_t2 = []
def rebalance(context, data):
c = context
dt=get_datetime()
# Extract weekly average of search query.
indicator = data.current('wiki',c.query); #c.query=nom de la colonne dans data
'''Compute the moving average t1'''
# Track our fetched values in a context variable to build up history.
context.past_queries_t1.append(indicator)
if len(context.past_queries_t1) > delta_t1:
del context.past_queries_t1[0]
if len(context.past_queries_t1) == delta_t1:
# Compute average over weeks in range
mean_indicator_t1 = np.mean(context.past_queries_t1)
''' Compute the moving average t2'''
# Track our fetched values in a context variable to build up history.
context.past_queries_t2.append(indicator)
if len(context.past_queries_t2) > delta_t2:
del context.past_queries_t2[0]
if len(context.past_queries_t2) == delta_t2:
# Compute average over weeks in range
mean_indicator_t2 = np.mean(context.past_queries_t2)
# Long or short depending on whether debt search frequency
# went down or up, respectively.
if np.isnan(mean_indicator_t2):
order_target_percent(c.security, 0)
elif mean_indicator_t1>mean_indicator_t2 and mean_indicator_t2>10 and data.can_trade(c.security):
order_target_percent(c.security, -1.0)
elif mean_indicator_t1<mean_indicator_t2 and mean_indicator_t2>10 and data.can_trade(c.security):
order_target_percent(c.security, 1.0)
else:
order_target_percent(c.security, 0)
# Set up the stuff for running the trading simulation
start = datetime(2008, 1, 1, 0, 0, 0, 0, pytz.utc)
end = datetime(2016, 12, 31, 0, 0, 0, 0, pytz.utc)
base_capital = 10000
# run the trading algorithm and save the results in perf
perf = run_algorithm(start, end, initialize, base_capital,bundle = 'quantopian-quandl')
return perf