Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © Maurizio-Ciullo
- // https://www.tradingcode.net/tradingview/truncate-numbers/
- //@version=5
- indicator(title="Indicatore Funzione Truncate to decimals example", overlay=true)
- // Truncate() truncates a given number to the specified
- // number of decimals.
- Truncate(number, decimals) =>
- factor = math.pow(10, decimals)
- int(number * factor) / factor
- // Make an input that sets the number of decimals to truncate to
- decimalAmount = input.int(1, title="Number of Decimals", minval=0)
- // Calculate the Simple Moving Average and its truncated variant
- smaValue = ta.sma(close, 20)
- smaTruncated = Truncate(smaValue, decimalAmount)
- // Plot the SMA and its truncated value
- plot(smaValue, color=color.purple, linewidth=2, title="SMA")
- plot(smaTruncated, color=color.yellow, linewidth=2,
- title="SMA Truncated")
- // Every 30 price bars, make a label that shows the
- // SMA value and its truncated to decimals version
- if bar_index % 30 == 0
- label.new(x=bar_index, y=smaValue, style=label.style_label_up,
- color=#f2f2f2, textcolor=color.black,
- text="SMA: " + str.tostring(smaValue, "0.000") +
- "\nTruncated: " + str.tostring(smaTruncated, "0.000"))
Advertisement
Comments
-
- all types of premium tradingview indicators codes available on telegram - https://t.me/tradingview_premium_indicator
Add Comment
Please, Sign In to add comment
Advertisement