Do you have any technical library or source code example to find the stocks which are sideways for few weeks or more after First stage of breakout move ?
Don't have an account? Join QuantConnect Today
QuantConnect Community Discussions
QUANTCONNECT COMMUNITY
LEAN is the open-source algorithmic trading engine powering QuantConnect. Founded in 2012 LEAN has been built by a global community of 180+ engineers and powers more than 300+ hedge funds today.
Join QuantConnect's Discord server for real-time support, where a vibrant community of traders and developers awaits to help you with any of your QuantConnect needs.
The Open-Quant League is a quarterly competition between universities and investment clubs for the best-performing strategy. The previous quarter's code is open-sourced, and competitors must adapt to survive.
VCP pattern is a Volatility Contraction pattern. No technical library or source code example provided.
Continue ReadingRefer to our Research Guidelines for high quality research posts.
Create an account on QuantConnect for the latest community delivered to your inbox.
Sign Up Today
|
|
|||||||
|
|
||||||||
|
VCP pattern (Volatility Contraction pattern)
Dharmesh Khalasi | July 2024
Do you have any technical library or source code example to find the stocks which are sideways for few weeks or more after First stage of breakout move ?
QuantConnectâ„¢ 2025. All Rights Reserved
Louis Szeto
Hi Dharmesh
Any dynamic-time candlestick is very subjective and is not easily captured using simple technical indicator. However, you can use some signal processing techniques and ML models to try to capture some. Although it is not VCP, I recommend you check on the research notebook in Head & Shoulders TA Pattern Detection (quantconnect.com) for similar techniques you might need.
Best
Louis
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.
Spacetime
Have added the fix inside the Initialize function in main.py to avoid the data normalization erorr.
Error: ArgumentException : The underlying equity asset (TLT) is set to Adjusted, please change this to DataNormalizationMode.Raw with the SetDataNormalization() method
Solution: self.Securities["TLT"].SetDataNormalizationMode(DataNormalizationMode.Raw)
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.
.ekz. INVESTOR
Thank you!
I made a minor update as well, removing the OptionsUtils.py, which is not used here and may cause confusion. Updated backtest attached.
I use that for managing spreads in another algo of mine I will share some day.
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.
.ekz. INVESTOR
Peter Guenther : Thanks for the kind words. Please let me know if you run into any issues. As Derek Melchin
pointed out, there are some bits we dont need anymore. specifically the queue and dequeue logic. I've removed those now.
One question i have, Peter: what exactly does this method do "derive_vola_waitdays" , the one with the log function. I'm not quite able to wrap my head around whats happening here. I'm a software engineer, with limited data science & trading knowldege.
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.
Peter Guenther
.ekz., these functions come from the In & Out discussion on Quantopian (link). The basic idea is that we want to stay out of the market (wait_days) for longer when there are more jitters in the market, i.e. when the market volatility is higher.
Regarding the volatility formula's general structure: There are certain advantages in using standard log-transformed returns instead of raw returns. I found this web site here that explains certain advantages: https://quantivity.wordpress.com/2011/02/21/why-log-returns/. I reckon the 0.6 was chosen to optimize the results. The multiplication with the square root of 252 is to express the standard deviation in annualized terms: https://www.fool.com/knowledge-center/how-to-calculate-annualized-volatility.aspx#:~:text=To%20present%20this%20volatility%20in,root%20of%20252%20is%2027.4%25..
In the wait_days formula, the constant is optimized, but the formula follows the general idea that we stay out for longer when the volatility is higher.
The returns_lookback also is a function of the volatility. I think that the idea here is that in volatile markets, the return differences that we are checking for (see Distilled_Bear signal) should show in shorter time periods.
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.
.ekz. INVESTOR
Thanks for this, @Peter!
This is exactly the level of detail I needed and the links are a huge help. Also, the historical context (the old quantopian thread) has been a big missing element; reading the previous conversations will fill a lot of gaps for me. Thanks again.
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.
Aalap Sharma
I tried running the algo with
self.go_inout_vs_dbear = 1 # 1=In&Out, 0=DistilledBear
and it fails with
Runtime Error: In Scheduled Event 'EveryDay: SPY: 30 min after MarketOpen', AttributeError : 'QualUp_inout' object has no attribute 'history_shift'
at signalcheck_inout
returns_sample = (self.history / self.history_shift - 1)
File "main.py" in main.py:line 248
AttributeError : 'QualUp_inout' object has no attribute 'history_shift' (Open Stacktrace)
Is that expected?
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.
Peter Guenther
Aalap Sharma, thanks for the note. I have added the creation of history_shift to our warmup (line 155), see the algo update.
Another issue: I still have to have a closer look but there seems to be an issue regarding maintaining the cash level in the current algo version. We are going more than $1 million into the negative territory, see the cash level chart :) In the order log, there are margin call warnings starting in about August 2020 and notes that we fill at stale prices. It seems that we are currently buying more than we can afford.
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.
Peter Guenther
Still looking at the issue, but I think the key problem is this: When the time to expiry is too short, or is running out, then we have to execute the call options, meaning that we are buying the underlying stock, resulting in cash running low and the margin calls that we see in the orders log. So, what we need:
The options must have a minimum duration (time to expiry) which is the rebalance time (plus X days to be safe; I think currently it is 60 days between rebalances). When rebalancing, we then exclude all options that do not have sufficient time left in terms of their expiry date which should prevent that we actually have to execute the options. This also means that we need to make sure that we sell all those options that we are currently holding which do not have sufficient time left on them (i.e. days to expiry < 60 + X).
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.
Peter Guenther
Great, so this looks promising: Attached is an update of the algo. We are not running into liquidity problems anymore that occurred due to calls that got executed.
Details:
In the OptionsManager.py, I have added the following lines to where we sort by expiry:
min_expiry = int(self.algo.GetParameter('reb_freq'))
contractsSortedByExpiration = [p for p in contractsSortedByExpiration if ((p.ID.Date - self.algo.Time).days > min_expiry+5)]
if len(contractsSortedByExpiration) == 0: return
For this, I have also added a new parameter that determines the rebalance frequency (reb_freq) and set it to 60 days.
We still have a few orders that get rejected due to insufficient buying power, so this might be the next issue that needs addressing. Also, we now have a bit of cash lying around ($50k at the end of the backtesting period) that does not get invested.
By the way, the CAGR is currently at about 344% ... although it's of course a wild ride to get there :)
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.
Spacetime
Peter Guenther ,
hmm... I have performed a quick backtest with your latest version of the algo and was receiving the below error.
I am not certain how to solve this yet, but if I find a solution, then will post it here.
14 | 14:00:05:
Backtest Handled Error: Order Error: id: 2, Insufficient buying power to complete order (Value:735), Reason: Id: 2, Initial Margin: 736, Free Margin: 0
01/02/2015 10:00:00 - Executed MarginCallOrder: TLT 160115C00125000 - Quantity: 1 @ 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.
Spacetime
Just adding on to my previous post.
self.SetStartDate(2015, 1, 1) is the starting date.
45 | 14:13:01:
Backtest Handled Error: Order Error: id: 307, Insufficient buying power to complete order (Value:6710), Reason: Id: 307, Initial Margin: 6712.75, Free Margin: 442.75
Order Error: id: 318, Insufficient buying power to complete order (Value:6345), Reason: Id: 318, Initial Margin: 6347.25, Free Margin: 5740.5
Order Error: id: 319, Insufficient buying power to complete order (Value:6135), Reason: Id: 319, Initial Margin: 6136, Free Margin: 5740.5
Order Error: id: 320, Insufficient buying power to complete order (Value:6955), Reason: Id: 320, Initial Margin: 6958.25, Free Margin: 5740.5
Order Error: id: 325, Insufficient buying power to complete order (Value:6704), Reason: Id: 325, Initial Margin: 6712, Free Margin: 1953.5
Order Error: id: 326, Insufficient buying power to complete order (Value:6885), Reason: Id: 326, Initial Margin: 6889.25, Free Margin: 1953.5
Order Error: id: 328, Insufficient buying power to complete order (Value:6825), Reason: Id: 328, Initial Margin: 6832.5, Free Margin: 6091.25
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.
Peter Guenther
Same thought here, I was also starting from 1 Jan 2015 onwards and was running into the same issue :) The algo seems to buy too much of the TLT option at the beginning. We can get past this by reducing the holdings from 1 to 0.85 (lines 325 and 350). However, anyway, the whole system collapses for some reason after Feb 2015 and then constantly underperforms. Backtest attached.
We will get our heads around this one, just a matter of time.
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.
.ekz. INVESTOR
I'll have a version soon that opens put credit spreads instead of calls. This is more in line with how one would trade 'in real life'.
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.
Mark hatlan
Thanks ekz for your options implementation. I've traded options for years manually, and can provide some more thoughts around using them.
The further out of the money the strike is, the cheaper they are to buy. But they are also less liquid. It quickly gets difficult to execute the order at a good price if the bid ask is 0.20 x 0.25, and low size on the bid or ask. If you are only trading $500 you can work with it, but buying $10,000 worth of a 0.25 option is 400 contracts, and if the size on the ask is 50 then you will likely pay more for that entire position.
So going far OTM liquidity and execution becomes troublesome with any kind of size, unless you are trading only the most liquid underlyings. Same thing goes with spreads, however spreads also require more patience when executing an order, especially to maximize profits. A spread quote may be 0.95 x 1.15, and you want a good fill so you place it at 1.05 and may wait for an hour or 2 to get it filled.
We can go the opposite way of OTM and go ITM. with ITM, the closer the strike is to zero the more the option behaves like a leverage stock position. If the stock is at 100, and you buy a 50.00 strike call, then if the stock goes up to 101, then the call will go up near $1.00 in value for example. But deep ITM options have less liquidity too, so also troublesome.
For this strategy I would suggest the following 2 option methods.
1. Buy Calls less than 1 standard deviation away between ATM and ITM. These should have good liquidity, and not be so cheap that execution becomes troubleshome.
2. sell an ATM put credit spread, and use those proceeds to buy a slightly OTM call. This means you put up the requirement for the short spread, and if the stock drops then you just lose the requirement. But if the stock takes off you have got a slightly OTM call for free.
I'm a trader learning to program, so I'm not capabale of implementing this logic in a strategy yet, but hopefully it helps.
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.
.ekz. INVESTOR
Good stuff, Mark! I'm a manual optionsrader as well, and I especially like your clever combo in #2 :)
In my manual options trading, I typically decide between selling put verticals and buying call verticals, depending on IV (with high IV, I'm a seller). Curious, would you use make a similar decision, or always sell put verticals, regardless?
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.
Mark hatlan
With the short put spread and long call method, your on both sides, so the IV premium isn't that important. Seems like you know that with a high IV you sell the trade, and low IV you buy the trade.
But if you're only doing one side either buying or selling, than IV becomes a lot more important in general. Regarding the IV and this strategy, it would be worth to see a backtest that trades regardless of upcoming earnings, and one that excludes a stock if there is an upcoming earnings and IV is high. A lot of people that trade options just do earnings plays, usually selling the IV premium.
For this strategy, there is are expectations that the stock will move up a lot, so I would be inclined to have a long call position instead of a spread only. A long spread is used when you want to be long the trade, but you sell a farther away strike to offset the purchase cost. So you would be expecting a move up in the underlying, but not a drastic move. This stock filtering I think is geared towards strong upward potential moves.
I also don't think this stock filtering is built to look for selling just bull put spreads. Really if your selling any spread only (not adding a long call/put) then you are deciding that the spread alone is very over priced, and the probability of the underlying reaching your short strike is low, so you sell it expecting to buy it back for cheaper at a later date. Usually this is around earnings.
The selling of a put spread to get a long call for very cheap seems like a good fit for this stock filtering.
I tried to keep my comment short, but thats hard to do when talking about options :)
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.
.ekz. INVESTOR
Brilliant. Love the thinking here. Agreed on all counts.
I will try to implement the PCS over the weekend/week.
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!