import datetime
import pandas
class LiquidUniverseSelection(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 1, 11)
self.SetCash(100000)
self.AddUniverse(self.CoarseSelectionFilter)
self.UniverseSettings.Resolution = Resolution.Minute
self.openingBar = None
# 1. Set the leverage to 2
self.UniverseSettings.Leverage = 2
def CoarseSelectionFilter(self, coarse):
sortedByDollarVolume = sorted(coarse, key=lambda c: c.DollarVolume, reverse=True)
self.filteredByPrice = [c.Symbol for c in sortedByDollarVolume if c.Price > 10]
return self.filteredByPrice[:1000]
def OnSecuritiesChanged(self, changes):
self.changes = changes
#self.Log(f"OnSecuritiesChanged({self.Time}):: {changes}")
for security in self.changes.RemovedSecurities:
if security.Invested:
self.Liquidate(security.Symbol)
for security in self.changes.AddedSecurities:
pass
def OnDataConsolidated(self, bar):
if bar.Time.hour == 9 and bar.Time.minute == 30:
self.openingBar = bar
else: self.openingBar = None
def OnData(self, data):
for i in self.filteredByPrice:
if data.ContainsKey(i):
self.df = self.History(i,2)
self.Log(self.df.info)
self.YesterdayHigh=self.df.high[1]
self.Consolidate(i, Resolution.Hour, self.OnDataConsolidated)
b=data[i].High
if self.openingBar != None:
if self.openingBar.low > self.YesterdayHigh and data[i].High > self.openingBar.High:
if data[i].High > self.YesterdayHigh and (self.openingBar != None and len(df.index)>=2):
self.SetHoldings(i, 0.10)
def EveryDayBeforeMarketClose(self):
self.Liquidate()
#if self.window.IsReady and window[0].Close > window[1].Close:
In The End i get Message:
Runtime Error: IndexError : index out of bounds at pandas._libs.util.validate_indexer File "util.pxd" in util.pxd:line 89 IndexError : index out of bounds Where i was wrong?
Zicai Feng
do you know which line of your code causes that error? normally the error should tell u which line
once you know, do "self.Debug" before that line, and print the supposed info, so you can revisit the debug file to see what your pandas dataframe is supposed to look like
Derek Melchin
Hi Roman,
The algorithm above is calling the `Consolidate` method for every security during `OnData`. To resolve the error, we should create a SymbolData class and set up the consolidators for each symbol inside the SymbolData constructor. See the attached backtest for reference.
Best,
Derek Melchin
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.
Roman Gobov
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!