Difference in % Change from Open - Watchlist Column

krahsloop

Member
VIP
Hi all, I'm using the watchlist code below (from the original post here) and it appears to calculate from yesterday's close:

Ruby:
# 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,AsPercent(c),
         if c > 0 then createcolor(0,204,75)
         else if c == 0 then color.light_gray
         else createcolor(250,90,0));

Would someone be able to help me modify the code to calculate from the open instead, i.e. "difference in % change from open"? I use a simple code to calculate change from open on my watchlist right now:

Ruby:
def ChangefromOpen = close / open - 1;

Just trying to figure out how to combine these two scripts basically. So for example, if SPX is currently up 1% from the open, and AMZN is up 4% from the open, the watchlist column would show 3%. Thanks everyone!
 

krahsloop

Member
VIP
Getting one step closer...below is a neat lower study that shows a visual of the % difference and is calculated from the open. Could someone help me strip down the code for use in a watchlist column? Again just looking for the difference in % change from the open.

Ruby:
declare lower;

input CorrelationWithSecurity = "SPX"; #hint CorrelationWithSecurity: Choose Correlation Ticker.
input showlabel = yes; #hint Showlabel: Display Label YES/No.
input showCloud = yes; #hint showCloud: Dislpay Clouds.

def reset = if getAggregationPeriod()>=aggregationPeriod.DAY then barnumber()==1 else (getday()!=getday()[1] or secondsFromTime(930)==0);
def firstBase = if reset then open else firstBase[1];
def secondBase = if reset then open(CorrelationWithSecurity) else secondBase[1];

#anchor point
addVerticalLine(reset,"",color.gray);

plot Zeroline = 0;
Zeroline.setDefaultColor(color.GRAY);
zeroline.hideBubble();
#current ticker
def close1 = if reset then open else close;
def currentTicker = if !isnaN(close) then (close1-firstBase)/firstBase else currentTicker[1];
plot RS = currentTicker;
RS.setDefaultColor(color.green);

#base ticker
def close2 =  if reset then open(correlationWithSecurity) else close(correlationWithSecurity);
def baseTicker = if !isnaN(close2) then (close2-secondBase)/secondBase else baseticker[1];
plot Base = baseTicker;
base.setDefaultColor(color.light_RED);


addlabel(showlabel," ",color.green);
addlabel(showlabel,Getsymbol() +" "+ aspercent(RS),if RS>0 then color.green else color.red );
addlabel(showlabel,"   ",color.black);
addlabel(showlabel," ",color.red);
addlabel(showlabel,CorrelationWithSecurity +" "+ aspercent(Base),if BAse>0 then color.green else color.red );

addcloud(if showCloud then rs else double.NaN,base,color.green,color.red);


#end
 

halcyonguy

Well-known member
VIP
Lifetime
Hi all, I'm using the watchlist code below (from the original post here) and it appears to calculate from yesterday's close:

Ruby:
# 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,AsPercent(c),
         if c > 0 then createcolor(0,204,75)
         else if c == 0 then color.light_gray
         else createcolor(250,90,0));

Would someone be able to help me modify the code to calculate from the open instead, i.e. "difference in % change from open"? I use a simple code to calculate change from open on my watchlist right now:

Ruby:
def ChangefromOpen = close / open - 1;

Just trying to figure out how to combine these two scripts basically. So for example, if SPX is currently up 1% from the open, and AMZN is up 4% from the open, the watchlist column would show 3%. Thanks everyone!


column study

set to day
calculate the % change, from day open to close, for SPX and chart stock.
subtract the %'s and display in a column.


zcoldaydiff
http://tos.mx/FuarhyH


Code:
# zcoldaydiff
# col_diff_from_open_0

# column, set to day

#https://usethinkscript.com/threads/difference-in-change-from-open-watchlist-column.11088/
# Difference in % Change from Open - Watchlist Column
# Thread starterkrahsloop  Start dateApr 26, 2022

#  I'm using the watchlist code below (from the original post here) and it appears to calculate from yesterday's close:

#Just trying to figure out how to combine these two scripts basically. So for example, if SPX is currently up 1% from the open, and AMZN is up 4% from the open, the watchlist column would show 3%. Thanks everyone!


# Difference in Percent Change vs SPX
# Select your choice of timeframe above

input start_price = open;
input start_offset = 0;
input stop_price = close;

# current symbol change
def stk_per = 100 * (stop_price - start_price[start_offset])/start_price[start_offset];


# SPX change
input sym1_offset = 0;
def sym1_start = open("SPX")[sym1_offset];
def sym1_stop = close("SPX");
def sym1_per = 100 * (sym1_stop - sym1_start[sym1_offset])/sym1_start[sym1_offset];

#input show_percents = yes;
#addlabel(show_percents, stk_per, color.magenta);
#addlabel(show_percents, sym1_per, color.magenta);

# current symbol change minus SPX change
def diff = stk_per - sym1_per;

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


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


a chart study, for testing

Code:
# col_diff_from_open_0

# chart, set to day

# Difference in Percent Change vs SPX
# Select your choice of timeframe above

input start_price = open;
input start_offset = 0;
input stop_price = close;

# current symbol change
def stk_per = 100 * (stop_price - start_price[start_offset])/start_price[start_offset];

# SPX change
input sym1_offset = 0;
def sym1_start = open("SPX")[sym1_offset];
def sym1_stop = close("SPX");
def sym1_per = 100 * (sym1_stop - sym1_start[sym1_offset])/sym1_start[sym1_offset];

input show_percents = yes;
addlabel(show_percents, stk_per, color.magenta);
addlabel(show_percents, sym1_per, color.magenta);

# current symbol change minus SPX change
def diff = stk_per - sym1_per;

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

krahsloop

Member
VIP
halcyonguy thanks so much for responding, it was getting lonely in here! I just tested out the code, and it seems to produce only the change from open of the symbol (not calculating the difference from SPX), and same on the chart study. As I'm reading the code it makes sense and seems like it should work. I'm guessing it just needs a minor tweak, but not sure what that tweak needs to be! The image below is my regular change from open on the left, then your column code on the right. Obviously it's not regular market hours so this is not a proper 'live' test, but seems like it would show the difference at this time still. Again thank you for your time and insight!

EVK0q04.jpg
 

halcyonguy

Well-known member
VIP
Lifetime
halcyonguy thanks so much for responding, it was getting lonely in here! I just tested out the code, and it seems to produce only the change from open of the symbol (not calculating the difference from SPX), and same on the chart study. As I'm reading the code it makes sense and seems like it should work. I'm guessing it just needs a minor tweak, but not sure what that tweak needs to be! The image below is my regular change from open on the left, then your column code on the right. Obviously it's not regular market hours so this is not a proper 'live' test, but seems like it would show the difference at this time still. Again thank you for your time and insight!

EVK0q04.jpg

hmm that is odd. i thought i copied the chart study for the watchlist, so they should be the same , but they aren't... looking at it

currently, chart study and column study show different numbers. they have the same code, same formulas.
when i copy the watchlist code back to be a chart study, the % number matches my original chart study. and the watchlist number seems to lag behind and/or just not updated as often.

there can be a delay before a watchlist column is updated. i think that is what is happening. as i watch my 2 studies, the column number lags behind.
i have 3 custom columns loaded and about 15 chart studies (with most disabled). if you have many column studies loaded, it may cause more of a delay.
 
Last edited:

halcyonguy

Well-known member
VIP
Lifetime
halcyonguy thanks so much for responding, it was getting lonely in here! I just tested out the code, and it seems to produce only the change from open of the symbol (not calculating the difference from SPX), and same on the chart study. As I'm reading the code it makes sense and seems like it should work. I'm guessing it just needs a minor tweak, but not sure what that tweak needs to be! The image below is my regular change from open on the left, then your column code on the right. Obviously it's not regular market hours so this is not a proper 'live' test, but seems like it would show the difference at this time still. Again thank you for your time and insight!

EVK0q04.jpg

you didn't post your code that you are comparing with, so i have no idea. they look to all have about a 4.5% difference.
did you want all of the code in post #1 to be fixed, used?

including the subtracting the two percent numbers?
. # current symbol change minus SPX change
. def c = a-b;
 

krahsloop

Member
VIP
Sorry about that, the code on the left column of the image is the simple Change from Open script I had on the bottom of the original post:

Ruby:
def ChangefromOpen = close / open - 1;

That said, I tested your watchlist code out a bit before close today and it seems to be working! Looking forward to trying it out during market hours Monday - I will update this thread if anything seems wonky. To answer your question about code to be used, my goal was basically to make the code at the very top of post #1 calculate percent difference from the open, rather than from the previous day. The code in post #2 is of course a chart study and not a watchlist study, I just thought it might be helpful as a supplement, if it was not possible to adjust the code in post #1 to calculate from the open and throughout the day.

Thank you again for your time and help, and also for your contribution to this community. I've gained so much understanding and many useful tools from your posts in various threads!
 
Top