Do you want to get informed about new posts via Email?

Relative Performance vs S&P 500 index

Author: Sammy_G

Creation Date: 10/14/2019 9:55 PM

I'm trying to plot the 12-period Relative Performance of a stock vs S&P 500. Here's the 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?

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?

Can you explain why ...

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

CODE:... is included in there? Are you expecting the Close of either the stock or S&P500 to have a negative closing price?

Please log in to see this code.

Also, if you're looking for relative metrics, you have to

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 ;)

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.

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.

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

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.

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?

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?

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

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.

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

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

Thx, Eugene.

------------------------------------------

@superticker:

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)

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.

------------------------------------------

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

To me ...

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

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.

CODE:... does not make sense at all. If the Close represents a

Please log in to see this code.

QUOTE: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.

rs2 (ratio of ROCs) is punctuated by periodic spikes of extreme values.

CODE:

Please log in to see this code.

I take the

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.

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.

QUOTE:What you're saying is that you can skip dividing by the S&P500 altogether, and I agree with you.

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

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

ROC/ROC with denominator approaching zero will give unreliable results

@superticker

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!

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!

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.

@Sammy_G

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

**RelativePerformance**

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