# Max() and AbsoluteValue() in ThinkOrSwim

#### Cribbage

##### Member
I've been backtesting a strategy for a while and realized I can probably automate what I'm doing with better Think Script. I currently use a study that tells me what percentage a stock moved above it's previous day's close. I rewrote the same study for the percentage it closes below the previous day's close. If the value is greater than 10% it changes color which makes it easier for me to manually test the information when I look back. This is the part I want to automate - I want to make 5 different versions of this watchlist columns (WLCs) so I can just pull up a stock from 5 days ago and the 5 columns will tell me what I'm looking for rather than manually entering the data.

plot PerMove = round (((high - close[1]) / close [1] * 100), 2);
PerMove.assignValueColor (if PerMove>10 then color.green else color.magenta);

The thing I'm getting hung up on is how to tell the WLCs which number to use. I want the column to return its value based on which ever absolute number is greater (I think absolute is the correct math term?). In other words:

Stock A's high was 15% higher then previous day, its low was -3%, WLC would show 15%
Stock B's high was 3% higher than previous day, low was -15%, WLC would show -15%

And preferably, if the absolute value of the percent move is below 9.5 I would like the column to be blank.

I know I should be able to write this but I can't for the life of me figure it out. Any help is appreciated.

Last edited:
@Cribbage Take a look at the Max() function in the Thinkscript Learning Center... That should get you headed in the right direction...
Okay, I see where you're going but I don't know how to convert the percent values into negative numbers. Obviously if the number is positive I don't have to worry about it, but is there a way to remove negative values from numbers so that in the case that there was a move >-10% it will show as being greater than any number between 0-9 that the other calculation my produce?

For example, if Stock XYZ's low was 15% lower than previous day close, and its high was 5% greater than prev day close, I want the column to plot the lower number so I have to make it positive in order for it to be greater than the high. Hopefully this makes sense and thanks for pointing me.

@Cribbage Check out the AbsValue() function... While you're there, check out the rest of the handy functions there... You'll surely come away learning more than you intended... I still go through the entire Learning Center looking for ways to improve my Thinkscript coding skills but other times I just stumble across them while looking for something else...

@Cribbage Check out the AbsValue() function... While you're there, check out the rest of the handy functions there... You'll surely come away learning more than you intended... I still go through the entire Learning Center looking for ways to improve my Thinkscript coding skills but other times I just stumble across them while looking for something else...
Just what I was looking for - Thanks Rad. I'll post the final code when I get it working.

@Cribbage Take a look at the Max() function in the Thinkscript Learning Center... That should get you headed in the right direction...
Thought I had it, but I still have a hiccup - Since I want to be able to export it to a spreadsheet, I want the negative values to show as negative. I used the close to help me determine whether it was an up/down move and then color-coded. So I've got a working script, but if I export it I still have to go in and manually enter the "-" sign on my spreadsheet. Is there anyway to tell it to keep the negative values despite using absolute numbers to gauge what shows? For instance, is there a way to tell it to show a negative value on the "magenta" and "red" line. Or can I multiply the value by -1 if its magenta or red somehow?

Here's what I've got:

def PerMoveHigh = round (((high - close[1]) / close [1] * 100), 2);
def PerMoveLow = round (((low - close[1]) / close [1] * 100), 2);

Plot PerMove = Max (PerMoveHigh, absvalue(PerMoveLow));

PerMove.assignValueColor
(if close > close[1] and PerMove>9.5 then color.green
else if close > close[1] and PerMove<9.501 then color.yellow
else if close < close[1] and PerMove<9.501 then color.magenta
else if close < close[1] and PerMove>9.5 then color.red
else color.white);

@Cribbage Here is an idea... Why not just test your conditions using AbsValue() but actually retain the original signed value for display and export...???

@rad14733 Because it has to choose between 2 values: a higher one/lower one. Unless I'm missing something, I have to tell the column to display either the high or the low. Can I use an "if/then" in my plot statement? I haven't seen this in think script before so I have no point of reference. I think I could finagle it if that's possible.

@rad14733 Because it has to choose between 2 values: a higher one/lower one. Unless I'm missing something, I have to tell the column to display either the high or the low. Can I use an "if/then" in my plot statement? I haven't seen this in think script before so I have no point of reference. I think I could finagle it if that's possible.

You can use virtually any conditional logic in a plot statement, just as you can for a def... The only difference is that def only stores the result whereas plot stores and displays the result...

Thanks for walking through that with me @rad14733.

This will give you a day's percent move from the previous day's close.

def PerMoveHigh = round (((high - close[1]) / close [1] * 100), 2);
def PerMoveLow = round (((low - close[1]) / close [1] * 100), 2);
def AbsHigh = PerMoveHigh;
def AbsLow = absvalue(permovelow);

Plot PerMove = if AbsLow>AbsHigh then permovelow else Permovehigh;

PerMove.assignValueColor
(if permove > 0 then color.green else if permove < 0 then color.red else color.white);

87k+ Posts
331 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?