Advertisement
Maurizio-Ciullo

Indicatore Funzione Truncate to decimals example

Aug 31st, 2023
600
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © Maurizio-Ciullo
  3.  
  4. // https://www.tradingcode.net/tradingview/truncate-numbers/
  5.  
  6. //@version=5
  7. indicator(title="Indicatore Funzione Truncate to decimals example", overlay=true)
  8.  
  9. // Truncate() truncates a given number to the specified
  10. // number of decimals.
  11. Truncate(number, decimals) =>
  12.     factor = math.pow(10, decimals)
  13.     int(number * factor) / factor
  14.  
  15. // Make an input that sets the number of decimals to truncate to
  16. decimalAmount = input.int(1, title="Number of Decimals", minval=0)
  17.  
  18. // Calculate the Simple Moving Average and its truncated variant
  19. smaValue     = ta.sma(close, 20)
  20. smaTruncated = Truncate(smaValue, decimalAmount)
  21.  
  22. // Plot the SMA and its truncated value
  23. plot(smaValue, color=color.purple, linewidth=2, title="SMA")
  24. plot(smaTruncated, color=color.yellow, linewidth=2,
  25.      title="SMA Truncated")
  26.  
  27. // Every 30 price bars, make a label that shows the
  28. // SMA value and its truncated to decimals version
  29. if bar_index % 30 == 0
  30.     label.new(x=bar_index, y=smaValue, style=label.style_label_up,
  31.          color=#f2f2f2, textcolor=color.black,
  32.          text="SMA: " + str.tostring(smaValue, "0.000") +
  33.              "\nTruncated: " + str.tostring(smaTruncated, "0.000"))
Advertisement
Comments
  • hemal9022
    296 days
    # text 0.11 KB | 0 0
    1. 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