Hello guys, I´m trying to take access to the candlestick patterns libraries that are in the GitHub LEAN documentation, but there is not too much information related to how to calculate it in a universe selection approach. Second, I want to know if there is any way to get access to TradeBars in order to obtain the OHLC of equities and with this information develop my own custom candlestick pattern?
In the next code, I want to use for the moment a simple condition to filter the stock that has an RSI value lower than 40 and the DojiStar built-in CandlestickPatterns library, but I don´t know how to use it in a universe selection approach:
import typing
import QuantConnect.Indicators.CandlestickPatterns
import datetime
class EMAMomentumUniverse(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 10, 11)
self.SetEndDate(2020, 11, 11)
self.SetCash(1000)
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverse(self.CoarseSelectionFunction)
self.averages = { }
def CoarseSelectionFunction(self, universe):
selected = []
universe = sorted(universe, key=lambda c: c.DollarVolume, reverse=True)
universe = [c for c in universe if (c.Price > 10)][:100]
for coarse in universe:
symbol = coarse.Symbol
if symbol not in self.averages:
# 1. Call history to get an array of 200 days of history data
history = self.History(symbol, 200, Resolution.Daily)
#2. Adjust SelectionData to pass in the history result
self.averages[symbol] = SelectionData(history)
#here is where im trying to create the bool variable.
self.hammer = DojiStar(symbol)
self.averages[symbol].update(self.Time, coarse.AdjustedPrice)
if self.averages[symbol].is_ready() and (self.averages[symbol].rsi.Current.Value < 40):
selected.append(symbol)
return selected[:10]
def OnSecuritiesChanged(self, changes):
for security in changes.RemovedSecurities:
self.Liquidate(security.Symbol)
for security in changes.AddedSecurities:
self.SetHoldings(security.Symbol, 0.10)
class SelectionData():
#3. Update the constructor to accept a history array
def __init__(self, history ):
self.rsi = RelativeStrengthIndex(14)
#4. Loop over the history data and update the indicators
for data in history.itertuples():
self.rsi.Update(data.Index[1], data.close)
def is_ready(self):
return self.rsi.IsReady
def update(self, time, price):
self.rsi.Update(time, price)
Thanks a lot!
Shile Wen
Hi Rene,
Please see this thread on how to calculate indicators and get TradeBars with a Universe Selection approach.
Best,
Shile Wen
Rene Ordosgoitia
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!