Hi all, I want to add all the list of symbols to a 'History' function to play with. Can you help me on this? My coarse selection example is below (This is the same one that Jing Wu posted before).
from QuantConnect.Data.UniverseSelection import *
class BasicTemplateAlgorithm(QCAlgorithm):
def __init__(self):
# set the flag for rebalance
self.reb = 1
# Number of stocks to pass CoarseSelection process
self.num_coarse = 250
# Number of stocks to long/short
self.num_fine = 20
self.symbols = None
self.first_month = 0
self.topFine = None
def Initialize(self):
self.SetCash(100000)
self.SetStartDate(2017,1,1)
# if not specified, the Backtesting EndDate would be today
self.SetEndDate(2018,1,7)
self.spy = self.AddEquity("SPY", Resolution.Daily).Symbol
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverse(self.CoarseSelectionFunction,self.FineSelectionFunction)
# Schedule the rebalance function to execute at the begining of each month
self.Schedule.On(self.DateRules.MonthStart(self.spy),
self.TimeRules.AfterMarketOpen(self.spy,5), Action(self.rebalance))
def CoarseSelectionFunction(self, coarse):
# if the rebalance flag is not 1, return null list to save time.
if self.reb != 1:
return self.topFine if self.topFine is not None else []
# make universe selection once a month
# drop stocks which have no fundamental data or have too low prices
selected = [x for x in coarse if (x.HasFundamentalData)
and (float(x.Price) > 5)]
sortedByDollarVolume = sorted(selected, key=lambda x: x.DollarVolume, reverse=True)
top = sortedByDollarVolume[:self.num_coarse]
return [i.Symbol for i in top]
def FineSelectionFunction(self, fine):
# return null list if it's not time to rebalance
if self.reb != 1:
return self.topFine if self.topFine is not None else []
self.reb = 0
# drop stocks which don't have the information we need.
# you can try replacing those factor with your own factors here
filtered_fine = [x for x in fine if x.OperationRatios.OperationMargin.Value
and x.ValuationRatios.PriceChange1M
and x.ValuationRatios.BookValuePerShare]
self.Log('remained to select %d'%(len(filtered_fine)))
# rank stocks by three factor.
sortedByfactor1 = sorted(filtered_fine, key=lambda x: x.OperationRatios.OperationMargin.Value, reverse=True)
sortedByfactor2 = sorted(filtered_fine, key=lambda x: x.ValuationRatios.PriceChange1M, reverse=True)
sortedByfactor3 = sorted(filtered_fine, key=lambda x: x.ValuationRatios.BookValuePerShare, reverse=True)
stock_dict = {}
# assign a score to each stock, you can also change the rule of scoring here.
for i,ele in enumerate(sortedByfactor1):
rank1 = i
rank2 = sortedByfactor2.index(ele)
rank3 = sortedByfactor3.index(ele)
score = sum([rank1*0.2,rank2*0.4,rank3*0.4])
stock_dict[ele] = score
# sort the stocks by their scores
self.sorted_stock = sorted(stock_dict.items(), key=lambda d:d[1],reverse=False)
sorted_symbol = [x[0] for x in self.sorted_stock]
# sotre the top stocks into the long_list and the bottom ones into the short_list
self.long = [x for x in sorted_symbol[:self.num_fine]]
self.short = [x for x in sorted_symbol[-self.num_fine:]]
self.topFine = [i.Symbol for i in self.long + self.short]
return self.topFine
As you can see above, we get 'self.topFine' for the candidates. Now I want to convert all of these 'self.topFine' list to dataframe by using 'History' function.
# Call the 'History' function for the list of self.topFine !
hist = self.History(self.topFine, 60, Resolution.Daily)
The above does not work. Can you help me? Thank you.
HanByul P
Hi all, Please disregard my question. I found a good way to do it in Jing Wu's another post. See below.
# Add all the list of symbols from Coarse Selection and Fine Selection to History function. symbols = [ x.Symbol.Value for x in self.topFine] for i in symbols: self.AddEquity(i, Resolution.Daily) history = self.History(symbols, 100, Resolution.Daily) if history is None: return data = {} for i in symbols: if i in history.index.levels[0]: data[i] = history.loc[i]['close'] df_price = pd.DataFrame(data,columns=data.keys())
HanByul P
Hi all, I got an error message as below. Can anyone explain about this and show me how to fix this? Thank you.
Runtime Error: In Scheduled Event 'SPY: MonthStart: SPY: 30 min after MarketOpen-4a940735306b4176a8b5ba937e68d8ff', Exception : This asset symbol ( ) was not found in your security list. Please add this security or check it exists before using it with 'Securities.ContainsKey(" ")'
at QuantConnect.Securities.SecurityManager.get_Item (QuantConnect.Symbol symbol) [0x00026] in <e6e93ccda46b4bdfabcdb401f035e462>:0
at QuantConnect.Securities.SecurityPortfolioManager.get_Item (QuantConnect.Symbol symbol) [0x00001] in <e6e93ccda46b4bdfabcdb401f035e462>:0 (Open Stacktrace)
HanByul P
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!