How to round up price to multiple of 5?

poutses

New member
I've tried other the ciel and the RoundUp functions and can't seem to get this right. Suppose price=3653.69829

When I use
Code:
ceil(price)
It just rounds up to the nearest integer and displays 3,654. Adding any other parameters to the ceil function gives an error.

When I use
Code:
RoundUp(price, 5);
It just displays 3,653.6983

When I use
Code:
RoundUp(price, -1);
It displays 3,660, not 3,655

Anyone have an idea on how to get it to round up to 3,655?
 
Solution
I've tried other the ciel and the RoundUp functions and can't seem to get this right. Suppose price=3653.69829

When I use
Code:
ceil(price)
It just rounds up to the nearest integer and displays 3,654. Adding any other parameters to the ceil function gives an error.

When I use
Code:
RoundUp(price, 5);
It just displays 3,653.6983

When I use
Code:
RoundUp(price, -1);
It displays 3,660, not 3,655

Anyone have an idea on how to get it to round up to 3,655?

Try

Code:
plot  price = 3653.69829;
AddLabel(1, Round((price / 5), 0) * 5);
I've tried other the ciel and the RoundUp functions and can't seem to get this right. Suppose price=3653.69829

When I use
Code:
ceil(price)
It just rounds up to the nearest integer and displays 3,654. Adding any other parameters to the ceil function gives an error.

When I use
Code:
RoundUp(price, 5);
It just displays 3,653.6983

When I use
Code:
RoundUp(price, -1);
It displays 3,660, not 3,655

Anyone have an idea on how to get it to round up to 3,655?

Try

Code:
plot  price = 3653.69829;
AddLabel(1, Round((price / 5), 0) * 5);
 
Solution
So that works for that specific example, but it doesn't work if the number changes to 3727.49. It then just rounds it down to 3725 instead of up to 3730.

So you don't want the nearest value ending in 5 which is what I thought you wanted. Since I am not a math expert, who can do this better, this seems to work for both of your requests.

Ruby:
input  price = 3727.49;
#AddLabel(1, Round((roundup(price,0) / 5), 0) * 5);

AddLabel(1, if Round((price / 5), 0) * 5 < price
            then Round(price / 10, 0) * 10
            else Round((price / 5), 0) * 5 );
 
Please read up on these functions:
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/Ceil
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/Floor

and these three:
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/Round
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/RoundDown
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/RoundUp

There are many ways to 'round' numbers. And then there is "bankers" rounding:
https://en.wiktionary.org/wiki/bankers'_rounding

Each has an application that it is best suited for. None of them are really interchangeable.

from your example, 3727.49 should round down to 3725.
you can try roundUp to get your preferred answer of 3730, but asking for round to get you that answer will not work.

@SleepyZ 's answer :
Code:
AddLabel(1, Round((price / 5), 0) * 5);
does provide the best way to round to the nearest multiple of 5.

Perhaps it is illustrative to see some results of different rounding:

AddLabel(1, Round((price / 5), 0) * 5);
--> 3725

AddLabel(1, RoundDown((price / 5), 0) * 5);
--> 3725

AddLabel(1, RoundUp((price / 5), 0) * 5);
--> 3730

AddLabel(yes, Round(price, 2 ));
--> 3727.49

AddLabel(yes, Round(price, 1 ));
--> 3727.5

AddLabel(yes, Round(price, 0 ));
--> 3727

AddLabel(yes, Round(price, -1 ));
--> 3730

AddLabel(yes, Round(price, -2 ));
--> 3700

Your example, rounding 27.49 up to 30 by thinking that the .49 rounds up to .5 and then .5 rounds up again to 30 is called double rounding, and is not generally employed by algorithms. The algorithm looks at the 27.4 and decides that .4 rounds down, and so 27.49 rounds down to 27 rather than up to 28 and therefore your request to get to the nearest 5 is based on 27 (which goes down) rather than 28 (which would go up).

This is probably a lot longer an answer than you wanted, but I spend a lot of time thinking about how different programs and databases work with rounding. And don't ask me to explain MS Excel does it. I don't want to think about it!!! ;-)

-mashume
 
Last edited:
So that works for that specific example, but it doesn't work if the number changes to 3727.49. It then just rounds it down to 3725 instead of up to 3730.

here is an example code to show 3 type of integer rounding, based on some devisor number
you want the rndup formula

Code:
input price = 3727.49;
input round_devisor = 5;

def rnd = floor((price + (round_devisor/2)) / round_devisor) * round_devisor;
def rndup = floor((price + round_devisor) / round_devisor) * round_devisor;
def rnddown = floor(price / round_devisor) * round_devisor;

addlabel(1, "orig " + price, color.white);
addlabel(1, "rounding devisor " + round_devisor, color.white);

addlabel(1, "round " + rnd, color.yellow);
addlabel(1, "round up " + rndup, color.green);
addlabel(1, "round down " + rnddown, color.red);
#


----------------------------------

@mashume
thanks for typing out those rounding examples. i added them to my collection of snippets.
there is a typo on the first one , should be 3725

AddLabel(1, Round((price / 5), 0) * 5);
--> 3730
 
@mashume
thanks for typing out those rounding examples. i added them to my collection of snippets.
there is a typo on the first one , should be 3725

AddLabel(1, Round((price / 5), 0) * 5);
--> 3730
Ugh. Typos. Fixed the post above. Thanks for pointing it out. Happy the examples are worth adding to your snippets collection.

-mashume
 

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
474 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