Custom Universes

Key Concepts

Introduction

A custom universe lets you select a basket of assets from a custom dataset.

Initialize Universes

To add a custom universe to your algorithm, in the Initializeinitialize method, pass your universe type and a selector function to the AddUniverseadd_universe method.

AddUniverse<MyCustomUniverseDataClass>("myCustomUniverse", Resolution.Daily, SelectorFunction)
self.add_universe(MyCustomUniverseDataClass, "myCustomUniverse", Resolution.DAILY, self.selector_function)

Receive Custom Data

The universe selector function receives a list of your custom objects and must return a list of Symbol objects. In the selector function definition, you can use any of the properties of your custom data type. The Symbol objects that you return from the selector function set the constituents of the universe.

public class MyCustomUniverseAlgorithm : QCAlgorithm
{
	private IEnumerable<Symbol> SelectorFunction(IEnumerable<MyCustomUniverseDataClass> data)
	{
        return (from singleStockData in data
               where singleStockData.CustomAttribute1 > 0
               orderby singleStockData.CustomAttribute2 descending
               select singleStockData.Symbol).Take(5);
    }
}
class MyCustomUniverseAlgorithm(QCAlgorithm):
	def selector_function(self, data: List[MyCustomUniverseDataClass]) -> List[Symbol]:
    	sorted_data = sorted([ x for x in data if x["CustomAttribute1"] > 0 ],
                         	key=lambda x: x["CustomAttribute2"],
                         	reverse=True)
    	return [x.symbol for x in sorted_data[:5]]

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: