CINXE.COM
Language / Methods
<!DOCTYPE html><html lang="en"> <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"><script> const url = window.location.href; const expectedUrl = url.replace( /^https:\/\/(?!beta)(\w+\.)?tradingview\.com/, 'https://www.tradingview.com', ); if (url !== expectedUrl) { window.location.replace(expectedUrl); } </script><meta name="astro-view-transitions-enabled" content="true"><meta name="astro-view-transitions-fallback" content="none"><link rel="icon" href="https://static.tradingview.com/static/images/favicon.ico"><link rel="icon" type="image/svg+xml" href="/pine-script-docs/favicon.svg"><title>Language / Methods</title><meta name="og:title" content="Language / Methods"><meta name="twitter:title" content="Language / Methods"><meta name="description" content="Everything you need to know about Pine Script™."><meta name="og:description" content="Everything you need to know about Pine Script™."><meta name="twitter:description" content="Everything you need to know about Pine Script™."><meta name="keywords" content="tradingview, pine, script, indicators, strategies"><meta name="og:image" content="/pine-script-docs/meta-image.png"><meta name="twitter:image" content="/pine-script-docs/meta-image.png"><meta name="og:image:width" content="1200"><meta name="og:image:height" content="630"><meta name="og:url" content="https://www.tradingview.com/pine-script-docs/language/methods/"><meta name="twitter:url" content="https://www.tradingview.com/pine-script-docs/language/methods/"><meta name="og:type" content="website"><meta name="twitter:card" content="summary_large_image"><meta name="twitter:site" content="@TradingView"><link rel="canonical" href="https://www.tradingview.com/pine-script-docs/language/methods/"><link rel="sitemap" href="/pine-script-docs/sitemap-index.xml"><link rel="stylesheet" href="/pine-script-docs/_astro/index.CI8A0IeF.css"> <link rel="stylesheet" href="/pine-script-docs/_astro/index.BL2bgAvq.css"> <style>.tv-spinner{display:none;position:absolute;margin:0 auto;border:0 solid rgb(149 152 161 / 20%);border-radius:50%;border-top-color:var(--tv-spinner-color, #2962ff);border-left-color:var(--tv-spinner-color, #2962ff);animation:tv-spinner-container-rotate .9s linear infinite}.tv-spinner-shown{display:block}.tv-spinner-size-large{top:calc(50% - 32px);left:calc(50% - 32px);width:56px;height:56px;border-width:4px}@keyframes tv-spinner-container-rotate{to{transform:rotate(360deg)}}:root{--page-background-color: #fff;background-color:var(--page-background-color)}:root[data-theme=dark]{--page-background-color: #000}body{background-color:var(--page-background-color)}#redirect-link{display:flex;position:absolute;inset:0;align-items:center;justify-content:center}#spinner{display:block;position:absolute;opacity:1;transition:opacity .2s} .twitter-tweet:not(.twitter-tweet-rendered){padding:var(--tc-padding, 1em);border:1px solid var(--tc-border-color, #cfd9de)}.twitter-tweet:not(.twitter-tweet-rendered)>:first-child{margin-top:0}.twitter-tweet:not(.twitter-tweet-rendered)>:last-child{margin-bottom:0}lite-youtube{background-color:#000;position:relative;display:block;contain:content;background-position:center center;background-size:cover;cursor:pointer;max-width:720px}lite-youtube:before{content:attr(data-title);display:block;position:absolute;top:0;background-image:linear-gradient(180deg,#000000ab,#0000008a 14%,#00000026 54%,#0000000d 72%,#0000 94%);height:99px;width:100%;font-family:YouTube Noto,Roboto,Arial,Helvetica,sans-serif;color:#eee;text-shadow:0 0 2px rgba(0,0,0,.5);font-size:18px;padding:25px 20px;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;box-sizing:border-box}lite-youtube:hover:before{color:#fff}lite-youtube:after{content:"";display:block;padding-bottom:56.25%}lite-youtube>iframe{width:100%;height:100%;position:absolute;top:0;left:0;border:0}lite-youtube>.lty-playbtn{display:block;width:100%;height:100%;background:no-repeat center/68px 48px;background-image:url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 68 48"><path d="M66.52 7.74c-.78-2.93-2.49-5.41-5.42-6.19C55.79.13 34 0 34 0S12.21.13 6.9 1.55c-2.93.78-4.63 3.26-5.42 6.19C.06 13.05 0 24 0 24s.06 10.95 1.48 16.26c.78 2.93 2.49 5.41 5.42 6.19C12.21 47.87 34 48 34 48s21.79-.13 27.1-1.55c2.93-.78 4.64-3.26 5.42-6.19C67.94 34.95 68 24 68 24s-.06-10.95-1.48-16.26z" fill="red"/><path d="M45 24 27 14v20" fill="white"/></svg>');position:absolute;cursor:pointer;z-index:1;filter:grayscale(100%);transition:filter .1s cubic-bezier(0,0,.2,1);border:0}lite-youtube:hover>.lty-playbtn,lite-youtube .lty-playbtn:focus{filter:none}lite-youtube.lyt-activated{cursor:unset}lite-youtube.lyt-activated:before,lite-youtube.lyt-activated>.lty-playbtn{opacity:0;pointer-events:none}.lyt-visually-hidden{clip:rect(0 0 0 0);clip-path:inset(50%);height:1px;overflow:hidden;position:absolute;white-space:nowrap;width:1px}lite-youtube>iframe{all:unset!important;width:100%!important;height:100%!important;position:absolute!important;inset:0!important;border:0!important}lite-vimeo{font-size:10px;background-color:#000;position:relative;display:block;contain:content;background-position:center center;background-size:cover}lite-vimeo:after{content:"";display:block;padding-bottom:56.25%}lite-vimeo>iframe{all:unset!important;width:100%!important;height:100%!important;position:absolute!important;inset:0!important;border:0!important}lite-vimeo>.ltv-playbtn{content:"";position:absolute;inset:0;width:100%;background:transparent;outline:0;border:0;cursor:pointer}lite-vimeo>.ltv-playbtn:before{width:6.5em;height:4em;background:#172322bf;opacity:.8;border-radius:.25rem;transition:all .2s cubic-bezier(0,0,.2,1)}lite-vimeo>.ltv-playbtn:focus:before{outline:auto}lite-vimeo:hover>.ltv-playbtn:before{background-color:#00adef;background-color:var(--ltv-color, #00adef);opacity:1}lite-vimeo>.ltv-playbtn:after{border-style:solid;border-width:1em 0 1em 1.7em;border-color:transparent transparent transparent #fff}lite-vimeo>.ltv-playbtn:before,lite-vimeo>.ltv-playbtn:after{content:"";position:absolute;top:50%;left:50%;transform:translate3d(-50%,-50%,0)}lite-vimeo.ltv-activated:before,lite-vimeo.ltv-activated>.ltv-playbtn{cursor:unset;opacity:0;pointer-events:none} </style><script type="module" src="/pine-script-docs/_astro/hoisted.6418KLaP.js"></script></head> <body> <div id="search-content-blur" hidden></div> <script> /* eslint-disable @typescript-eslint/typedef */ window.ThemeProvider = (() => { function getCurrent() { return ( // eslint-disable-next-line no-restricted-syntax typeof localStorage !== 'undefined' && localStorage.getItem('tv-docs-theme') ); } const storedTheme = getCurrent(); const theme = storedTheme || (window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark'); document.documentElement.dataset.theme = theme === 'light' ? 'light' : 'dark'; document.documentElement.classList.toggle( 'theme-dark', theme === 'dark', ); // add support for ui-lib themes document.documentElement.classList.toggle( 'sl-theme-dark', theme === 'dark', ); // add support for shoelace themes return { updatePickers(themeArg = storedTheme || 'auto') { let currentTheme = themeArg; if (currentTheme === 'unknown') { currentTheme = getCurrent() || 'auto'; } document .querySelectorAll('docs-theme-select') .forEach((picker) => { const select = picker.querySelector('select'); if (select) select.value = currentTheme; /** @type {HTMLTemplateElement | null} */ const tmpl = document.querySelector(`#theme-icons`); const newIcon = tmpl && tmpl.content.querySelector('.' + currentTheme); if (newIcon) { const oldIcon = picker.querySelector('svg.label-icon'); if (oldIcon) { oldIcon.replaceChildren( ...newIcon.cloneNode(true).childNodes, ); } } }); }, }; })(); /* eslint-enable @typescript-eslint/typedef */ </script><template id="theme-icons"><svg width="16" height="16" viewBox="0 0 28 28" class="light" data-icon="theme/sun-28"> <symbol id="ai:local:theme/sun-28"><g fill="currentColor"><path d="M14 3h1.5v5H14V3Zm0 18h1.5v5H14v-5Zm12-5.5V14h-5v1.5h5ZM8 14v1.5H3V14h5Zm15.3-7-1-1-3.6 3.6 1.1 1 3.5-3.5ZM9.5 18.7l1.1 1.1-3.5 3.5-1-1 3.4-3.6ZM22 23.3l1-1-3.6-3.6-1 1.1 3.5 3.5ZM10.3 9.6l-1.1 1-3.5-3.5 1-1 3.6 3.5Z"/><path fill-rule="evenodd" d="M19 14.5a4.5 4.5 0 1 1-9 0 4.5 4.5 0 0 1 9 0Zm-1.5 0a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z" clip-rule="evenodd"/></g></symbol><use xlink:href="#ai:local:theme/sun-28"></use> </svg><svg width="16" height="16" viewBox="0 0 28 28" class="dark" data-icon="theme/moon-28"> <symbol id="ai:local:theme/moon-28"><path fill="currentColor" fill-rule="evenodd" d="M21 7.02A9.23 9.23 0 0 0 15.2 5 9.1 9.1 0 0 0 6 14c0 4.97 4.12 9 9.2 9a9.33 9.33 0 0 0 5.8-2.02A7 7 0 0 1 14.36 14 7 7 0 0 1 21 7.02Zm-3.95-.3a7.91 7.91 0 0 0-1.85-.22A7.6 7.6 0 0 0 7.5 14a7.6 7.6 0 0 0 7.7 7.5 8 8 0 0 0 1.85-.22A8.46 8.46 0 0 1 12.86 14c0-3.1 1.69-5.8 4.19-7.28Z" clip-rule="evenodd"/></symbol><use xlink:href="#ai:local:theme/moon-28"></use> </svg><svg width="16" height="16" viewBox="0 0 28 28" class="auto" data-icon="theme/system-28"> <symbol id="ai:local:theme/system-28"><path fill="currentColor" d="M8 4h1v2H8V4Zm0 9v2h1v-2H8Zm6-3V9h-2v1h2ZM3 9v1h2V9H3Zm5.5 3a2.5 2.5 0 1 0 0-5 2.5 2.5 0 0 0 0 5Zm4.2-6-.7-.7-1.4 1.4.7.7L12.7 6ZM5 13.7l-.7-.7 1.4-1.4.7.7L5 13.7Zm7.7-.7-.7.7-1.4-1.4.7-.7 1.4 1.4ZM4.3 6l.7-.7 1.4 1.4-.7.7L4.3 6Zm3 17 14-14 .8.7L8 23.7l-.7-.7Zm17.2-.1a3.5 3.5 0 0 1-3.4-5.9H21a4 4 0 1 0 3.5 5.9Z"/></symbol><use xlink:href="#ai:local:theme/system-28"></use> </svg></template> <div class="backdrop" data-mobile-menu-backdrop data-astro-cid-h2irkosh></div> <div class="menu-container" data-astro-cid-h2irkosh> <div class="header" data-astro-cid-h2irkosh> <div class="header-group" data-astro-cid-h2irkosh> <div id="version-select" data-astro-cid-kx7qoxgq> <label style="--sl-select-width: undefined; --sl-label-icon-size: 16px;" data-astro-cid-lmznfliq> <span class="sr-only" data-astro-cid-lmznfliq>Version</span> <select value="v6" data-has-border no-icon="true" data-astro-cid-lmznfliq> <option value="v6" selected="true" data-astro-cid-lmznfliq>Version 6</option><option value="v5" data-astro-cid-lmznfliq>Version 5</option><option value="v4" data-astro-cid-lmznfliq>Version 4</option><option value="v3" data-astro-cid-lmznfliq>Version 3</option> </select> <svg width="16" height="16" viewBox="0 0 24 24" class="icon caret" data-astro-cid-lmznfliq data-icon="theme/down-caret"> <symbol id="ai:local:theme/down-caret"><path fill="currentColor" d="M17 9.17a1 1 0 0 0-1.41 0L12 12.71 8.46 9.17a1 1 0 1 0-1.41 1.42l4.24 4.24a1.002 1.002 0 0 0 1.42 0L17 10.59a1.002 1.002 0 0 0 0-1.42Z"/></symbol><use xlink:href="#ai:local:theme/down-caret"></use> </svg> </label> </div> <docs-theme-select class="" data-astro-cid-3wpspbi7> <label style="--sl-select-width: 48px; --sl-label-icon-size: 28px;" data-astro-cid-lmznfliq> <span class="sr-only" data-astro-cid-lmznfliq>Theme</span> <svg width="28" height="28" viewBox="0 0 28 28" class="icon label-icon" data-button data-round data-astro-cid-lmznfliq data-icon="theme/system-28"> <use xlink:href="#ai:local:theme/system-28"></use> </svg> <select value="auto" data-button data-astro-cid-lmznfliq> <option value="dark" data-astro-cid-lmznfliq> Dark </option><option value="light" data-astro-cid-lmznfliq> Light </option><option value="auto" selected="true" data-astro-cid-lmznfliq> Auto </option> </select> </label> </docs-theme-select> <script> ThemeProvider.updatePickers('unknown'); </script> </div> <div class="header-group" data-astro-cid-h2irkosh> <div class="not-content" style="stroke-width:2px" data-astro-cid-pkzv2hgs> <button id="mobile-menu-back-button" title="Close menu" data-astro-cid-pkzv2hgs class="not-content stvb-base stvb-pointer stvb-gray stvb-medium stvb-primary stvb-icon"> <svg width="28" height="28" viewBox="0 0 28 28" data-astro-cid-h2irkosh data-icon="theme/arrow-back"> <symbol id="ai:local:theme/arrow-back"><g fill="none"><g clip-path="url(#a)"><path stroke="var(--arrow-fill-color, #131722)" d="m17 20-6-6 6-6"/></g><defs><clipPath id="a"><path fill="#fff" d="M28 28H0V0h28z"/></clipPath></defs></g></symbol><use xlink:href="#ai:local:theme/arrow-back"></use> </svg> </button> </div> </div> </div> <aside id="nav" class="keep-visible" style="--navbar-right-border-width: 0px" data-astro-cid-sa57sq6l> <div class="sidebar-viewport slick-scroll" data-astro-cid-sa57sq6l> <div class="sidebar" data-mobile data-astro-cid-sa57sq6l> <ul class="toc" aria-label="Docs sidebar" data-astro-cid-sa57sq6l> <li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/welcome" data-astro-cid-omxx3dey>Welcome to Pine Script™ v6</a></li><li class="item" data-astro-cid-omxx3dey><details data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>Pine Script™ primer</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/primer/first-steps" data-astro-cid-omxx3dey>First steps</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/primer/first-indicator" data-astro-cid-omxx3dey>First indicator</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/primer/next-steps" data-astro-cid-omxx3dey>Next steps</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><details open data-is-parent data-is-on-path data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>Language</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/execution-model" data-astro-cid-omxx3dey>Execution model</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/time-series" data-astro-cid-omxx3dey>Time series</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/script-structure" data-astro-cid-omxx3dey>Script structure</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/identifiers" data-astro-cid-omxx3dey>Identifiers</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/operators" data-astro-cid-omxx3dey>Operators</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/variable-declarations" data-astro-cid-omxx3dey>Variable declarations</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/conditional-structures" data-astro-cid-omxx3dey>Conditional structures</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/loops" data-astro-cid-omxx3dey>Loops</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/type-system" data-astro-cid-omxx3dey>Type system</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/built-ins" data-astro-cid-omxx3dey>Built-ins</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/user-defined-functions" data-astro-cid-omxx3dey>User-defined functions</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/objects" data-astro-cid-omxx3dey>Objects</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/enums" data-astro-cid-omxx3dey>Enums</a></li><li class="item" data-current data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/methods" data-astro-cid-omxx3dey>Methods</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/arrays" data-astro-cid-omxx3dey>Arrays</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/matrices" data-astro-cid-omxx3dey>Matrices</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/maps" data-astro-cid-omxx3dey>Maps</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><details data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>Concepts</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/alerts" data-astro-cid-omxx3dey>Alerts</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/backgrounds" data-astro-cid-omxx3dey>Backgrounds</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/bar-coloring" data-astro-cid-omxx3dey>Bar coloring</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/bar-plotting" data-astro-cid-omxx3dey>Bar plotting</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/bar-states" data-astro-cid-omxx3dey>Bar states</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/chart-information" data-astro-cid-omxx3dey>Chart information</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/colors" data-astro-cid-omxx3dey>Colors</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/fills" data-astro-cid-omxx3dey>Fills</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/inputs" data-astro-cid-omxx3dey>Inputs</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/levels" data-astro-cid-omxx3dey>Levels</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/libraries" data-astro-cid-omxx3dey>Libraries</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/lines-and-boxes" data-astro-cid-omxx3dey>Lines and boxes</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/non-standard-charts-data" data-astro-cid-omxx3dey>Non-standard charts data</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/other-timeframes-and-data" data-astro-cid-omxx3dey>Other timeframes and data</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/plots" data-astro-cid-omxx3dey>Plots</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/repainting" data-astro-cid-omxx3dey>Repainting</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/sessions" data-astro-cid-omxx3dey>Sessions</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/strategies" data-astro-cid-omxx3dey>Strategies</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/tables" data-astro-cid-omxx3dey>Tables</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/text-and-shapes" data-astro-cid-omxx3dey>Text and shapes</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/time" data-astro-cid-omxx3dey>Time</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/timeframes" data-astro-cid-omxx3dey>Timeframes</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><details data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>Writing scripts</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/writing/style-guide" data-astro-cid-omxx3dey>Style guide</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/writing/debugging" data-astro-cid-omxx3dey>Debugging</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/writing/profiling-and-optimization" data-astro-cid-omxx3dey>Profiling and optimization</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/writing/publishing" data-astro-cid-omxx3dey>Publishing scripts</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/writing/limitations" data-astro-cid-omxx3dey>Limitations</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><details data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>FAQ</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/general" data-astro-cid-omxx3dey>General</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/alerts" data-astro-cid-omxx3dey>Alerts</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/data-structures" data-astro-cid-omxx3dey>Data structures</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/functions" data-astro-cid-omxx3dey>Functions</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/indicators" data-astro-cid-omxx3dey>Indicators</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/other-data-and-timeframes" data-astro-cid-omxx3dey>Other data and timeframes</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/programming" data-astro-cid-omxx3dey>Programming</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/strings-and-formatting" data-astro-cid-omxx3dey>Strings and formatting</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/error-messages" data-astro-cid-omxx3dey>Error messages</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/release-notes" data-astro-cid-omxx3dey>Release notes</a></li><li class="item" data-astro-cid-omxx3dey><details data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>Migration guides</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/migration-guides/to-pine-version-6" data-astro-cid-omxx3dey>To Pine Script™ version 6</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/migration-guides/to-pine-version-5" data-astro-cid-omxx3dey>To Pine Script™ version 5</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/migration-guides/to-pine-version-4" data-astro-cid-omxx3dey>To Pine Script™ version 4</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/migration-guides/to-pine-version-3" data-astro-cid-omxx3dey>To Pine Script™ version 3</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/where-can-i-get-more-information" data-astro-cid-omxx3dey>Where can I get more information?</a></li> </ul> <div class="toc-bottom" data-astro-cid-sa57sq6l></div> </div> </div> </aside> </div> <header class="header" data-astro-cid-d74r2unp> <nav role="navigation" aria-label="Main Navigation" data-astro-cid-d74r2unp> <mobile-menu-button id="mobile-menu-button-wc" data-astro-cid-oojooh3d> <!-- Annoyingly I need to wrap this. TODO: improve this --> <div id="mobile-menu-button-header" data-astro-cid-oojooh3d> <div class="not-content" style="" data-astro-cid-pkzv2hgs> <button title="Open navigation menu" data-astro-cid-pkzv2hgs class="not-content stvb-base stvb-pointer stvb-gray stvb-medium stvb-secondary stvb-icon stvb-icon-force-color stvb-force-no-border"> <svg width="28" height="28" viewBox="0 0 24 24" data-astro-cid-oojooh3d data-icon="theme/bars"> <symbol id="ai:local:theme/bars"><path fill="currentColor" d="M3 8h18a1 1 0 1 0 0-2H3a1 1 0 0 0 0 2Zm18 8H3a1 1 0 0 0 0 2h18a1 1 0 0 0 0-2Zm0-5H3a1 1 0 0 0 0 2h18a1 1 0 0 0 0-2Z"/></symbol><use xlink:href="#ai:local:theme/bars"></use> </svg> </button> </div> </div> </mobile-menu-button> <div data-hide-when-search data-astro-cid-d74r2unp> <div class="header-logo" data-astro-cid-tycb33lk> <a href="/pine-script-docs/" aria-label="Home button" data-astro-cid-tycb33lk> <div class="documentation-logo logo" data-astro-cid-tycb33lk><svg width="188" height="44" viewBox="0 0 188 44" fill="none" xmlns="http://www.w3.org/2000/svg"> <path fill-rule="evenodd" clip-rule="evenodd" d="M26.9989 28.0001L18.4281 20.7531L11.3382 23.2611C10.402 23.5923 9.60771 22.5041 10.2085 21.7134L21.0877 7.39502C21.4749 6.88536 22.2346 6.86567 22.6478 7.35459L34.7627 21.6923C35.1531 22.1543 35.0472 22.8544 34.5376 23.1802L26.9989 28.0001ZM32.4978 22.1105L27.1641 25.5207L19.7195 19.2258C19.1767 18.7669 18.4312 18.6305 17.7611 18.8676L13.8245 20.2602L21.9253 9.59841L32.4978 22.1105Z" fill="currentColor"/> <path d="M33.432 28.0842L38.3592 34.7471C38.7427 35.2657 38.3724 36 37.7274 36H6.27248C5.62746 36 5.25722 35.2657 5.64074 34.7471L10.8523 27.6996C10.9484 27.5697 11.0822 27.4725 11.2356 27.4214L17.6878 25.2707C17.9285 25.1904 18.1932 25.2314 18.3984 25.3806L26.2854 31.1166C26.5445 31.3051 26.8922 31.3173 27.1639 31.1475L32.3838 27.885C32.7316 27.6677 33.1881 27.7544 33.432 28.0842Z" fill="currentColor"/> <path d="M59.56 13.96C63.016 13.96 65.536 16.456 65.536 19.696C65.536 22.936 63.016 25.432 59.56 25.432H57.376V31H53.896V13.96H59.56ZM59.512 22.168C61.096 22.168 62.152 21.088 62.152 19.696C62.152 18.304 61.096 17.224 59.512 17.224H57.376V22.168H59.512ZM70.8636 14.944C70.8636 16.096 69.9276 17.032 68.7516 17.032C67.6236 17.032 66.6636 16.096 66.6636 14.944C66.6636 13.792 67.6236 12.856 68.7516 12.856C69.9276 12.856 70.8636 13.792 70.8636 14.944ZM67.1196 31V19H70.4076V31H67.1196ZM73.0849 31V19H76.3729V20.368C76.9969 19.408 78.2449 18.736 79.8049 18.736C82.7089 18.736 84.4369 20.848 84.4369 23.968V31H81.1489V24.52C81.1489 22.792 80.4049 21.736 79.0369 21.736C77.5249 21.736 76.3729 22.84 76.3729 25.144V31H73.0849ZM86.2041 25C86.2041 21.448 88.8681 18.736 92.5161 18.736C95.6601 18.736 98.4201 20.752 98.4201 24.64C98.4201 24.928 98.4201 25.264 98.3721 25.768H89.3961C89.5881 27.376 91.0041 28.264 92.5881 28.264C94.0761 28.264 95.1561 27.568 95.6841 26.752L98.1321 28.576C97.0281 30.184 95.0841 31.264 92.5641 31.264C88.9881 31.264 86.2041 28.792 86.2041 25ZM92.4201 21.448C91.1961 21.448 89.8281 22.072 89.5161 23.536H95.1321C94.8441 22.12 93.6441 21.448 92.4201 21.448ZM104.412 28.24L107.076 26.008C107.916 27.28 109.38 28.024 110.772 28.024C112.14 28.024 113.052 27.352 113.052 26.368C113.052 25.408 112.356 24.736 110.676 24.16L109.236 23.656C106.5 22.696 105.036 21.112 105.036 18.784C105.036 15.568 107.484 13.672 110.796 13.672C112.884 13.672 114.708 14.392 116.172 16.024L113.868 18.4C113.052 17.416 111.996 16.936 110.82 16.936C109.644 16.936 108.54 17.44 108.54 18.448C108.54 19.48 109.38 19.936 111.18 20.584L112.548 21.088C115.044 22 116.604 23.584 116.604 26.152C116.58 29.2 114.156 31.288 110.628 31.288C107.916 31.288 105.636 30.112 104.412 28.24ZM129.859 27.808C128.827 29.872 126.667 31.264 124.171 31.264C120.619 31.264 117.811 28.624 117.811 25C117.811 21.376 120.619 18.736 124.171 18.736C126.667 18.736 128.827 20.128 129.859 22.192L127.003 23.728C126.523 22.624 125.491 21.808 124.171 21.808C122.443 21.808 121.123 23.152 121.123 25C121.123 26.848 122.443 28.192 124.171 28.192C125.491 28.192 126.523 27.376 127.003 26.272L129.859 27.808ZM131.482 31V19H134.77V20.92C135.178 19.744 136.402 18.856 137.746 18.856C138.082 18.856 138.442 18.88 138.85 19V22.336C138.346 22.168 137.89 22.072 137.362 22.072C135.802 22.072 134.77 23.296 134.77 25.264V31H131.482ZM144.215 14.944C144.215 16.096 143.279 17.032 142.103 17.032C140.975 17.032 140.015 16.096 140.015 14.944C140.015 13.792 140.975 12.856 142.103 12.856C143.279 12.856 144.215 13.792 144.215 14.944ZM140.471 31V19H143.759V31H140.471ZM146.436 36.016V19H149.724V20.344C150.276 19.576 151.548 18.736 153.204 18.736C156.396 18.736 158.844 21.592 158.844 25C158.844 28.408 156.396 31.264 153.204 31.264C151.548 31.264 150.276 30.424 149.724 29.656V36.016H146.436ZM155.508 25C155.508 23.128 154.284 21.736 152.484 21.736C150.684 21.736 149.46 23.128 149.46 25C149.46 26.872 150.684 28.264 152.484 28.264C154.284 28.264 155.508 26.872 155.508 25ZM168.407 30.88C167.879 31.048 167.231 31.144 166.319 31.144C163.775 31.144 161.735 29.728 161.735 26.8V21.88H159.311V19H161.735V15.664H165.023V19H168.407V21.88H165.023V26.152C165.023 27.616 165.647 28.192 167.063 28.192C167.591 28.192 168.023 28.12 168.407 27.976V30.88ZM177.626 22.648V13.96H179.978L181.874 16.888L183.746 13.96H186.122V22.648H183.698V18.184L181.874 20.968L180.026 18.136V22.648H177.626ZM171.386 22.648V16.456H169.058V13.96H176.258V16.456H173.93V22.648H171.386Z" fill="currentColor"/> </svg> </div> </a> </div> </div> <div class="flex" data-astro-cid-d74r2unp></div> <div class="flex" data-astro-cid-d74r2unp> <div class="search-container" data-astro-cid-fg37foga> <div class="search-wrapper" data-astro-cid-fg37foga> <input class="search-input" type="text" placeholder="Search docs" name="s" value="" data-astro-cid-fg37foga> <svg width="28" height="28" viewBox="0 0 28 28" class="search-button" data-astro-cid-fg37foga data-icon="theme/search"> <symbol id="ai:local:theme/search"><path fill="currentColor" fill-rule="evenodd" d="M18.5 12.5a6 6 0 1 1-12 0 6 6 0 0 1 12 0Zm-1.25 5.8a7.5 7.5 0 1 1 1.06-1.06l4.22 4.23.53.53L22 23.06l-.53-.53-4.22-4.22Z" clip-rule="evenodd"/></symbol><use xlink:href="#ai:local:theme/search"></use> </svg> <button class="search-clear" type="button" title="Reset" data-search-clear data-astro-cid-fg37foga>Clear</button> <span class="divider" data-astro-cid-fg37foga></span> <button class="search-close" type="button" title="Close" data-search-close data-astro-cid-fg37foga> <svg width="18" height="18" viewBox="0 0 18 18" data-astro-cid-fg37foga data-icon="theme/cross-18"> <use xlink:href="#ai:local:theme/cross-18"></use> </svg> </button> </div> <div id="search-results-wrapper" data-astro-cid-fg37foga> <aside id="search-results" hidden data-astro-cid-fg37foga> <!-- Don't use h1 because when built it will be used as the first heading on the page --> <div class="heading" data-astro-cid-fg37foga>Search results</div> <div id="search-results-contents" data-astro-cid-fg37foga></div> </aside> </div> </div> </div> <ul class="links" data-astro-cid-d74r2unp> </ul> <div class="flex" data-astro-cid-d74r2unp></div> <div data-hide-when-search data-astro-cid-d74r2unp> <div id="version-select" data-hideable="true" data-astro-cid-kx7qoxgq> <label style="--sl-select-width: undefined; --sl-label-icon-size: 16px;" data-astro-cid-lmznfliq> <span class="sr-only" data-astro-cid-lmznfliq>Version</span> <select value="v6" data-has-border no-icon="true" data-astro-cid-lmznfliq> <option value="v6" selected="true" data-astro-cid-lmznfliq>Version 6</option><option value="v5" data-astro-cid-lmznfliq>Version 5</option><option value="v4" data-astro-cid-lmznfliq>Version 4</option><option value="v3" data-astro-cid-lmznfliq>Version 3</option> </select> <svg width="16" height="16" viewBox="0 0 24 24" class="icon caret" data-astro-cid-lmznfliq data-icon="theme/down-caret"> <use xlink:href="#ai:local:theme/down-caret"></use> </svg> </label> </div> </div> <button id="search-button" data-astro-cid-6zeqadij> <svg width="28" height="28" viewBox="0 0 28 28" data-astro-cid-6zeqadij data-icon="theme/search"> <use xlink:href="#ai:local:theme/search"></use> </svg> </button> <script data-base-url="/pine-script-docs"> async function loadPageFind() { const base = document.currentScript.getAttribute('data-base-url'); const pageFindBundleUrl = `${base}/pagefind/`; const pagefind = await import(`${pageFindBundleUrl}pagefind.js`); await pagefind.options({ bundlePath: pageFindBundleUrl, }); window.pagefind = pagefind; } loadPageFind().catch(); </script> <docs-theme-select class="hide-with-breakpoint-568" data-astro-cid-3wpspbi7> <label style="--sl-select-width: 48px; --sl-label-icon-size: 28px;" data-astro-cid-lmznfliq> <span class="sr-only" data-astro-cid-lmznfliq>Theme</span> <svg width="28" height="28" viewBox="0 0 28 28" class="icon label-icon" data-button data-round data-astro-cid-lmznfliq data-icon="theme/system-28"> <use xlink:href="#ai:local:theme/system-28"></use> </svg> <select value="auto" data-button data-astro-cid-lmznfliq> <option value="dark" data-astro-cid-lmznfliq> Dark </option><option value="light" data-astro-cid-lmznfliq> Light </option><option value="auto" selected="true" data-astro-cid-lmznfliq> Auto </option> </select> </label> </docs-theme-select> <script> ThemeProvider.updatePickers('unknown'); </script> </nav> </header> <div id="image-lightbox" class="not-content" hidden data-astro-cid-kws7taxh> <div class="button-wrapper" data-astro-cid-kws7taxh> <div class="not-content" style="" data-astro-cid-pkzv2hgs> <button id="lightbox-close-button" title="Close image preview" data-astro-cid-pkzv2hgs class="not-content stvb-base stvb-pointer stvb-black stvb-medium stvb-secondary stvb-icon stvb-force-no-border"> <svg width="24" height="24" viewBox="0 0 18 18" data-astro-cid-kws7taxh data-icon="theme/cross-18"> <symbol id="ai:local:theme/cross-18"><g fill="none"><g clip-path="url(#a)"><path fill="currentColor" fill-rule="evenodd" d="M5.53 4.47 4.47 5.53 7.94 9l-3.47 3.47 1.06 1.06L9 10.06l3.47 3.47 1.06-1.06L10.06 9l3.47-3.47-1.06-1.06L9 7.94 5.53 4.47Z" clip-rule="evenodd"/></g><defs><clipPath id="a"><path fill="#fff" d="M0 0h18v18H0z"/></clipPath></defs></g></symbol><use xlink:href="#ai:local:theme/cross-18"></use> </svg> </button> </div> </div> <img id="lightbox-image" src="" data-astro-cid-kws7taxh> </div> <div id="page-container" data-astro-cid-xgirumru> <aside id="nav" class="" style="" data-astro-cid-sa57sq6l> <div class="sidebar-viewport slick-scroll" data-astro-cid-sa57sq6l> <div class="sidebar" data-astro-cid-sa57sq6l> <ul class="toc" aria-label="Docs sidebar" data-astro-cid-sa57sq6l> <li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/welcome" data-astro-cid-omxx3dey>Welcome to Pine Script™ v6</a></li><li class="item" data-astro-cid-omxx3dey><details data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>Pine Script™ primer</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <symbol id="ai:local:theme/right-caret"><path fill="currentColor" d="m14.83 11.29-4.24-4.24a1 1 0 1 0-1.42 1.41L12.71 12l-3.54 3.54a1 1 0 0 0 0 1.41 1 1 0 0 0 .71.29 1 1 0 0 0 .71-.29l4.24-4.24a1.002 1.002 0 0 0 0-1.42Z"/></symbol><use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/primer/first-steps" data-astro-cid-omxx3dey>First steps</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/primer/first-indicator" data-astro-cid-omxx3dey>First indicator</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/primer/next-steps" data-astro-cid-omxx3dey>Next steps</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><details open data-is-parent data-is-on-path data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>Language</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/execution-model" data-astro-cid-omxx3dey>Execution model</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/time-series" data-astro-cid-omxx3dey>Time series</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/script-structure" data-astro-cid-omxx3dey>Script structure</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/identifiers" data-astro-cid-omxx3dey>Identifiers</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/operators" data-astro-cid-omxx3dey>Operators</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/variable-declarations" data-astro-cid-omxx3dey>Variable declarations</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/conditional-structures" data-astro-cid-omxx3dey>Conditional structures</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/loops" data-astro-cid-omxx3dey>Loops</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/type-system" data-astro-cid-omxx3dey>Type system</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/built-ins" data-astro-cid-omxx3dey>Built-ins</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/user-defined-functions" data-astro-cid-omxx3dey>User-defined functions</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/objects" data-astro-cid-omxx3dey>Objects</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/enums" data-astro-cid-omxx3dey>Enums</a></li><li class="item" data-current data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/methods" data-astro-cid-omxx3dey>Methods</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/arrays" data-astro-cid-omxx3dey>Arrays</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/matrices" data-astro-cid-omxx3dey>Matrices</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/maps" data-astro-cid-omxx3dey>Maps</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><details data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>Concepts</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/alerts" data-astro-cid-omxx3dey>Alerts</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/backgrounds" data-astro-cid-omxx3dey>Backgrounds</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/bar-coloring" data-astro-cid-omxx3dey>Bar coloring</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/bar-plotting" data-astro-cid-omxx3dey>Bar plotting</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/bar-states" data-astro-cid-omxx3dey>Bar states</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/chart-information" data-astro-cid-omxx3dey>Chart information</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/colors" data-astro-cid-omxx3dey>Colors</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/fills" data-astro-cid-omxx3dey>Fills</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/inputs" data-astro-cid-omxx3dey>Inputs</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/levels" data-astro-cid-omxx3dey>Levels</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/libraries" data-astro-cid-omxx3dey>Libraries</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/lines-and-boxes" data-astro-cid-omxx3dey>Lines and boxes</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/non-standard-charts-data" data-astro-cid-omxx3dey>Non-standard charts data</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/other-timeframes-and-data" data-astro-cid-omxx3dey>Other timeframes and data</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/plots" data-astro-cid-omxx3dey>Plots</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/repainting" data-astro-cid-omxx3dey>Repainting</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/sessions" data-astro-cid-omxx3dey>Sessions</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/strategies" data-astro-cid-omxx3dey>Strategies</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/tables" data-astro-cid-omxx3dey>Tables</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/text-and-shapes" data-astro-cid-omxx3dey>Text and shapes</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/time" data-astro-cid-omxx3dey>Time</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/timeframes" data-astro-cid-omxx3dey>Timeframes</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><details data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>Writing scripts</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/writing/style-guide" data-astro-cid-omxx3dey>Style guide</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/writing/debugging" data-astro-cid-omxx3dey>Debugging</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/writing/profiling-and-optimization" data-astro-cid-omxx3dey>Profiling and optimization</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/writing/publishing" data-astro-cid-omxx3dey>Publishing scripts</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/writing/limitations" data-astro-cid-omxx3dey>Limitations</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><details data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>FAQ</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/general" data-astro-cid-omxx3dey>General</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/alerts" data-astro-cid-omxx3dey>Alerts</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/data-structures" data-astro-cid-omxx3dey>Data structures</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/functions" data-astro-cid-omxx3dey>Functions</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/indicators" data-astro-cid-omxx3dey>Indicators</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/other-data-and-timeframes" data-astro-cid-omxx3dey>Other data and timeframes</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/programming" data-astro-cid-omxx3dey>Programming</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/strings-and-formatting" data-astro-cid-omxx3dey>Strings and formatting</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/error-messages" data-astro-cid-omxx3dey>Error messages</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/release-notes" data-astro-cid-omxx3dey>Release notes</a></li><li class="item" data-astro-cid-omxx3dey><details data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>Migration guides</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/migration-guides/to-pine-version-6" data-astro-cid-omxx3dey>To Pine Script™ version 6</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/migration-guides/to-pine-version-5" data-astro-cid-omxx3dey>To Pine Script™ version 5</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/migration-guides/to-pine-version-4" data-astro-cid-omxx3dey>To Pine Script™ version 4</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/migration-guides/to-pine-version-3" data-astro-cid-omxx3dey>To Pine Script™ version 3</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/where-can-i-get-more-information" data-astro-cid-omxx3dey>Where can I get more information?</a></li> </ul> <div class="toc-bottom" data-astro-cid-sa57sq6l></div> </div> </div> </aside> <main class="main-pane" data-page-type="md" data-astro-cid-xgirumru> <a id="top" data-astro-cid-xgirumru></a> <main class="content" data-toc-shown data-has-labels-above data-astro-cid-ju3wuhkz> <div class="content-width" data-astro-cid-xgirumru> <div class="breadcrumbs" data-pagefind-ignore data-astro-cid-wlavna2o> <a href="/pine-script-docs" aria-label="Return back to the documentation home page." data-astro-cid-wlavna2o> <span data-astro-cid-wlavna2o>User Manual</span> </a> <span class="divider" data-astro-cid-wlavna2o>/</span> <a href="/pine-script-docs/language/execution-model" data-astro-cid-wlavna2o>Language</a> <span class="divider" data-astro-cid-wlavna2o>/</span> <span class="current-item" data-astro-cid-wlavna2o>Methods</span> </div> <div id="article-labels" class="not-content" data-astro-cid-i7sip6yg><div style="background-color: #651FFF" data-astro-cid-i7sip6yg>ADVANCED</div></div> </div> <div id="slot-container" data-has-labels data-astro-cid-xgirumru> <h1 id="methods" class="md-heading"><a href="#methods">Methods<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h1> <aside aria-label="Notice!" class="tv-informer not-content" style="--informer-header-color:#FF9800;--informer-back-color-light:#FFF3E0;--informer-back-color-dark:#33261A"><div class="tv-informer-icon"><svg viewBox="0 0 18 18" width="18" height="18"><path fill-rule="evenodd" d="M9 17A8 8 0 1 0 9 1a8 8 0 0 0 0 16ZM9 4c-.79 0-1.38.7-1.25 1.48l.67 4.03a.59.59 0 0 0 1.16 0l.67-4.03A1.27 1.27 0 0 0 9 4Zm0 8a1 1 0 1 0 0 2 1 1 0 0 0 0-2Z" fill="currentColor"></path></svg></div><div class="tv-informer-content"><p><span class="tv-informer-header">Notice!</span>This page contains advanced material. If you are a beginning Pine Script™ programmer, we recommend you become familiar with other, more accessible Pine Script™ features before you venture here.</p></div></aside> <h2 id="introduction" class="md-heading"><a href="#introduction">Introduction<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h2> <p>Pine Script™ methods are specialized functions associated with values of specific <a href="/pine-script-docs/language/type-system/#types">built-in types</a>, <a href="/pine-script-docs/language/type-system/#user-defined-types">user-defined types</a>, or <a href="/pine-script-docs/language/type-system/#enum-types">enum types</a>. They behave the same as regular functions in most regards while offering a shorter, more convenient syntax. Users can access methods using <em>dot notation</em> syntax on variables of the associated type, similar to accessing the fields of a Pine Script™ <a href="/pine-script-docs/language/objects/">object</a>.</p> <h2 id="built-in-methods" class="md-heading"><a href="#built-in-methods">Built-in methods<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h2> <p>Pine Script™ includes built-in methods for all <em>special types</em>, including <a href="https://www.tradingview.com/pine-script-reference/v6/#type_array">array</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#type_matrix">matrix</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#type_map">map</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#type_line">line</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#type_linefill">linefill</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#type_box">box</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#type_polyline">polyline</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#type_label">label</a>, and <a href="https://www.tradingview.com/pine-script-reference/v6/#type_table">table</a>. These methods provide users with a more concise way to call specialized routines for these types within their scripts.</p> <p>When using these special types, the expressions:</p> <div class="expressive-code not-content"><link rel="stylesheet" href="/pine-script-docs/_astro/ec.dtidy.css"/><script type="module" src="/pine-script-docs/_astro/ec.qopfj.js"></script><figure class="frame"><figcaption class="header"></figcaption><pre data-language="text"><code><div class="ec-line"><div class="code"><span style="--0:#e1e4e8;--1:#24292e"><namespace>.<functionName>([paramName =] <objectName>, …)</span></div></div></code></pre></figure></div> <p>and:</p> <div class="expressive-code not-content"><figure class="frame"><figcaption class="header"></figcaption><pre data-language="text"><code><div class="ec-line"><div class="code"><span style="--0:#e1e4e8;--1:#24292e"><objectName>.<functionName>(…)</span></div></div></code></pre></figure></div> <p>are equivalent. For example, rather than using:</p> <div class="pine-colorizer not-content">array.get(id, index)</div> <p>to get the value from an array <code dir="auto">id</code> at the specified <code dir="auto">index</code>, we can simply use:</p> <div class="pine-colorizer not-content">id.get(index)</div> <p>to achieve the same effect. This notation eliminates the need for users to reference the function’s namespace, as <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_array.get">get()</a> is a method of <code dir="auto">id</code> in this context.</p> <p>Written below is a practical example to demonstrate the usage of built-in methods in place of functions.</p> <p>The following script computes Bollinger Bands over a specified number of prices sampled once every <code dir="auto">n</code> bars. It calls <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_array.push">array.push()</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_array.shift">array.shift()</a> to queue <code dir="auto">sourceInput</code> values through the <code dir="auto">sourceArray</code>, then <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_array.avg">array.avg()</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_array.stdev">array.stdev()</a> to compute the <code dir="auto">sampleMean</code> and <code dir="auto">sampleDev</code>. The script then uses these values to calculate the <code dir="auto">highBand</code> and <code dir="auto">lowBand</code>, which it plots on the chart along with the <code dir="auto">sampleMean</code>:</p> <p><img src="/pine-script-docs/_astro/Methods_custom_bb.DR1SlFGg_2tQfSW.webp" alt="image" width="1814" height="844" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator("Custom Sample BB", overlay = true) float sourceInput = input.source(close, "Source") int samplesInput = input.int(20, "Samples") int n = input.int(10, "Bars") float multiplier = input.float(2.0, "StdDev") var array<float> sourceArray = array.new<float>(samplesInput) var float sampleMean = na var float sampleDev = na // Identify if `n` bars have passed. if bar_index % n == 0 // Update the queue. array.push(sourceArray, sourceInput) array.shift(sourceArray) // Update the mean and standard deviaiton values. sampleMean := array.avg(sourceArray) sampleDev := array.stdev(sourceArray) * multiplier // Calculate bands. float highBand = sampleMean + sampleDev float lowBand = sampleMean - sampleDev plot(sampleMean, "Basis", color.orange) plot(highBand, "Upper", color.lime) plot(lowBand, "Lower", color.red)</div> <p>Let’s rewrite this code to utilize methods rather than built-in functions. In this version, we have replaced all built-in <a href="https://www.tradingview.com/pine-script-reference/v6/#type_array">array.*</a> functions in the script with equivalent methods:</p> <div class="pine-colorizer not-content">//@version=6 indicator("Custom Sample BB", overlay = true) float sourceInput = input.source(close, "Source") int samplesInput = input.int(20, "Samples") int n = input.int(10, "Bars") float multiplier = input.float(2.0, "StdDev") var array<float> sourceArray = array.new<float>(samplesInput) var float sampleMean = na var float sampleDev = na // Identify if `n` bars have passed. if bar_index % n == 0 // Update the queue. sourceArray.push(sourceInput) sourceArray.shift() // Update the mean and standard deviaiton values. sampleMean := sourceArray.avg() sampleDev := sourceArray.stdev() * multiplier // Calculate band values. float highBand = sampleMean + sampleDev float lowBand = sampleMean - sampleDev plot(sampleMean, "Basis", color.orange) plot(highBand, "Upper", color.lime) plot(lowBand, "Lower", color.red)</div> <p>Note that:</p> <ul> <li>We call the array methods using <code dir="auto">sourceArray.*</code> rather than referencing the <a href="https://www.tradingview.com/pine-script-reference/v6/#type_array">array</a> namespace.</li> <li>We do not include <code dir="auto">sourceArray</code> as a parameter when we call the methods since they already reference the object.</li> </ul> <h2 id="user-defined-methods" class="md-heading"><a href="#user-defined-methods">User-defined methods<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h2> <p>Pine Script™ allows users to define custom methods for use with objects of any built-in or user-defined type. Defining a method is essentially the same as defining a function, but with two key differences:</p> <ul> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#kw_method">method</a> keyword must be included before the function name.</li> <li>The type of the first parameter in the signature must be explicitly declared, as it represents the type of object that the method will be associated with.</li> </ul> <div class="expressive-code not-content"><figure class="frame"><figcaption class="header"></figcaption><pre data-language="text"><code><div class="ec-line"><div class="code"><span style="--0:#e1e4e8;--1:#24292e">[export] method <functionName>(<paramType> <paramName> [= <defaultValue>], …) =></span></div></div><div class="ec-line"><div class="code"><span class="indent"><span style="--0:#e1e4e8;--1:#24292e"> </span></span><span style="--0:#e1e4e8;--1:#24292e"><functionBlock></span></div></div></code></pre></figure></div> <p>Let’s apply user-defined methods to our previous Bollinger Bands example to encapsulate operations from the global scope, which will simplify the code and promote reusability. See this portion from the example:</p> <div class="pine-colorizer not-content">// Identify if `n` bars have passed. if bar_index % n == 0 // Update the queue. sourceArray.push(sourceInput) sourceArray.shift() // Update the mean and standard deviaiton values. sampleMean := sourceArray.avg() sampleDev := sourceArray.stdev() * multiplier // Calculate band values. float highBand = sampleMean + sampleDev float lowBand = sampleMean - sampleDev</div> <p>We will start by defining a simple method to queue values through an array in a single call.</p> <p>This <code dir="auto">maintainQueue()</code> method invokes the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_array.push">push()</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_array.shift">shift()</a> methods on a <code dir="auto">srcArray</code> when <code dir="auto">takeSample</code> is true and returns the object:</p> <div class="pine-colorizer not-content">// @function Maintains a queue of the size of `srcArray`. // It appends a `value` to the array and removes its oldest element at position zero. // @param srcArray (array<float>) The array where the queue is maintained. // @param value (float) The new value to be added to the queue. // The queue's oldest value is also removed, so its size is constant. // @param takeSample (bool) A new `value` is only pushed into the queue if this is true. // @returns (array<float>) `srcArray` object. method maintainQueue(array<float> srcArray, float value, bool takeSample = true) => if takeSample srcArray.push(value) srcArray.shift() srcArray</div> <p>Note that:</p> <ul> <li>Just as with user-defined functions, we use the <code dir="auto">@function</code> <a href="/pine-script-docs/language/script-structure/#compiler-annotations">compiler annotation</a> to document method descriptions.</li> </ul> <p>Now we can replace <code dir="auto">sourceArray.push()</code> and <code dir="auto">sourceArray.shift()</code> with <code dir="auto">sourceArray.maintainQueue()</code> in our example:</p> <div class="pine-colorizer not-content">// Identify if `n` bars have passed. if bar_index % n == 0 // Update the queue. sourceArray.maintainQueue(sourceInput) // Update the mean and standard deviaiton values. sampleMean := sourceArray.avg() sampleDev := sourceArray.stdev() * multiplier // Calculate band values. float highBand = sampleMean + sampleDev float lowBand = sampleMean - sampleDev</div> <p>From here, we will further simplify our code by defining a method that handles all Bollinger Band calculations within its scope.</p> <p>This <code dir="auto">calcBB()</code> method invokes the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_array.avg">avg()</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_array.stdev">stdev()</a> methods on a <code dir="auto">srcArray</code> to update <code dir="auto">mean</code> and <code dir="auto">dev</code> values when <code dir="auto">calculate</code> is true. The method uses these values to return a tuple containing the basis, upper band, and lower band values respectively:</p> <div class="pine-colorizer not-content">// @function Computes Bollinger Band values from an array of data. // @param srcArray (array<float>) The array where the queue is maintained. // @param multiplier (float) Standard deviaiton multiplier. // @param calcuate (bool) The method will only calculate new values when this is true. // @returns A tuple containing the basis, upper band, and lower band respectively. method calcBB(array<float> srcArray, float mult, bool calculate = true) => var float mean = na var float dev = na if calculate // Compute the mean and standard deviation of the array. mean := srcArray.avg() dev := srcArray.stdev() * mult [mean, mean + dev, mean - dev]</div> <p>With this method, we can now remove Bollinger Band calculations from the global scope and improve code readability:</p> <div class="pine-colorizer not-content">// Identify if `n` bars have passed. bool newSample = bar_index % n == 0 // Update the queue and compute new BB values on each new sample. [sampleMean, highBand, lowBand] = sourceArray.maintainQueue(sourceInput, newSample).calcBB(multiplier, newSample)</div> <p>Note that:</p> <ul> <li>Rather than using an <code dir="auto">if</code> block in the global scope, we have defined a <code dir="auto">newSample</code> variable that is only true once every <code dir="auto">n</code> bars. The <code dir="auto">maintainQueue()</code> and <code dir="auto">calcBB()</code> methods use this value for their respective <code dir="auto">takeSample</code> and <code dir="auto">calculate</code> parameters.</li> <li>Since the <code dir="auto">maintainQueue()</code> method returns the object that it references, we’re able to call <code dir="auto">calcBB()</code> from the same line of code, as both methods apply to <code dir="auto">array<float></code> instances.</li> </ul> <p>Here is how the full script example looks now that we’ve applied our user-defined methods:</p> <div class="pine-colorizer not-content">//@version=6 indicator("Custom Sample BB", overlay = true) float sourceInput = input.source(close, "Source") int samplesInput = input.int(20, "Samples") int n = input.int(10, "Bars") float multiplier = input.float(2.0, "StdDev") var array<float> sourceArray = array.new<float>(samplesInput) // @function Maintains a queue of the size of `srcArray`. // It appends a `value` to the array and removes its oldest element at position zero. // @param srcArray (array<float>) The array where the queue is maintained. // @param value (float) The new value to be added to the queue. // The queue's oldest value is also removed, so its size is constant. // @param takeSample (bool) A new `value` is only pushed into the queue if this is true. // @returns (array<float>) `srcArray` object. method maintainQueue(array<float> srcArray, float value, bool takeSample = true) => if takeSample srcArray.push(value) srcArray.shift() srcArray // @function Computes Bollinger Band values from an array of data. // @param srcArray (array<float>) The array where the queue is maintained. // @param multiplier (float) Standard deviaiton multiplier. // @param calcuate (bool) The method will only calculate new values when this is true. // @returns A tuple containing the basis, upper band, and lower band respectively. method calcBB(array<float> srcArray, float mult, bool calculate = true) => var float mean = na var float dev = na if calculate // Compute the mean and standard deviation of the array. mean := srcArray.avg() dev := srcArray.stdev() * mult [mean, mean + dev, mean - dev] // Identify if `n` bars have passed. bool newSample = bar_index % n == 0 // Update the queue and compute new BB values on each new sample. [sampleMean, highBand, lowBand] = sourceArray.maintainQueue(sourceInput, newSample).calcBB(multiplier, newSample) plot(sampleMean, "Basis", color.orange) plot(highBand, "Upper", color.lime) plot(lowBand, "Lower", color.red)</div> <h2 id="method-overloading" class="md-heading"><a href="#method-overloading">Method overloading<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h2> <p>User-defined methods can override and overload existing built-in and user-defined methods with the same identifier. This capability allows users to define multiple routines associated with different parameter signatures under the same method name.</p> <p>As a simple example, suppose we want to define a method to identify a variable’s type. Since we must explicitly specify the type of object associated with a user-defined method, we will need to define overloads for each type that we want it to recognize.</p> <p>Below, we have defined a <code dir="auto">getType()</code> method that returns a string representation of a variable’s type with overloads for the five primitive types:</p> <div class="pine-colorizer not-content">// @function Identifies an object's type. // @param this Object to inspect. // @returns (string) A string representation of the type. method getType(int this) => na(this) ? "int(na)" : "int" method getType(float this) => na(this) ? "float(na)" : "float" method getType(bool this) => // "bool" values only have two states, `true` and `false`, but never `na`. "bool" method getType(color this) => na(this) ? "color(na)" : "color" method getType(string this) => na(this) ? "string(na)" : "string"</div> <p>Now we can use these overloads to inspect some variables. This script uses <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format">str.format()</a> to format the results from calling the <code dir="auto">getType()</code> method on five different variables into a single <code dir="auto">results</code> string, then displays the string in the <code dir="auto">lbl</code> label using the built-in <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_label.set_text">set_text()</a> method:</p> <p><img src="/pine-script-docs/_astro/Methods_overloads_type_inspection.BxAK0zpG_2p2qvn.webp" alt="image" width="1210" height="328" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator("Type Inspection") // @function Identifies an object's type. // @param this Object to inspect. // @returns (string) A string representation of the type. method getType(int this) => na(this) ? "int(na)" : "int" method getType(float this) => na(this) ? "float(na)" : "float" method getType(bool this) => na(this) ? "bool(na)" : "bool" method getType(color this) => na(this) ? "color(na)" : "color" method getType(string this) => na(this) ? "string(na)" : "string" a = 1 b = 1.0 c = true d = color.white e = "1" // Inspect variables and format results. results = str.format( "a: {0}\nb: {1}\nc: {2}\nd: {3}\ne: {4}", a.getType(), b.getType(), c.getType(), d.getType(), e.getType() ) var label lbl = label.new(0, 0) lbl.set_x(bar_index) lbl.set_text(results)</div> <p>Note that:</p> <ul> <li>The underlying type of each variable determines which overload of <code dir="auto">getType()</code> the compiler will use.</li> <li>The method will append “(na)” to the output string when a variable is <code dir="auto">na</code> to demarcate that it is empty.</li> </ul> <h2 id="advanced-example" class="md-heading"><a href="#advanced-example">Advanced example<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h2> <p>Let’s apply what we’ve learned to construct a script that estimates the cumulative distribution of elements in an array, meaning the fraction of elements in the array that are less than or equal to any given value.</p> <p>There are many ways in which we could choose to tackle this objective. For this example, we will start by defining a method to replace elements of an array, which will help us count the occurrences of elements within a range of values.</p> <p>Written below is an overload of the built-in <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_array.fill">fill()</a> method for <code dir="auto">array<float></code> instances. This overload replaces elements in a <code dir="auto">srcArray</code> within the range between the <code dir="auto">lowerBound</code> and <code dir="auto">upperBound</code> with an <code dir="auto">innerValue</code>, and replaces all elements outside the range with an <code dir="auto">outerValue</code>:</p> <div class="pine-colorizer not-content">// @function Replaces elements in a `srcArray` between `lowerBound` and `upperBound` with an `innerValue`, // and replaces elements outside the range with an `outerValue`. // @param srcArray (array<float>) Array to modify. // @param innerValue (float) Value to replace elements within the range with. // @param outerValue (float) Value to replace elements outside the range with. // @param lowerBound (float) Lowest value to replace with `innerValue`. // @param upperBound (float) Highest value to replace with `innerValue`. // @returns (array<float>) `srcArray` object. method fill(array<float> srcArray, float innerValue, float outerValue, float lowerBound, float upperBound) => for [i, element] in srcArray if (element >= lowerBound or na(lowerBound)) and (element <= upperBound or na(upperBound)) srcArray.set(i, innerValue) else srcArray.set(i, outerValue) srcArray</div> <p>With this method, we can filter an array by value ranges to produce an array of occurrences. For example, the expression:</p> <div class="pine-colorizer not-content">srcArray.copy().fill(1.0, 0.0, min, val)</div> <p>copies the <code dir="auto">srcArray</code> object, replaces all elements between <code dir="auto">min</code> and <code dir="auto">val</code> with 1.0, then replaces all elements above <code dir="auto">val</code> with 0.0. From here, it’s easy to estimate the output of the cumulative distribution function at the <code dir="auto">val</code>, as it’s simply the average of the resulting array:</p> <div class="pine-colorizer not-content">srcArray.copy().fill(1.0, 0.0, min, val).avg()</div> <p>Note that:</p> <ul> <li>The compiler will only use this <code dir="auto">fill()</code> overload instead of the built-in when the user provides <code dir="auto">innerValue</code>, <code dir="auto">outerValue</code>, <code dir="auto">lowerBound</code>, and <code dir="auto">upperBound</code> arguments in the call.</li> <li>If either <code dir="auto">lowerBound</code> or <code dir="auto">upperBound</code> is <code dir="auto">na</code>, its value is ignored while filtering the fill range.</li> <li>We are able to call <code dir="auto">copy()</code>, <code dir="auto">fill()</code>, and <code dir="auto">avg()</code> successively on the same line of code because the first two methods return an <code dir="auto">array<float></code> instance.</li> </ul> <p>We can now use this to define a method that will calculate our empirical distribution values. The following <code dir="auto">eCDF()</code> method estimates a number of evenly spaced ascending <code dir="auto">steps</code> from the cumulative distribution function of a <code dir="auto">srcArray</code> and pushes the results into a <code dir="auto">cdfArray</code>:</p> <div class="pine-colorizer not-content">// @function Estimates the empirical CDF of a `srcArray`. // @param srcArray (array<float>) Array to calculate on. // @param steps (int) Number of steps in the estimation. // @returns (array<float>) Array of estimated CDF ratios. method eCDF(array<float> srcArray, int steps) => float min = srcArray.min() float rng = srcArray.range() / steps array<float> cdfArray = array.new<float>() // Add averages of `srcArray` filtered by value region to the `cdfArray`. float val = min for i = 1 to steps val += rng cdfArray.push(srcArray.copy().fill(1.0, 0.0, min, val).avg()) cdfArray</div> <p>Lastly, to ensure that our <code dir="auto">eCDF()</code> method functions properly for arrays containing small and large values, we will define a method to normalize our arrays.</p> <p>This <code dir="auto">featureScale()</code> method uses array <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_array.min">min()</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_array.range">range()</a> methods to produce a rescaled copy of a <code dir="auto">srcArray</code>. We will use this to normalize our arrays prior to invoking the <code dir="auto">eCDF()</code> method:</p> <div class="pine-colorizer not-content">// @function Rescales the elements within a `srcArray` to the interval [0, 1]. // @param srcArray (array<float>) Array to normalize. // @returns (array<float>) Normalized copy of the `srcArray`. method featureScale(array<float> srcArray) => float min = srcArray.min() float rng = srcArray.range() array<float> scaledArray = array.new<float>() // Push normalized `element` values into the `scaledArray`. for element in srcArray scaledArray.push((element - min) / rng) scaledArray</div> <p>Note that:</p> <ul> <li>This method does not include special handling for divide by zero conditions. If <code dir="auto">rng</code> is 0, the value of the array element will be <code dir="auto">na</code>.</li> </ul> <p>The full example below queues a <code dir="auto">sourceArray</code> of size <code dir="auto">length</code> with <code dir="auto">sourceInput</code> values using our previous <code dir="auto">maintainQueue()</code> method, normalizes the array’s elements using the <code dir="auto">featureScale()</code> method, then calls the <code dir="auto">eCDF()</code> method to get an array of estimates for <code dir="auto">n</code> evenly spaced steps on the distribution. The script then calls a user-defined <code dir="auto">makeLabel()</code> function to display the estimates and prices in a label on the right side of the chart:</p> <p><img src="/pine-script-docs/_astro/Methods_empirical_distribution.BdS9hiv6_Z1t1cl8.webp" alt="image" width="1814" height="844" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator("Empirical Distribution", overlay = true) float sourceInput = input.source(close, "Source") int length = input.int(20, "Length") int n = input.int(20, "Steps") // @function Maintains a queue of the size of `srcArray`. // It appends a `value` to the array and removes its oldest element at position zero. // @param srcArray (array<float>) The array where the queue is maintained. // @param value (float) The new value to be added to the queue. // The queue's oldest value is also removed, so its size is constant. // @param takeSample (bool) A new `value` is only pushed into the queue if this is true. // @returns (array<float>) `srcArray` object. method maintainQueue(array<float> srcArray, float value, bool takeSample = true) => if takeSample srcArray.push(value) srcArray.shift() srcArray // @function Replaces elements in a `srcArray` between `lowerBound` and `upperBound` with an `innerValue`, // and replaces elements outside the range with an `outerValue`. // @param srcArray (array<float>) Array to modify. // @param innerValue (float) Value to replace elements within the range with. // @param outerValue (float) Value to replace elements outside the range with. // @param lowerBound (float) Lowest value to replace with `innerValue`. // @param upperBound (float) Highest value to replace with `innerValue`. // @returns (array<float>) `srcArray` object. method fill(array<float> srcArray, float innerValue, float outerValue, float lowerBound, float upperBound) => for [i, element] in srcArray if (element >= lowerBound or na(lowerBound)) and (element <= upperBound or na(upperBound)) srcArray.set(i, innerValue) else srcArray.set(i, outerValue) srcArray // @function Estimates the empirical CDF of a `srcArray`. // @param srcArray (array<float>) Array to calculate on. // @param steps (int) Number of steps in the estimation. // @returns (array<float>) Array of estimated CDF ratios. method eCDF(array<float> srcArray, int steps) => float min = srcArray.min() float rng = srcArray.range() / steps array<float> cdfArray = array.new<float>() // Add averages of `srcArray` filtered by value region to the `cdfArray`. float val = min for i = 1 to steps val += rng cdfArray.push(srcArray.copy().fill(1.0, 0.0, min, val).avg()) cdfArray // @function Rescales the elements within a `srcArray` to the interval [0, 1]. // @param srcArray (array<float>) Array to normalize. // @returns (array<float>) Normalized copy of the `srcArray`. method featureScale(array<float> srcArray) => float min = srcArray.min() float rng = srcArray.range() array<float> scaledArray = array.new<float>() // Push normalized `element` values into the `scaledArray`. for element in srcArray scaledArray.push((element - min) / rng) scaledArray // @function Draws a label containing eCDF estimates in the format "{price}: {percent}%" // @param srcArray (array<float>) Array of source values. // @param cdfArray (array<float>) Array of CDF estimates. // @returns (void) makeLabel(array<float> srcArray, array<float> cdfArray) => float max = srcArray.max() float rng = srcArray.range() / cdfArray.size() string results = "" var label lbl = label.new(0, 0, "", style = label.style_label_left, text_font_family = font.family_monospace) // Add percentage strings to `results` starting from the `max`. cdfArray.reverse() for [i, element] in cdfArray results += str.format("{0}: {1}%\n", max - i * rng, element * 100) // Update `lbl` attributes. lbl.set_xy(bar_index + 1, srcArray.avg()) lbl.set_text(results) var array<float> sourceArray = array.new<float>(length) // Add background color for the last `length` bars. bgcolor(bar_index > last_bar_index - length ? color.new(color.orange, 80) : na) // Queue `sourceArray`, feature scale, then estimate the distribution over `n` steps. array<float> distArray = sourceArray.maintainQueue(sourceInput).featureScale().eCDF(n) // Draw label. makeLabel(sourceArray, distArray)</div> </div> <div class="pagination-buttons not-content" data-astro-cid-xgirumru> <a href="/pine-script-docs/language/enums" class="pagination-card" data-pagefind-ignore data-astro-cid-assl6cvf> <p data-astro-cid-assl6cvf>Previous</p> <h4 class="pagination-card-header" data-astro-cid-assl6cvf> <svg width="28" height="28" viewBox="0 0 28 28" data-astro-cid-assl6cvf data-icon="theme/arrow-back"> <use xlink:href="#ai:local:theme/arrow-back"></use> </svg> Enums </h4> </a> <a href="/pine-script-docs/language/arrays" class="pagination-card" data-pagefind-ignore data-astro-cid-assl6cvf> <p data-astro-cid-assl6cvf>Next</p> <h4 class="pagination-card-header" data-astro-cid-assl6cvf> Arrays <svg width="28" height="28" viewBox="0 0 28 28" data-astro-cid-assl6cvf data-icon="theme/arrow"> <symbol id="ai:local:theme/arrow"><path fill="none" stroke="var(--arrow-fill-color, #131722)" d="m11 8 6 6-6 6"/></symbol><use xlink:href="#ai:local:theme/arrow"></use> </svg> </h4> </a> </div> </main> <div id="toc" data-pagefind-ignore="all" data-astro-cid-oor6cujd><aside class="document-toc-container ml-4 w-48" data-astro-cid-oor6cujd><section id="toc-scroll-section" class="slick-scroll" data-astro-cid-oor6cujd><header data-astro-cid-oor6cujd><h2 class="toc-header" data-astro-cid-oor6cujd>On this page</h2></header><ul class="" id="toc-entries" data-astro-cid-oor6cujd><a href="#methods" aria-current="true" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-2" data-current data-astro-cid-oor6cujd>Overview</li></a><a href="#introduction" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-2" data-astro-cid-oor6cujd>Introduction</li></a><a href="#built-in-methods" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-2" data-astro-cid-oor6cujd>Built-in methods</li></a><a href="#user-defined-methods" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-2" data-astro-cid-oor6cujd>User-defined methods</li></a><a href="#method-overloading" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-2" data-astro-cid-oor6cujd>Method overloading</li></a><a href="#advanced-example" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-2" data-astro-cid-oor6cujd>Advanced example</li></a></ul></section><div class="toc-bottom-fade" data-astro-cid-oor6cujd></div><div class="back-top-space" data-astro-cid-oor6cujd><div class="not-content" style="" data-astro-cid-pkzv2hgs> <a id="back-top-button" title="Back to top" href="#top" data-astro-cid-pkzv2hgs class="floating-button not-content stvb-base stvb-pointer stvb-gray stvb-medium stvb-secondary stvb-icon stvb-icon-force-color stvb-force-no-border"> <svg width="18" height="18" viewBox="0 0 18 18" data-astro-cid-oor6cujd data-icon="theme/arrow-up"> <symbol id="ai:local:theme/arrow-up"><g fill="none"><g clip-path="url(#a)"><path fill="currentColor" fill-rule="evenodd" d="m9 3.5.5.44 4 3.5-1 1.12-2.75-2.4V14h-1.5V6.15L5.49 8.56l-.98-1.12 4-3.5L9 3.5Z" clip-rule="evenodd"/></g><defs><clipPath id="a"><path fill="#fff" d="M0 0h18v18H0z"/></clipPath></defs></g></symbol><use xlink:href="#ai:local:theme/arrow-up"></use> </svg> </a> </div> </div></aside></div> </main> </div> <footer data-astro-cid-b6pf7ola> <div class="footer-content" data-astro-cid-b6pf7ola> <ul data-astro-cid-b6pf7ola> <li data-astro-cid-b6pf7ola> <a href="https://www.tradingview.com/chat/#BfmVowG1TZkKO235" data-astro-cid-b6pf7ola>Pine Q&A chat</a> </li><li data-astro-cid-b6pf7ola> <a href="https://stackoverflow.com/questions/tagged/pine-script" data-astro-cid-b6pf7ola>Stack Overflow</a> ↗ </li><li data-astro-cid-b6pf7ola> <a href="https://t.me/PineCodersQA" data-astro-cid-b6pf7ola>Telegram</a> ↗ </li><li data-astro-cid-b6pf7ola> <a href="https://www.reddit.com/r/TradingView/" data-astro-cid-b6pf7ola>Reddit</a> ↗ </li> </ul> <div class="flex" data-astro-cid-b6pf7ola></div> <div class="copyright" data-astro-cid-b6pf7ola>Copyright © 2024 TradingView, Inc.</div> </div> </footer> </body></html>