Percent Change from *Close* Watchlist Column

krahsloop

Member
VIP
Hi, I am looking for custom watchlist code that will track percent change from the day's closing price, basically from 4:00pm EST (the closing bell) until the rest of that calendar/after hours trading day. It would function the same way as the Percent Change from Open custom watchlist column referenced below (assembled from this thread: https://usethinkscript.com/threads/sorting-custom-watch-list-column.6181/#post-80479), but with the day's closing price as the start/0%. I tried to modify it , but switching out all the "ChangefromOpen" to "ChangefromClose" did not work at all. :)

Here is that Percent Change from Open study for reference, don't know if some lines or elements could be modified to create what I'm trying for.

Ruby:
# change percent number , x100 , round to 2 digits
def show_negative_sign = yes;

def ChangefromOpen = round(100 * (close / open - 1), 2);
AddLabel(yes,
 (if show_negative_sign and ChangefromOpen < 0 then "-" else "") +
 (if (ChangefromOpen < 1 and ChangefromOpen > 0) then "0" else "") +
 absvalue( ChangefromOpen) + "%",
 (if ChangefromOpen > 0 then color.GREEN else if ChangefromOpen < 0 then color.RED else color.WHITE));

#


Alternatively/additionally, here is code that scans for after hours percent change, not sure if it could be converted to a live watchlist as described above:

Ruby:
#Wizard text: The
#Wizard input: price
#Wizard text: has moved
#Wizard input: operator
#Wizard input: percent_change
#Wizard text: %  in after hours trading

input closing_time = 1559;
input open_time = 0930;
input price = close;
input operator = {default "greater than","less than"};
input percent_change = 1.00;


def time_until_close = SecondsTillTime(closing_time);
def time_until_open = SecondsTillTime(open_time);
def closing_bell = time_until_close == 0;
rec closing_price = CompoundValue(1, if closing_bell then price else closing_price[1], price);
def after_closing_bell = time_until_close <= 0;
def before_opening_bell = time_until_open  >= 0  ;
def afterhours_percent_change = 100 * (price / closing_price - 1);
def meet_scan_criteria;

switch (operator) {
case "greater than":
    meet_scan_criteria = afterhours_percent_change >= percent_change;
case "less than":
    meet_scan_criteria = afterhours_percent_change <=  percent_change;
}

plot scan = (after_closing_bell or before_opening_bell) AND meet_scan_criteria;


Many thanks to the community!
 
Last edited:
Solution
Figured it out! Use the code below and of course check the "Include Extended Hours Trading Session" box, and use a lower time frame (I use the 1-minute so it is the most up to date):

Ruby:
def openPrice = if SecondsTillTime(1600) == 0 then open else openPrice[1];

def pctChangeFromOpen = (close - OpenPrice) / OpenPrice;

AddLabel(yes, AsPercent(pctChangeFromOpen), if pctChangeFromOpen >= 0 then color.green else color.red);
#AssignBackgroundColor(if pctChangeFromOpen > 0 then color.green else color.red);

krahsloop

Member
VIP
Figured it out! Use the code below and of course check the "Include Extended Hours Trading Session" box, and use a lower time frame (I use the 1-minute so it is the most up to date):

Ruby:
def openPrice = if SecondsTillTime(1600) == 0 then open else openPrice[1];

def pctChangeFromOpen = (close - OpenPrice) / OpenPrice;

AddLabel(yes, AsPercent(pctChangeFromOpen), if pctChangeFromOpen >= 0 then color.green else color.red);
#AssignBackgroundColor(if pctChangeFromOpen > 0 then color.green else color.red);
 
Solution

Similar threads

Top