"Simple" Relative Performance Chart

John Drake

New member
I'm trying to create a chart study that will be similar to the Percent change option in TOS Settings/Price. Whereas that particular chart results in the benchmark (let's say the SPY) being plotted along with the comparison issues, I'm trying to replicate the type referred to in a Stocks and Commodities magazine article from May, 2001. Rather than show the benchmark issue as tracking its percentage change throughout the period of the chart, it is plotted as zero. The comparison issues are plotted using the difference between their percentage change and that of the benchmark. It is this difference that is plotted, the comparison issues' lines moving around the benchmark's zero line. I think this is visually more informative than that provided by the TOS Percent change format.

In essence, stock ABC plots a line that is ((today's close [for each bar] minus the close of the first bar on the left end of the chart) divided by (the close of the first bar)) minus ((today's close of SPY minus the close of SPY first bar) divided by (the close of the first bar )).

So that, ABC closes at 59.25 today vs. 37.85 the first bar on the chart, a difference of 21.4. Divide 21.4 by 37.85 for a percentage gain of 56.54%. SPY closes today at 507.25, vs. 412.87 the first bar on the chart, for a difference of 94.38. Divide that by 412.87 and get a percentage change of 22.85%. The last step is to subtract the benchmark's percent change, 22.85%, from that of the comparison issue, 56.54%, to get the difference of 33.69%. It is this difference that is plotted for the comparison issue, ABC. SPY's action, because it has been subtracted from that of ABC, is plotted as a zero line.
It would be great if, as with the TOS Percent Change chart, the user could add any number of comparison issue symbols and if the benchmark issue and lookback period could be manually input.

I'm very grateful for any help provided.
 
Last edited by a moderator:
Solution
I'm trying to create a chart study that will be similar to the Percent change option in TOS Settings/Price. Whereas that particular chart results in the benchmark (let's say the SPY) being plotted along with the comparison issues, I'm trying to replicate the type referred to in a Stocks and Commodities magazine article from May, 2001. Rather than show the benchmark issue as tracking its percentage change throughout the period of the chart, it is plotted as zero. The comparison issues are plotted using the difference between their percentage change and that of the benchmark. It is this difference that is plotted, the comparison issues' lines moving around the benchmark's zero line. I think this is visually more informative than...
I'm trying to create a chart study that will be similar to the Percent change option in TOS Settings/Price. Whereas that particular chart results in the benchmark (let's say the SPY) being plotted along with the comparison issues, I'm trying to replicate the type referred to in a Stocks and Commodities magazine article from May, 2001. Rather than show the benchmark issue as tracking its percentage change throughout the period of the chart, it is plotted as zero. The comparison issues are plotted using the difference between their percentage change and that of the benchmark. It is this difference that is plotted, the comparison issues' lines moving around the benchmark's zero line. I think this is visually more informative than that provided by the TOS Percent change format.

In essence, stock ABC plots a line that is ((today's close [for each bar] minus the close of the first bar on the left end of the chart) divided by (the close of the first bar)) minus ((today's close of SPY minus the close of SPY first bar) divided by (the close of the first bar )).

So that, ABC closes at 59.25 today vs. 37.85 the first bar on the chart, a difference of 21.4. Divide 21.4 by 37.85 for a percentage gain of 56.54%. SPY closes today at 507.25, vs. 412.87 the first bar on the chart, for a difference of 94.38. Divide that by 412.87 and get a percentage change of 22.85%. The last step is to subtract the benchmark's percent change, 22.85%, from that of the comparison issue, 56.54%, to get the difference of 33.69%. It is this difference that is plotted for the comparison issue, ABC. SPY's action, because it has been subtracted from that of ABC, is plotted as a zero line.
It would be great if, as with the TOS Percent Change chart, the user could add any number of comparison issue symbols and if the benchmark issue and lookback period could be manually input.

I hope this is reasonably clear. Of course I'll be glad to clarify where necessary and, if requested, send a .pdf copy of the S&C article to anyone who'd like to read it, either for clarification in this question or just for the enjoyment.

I'm very grateful for any help provided.

See if this helps

Screenshot 2024-03-07 150403.png
Code:
#PerceentChange_Chart_v_Benchmark

declare lower;

input comparisonlength = 14;
input hiAlert          = 0;
input loAlert          = 0;
input benchmarksymbol  = "SPY";
input labels           = yes;
input spacer           = yes;

def bar                     = BarNumber();
def chartsymbolbeginprice   = if bar == 1 then close else chartsymbolbeginprice[1];
def chartBMbeginprice       = if bar == 1 then close("SPY") else chartBMbeginprice[1];
def currentsymbolprice      = close;
def currentBMprice          = close(benchmarksymbol);

#Chart Symbol
def percentchange_chartsym  =
(currentsymbolprice - chartsymbolbeginprice) / chartsymbolbeginprice * 100;

AddLabel(labels, "Chart: " + GetSymbol() + " | B: " + AsDollars(chartsymbolbeginprice) + "  E: " + AsDollars(currentsymbolprice) + "   PC:: " + Round(percentchange_chartsym) + "%", if currentsymbolprice > chartsymbolbeginprice then Color.GREEN else Color.RED);

AddLabel(spacer, " | ", Color.WHITE);

#Benchmark Symbol
def percentchange_BM  =
(currentBMprice - chartBMbeginprice) / chartBMbeginprice * 100;

AddLabel(labels, "Benchmark: " + benchmarksymbol + " | B: " + AsDollars(chartBMbeginprice) + "  E: " + AsDollars(currentBMprice) + "   PC: " + Round(percentchange_BM) + "%", if currentBMprice > chartBMbeginprice then Color.GREEN else Color.RED);

AddLabel(spacer, " | ", Color.WHITE);

#Comparison
def comparison = percentchange_chartsym - percentchange_BM;
AddLabel(labels, "Chart v Benchmark: " + Round(comparison) + "%", if comparison > 0 then Color.GREEN else Color.RED);

#Percent Change

plot PercentChg =  100 * (Round(currentBMprice) / Round(currentBMprice[comparisonlength]) - 1);

plot pHiAlert = hiAlert;
plot pLoAlert = loAlert;

PercentChg.DefineColor("HiAlert", GetColor(5));
PercentChg.DefineColor("Normal", GetColor(7));
PercentChg.DefineColor("LoAlert", GetColor(1));
PercentChg.AssignValueColor(if PercentChg > hiAlert then PercentChg.Color("HiAlert") else if PercentChg < loAlert then PercentChg.Color("LoAlert") else PercentChg.Color("Normal"));
pHiAlert.SetDefaultColor(GetColor(8));
pLoAlert.SetDefaultColor(GetColor(8));

#
 
Solution

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Thank you, Sleepy Z. I appreciate the effort you made to create this study.

As I mentioned in my original post, my goal was, in appearance, as similar as possible to the Percent Change chart on TOS, which, as you probably already know, is reached by checking the Show Price as Percentage in the Price Axis tab. The benchmark is input through the chart and a number of comparison symbols are input via the Studies/Add Studies/Comparison procedure. These comparison symbols then appear in the Price subgraph as lines along with that of the benchmark symbol. The lines plotted represent the daily percentage change of that day's close to the close of the first bar on the left end of the chart.
What I wanted to accomplish was to take this one step further by, before plotting any lines, having each of the comparison symbols subtract from its daily percentage change the daily percentage change of the benchmark, as you show in your study. The plot, then is the difference, each day, between the comparison symbols' percent change from day 1, and the benchmark's percent change from day 1. Each comparison symbol would be plotting its own such line.

I was able to come up with a rudimentary script similar in basic function to yours. Needless to say, mine did not have the colors, labels, etc. Here is a link to that study as applied in a workspace created just for the study: http://tos.mx/dTrfopC . This is limited to just the lower pane as I could not figure out how to put it in the upper pane without also showing the price line of the benchmark. Having the created study in the lower pane allowed me to unclick the show price subgraph in the General tab and the show volume subgraph in the Equities tab so that only the lower pane appears.

The problem I've run into is that I cannot figure out how to get more than one comparison symbol in there at a time.

FYI, I was unable to find any code in TOS that showed how the Percent Change chart was able to have the comparison symbols input by the Add Studies procedure or what the code was used to make the appropriate calculations. Perhaps with your experience and expertise you may deduce what took place scriptwise.

Again, many thanks for your efforts. I'm grateful to you.

J.D.
 
Last edited by a moderator:
Thank you, Sleepy Z. I appreciate the effort you made to create this study.

As I mentioned in my original post, my goal was, in appearance, as similar as possible to the Percent Change chart on TOS, which, as you probably already know, is reached by checking the Show Price as Percentage in the Price Axis tab. The benchmark is input through the chart and a number of comparison symbols are input via the Studies/Add Studies/Comparison procedure. These comparison symbols then appear in the Price subgraph as lines along with that of the benchmark symbol. The lines plotted represent the daily percentage change of that day's close to the close of the first bar on the left end of the chart.
What I wanted to accomplish was to take this one step further by, before plotting any lines, having each of the comparison symbols subtract from its daily percentage change the daily percentage change of the benchmark, as you show in your study. The plot, then is the difference, each day, between the comparison symbols' percent change from day 1, and the benchmark's percent change from day 1. Each comparison symbol would be plotting its own such line.

I was able to come up with a rudimentary script similar in basic function to yours. Needless to say, mine did not have the colors, labels, etc. Here is a link to that study as applied in a workspace created just for the study: http://tos.mx/dTrfopC . This is limited to just the lower pane as I could not figure out how to put it in the upper pane without also showing the price line of the benchmark. Having the created study in the lower pane allowed me to unclick the show price subgraph in the General tab and the show volume subgraph in the Equities tab so that only the lower pane appears.

The problem I've run into is that I cannot figure out how to get more than one comparison symbol in there at a time.

FYI, I was unable to find any code in TOS that showed how the Percent Change chart was able to have the comparison symbols input by the Add Studies procedure or what the code was used to make the appropriate calculations. Perhaps with your experience and expertise you may deduce what took place scriptwise.

Again, many thanks for your efforts. I'm grateful to you.

J.D.

See if this helps.
1. It is a lower study that hides the price plots, in the code, and the price subgraph in settings.
2. It has movable bubbles.
3. It used the script function to create the multiple symbols. It should be easy for you to add more symbols using the method for the 4 symbols displayed.
4. The script function will allow you to use the plots within the script as output. By clicking within the parentheses of comp() you can choose any other plots than the comparison available in the plot dropdown
Screenshot 2024-03-22 152940.png

5. The image shows the code in the lower panel. You can move it to the upper panel with the % prices if that is what you want.

Screenshot 2024-03-22 152348.png
Code:
#PerceentChange_Chart_v_Benchmark

declare lower;
HidePricePlot();
input showbubbles = yes;
input showlabel    = yes;
AddLabel(showlabel, "Comparison of Symbols Percent Change to Benchmark symbol (SPY) Percent Change", Color.WHITE);
script comp {
    input Sym              = "AAPL";
    input comparisonlength = 14;
    input hiAlert          = 0;
    input loAlert          = 0;
    input benchmarksymbol  = "SPY";
    input labels           = yes;
    input spacer           = yes;

    def sym_close = if IsNaN(close(Sym)) then sym_close[1] else close(Sym);

    def bar                     = BarNumber();
    def chartsymbolbeginprice   = if bar == 1 then sym_close else chartsymbolbeginprice[1];
    def chartBMbeginprice       = if bar == 1 then close("SPY") else chartBMbeginprice[1];
    def currentsymbolprice      = sym_close;
    def currentBMprice          = close(benchmarksymbol);

#Chart Symbol
    def percentchange_chartsym  =
(currentsymbolprice - chartsymbolbeginprice) / chartsymbolbeginprice * 100;

    AddLabel(labels, "Chart: " + GetSymbol() + " | B: " + AsDollars(chartsymbolbeginprice) + "  E: " + AsDollars(currentsymbolprice) + "   PC:: " + Round(percentchange_chartsym) + "%", if currentsymbolprice > chartsymbolbeginprice then Color.GREEN else Color.RED);

    AddLabel(spacer, " | ", Color.WHITE);

#Benchmark Symbol
    def percentchange_BM  =
(currentBMprice - chartBMbeginprice) / chartBMbeginprice * 100;

    AddLabel(labels, "Benchmark: " + benchmarksymbol + " | B: " + AsDollars(chartBMbeginprice) + "  E: " + AsDollars(currentBMprice) + "   PC: " + Round(percentchange_BM) + "%", if currentBMprice > chartBMbeginprice then Color.GREEN else Color.RED);

    AddLabel(spacer, " | ", Color.WHITE);

#Comparison
    plot comparison = percentchange_chartsym - percentchange_BM;
    AddLabel(labels, "Chart v Benchmark: " + Round(comparison) + "%", if comparison > 0 then Color.GREEN else Color.RED);

#Percent Change

    plot PercentChg =  100 * (Round(currentBMprice) / Round(currentBMprice[comparisonlength]) - 1);

    plot pHiAlert = hiAlert;
    plot pLoAlert = loAlert;

    PercentChg.DefineColor("HiAlert", GetColor(5));
    PercentChg.DefineColor("Normal", GetColor(7));
    PercentChg.DefineColor("LoAlert", GetColor(1));
    PercentChg.AssignValueColor(if PercentChg > hiAlert then PercentChg.Color("HiAlert") else if PercentChg < loAlert then PercentChg.Color("LoAlert") else PercentChg.Color("Normal"));
    pHiAlert.SetDefaultColor(GetColor(8));
    pLoAlert.SetDefaultColor(GetColor(8));
}


plot l_100 = 100;
l_100.SetDefaultColor(Color.BLACK);

input sym1 = "AAPL";
input sym2 = "META";
input sym3 = "AMZN";
input sym4 = "TSLA";

plot x1 = comp(sym1).comparison;
plot x2 = comp(sym2).comparison;
plot x3 = comp(sym3).comparison;
plot x4 = comp(sym4).comparison;

input bubblemover = 0;
def b = bubblemover;
def mover = showbubbles and IsNaN(close[b]) and !IsNaN(close[b + 1]);
AddChartBubble(mover, x1[b + 1], sym1 + " : " + Round(x1[b + 1]) + "%", x1.TakeValueColor());
AddChartBubble(mover, x2[b + 1], sym2 + " : " + Round(x2[b + 1]) + "%", x2.TakeValueColor());
AddChartBubble(mover, x3[b + 1], sym3 + " : " + Round(x3[b + 1]) + "%", x3.TakeValueColor());
AddChartBubble(mover, x4[b + 1], sym4 + " : " + Round(x4[b + 1]) + "%", x4.TakeValueColor());

#
 
Many thanks, SleepyZ! This looks just like what I had in mind! I'm looking forward to learning to implement your suggestions and how the snippets of code fit in, perhaps for use in other applications later on.

I'm very grateful to you.

-John Drake
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
385 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top