Relative performance specific time period(s)

ReyHak

Member
Hi, new here!
Unfortunately I am new in ThinkScripts and really hopping someone can help me build this. Finviz.com (https://finviz.com/screener.ashx?v=141) has column for each stocks indicating relative performance to SPX for each stocks for various time frames (one day, one week, one month etc...) I believe the calculation for daily for example is closing price of a stock / closing price of SPX. This is extremely useful to stop stocks/sectors with better performance than SPX (or any market) for different time periods, this is specially useful during market pull backs.
I know we can currently chart this with TOS but I would like to have these as columns on my watch list, is this possible or already exist? If not, can anyone help me build this for various time frames?
Any help will be greatly appreciated. thank you
pE1PAan.jpg
 
Last edited by a moderator:

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

@ReyHak There is a built-in watchlist column called "RelativeStrength". According to ThinkorSwim's definition (https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/R-S/RelativeStrength), it calculates "the closing price of a stock/closing price of SPX". It doesnt give percentage values, but rather a ratio number.

I seem unable to replicate the performance percentages shown at Finviz.com. The Finviz values apparently do not match the current weekly, monthly, yearly, etc. percentage changes when compared to Thinkorswim.

This code is an idea I had while looking at this. It should take the percent change of the stocks in a watchlist, subtract the percent change of the SPX, and gives the difference. It might help in finding stocks that are overperforming/underperforming SPX.
Code:
# Difference in Percent Change vs SPX
# Select your choice of timeframe above

# current symbol change
def a = (close-close[1])/close[1];

# SPX change
def sym = close("SPX");
def b = (sym-sym[1])/sym[1];

# current symbol change minus SPX change
def c = a-b;

# gives the difference as a percent value
AddLabel(1,"Performance vs SPX = " + AsPercent(c),
         if c > 0 then createcolor(0,204,75)
         else if c == 0 then color.light_gray
         else createcolor(250,90,0));

#end code

And, this next code gives the same results as the built-in "%Change" watchlist column, but it can be changed to different aggregations other than the standard daily timeframe.
Code:
# Percent Change
# Select your choice of timeframe above

def a = (close-close[1])/close[1];

AddLabel(1,AsPercent(a),
         if a > 0 then createcolor(0,204,75)
         else if a == 0 then color.light_gray
         else createcolor(250,90,0));

#end code

Maybe this will help, maybe not. Best wishes! :)
 
Last edited:
. . . how do I change the time frame on the first code? let's say I want to see the relative performance for the past 5 days?
@ReyHak Once you add the code to your watchlist column, at the top left of the watchlist editor, beside the box for your custom column name, is a dropdown tab. Select your choice of timeframe from that list.

If you want to display multiple columns with different timeframes (such as weekly, monthly, and yearly), you will need to create the same number of custom columns as the different timeframes that you want, and assign a separate timeframe to each.
 
@ReyHak Here is a scan for the difference in percentage. I put notes in the code.

Code:
# Scan for Difference in Percent Change vs SPX
# Select your choice of timeframe above

def a = (close-close[1])/close[1];
def sym = close("SPX");
def b = (sym-sym[1])/sym[1];
def c = Round((a-b)*100,0);

# "1" scans for any stocks that are outperforming SPX by 1% or greater.
# Change it whatever positive value you like -
# as an example, "10" will find stocks that are
# outperforming SPX by 10% or greater.
plot scan = c > 1;

#end code

Edit: Removed a line in the code notes I had forgotten to take out before posting.
 
Last edited:
Hi, I am newmember in programming, I looking for way how to writte something like rolling period for calculation ("isPeriodRolled" in VWAP).

there is example:

input price = close;

input symbol_SPY = "SPY";

def data1 = ((close (symbol_SPY) - close (symbol_SPY) [1])*100);

plot SPY = data1;

plot AccDist_SPY = TotalSum(data1);

AccDist_SPY.SetDefaultColor (Color.RED);

and need do the same calculation but separately for each week, month...
and plot to one chart

thanky

Rich
 
I'm new to thinkorswim, I'm trying to see the percentage change of a stock vs dow. Simlare to the price relative indicator, can't see to find that in thinkorswim thus far.

Thanks!
 
@ReyHak Here is a scan for the difference in percentage. I put notes in the code.

Code:
# Scan for Difference in Percent Change vs SPX
# Select your choice of timeframe above

def a = (close-close[1])/close[1];
def sym = close("SPX");
def b = (sym-sym[1])/sym[1];
def c = Round((a-b)*100,0);

# "1" scans for any stocks that are outperforming SPX by 1% or greater.
# Change it whatever positive value you like -
# as an example, "10" will find stocks that are
# outperforming SPX by 10% or greater.
plot scan = c > 1;

#end code

Edit: Removed a line in the code notes I had forgotten to take out before posting.

@Pensar : Is there way to add a count for this? I've been trying endlessly !! For example Scan for stocks that have outperformed the "SPX" by a certain percentage of bars in a given period. Say, 65% of the bars in a given time frame.

This is the code I have:


input symbol = “SPY”;


def S1_open = open(symbol) ;
def S1_close = close(symbol);

def S1_difference = S1_close - S1_open;
def S1_percent = (S1_difference/ S1_open) * 100;



input barsago = 252;


def var = ((close - open) / close) * 100 is greater than S1_percent;
def count = sum(var, barsago);
plot scan = count is greater than S1_percent ;
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
436 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