Hello all,
If you use Minervini's Volatility Contraction Pattern, you may have seen me post before on how I'm trying to code something to detect them in a chart. It hasn't been easy with my limited time between work and family, but I did create something that makes it easier calculate it.
Minervini counts each contraction, it's depth and the length of the base to get a sense of the stock base's quality.
So for example if you're looking at TDG, it has three contractions since the beginning of a base:
You are supposed to determine the % amount each contraction, where the quality is judged by each contraction getting smaller over time until it is < 10%. If it breaks out of this last contraction, it is theoretically buyable and you can place your stop at the bottom of the last contraction for a low-risk trade.
It becomes a pain however to pull out your calculator and calculate this percent difference.
So I created some code in my chart to do this all for me. The only thing I have to do is draw each trendline, and then give each one a study ID of "T1", "T2", "T3", and so forth.
T1, for example, just double-click the trendline and enter in the ID:
The rest is automatic. Again taking the TDG example:
Now you can see the VCP charateristics in the chart title:
7W 9/8/4 3T
This means the length from first to last contraction is 7 weeks long, the first contraction was 9%, then 8%, then 4% and there was a total of 3 contractions.
And there you have it.
Here's the code you put into the chart formula. I place it after the plotting of the bars:
// Calculate VCP characteristics
// Set Default Values
VCPStart = 1e9;
VCPEnd = 0;
T = 0;
Pct = "";
// Loop through 5 contractions (there's usually less than this but you can adjust as you see fit
for (i = 1; i <=5; i++)
{
// Create the Study ID number, T1, T2, ...
Tstr = "T" + NumToStr(i, 1.0, false);
// Get the trendline with the Study ID given the current chart
trendline = Study(Tstr, GetChartID() );
// extract the x and y values of the trendline
// Credit: https://www.amibroker.com/kb/2006/03/07/getting-x-y-co-ordinates-of-study/
StartX = LastValue( ValueWhen( ExRem( trendline, 0 ), DateTime() ) );
EndX = LastValue( ValueWhen( trendline, DateTime() ) );
StartY = LastValue( ValueWhen( ExRem( trendline, 0 ), trendline ) );
EndY = LastValue( ValueWhen( trendline, trendline ) );
// Set the beginning and end of the base
VCPStart = IIf(i==1, StartX, VCPStart);
VCPEnd = IIf(DateTimeDiff(EndX, VCPEnd)>0, EndX, VCPEnd);
// if the trendline exists (nonzero start date), increment the total trendlines found and append the latest contraction % into a string
if (startX > 0)
{
T++;
Pct += numtostr(abs(EndY/StartY-1)*100, 2.0, False) + "/";
}
}
// set the title to add the VCP characteristics using simple string manipulation
Title += " VCP Profile = " + NumToStr(DateTimeDiff(VCPEnd, VCPStart)/3600/24/7, 2.0, False) + "W " + StrTrim(Pct, "/") + " " + NumToStr(T, 1.0, False) + "T";
Enjoy,
Mike