Has anyone worked on detecting historical gaps and fills in candlestick charts? For example, on a daily chart, you might get a close at $100 and an open at $120 the next day. As long as the price hasn't gone back to $100 since the gap, that gap isn't considered "filled". I have a few ideas on how to handle this with rolling windows but wanted to throw it out to the community first.
Derek Melchin
Hi Grateful Trading,
Consider reviewing this related thread. Although the example doesn't "fill the gap", it demonstrates how we can calculate the gap percentage when entering the trade.
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.
Grateful Trading
Thank you Derek Melchin The thread is helpful, but I'm looking for more of a log of past gaps, and a flag to mark whether they've been filled or not. I'm imagining a "GapFinder" method that calls for historical data for a given ticker and timeframe and builds the log.
Derek Melchin
Hi Grateful Trading,
To build a log of historical gaps and if they have been filled or not, we can define a GapFinder class:
class GapFinder: def __init__(self, algorithm, symbol, days): # Warm up history history = algorithm.History(symbol, days + 1, Resolution.Daily) if history.empty: self.log = pd.DataFrame() return history = history.loc[symbol].drop('volume', axis=1) # Calculate gaps history['yesterdays_close'] = history['close'].shift(1) history['gap'] = history.open - history.yesterdays_close history = history.dropna() # Check which gaps have filled for time, row in history.iterrows(): if row.gap == 0: continue filled = False for future_time, future_row in history.loc[time:].iterrows(): gap_up_filled = row.gap > 0 and future_row.low <= row.close gap_down_filled = row.gap < 0 and future_row.high >= row.close if gap_up_filled or gap_down_filled: filled = True break history.loc[time, 'filled'] = filled # Save gap log self.log = history
See the attached backtest for reference. To have the GapFinder log updated as time progresses in a backtest, we just need to setup some consolidators.
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.
Grateful Trading
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!