from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
import numpy as np
import random as rand
rand.seed(22)
class RandomShortUniverseAlgo(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2015, 1, 1) # Set Start Date
self.SetEndDate(2015, 2, 1)
self.SetCash(1000000) # Set Strategy Cash
self._maxCoarse = 800
self.AddUniverse(self.CoarseSelectionFunction)
def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
Arguments:
data: Slice object keyed by symbol containing the stock data
'''
all_symbols = [ x.Value for x in self.Portfolio.Keys ]
self.Log("all: " + str(all_symbols))
def CoarseSelectionFunction(self, coarse):
self.Log('Length coarse: {}'.format(len(list(coarse))))
filtered = [x for x in coarse if x.HasFundamentalData]
self.Log("Num filtered for fundamental data: {}".format(len(filtered)))
filtered = [x.Symbol for x in filtered if x.Price > 0.001]
self.Log("Num filtered for price: {}".format(len(filtered)))
selection = rand.sample(filtered, self._maxCoarse)
#self.Log('Universe {}'.format(list(selection)))
return selection