Relative Performance vs S&P 500 index
Author: Sammy_G
Creation Date: 10/14/2019 9:55 PM
profile picture

Sammy_G

#1
I'm trying to plot the 12-period Relative Performance of a stock vs S&P 500. Here's the code:
CODE:
Please log in to see this code.


Looks ok to me but can you please check to see that I didn't make a mistake?

Also, does there exist an Indicator (NOT talking about the RSI!!) which I could simply drag-and-drop that does the same job?
profile picture

superticker

#2
Can you explain why ...
CODE:
Please log in to see this code.
... is included in there? Are you expecting the Close of either the stock or S&P500 to have a negative closing price?

Also, if you're looking for relative metrics, you have to first divide out their dollars units (e.g., converting them to percentages--which are unitless--would be one option; using a ZScore would be another option) before trying to compare them relatively. In other words, you want to contrast "unitless" qualities. Trying to contrast the dollars of one stock with the dollars of another doesn't make sense (unless they are relatively the same price to begin with).
profile picture

Eugene

#3
Sammy_G, if you look up our Knowledge Base (the Wiki) you could find this, for example:

WealthScript Techniques | Relative Performance Chart

Also there are numerous threads discussing relative performance on the forum which could easily be found by simply searching the keyword. Don't hesitate to give this a try next time ;)

QUOTE:
Also, does there exist an Indicator (NOT talking about the RSI!!) which I could simply drag-and-drop that does the same job?

Hmm, nobody asked for this before. Let me put it on our backlog for later to decide. But we've got this:

External Symbol Plotter plots external symbol's OHLC data as a drag and drop indicator.
profile picture

Sammy_G

#4
@superticker
I copied that line from another of my scripts w/o thinking; I guess the Abs is not really required but it doesn't affect the results either.

@Eugene
I reviewed the 2 codes in the Wiki reference, neither suits my needs. I'll search the forums and get back.
profile picture

Sammy_G

#5
Hi Eugene,
I did a search of the forums and studied the examples & posted code.
In essence, you compute the price ratio of the clicked symbol vs a benchmark (in this case SP500) and then calculate the percent change of the ratio to make it comparable across all symbols.

That is exactly what I've done in the code above - by using ROC and a particular lookback period (12, which of course can be changed).
As far as I can tell my code is correct. Can you please confirm?
profile picture

Eugene

#6
Yes, the code computes the ratio and then calculates the 12-period percent change of it.
profile picture

superticker

#7
QUOTE:
you compute the price ratio of the clicked symbol vs a benchmark (in this case SP500) and then calculate the percent change of the ratio to make it comparable across all symbols.
That's a dangerous way to say it. One might erroneously think from that statement you can compute the "ratio" by dividing a penny stock's price (in cents) by Google's price (which is in the 1000s of dollars per share). That's incorrect. A better way to say it is to divide out the dollar units of the penny stock and the Google stock separately (so they are "unitless"), then take their ratio. If you fail to do that, then the penny stock will vary by 1/10000th of the Google stock all the time, and you won't have a fair comparison.

So if you want to divide out the units of each stock using percentages (as oppose to using a ZScore) ...
CODE:
Please log in to see this code.

Now you can contrast percentageChgIndex with percentageChgStock, and you can do that a number of ways. For example, you could [1] subtract the two (which is what I do), or you could [2] divide the two (but you somehow need to deal with a negative numerator and denominator if you're dividing).

And there are many other ways to contrast these. We just covered two here.

Now you can repeat two contrasting methods using ZScores (Wealth Lab has an indicator for that) or some other normalizing approach.
profile picture

Sammy_G

#8
Thx, Eugene.
------------------------------------------

@superticker:
QUOTE:
That's a dangerous way to say it. One might erroneously think from that statement you can compute the "ratio" by dividing a penny stock's price (in cents) by Google's price (which is in the 1000s of dollars per share). That's incorrect. A better way to say it is to divide out the dollar units of the penny stock and the Google stock separately (so they are "unitless"), then take their ratio.


If I understand you correctly you're saying do the ROC of the 2 symbols first then take their ratio (or subtract, etc).
Check out this code in which Series rs1 is ROC of the price ratio (what I use) and Series rs2 is ratio of ROCs (what you suggest)
CODE:
Please log in to see this code.

Plz check across multiple symbols.

I do believe rs1 (ROC of ratio) gives the proper result while rs2 (ratio of ROCs) is punctuated by periodic spikes of extreme values.
profile picture

superticker

#9
To me ...
CODE:
Please log in to see this code.
... does not make sense at all. If the Close represents a penny stock, you're going to get a really tiny ratio. In contrast, if the Close is Google's stock (which is in the 1000s of dollars per share), you'll get a really big ratio and totally different behavior. Try it and you'll see. But do what makes sense to you. Just don't expect to publish it that way.

QUOTE:
rs2 (ratio of ROCs) is punctuated by periodic spikes of extreme values.
And that's to be expected. Just smooth it out with an EMA or an Inverse Fisher filter (Wealth Lab has one) followed by an EMA. You'll need to deal with the negative values somehow if you're dividing the two ROCs, which is why I don't do it that way. I take their difference instead to avoid dealing with the negative numerator or denominator in the ROC ratio calculation.
CODE:
Please log in to see this code.

I take the difference of the two ROCs, then smooth that out with an EMA. In either case, you're going to have to do some smoothing, which is expected. What you're interested in is the smoothed response, but I would still plot the histogram of the unsmoothed response. See the attachment for the plots.
profile picture

Sammy_G

#10
QUOTE:
does not make sense at all. If the Close represents a penny stock, you're going to get a really tiny ratio

I don't trade penny stocks but even if did the % CHANGES in the ratio will be the same when the magnitude of change is same. So whether a penny stock jumps from 0.0001 to 0.0002 (100% increase) or another stock jumps from 10 to 20 (100% increase) the % change is similar and it will be reflected as such in the plot (as long as the software handles decimals to sufficient length and doesn't round it off).

And b/c the prices are always +ve that's why I do the price ratio first and then the ROC of the ratio, not the other way around. Simple AND robust.

I agree that you can smooth the plot further, if required.

Regards.
profile picture

KGo

#11
QUOTE:
why I do the price ratio first and then the ROC

I agree. ROC/ROC with denominator approaching zero will give unreliable results that smoothing cannot undo. Division by zero in a WL series equals zero while division by near zero would yield a large result. A one penny price change can radically change the numbers. For me this disqualifies the ROC/ROC method.

Subtracting two ROCs is also viable.
profile picture

superticker

#12
QUOTE:
whether a penny stock jumps from 0.0001 to 0.0002 (100% increase) or another stock jumps from 10 to 20 (100% increase) the % change is similar and it will be reflected as such in the plot
What you're saying is that you can skip dividing by the S&P500 altogether, and I agree with you.

There's probably a good way of doing the ratio first, but you need to normalize their respective price ranges (for both stock and index) prior to computing the ratio; otherwise, their range imbalance will offset your calculation from one stock to another.

QUOTE:
ROC/ROC with denominator approaching zero will give unreliable results
I agree. There's also the problem of how to deal with negative numbers in the numerator or denominator as well. This approach is not good.
profile picture

Sammy_G

#13
@superticker
QUOTE:
but you need to normalize their respective price ranges (for both stock and index) prior to computing the ratio

Actually, when you apply ROC on the price ratio (stock/index) you ARE normalizing the results across all stocks (the actual ratio values become irrelevant, only the % changes matter).
Ciao!
profile picture

Eugene

#14
QUOTE:
Also, does there exist an Indicator (NOT talking about the RSI!!) which I could simply drag-and-drop that does the same job?

Look forward to it in Community Indicators v2019.11.
profile picture

Eugene

#15
@Sammy_G

fyi: Added RelativePerformance indicator to Community Indicators v2019.11:

RelativePerformance
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).