Back

Buy-On-Gap Strategy Logic

I am trying to code an intraday strategy that buys on a gap from open to close. The strategy would, after the market opens, compare that day's opening price with yesterday's low. If today opens sufficiently below yesterday's low, then the strategy will buy the stock and then sell at the end of the day, on close. My question is, how do i identify yesterdays low and today's open (easy to do with Daily Bars, after each bar is complete) while using minute or tick data for the back test, since all holding periods are intraday? Is there a way to add a symbol with two different time resolutions so you can get high/low/close/open data for different time windows?
Update Backtest








Hey Reuben, to answer your question directly it not possible to add one symbol with two different resolutions -- however its not required for your algorithm, you could use logic like this:


private DateTime _date = new DateTime();
private decimal _low = Decimal.MaxValue;

public void OnData(TradeBars data)
{
var barOpen = data["SPY"].Open;
var barLow = data["SPY"].Low;

if (Time.Date != _date.Date)
{
//Date changed, check for gap down from yesterday's *low*:
if (barOpen < _low * 0.99m) {
//Buy on gaps down.
}

//Reset variables to detect next morning.
_low = barLow;
_date = Time.Date;
}

if (barLow < _low)
{
//Build our lowest point.
_low = data["SPY"].Low;
}
}


OR: You could create a custom consolidator which builds daily bars and use your existing daily bar logic. Check out the "How do I use Consolidators?" QC University algorithm.
0

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.


Jared,

Will the above logic place the order right after market open? If i understand correctly, the code above will compare today's open with yesterdays low and then place an order tomorrow? I want to buy on the open that has gapped down and then sell on the close of that same TradeBar. I had understood that everthing in the OnData(TradeBars data) block would take place after that TradeBar was complete (ie, at today's close). Am I not understanding this correctly?
0

You're correct trades occur after the tradebar is complete -- I was assuming minute data for the logic, and only focused on buying the gap down. I believe with that logic you'd be buying 1 bar into the day (09:31) based on the opening price of the opening bar. You could implement addition checks for 3.45pm to close out the position.
0

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.


Update Backtest





0

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.


Loading...

This discussion is closed