Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Can you access the Benchmark in coding?

I have searched the API to no avail. Can access to the given benchmark be achieved and used in an algorithm? As a filter for instance?

8 responses

Failing which can you re-load a given benchmark's daily prices as a pandas dataframe and use transforms of those prices as a filter? History seems to provide access to the total universe but this may not include the desired benchmark.

Force a sid to be present in your universe (call sid or symbol function in initialize) so that history will load the beanchmark data you need.

def initialize(context):  
    benchmark = symbol('SPY')  

Thanks Luca I'll give it a try
problem is you then have to exclude it for actual trading?

And there again how do you call / create history JUST for the benchmark? It how do you create separate history JUST for the benchmark?

I guess you have to manually exclude the benchmark while you iterate through data:

def handle_data(context, data):  
    for sid in data:  
          if sid == benchmark:  
              continue  
    [...]  

Also, I don't believe you can load history only for the benchmark; history loads data of all sids in current universe .

In the new Q2 framework;

benchmarkHistory = data.history(benchmarkAsset,'price',60,'1d') will return an object containing only your "benchmark Asset's " last 60 days of prices.
trackingHistory = data.history(trackingAssets,'price',60,'1d') will return an object containing only your "tracking Asset's " last 60 days of prices.

Basically all "data.history" calls require an itterable list of stocks, instead of iterating over all of your stocks.

It might be less computationally expensive to make one call, and then grab what you need:

allHistory = data.history(allAssets,'price',60,'1d')
benchmarkHistory = allHistory[benchmarkAsset]
trackingHistory = allHistory[trackingAssets]

Then the database is only pinged once and you are just taking slices of the returned dataframe (I'm assuming that every time data.history is encountered, there is a read from disk).

Grant, I agree. The only defense of the approach I proposed, is it may be more intuitive. Having said that if the cost is noticeably greater for one approach over the other then cost trumps readability.