Combine PLOT and ROUND?

Fitzman

New member
Disclaimer 1: I searched but couldn't find an answer.
Disclaimer 2: might be a stupid question, but I just started learning to think script.

I have been working on a watchlist column code to display 30 week moving averages 12 days ago (might not be working correctly yet..., but this post is about the displayed format).


Code:
# Calculate the percentage difference between the close price from 12 days ago and the 30-period SMA from 12 days ago

def weeklySMALength = 30;
def movingAvg = Average(close, weeklySMALength);

def closeFiveDaysAgo = close[12];
def movingAvgFiveDaysAgo = movingAvg[12];

def percentageDifference = (closeFiveDaysAgo - movingAvgFiveDaysAgo) / movingAvgFiveDaysAgo * 120;
def roundedPercentage = Round(percentageDifference, 0);

plot myPercentageDifference = roundedPercentage;

def priceBelowMA = closeFiveDaysAgo < movingAvgFiveDaysAgo;

myPercentageDifference.AssignValueColor(Color.BLACK);
AssignBackgroundColor(if priceBelowMA then Color.BLUE else Color.BLACK);

Watchlist column link

My desired plot in the cell is the whole numbers/integer/number without any decimals only, e.g., -1, -13, -34, etc... . Here is my dilemma:

A) If I use the plot function, it will always plot with one decimal, but the column will sort correctly
B) If I use the AddLabel function to print the value, it will omit the decimal, but the column no longer sorts correctly because it's now as string (it will sort 1, 2, 21, 3, 33, 4, 41, etc...)

I tried to combine PLOT and ROUND to zero decimals, but it does not seem to work. The plotted number is always still with one decimal... Could anyone advise on how to use PLOT and return an integer/whole number without a decimal? Or anther way to return an integer that sorts correctly in the column.

Thanks,
Fitz.

Thanks,
Jan.
 
Disclaimer 1: I searched but couldn't find an answer.
Disclaimer 2: might be a stupid question, but I just started learning to think script.

I have been working on a watchlist column code to display 30 week moving averages 12 days ago (might not be working correctly yet..., but this post is about the displayed format).


Code:
# Calculate the percentage difference between the close price from 12 days ago and the 30-period SMA from 12 days ago

def weeklySMALength = 30;
def movingAvg = Average(close, weeklySMALength);

def closeFiveDaysAgo = close[12];
def movingAvgFiveDaysAgo = movingAvg[12];

def percentageDifference = (closeFiveDaysAgo - movingAvgFiveDaysAgo) / movingAvgFiveDaysAgo * 120;
def roundedPercentage = Round(percentageDifference, 0);

plot myPercentageDifference = roundedPercentage;

def priceBelowMA = closeFiveDaysAgo < movingAvgFiveDaysAgo;

myPercentageDifference.AssignValueColor(Color.BLACK);
AssignBackgroundColor(if priceBelowMA then Color.BLUE else Color.BLACK);

Watchlist column link

My desired plot in the cell is the whole numbers/integer/number without any decimals only, e.g., -1, -13, -34, etc... . Here is my dilemma:

A) If I use the plot function, it will always plot with one decimal, but the column will sort correctly
B) If I use the AddLabel function to print the value, it will omit the decimal, but the column no longer sorts correctly because it's now as string (it will sort 1, 2, 21, 3, 33, 4, 41, etc...)

I tried to combine PLOT and ROUND to zero decimals, but it does not seem to work. The plotted number is always still with one decimal... Could anyone advise on how to use PLOT and return an integer/whole number without a decimal? Or anther way to return an integer that sorts correctly in the column.

Thanks,
Fitz.

Thanks,
Jan.

numbers sort right to left
test sorts left to right
if you want to sort text, from a addlabel(), then all data needs to have the same quantity of characters.
the label needs to add leading characters (or zeros)

look at the addlabel code lines in these studies
https://usethinkscript.com/threads/...tion-from-addlabel-function.10456/#post-92584

https://usethinkscript.com/threads/watchlist-script-reformat.16687/#post-132460
 
Last edited:

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