How can I trim long (useless) numbers?

rottentrade

Member
Thinkscript allows you to roundup or down the last numbers to your liking, but is there a way to "trim" the front numbers and only see the last x numbers? I test my strategies with chart bubbles showing long numbers like "12345678.90". I would like this to be formatted as "78.90". Or for ES, format 3683.25, 3685.75, 3692.50 to 83.25, 85.75, 92.50, etc.
 
Solution
Thinkscript allows you to roundup or down the last numbers to your liking, but is there a way to "trim" the front numbers and only see the last x numbers? I test my strategies with chart bubbles showing long numbers like "12345678.90". I would like this to be formatted as "78.90". Or for ES, format 3683.25, 3685.75, 3692.50 to 83.25, 85.75, 92.50, etc.

to trim this
12345678.90
into this
78.90

Code:
def x1 = 12345678.90;
# move decimal point left 2
def x2 = x1 / 100;
# remove integer portion 
def x3 = x2 - floor(x2);
# shift digits, move decimal point
def x4 = x3 * 100;

addlabel(1, x1,color.yellow);
addlabel(1, x4,color.yellow);
Thinkscript allows you to roundup or down the last numbers to your liking, but is there a way to "trim" the front numbers and only see the last x numbers? I test my strategies with chart bubbles showing long numbers like "12345678.90". I would like this to be formatted as "78.90". Or for ES, format 3683.25, 3685.75, 3692.50 to 83.25, 85.75, 92.50, etc.

to trim this
12345678.90
into this
78.90

Code:
def x1 = 12345678.90;
# move decimal point left 2
def x2 = x1 / 100;
# remove integer portion 
def x3 = x2 - floor(x2);
# shift digits, move decimal point
def x4 = x3 * 100;

addlabel(1, x1,color.yellow);
addlabel(1, x4,color.yellow);
 
Solution
Thank you so much! I have another question:

I want to apply your formula to 4 variables (eg. open, high, low, close). How would I do that without writing the formula for each variable?

the function, script, can be used to create a sub routine within the main study.
it is used when a collection of formulas, will be used multiple times.

i added an input to choose how many digits to trim to. the default is 2.


Ruby:
# trim_digits2_02b

#  "trim" the front numbers and only see the last x numbers
# ex, this number, 12345678.90 , will be changed to  78.90

script trim1 {
# trim the digits left of the decimal point, down to x digits
input x = 0;
input digits = 2;
# move decimal point left 2
def dig = Power(10, digits);
def x2 = x / dig;
# remove integer portion
def x3 = x2 - Floor(x2);
# shift digits, move decimal point
plot z = x3 * dig;
}

# test numbers
#def x1 = 12345678.92;
#def x2 = 12345644.55;
#def x3 = 12345633.77;
#def x4 = 12345611.66;

def x1 = open;
def x2 = high;
def x3 = low;
def x4 = close;

input trim_digits = 2;

def x1_trim = trim1(x1, trim_digits);
def x2_trim = trim1(x2, trim_digits);
def x3_trim = trim1(x3, trim_digits);
def x4_trim = trim1(x4, trim_digits);

AddLabel(1, x1, Color.YELLOW);
AddLabel(1, x1_trim, Color.YELLOW);
AddLabel(1, " ", Color.black);
AddLabel(1, x2, Color.YELLOW);
AddLabel(1, x2_trim, Color.YELLOW);
AddLabel(1, " ", Color.black);
AddLabel(1, x3, Color.YELLOW);
AddLabel(1, x3_trim, Color.YELLOW);
AddLabel(1, " ", Color.black);
AddLabel(1, x4, Color.YELLOW);
AddLabel(1, x4_trim, Color.YELLOW);
#

https://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/script
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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