Back to Community
Match sid of stocks available in quantopian with csv imported

I have a csv file which contains list of stocks in
Symbol , Start_range , End range type.I want to match those symbols with equivalent quantopian symbols and then sids to make a set of stocks to be traded.Can anyone give an example for this

4 responses

I have a post about creating SID from string and its limitations and alternatives.
Basically you have to setup in your code a big switch statement say
if "AAPL":
return sid(2440)
.. ..

Quantopian pre-process your code to check all SID you are using and do some magics so no dynamic SIDs

That is like creating a dictionary of symbol and SID.Is there any available

When you use fetch_csv, the symbols automatically get mapped to the sids. If your universe of stocks is dynamic, and changes over time in the file, you want to use the "unvierse_func" parameter. Here's an example:


The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Those looking to use an external "fetcher" symbol list might use this technique to get your securities, by date, into and out of your list.

The data has some funky dates in it, and frankly the whole fetcher auto-synchronization mechanism is utterly unintuitive. Still I think if you futz around with your data you can get your symbol lists into a local array and use the SQLAlchemy "in_(array here)" mechanism to build your custom security lists from external data.

And you can tidy up the SQL query a bit, prolly don't need the market cap filter. But this seems to work somewhat. Again, fetcher sync is not entirely clear. Note the staggered dates in the file. There's prolly a way to fix this up and eliminate the dummydate. So, hey, if you fix it, share it.

def before_trading_start(context):  
    f = fundamentals  
    now = get_datetime().date()  
    if ('FetcherDeck' not in context):  
    # Select the set for this date  
    symbolListDate = datetime.datetime.strptime(context.FetcherDeck['ProcessDate'][0], "%Y-%m-%d").date()  
    if (symbolListDate != now):  
    # Add in any new symbols  
    added = context.FetcherDeck['Added'][0].split('|')  
    for newStock in added:  
    # Remove unwanted symbols  
    context.ToClose = context.FetcherDeck['Removed'][0].split('|')  
    for oldStock in context.ToClose:  
        if (oldStock in context.Symbols):  
    fundamentalDeck = get_fundamentals(  
        .filter(f.valuation.market_cap >= 1e7)  
        .filter(f.valuation.market_cap <= 1e12)  
Clone Algorithm
Backtest from to with initial capital
Total Returns
Max Drawdown
Benchmark Returns
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 55677c9f5eb27210a203f9b6
There was a runtime error.