Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
symbol() and sid() undefined

When I do

symbol('QQQ')

I get the following error

NameErrorTraceback (most recent call last)
in ()
----> 1 symbol('QQQ')

NameError: name 'symbol' is not defined

What module do I need to import?

Saleem

9 responses

symbols works in the research environment. Either symbols or symbol work in algorithms. The parameters are a little different in each though.

See https://www.quantopian.com/help#api-symbols and https://www.quantopian.com/help#quantopian_research_symbols

I have the same problem. The references listed by Dan Whitnable still do not answer the question: what need to be imported to avoid the error?

Try defining them in initialize using context.stocks = symbols('QQQ') or context.stocks = symbols('QQQ', 'SP', 'AAPL').
Works like a charm. Eventually, you might want to use sids instead. My contest algo used to get rejected for using symbols. After switching to sids there was no problem.

@Yong Liu Are you running standalone Zipline, an algorithm online via Quantopian, or a notebook via Quantopian? Technically, the symbol method resides in 'zipline.api' (look for 'symbol' here http://www.zipline.io/appendix.html ). If you are running offline then do this

from zipline.api import symbol, symbols

However, when using the Quantopian online platform there is no need to do this (actually don't do that)

Thank you, Dan and Bryan, for the quick response!
I just cloned a simple program in notebook, and it does not run. Here is the program:


import talib
import pandas as pd
from zipline.api import symbol, symbols

-----------------------------------------

stock = sid(24)
Fast, Slow, Sig = 12, 26, 9

-----------------------------------------

def initialize(context):
schedule_function(trade, date_rules.every_day(), time_rules.market_open(hours=1))

def trade(context,data):
bars = Fast + Slow + Sig

prices = data.history(stock, 'price', bars, '1d')  
macd, signal, hist = talib.MACD(prices, Fast, Slow, Sig)  

if macd[-1] > signal[-1]:  
    order_target_percent(stock, 0.8)  
elif macd[-1] < signal[-1]:  
    order_target_percent(stock, 0)  


record(macd = macd[-1], signal = signal[-1]) 

It still does not work after adding the zipline.api. I ran this in Quantopian online platform.
Is there some hidden environment set up I need to do in general? The program looks extremely simple, and I did not make any change except trying out symbol vs sid. None of them works. The error message is like the following:

NameErrorTraceback (most recent call last)
in ()
4
5 # -----------------------------------------
----> 6 stock = sid(24)
7 Fast, Slow, Sig = 12, 26, 9
8 # -----------------------------------------

NameError: name 'sid' is not defined

For symbol(), I get the following error message:

RuntimeErrorTraceback (most recent call last)
in ()
4
5 # -----------------------------------------
----> 6 stock = symbol('amd')
7 Fast, Slow, Sig = 12, 26, 9
8 # -----------------------------------------

/build/src/qexec_repo/zipline_repo/zipline/utils/api_support.pyc in wrapped(*args, **kwargs) 52 raise RuntimeError(
53 'zipline api method %s must be called during a simulation.'
---> 54 % f.name
55 )
56 return getattr(algo_instance, f.name)(*args, **kwargs)

RuntimeError: zipline api method symbol must be called during a simulation.

Does that mean symbol() will not work in research or notebook?

Hello Yong.
There are more experienced people here than me, but this might help.
Most of my work is done in the Algorithms section of the online Quantopian tool, so I can't help with Zipline.
This implementation of your code is intended to be ran from within an Algorithm.
Note: It's important to use 'context' when passing information between functions.
Hope this helps.

I'll add on to what @Bryan Richardson posted. Generally the notebook environment is for analyzing data and for research. The IDE environment is for running backtests and actual strategy coding.

@Yong Liu mentioned in the previous post "I just cloned a simple program in notebook, and it does not run."

Programs are meant to be run in the IDE and NOT notebooks. Additionally, when using either the online Quantopian IDE or notebooks it's NEVER necessary to import Zipline. While Zipline is the underlying backtest engine for the IDE it's completely wrapped for you in the IDE environment. Maybe review the tutorials at https://www.quantopian.com/tutorials/getting-started.

Attached is a simple research notebook showing that the 'symbols' method does (magically) work without any imports.

Thank you for the examples, Bryan and Dan.

Looks like my implicit assumption that any program I cloned should be able to run out of the box is NOT true.

For now, your examples solved my problem. Thanks.