Back

[Strategy] Opening Range Breakout

Hey all!

Here's an algorithm I threw together to try and showcase various features and techniques available within QuantConnect. This algorithm tries to detect an opening breakout in the direction of the short term trend and takes a position in that direction. The opening breakout is nominally defined as trading outside of the range established in the first 3 minutes of trading. Orders are always submitted with accompanying stop market orders which are gradually tightened using PSAR after some initial profit condition is met.

I've tried to keep it fairly simple but still provide a good framework. As an algorithm grows in complexity, it's typically beneficial to split the pieces out into stand-alone components that can be reused between algorithms, but for the sake of simplicity and understandability, I've left all the code in one file.

In this algorithm you'll see various features and techniques including:
* Scheduled Daily Events
* History function to warm up indicators manually
* StopMarket order updates
* Dynamic position sizing using ATR/allowable losses
* Custom indicator on custom (30 min) interval
* Custom leverage settings
* Decent logging functionality
* Plotting considerations made for backtest and live

There's still plenty of work to be done here before it is made into a good, reliably profitable algorithm. Some things that could be worked on is:

* Setting the stop loss % based on the position size instead of a constant, this way we decide to lose a max% of our portfolio per trade.

* Better entry execution could be done by not entering the position once the signal is generated, but to wait for a reversion to VWAP or for a price point that is ~2 standard deviations from the mean in the beneficial direction.

* Better exit execution, currently it exits based on the stop loss criteria and the encroaching PSAR.

* There's also work that could be done to allow the algorithm to enter a position in the opposite direction of the trend, maybe given a larger breakout threshold from the opening range.

These are just a few areas of the algorithm that could be improved. If you make improvements, please share the algorithm back to this thread and we as a community can learn together!

EDIT: Looks like a previous edit removed the attached algorithm, here it is, enjoy!
Update Backtest






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.



Thank you for putting together these examples!
Nik
0

Hey @Michael great work! I'm learning a lot from this example.

I have a rather silly question, why do you use PSARMin.Update((TradeBar)Security.GetLastData()); in line 236 instead of register the indicator?

JJ
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.


Hey @JJ, not silly at all! There's no solid reason for it being like that, it's purely the result of me iterating on the design. At one point I was only computing it during certain conditions, such as open position, ect... but ended up moving it there so I could see plots to better understand how a second level PSAR behaves.
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.


Thanks Michael!
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.


Hi there,

I think there is a mistake in the definition of the STD14 and ATR14 indicators. In line 135 and 13 the long term indicators are defined with daily resolution:// define our longer term indicators
STD14 = STD(Symbol, 14, Resolution.Daily);
ATR14 = ATR(Symbol, 14, resolution: Resolution.Daily);

But when the indicators are smoothed, the EMA period is in hours:// smooth our ATR over a week, we'll use this to determine if recent volatilty warrants entrance
var oneWeekInMarketHours = (int)(5 * 6.5);
SmoothedATR14 = new ExponentialMovingAverage("Smoothed_" + ATR14.Name, oneWeekInMarketHours).Of(ATR14);
// smooth our STD over a week as well
SmoothedSTD14 = new ExponentialMovingAverage("Smoothed_" + STD14.Name, oneWeekInMarketHours).Of(STD14);

So I test the algorithm with the long term indicators in hours and the results become negatives!

Another question, in the algorithm two schedules are used:// schedule an event to run every day at five minutes after our Symbol's market open
Schedule.Event("MarketOpenSpan")
.EveryDay(Symbol)
.AfterMarketOpen(Symbol, minutesAfterOpen: OpeningSpanInMinutes)
.Run(MarketOpeningSpanHandler);

Schedule.Event("MarketOpen")
.EveryDay(Symbol)
.AfterMarketOpen(Symbol, minutesAfterOpen: -1)
.Run(() => PSARMin.Reset());

Why the PSARMin indicator is reseted just a minute before market opens? Is the same to reset the indicator in the OnEndOFDay method?

Thanks in advance,

JJ
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.


Hey @JJ!

Spotted another bug, great catch! Looks like another hold over from me iterating on the design. The indicators are intended to be on the daily time frame and the smoothers are intended to have a half interval of the long term. So The STD14/ATR14 definitions are as intended, but the smoothers should be defined using a period of 7 (half of the 14 period). I did a quick change and didn't see differences.

As for the scheduled events, great question! There's actually a very large difference between the two. OnEndOfDay events will fire 10 minutes before the market closes. This gives the algorithm enough time to do some processing and submit some final orders. The issue is that with a second level indicator those last 10 minutes will completely dominate the indicator's state, so by resetting it just before market open I'm guaranteeing that the PSAR is fresh and only includes data since market open.

For me, scheduled events is my preferred way of performing an indicator reset since I guarantee what time of day it happens.
1

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.


did you have a chance to correct the algo?
0

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