Tharp's Market System Quality Number (SQN)

Here's how I created a color-coded line showing the market's 100-day SQN., like Tharp shows in his fantastic book, the Definitive Guide to Position Sizing Strategies.

Hope you like it!

image

// Market SQN Study
// Van Tharp's System Quality Number in The Definitive Guide to Position Sizing Strategies, p85.
// Color scale based on Tharp's Thoughts Newsletter, Oct. 2018.

Periods = 100; // Need minimum of 30 observations, and maximum of 100 for the color scale to work accurately.
Expectancy = ROC(C,Periods) / Periods;
R = ROC (C,1); // Use daily change in value as a proxy for R-multiples
SDofR = StDev( R , Periods , True );
MSQN = ( Expectancy / SDofR ) * ( sqrt(Periods) );

VeryWeak = MSQN < -0.7;
Weak = (MSQN >= -0.7) AND (MSQN < 0);
Neutral = (MSQN >= 0) AND (MSQN < 0.7);
Strong = (MSQN >= 0.7) AND (MSQN < 1.47);
VeryStrong = MSQN >= 1.47;

VeryWeakLine = IIf( VeryWeak OR Ref(VeryWeak,-1) , MSQN , Null);
WeakLine = IIf( Weak OR Ref(Weak,-1) , MSQN , Null);
NeutralLine = IIf( Neutral OR Ref(Neutral,-1) , MSQN , Null);
StrongLine = IIf( Strong OR Ref(Strong,-1) , MSQN , Null);
VeryStrongLine = IIf( VeryStrong OR Ref(VeryStrong,-1) , MSQN , Null);

Plot( VeryWeakLine , "Very weak", colorDarkRed, styleThick);
Plot( WeakLine , "Weak", colorBrown, styleThick);
Plot( NeutralLine  , "Neutral", colorDarkYellow, styleThick);
Plot( StrongLine  , "Strong", colorDarkGreen, styleThick);
Plot( VeryStrongLine  , "Very strong", colorGreen, styleThick);
11 Likes

@PeterD thanks for sharing.

You may improve it a bit using a single plot (directly using MSQN) defining a new "plotColor" array created using some nested IIf() to assign the color based on the different conditions (something similar to what was done in this example).

And if you want to display the corresponding status as a string in the title, you can do it similarly using a nested WriteIf() for the selected value.

2 Likes

Thanks, Giuseppe! Those are great ideas. I liked that article too. It's amazing how much there is to learn.

@PeterD Thanks on behalf of all for sharing the code here.

I shaved off a lot of computational cycles for the same output.
Kindly try the new code as a variant.

Periods = 100;				// min 30 observations & max of 100 for the color scale to work accurately.
Expectancy = ROC( C, Periods) / Periods;
R = ROC ( C, 1); 			// Use daily change in value as a proxy for R-multiples
SDofR = StDev( R , Periods , True);
MSQN = ( Expectancy / SDofR ) * ( sqrt( Periods));

VeryWeak   = MSQN < -0.7;
Weak       = MSQN >= -0.7 AND MSQN < 0;
Neutral    = MSQN >= 0    AND MSQN < 0.7;
Strong     = MSQN >= 0.7  AND MSQN < 1.47;
VeryStrong = MSQN >= 1.47;

PlotC = IIf( VeryWeak  OR Ref( VeryWeak, -1), colorDarkRed,
	IIf( Weak OR Ref( Weak, -1), colorBrown,
	IIf( Neutral OR Ref( Neutral, -1), colorDarkYellow,
	IIf( Strong OR Ref( Strong, -1), colorDarkGreen,
	IIf( VeryStrong OR Ref( VeryStrong, -1), colorGreen, Null )))));
Plot( MSQN  , "MSQN", PlotC, styleThick);

image

The colour overlap may slightly differ because of the order of function calls in both the codes but performance trade-off will compensate it.
However, the MSQN Values will not differ.

Eliminating the extra calls will give 25-30% faster execution time.

@beppe Thanks for the suggestion.

11 Likes

@travick, I like how you implemented that. Thanks!

I appreciate how you and @beppe are helping me hone my skills!

3 Likes