Sorting Custom Watch List Column

TOSUser21

New member
I have created the following Custom Watch List Column:

Code:
def capital = (volume*(close-close from 1 bars ago));
AddLabel(yes," " + AsDollars(capital));

The calculation works, but I am unable to sort the column in ascending or descending order. Do you know how I can edit this script so that I can sort the column accurately?

Thank you in advance for your help.
 
Solution
Sorry for the confusion, I'm looking for the values to be green/red if positive/negative as in the picture. The main thing I'm trying to figure out though is how to get the values in actual alphanumeric order. I will take them monochrome if necessary! :)

maybe this is what you are looking for.
here is an updated version, with leading 0, so the text data sorts in a column.

a column study that displays the % change in price , from open

share link , column study, set to day
colsort01
http://tos.mx/TqdLyjk



column study

can manually change a variable, show_negative_sign, to yes or no, to show a negative sign.
for altering how a column is sorted. without a negative sign, big positive and big negative numbers are close...
@TOSUser21 The problem you are encountering has to do with the fact that when using AddLabel() for a Custom WatchList Column all values are considered Strings... By switching to plot instead of AddLabel() you will eliminate this issue... The following code will work as expected but without the AsDollars() which can only be used with AddLabel() and AddChartBubble() to display numbers as Strings... You can't have it both ways... Forget about the dollar signs as they aren't required, nor should you be worried about how many minimum decimals points are displayed...

Ruby:
def capital = volume * (close - close[1]);
plot Data = Round(capital, 2);
 

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

rad14733--thank you very much for your feedback. I really appreciate your help. The code works and the sort is now accurate. Is there a way for commas to be inserted so that 1000.00 is displayed as 1,000.00? If not, then can three digits after the decimal point be displayed? That way I can put the result in thousands and then think of the decimal point as a comma so 1000.00 would be displayed as 1.000? Thank you again!
 
@TOSUser21 I'm glad that works for you... No, we cannot add commas because that sends us back into the realm of being a string rather than a number... Numbers don't have commas or dollar signs, only strings do... And then we can't sort those strings like we can numbers... Settle for what works and don't sweat the fine details...

As for decimal points, you can change the 2 to 3 in the Round() function, or omit the function altogether and let the raw number be displayed...
 
... can three digits after the decimal point be displayed? That way I can put the result in thousands and then think of the decimal point as a comma so 1000.00 would be displayed as 1.000? Thank you again!
divide the number by 1000, then set rounding to 3 digits.
Round( capital/1000 , 3);
 
Hi all, I have a similar issue trying to get a custom watchlist for percent change from open, to sort alphanumerically. This is the code, which works well all but for the double digit sorting:

Code:
def ChangefromOpen = close / open - 1;
AddLabel(yes, AsPercent(ChangefromOpen), if ChangefromOpen>0 then color.GREEN else if ChangefromOpen<0 then color.RED else color.WHITE);

What I'm getting tripped up by is how to incorporate the dynamic colors into the 'plot' syntax, rather than the 'AddLabel' syntax.
 
Hi all, I have a similar issue trying to get a custom watchlist for percent change from open, to sort alphanumerically. This is the code, which works well all but for the double digit sorting:

Code:
def ChangefromOpen = close / open - 1;
AddLabel(yes, AsPercent(ChangefromOpen), if ChangefromOpen>0 then color.GREEN else if ChangefromOpen<0 then color.RED else color.WHITE);

What I'm getting tripped up by is how to incorporate the dynamic colors into the 'plot' syntax, rather than the 'AddLabel' syntax.

to sort numbers 'correctly' with addlabel, you need to add leading 0's, for numbers < 10 , if 2 digit numbers. change if numbers have more digits.

Code:
def ChangefromOpen = close / open - 1;
AddLabel(yes, 
(if ChangefromOpen < 10 then "0" else "") +  AsPercent(ChangefromOpen),
 if ChangefromOpen>0 then color.GREEN else if ChangefromOpen<0 then color.RED else color.WHITE);
 
to sort numbers 'correctly' with addlabel, you need to add leading 0's, for numbers < 10 , if 2 digit numbers. change if numbers have more digits.

Code:
def ChangefromOpen = close / open - 1;
AddLabel(yes,
(if ChangefromOpen < 10 then "0" else "") +  AsPercent(ChangefromOpen),
 if ChangefromOpen>0 then color.GREEN else if ChangefromOpen<0 then color.RED else color.WHITE);

Thanks so much for the response halcyonguy! I see the logic here, however the result is only a 0 added in front of all the rows, still sorted by the first digit (whether in the 1's or 10's place). Is there a way to do the color assignments using plot, or is AddLabel required when working with dynamic colors?

b6QyaJ1.jpg
 
Thanks so much for the response halcyonguy! I see the logic here, however the result is only a 0 added in front of all the rows, still sorted by the first digit (whether in the 1's or 10's place). Is there a way to do the color assignments using plot, or is AddLabel required when working with dynamic colors?

b6QyaJ1.jpg
maybe the variable is 1/100 of what i thought it would be. i don't use aspercent(). maybe it needs to be,

(if ChangefromOpen < 0.10 then "0" else "")

what do you want to color?
in a column, the addlabel color is the font color.
if you want to change the background color of a cell in a column, use an if-then in this,

assignbackgroundcolor( if x == 1 then color.green else color.red);

another post talking about column colors
https://usethinkscript.com/threads/moving-average-crossovers-for-thinkorswim.229/page-10#post-64703
 
Last edited:
Sorry for the confusion, I'm looking for the values to be green/red if positive/negative as in the picture. The main thing I'm trying to figure out though is how to get the values in actual alphanumeric order. I will take them monochrome if necessary! :)

zvegupQ.jpg
 
Sorry for the confusion, I'm looking for the values to be green/red if positive/negative as in the picture. The main thing I'm trying to figure out though is how to get the values in actual alphanumeric order. I will take them monochrome if necessary! :)

maybe this is what you are looking for.
here is an updated version, with leading 0, so the text data sorts in a column.

a column study that displays the % change in price , from open

share link , column study, set to day
colsort01
http://tos.mx/TqdLyjk



column study

can manually change a variable, show_negative_sign, to yes or no, to show a negative sign.
for altering how a column is sorted. without a negative sign, big positive and big negative numbers are close together.

Code:
# colsort01

# https://usethinkscript.com/threads/sorting-custom-watch-list-column.6181/#post-80479

# 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 < 10 and ChangefromOpen > -10) then "0" else "")  +
 absvalue( ChangefromOpen) + "%",
 (if ChangefromOpen > 0 then color.GREEN else if ChangefromOpen < 0 then color.RED else color.WHITE));


# code for 3 digits
# (if (ChangefromOpen < 1 and ChangefromOpen > 0) then "00" else if (ChangefromOpen < 10 and ChangefromOpen >= 1) then "0" else "")

# code for 2 digits
# (if (ChangefromOpen < 1 and ChangefromOpen > 0) then "0" else "") +

# add a negative sign
# (if ChangefromOpen < 0 then "-" else "") +
#
extra code at the end for swapping



a lower study, for showing the % values on each bar, to verify the numbers
Code:
declare lower;
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 < 10 and ChangefromOpen > -10) then "0" else "")  +
 absvalue( ChangefromOpen) + "%",
 (if ChangefromOpen > 0 then color.GREEN else if ChangefromOpen < 0 then color.RED else color.WHITE));

plot z =  ChangefromOpen ;

addchartbubble(1, 0, "C" + close + "\nO" + open + "\n" + ChangefromOpen + "%", color.yellow, yes);


99wk3Zd.jpg
 
Solution
If you don't want the leading zeros (I hate them) you can uses spaces instead--you can't see the spaces since the column is right justified but they result in correct sorting:

For < 100% and > -100%:
Code:
AddLabel( yes, 
    (if i > -0.1 and i < 0.1 then " " else "") + 
    AsPercent(i));

For < 1,000% and > -1,000%:
Code:
AddLabel( yes,
    (if i > -1 and i < 1 then " " else "") +
    (if i > -0.1 and i < 0.1 then " " else "") +
    AsPercent(i));
 
If you don't want the leading zeros (I hate them) you can uses spaces instead--you can't see the spaces since the column is right justified but they result in correct sorting:

For < 100% and > -100%:
Code:
AddLabel( yes,
    (if i > -0.1 and i < 0.1 then " " else "") +
    AsPercent(i));

For < 1,000% and > -1,000%:
Code:
AddLabel( yes,
    (if i > -1 and i < 1 then " " else "") +
    (if i > -0.1 and i < 0.1 then " " else "") +
    AsPercent(i));
i like a space better too. i didn't test that, thought it wouldn't sort correctly. thanks
 
Hey guys, thanks so much for your insight on this! I'm trying to figure out how to integrate the leading zero-free code from PlotinusRedux to halcyonguy's excellent script at the moment; AsPercent seems to elude me every time!

Out of curiosity, is there a way to sort a column as we're doing here so that it goes continuously from higher positive to lower negative, rather than highest to lowest positive, then highest to lowest negative as seen here? It doesn't affect the super usefulness of this great solution for me, just wondering!

R8J7UPU.jpg
 
Hey guys, thanks so much for your insight on this! I'm trying to figure out how to integrate the leading zero-free code from PlotinusRedux to halcyonguy's excellent script at the moment; AsPercent seems to elude me every time!

Out of curiosity, is there a way to sort a column as we're doing here so that it goes continuously from higher positive to lower negative, rather than highest to lowest positive, then highest to lowest negative as seen here? It doesn't affect the super usefulness of this great solution for me, just wondering!

R8J7UPU.jpg
AsPercent() expects numbers not multiplied by 100--i.e., 0.01 = 1%.

Hmm, all my percents had the same sign--I used it for % off 52 week high--mixing positive and negatives what I gave won't work, you'd have to put the space after the +/- to avoid leading zeros:
Code:
def i = Round(close / open - 1, 4);

AddLabel( yes,
    (if i < 0 then "-" else "+") +
    (if i < 0.1 and i > -0.1 then " " else "") +
    AsPercent(AbsValue(i)),
    if i > 0 then Color.GREEN
    else if i < 0 then Color.RED
    else Color.WHITE);

But that still leads to the sorting error you noticed above--i.e., positives from high to low followed by negatives from low to high. I don't know a way around that--you'd think using things like AsPercent() TOS would be smart enough to know they're numbers not text, but it isn't.

% Off High worked because all the numbers are negative:
Code:
def i = Round(close / Highest(high, 253) - 1, 3);

AddLabel(
    yes,
    (if i > -0.1 and i < 0.1 then " " else "") + AsPercent(i),
    if i > -0.20 then Color.Current else Color.LIGHT_RED);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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