Max() and AbsoluteValue() in ThinkOrSwim - useThinkScript Community (2024)

Sort by dateSort by votes

rad14733

Well-known member

VIP

  • Jun 19, 2021
  • #2

Upvote0Downvote

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

SIGN UP NOW LEARN MORE

Cribbage

Member
  • Jun 21, 2021
  • #3

rad14733 said:

@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.

Upvote0Downvote

rad14733

Well-known member

VIP

  • Jun 21, 2021
  • #4

@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...

Upvote0Downvote

Cribbage

Member
  • Jun 21, 2021
  • #5

rad14733 said:

@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.

Upvote0Downvote

Cribbage

Member
  • Jun 22, 2021
  • #6

rad14733 said:

@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);

Upvote0Downvote

rad14733

Well-known member

VIP

  • Jun 22, 2021
  • #7

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

Upvote0Downvote

Cribbage

Member
  • Jun 22, 2021
  • #8

@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.

Upvote0Downvote

rad14733

Well-known member

VIP

  • Jun 22, 2021
  • #9

Cribbage said:

@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...

Upvote0Downvote

Cribbage

Member
  • Jun 22, 2021
  • #10

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);

Upvote0Downvote

You must log in or register to reply here.

Max() and AbsoluteValue() in ThinkOrSwim - useThinkScript Community (2024)
Top Articles
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated:

Views: 5758

Rating: 4.7 / 5 (57 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.