Rounding Issues

sawyersweetman

Member
Plus
How to round a number that results from an equation?

I had chat GBT right this.

Im having trouble getting the "shares" result to be a round number

input length = 14;

def dailyAtr = reference ATR(length);

def atrDividedBy4 = dailyAtr / 4;
def result = round(10 / atrDividedBy4);


# Plotting the result of ATR / 4 as a chart label on the 1-minute chart

AddLabel(yes, "STOP: " + AsText(atrDividedBy4, NumberFormat.TWO_DECIMAL_PLACES), Color.GREEN);

AddLabel(yes, "SHARES: " + AsText(result), Color.CYAN);
 
Solution
Im not getting it,

def result = round(20 / atrDividedBy4,0);

Im getting for ex. 25.00 used to be 25.5

I had a look at the floor and ceil but they seem to just round aswell without removing the digits.

How do i make this is integar, Not a whole number, forgive my previous poor parlance

@sawyersweetman
several issues,
you are confused on the use of rounding functions and addlabel.

you are asking basic questions, that a couple minutes reading the online manual will solve.
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/Round
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/Floor...
How to round a number that results from an equation?

I had chat GBT right this.

Im having trouble getting the "shares" result to be a round number

input length = 14;

def dailyAtr = reference ATR(length);

def atrDividedBy4 = dailyAtr / 4;
def result = round(10 / atrDividedBy4);


# Plotting the result of ATR / 4 as a chart label on the 1-minute chart

AddLabel(yes, "STOP: " + AsText(atrDividedBy4, NumberFormat.TWO_DECIMAL_PLACES), Color.GREEN);

AddLabel(yes, "SHARES: " + AsText(result), Color.CYAN);

sorry, i have no idea what you are trying to do , or what you expect to see, or what is wrong.

round is a basic function, that is easy to look up and understand. although i will say, that the example is overly complicated and confusing.
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/Round

why do you have 10/ ... ?
def result = round(10 / atrDividedBy4);

There is supposed to be a second parameter that lists how many digits to the right that the rounding will do.
 
Last edited:

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

sorry, i have no idea what you are trying to do , or what you expect to see, or what is wrong.

round is a basic function, that is easy to look up and understand. although i will say, that the example is overly complicated and confusing.
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/Round

why do you have 10/ ... ?
def result = round(10 / atrDividedBy4);

There is supposed to be a second parameter that lists how many digits to the right that the rounding will do.
Hey @halconguy 10 is just an placeholder for whatever amount you want to risk per trade.

to calculate risk per trade im using ATR / 4 and then im taking the amount i want to risk and divide it by ATR/4

Some more explanation, this is for a situation where i just want to get in based on price and dont have a consolidation or something to risk of off. i can load this information into my active trader and if its at a price i like then just pay the order or i can offer up myself either way its for entering based on price with no clear price structure to risk off

Im just want that result for "shares" to be a round number, its not a big deal having the decimal places but it would be cleaner if it was a whole number since i wont be entering fractions of shares in my active trader.

I saw this post on chat GBT and gave it a shot. Chat GBT works very well. its not perfect but if you go back and forth its really amazing what it can do.

@halcony thank you for the link, so rounding is not what i need to do. i need to eliminate the decimal and make it a whole number
 
Last edited by a moderator:
I saw this post on chat GBT and gave it a shot. Chat GBT works very well. its not perfect but if you go back and forth its really amazing what it can do.

@halcony thank you for the link, so rounding is not what i need to do. i need to eliminate the decimal and make it a whole number
if you want to remove digits on the right side of a number, that is called rounding.
round(x, 0) will round to 0 places, get rid of the digits on right Side of decimal point.
Floor() and ceil() are other rounding functions

follow that link above to round(). then click on menu buttom at top left (3 horizontal lines) to see a list of functions. look at every one and learn what they do.
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig
 
Last edited:
Im not getting it,

def result = round(20 / atrDividedBy4,0);

Im getting for ex. 25.00 used to be 25.5

I had a look at the floor and ceil but they seem to just round aswell without removing the digits.

How do i make this is integar, Not a whole number, forgive my previous poor parlance
 
Im not getting it,

def result = round(20 / atrDividedBy4,0);

Im getting for ex. 25.00 used to be 25.5

I had a look at the floor and ceil but they seem to just round aswell without removing the digits.

How do i make this is integar, Not a whole number, forgive my previous poor parlance

@sawyersweetman
several issues,
you are confused on the use of rounding functions and addlabel.

you are asking basic questions, that a couple minutes reading the online manual will solve.
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/Round
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/Floor
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/AsText


def result = round( 10 / atrDividedByx );
this will round the formula. but because the 2nd parameter, rounding digits, is missing, it defaults to 2, and will have a number with 2 digits to the right of the decimal point.

if 0 is used, then this will be an integer, no digits after decimal point.
def result = round( 10 / atrDividedByx , 0 );


if astext() is used like this, to display an integer, it will show 2 digits to the right of decimal point 1.00
AddLabel(yes, "STOP: " + AsText(atrDividedByx, NumberFormat.TWO_DECIMAL_PLACES), Color.GREEN);



def result = round(20 / atrDividedBy4,0);
Im getting for ex. 25.00 used to be 25.5
I had a look at the floor and ceil but they seem to just round aswell without removing the digits.
..
that round() and floor() are working fine.
you didn't list all the code, but i saw some in an earlier post, so i can guess you are using astext() with some parameter that is displaying the digits.
the problem is how you are displaying the number in addlabel.

stop using astext().


notes,
i try not to use constants in formulas, so i replaced them with variables, so they can be user changed.

use variable names that describe what the data is


i copied the ATR code into this study, it's just 1 formula, no reason to reference it.

Code:
# ATR
input atr_length = 14;
input averageType = AverageType.WILDERS;
#plot ATR = MovingAverage(averageType, TrueRange(high, close, low), length);
#ATR.SetDefaultColor(GetColor(8));
def ATR1 = MovingAverage(averageType, TrueRange(high, close, low), atr_length);


input atr_factor = 4.0;
#def atrDividedByx = Atr1 / atr_factor;
def atrDividedByx = round(Atr1 / atr_factor, 2);
input risk_dollars = 10.0;
#def result = round(10 / atrDividedByx);
def shares = round(risk_dollars / atrDividedByx, 0);

def stop_level = close - atrDividedByx;


addlabel(1, " ", color.black);
addlabel(1, "ATR: " + atr1, color.yellow);
addlabel(1, "ATR factor: " + atr_factor, color.yellow);
addlabel(1, "adjusted ATR: " + atrDividedByx, color.yellow);
addlabel(1, "risk amount: " + asdollars(risk_dollars), color.yellow);

addlabel(1, " ", color.black);
#AddLabel(yes, "STOP: " + AsText(atrDividedByx, NumberFormat.TWO_DECIMAL_PLACES), Color.GREEN);
AddLabel(yes, "STOP: " + atrDividedByx, Color.GREEN);
AddLabel(1, "STOP level: " + stop_level , Color.yellow);
AddLabel(yes, "SHARES: " + AsText(shares), Color.CYAN);
AddLabel(yes, "SHARES: " + shares, Color.yellow);
addlabel(1, " ", color.black);
#
 
Last edited by a moderator:
Solution

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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