1

What's the proper way to use import based on different conditions in Tradingview Pine Script?

I tried:

if (condition)
  import name/library/version as alias

But it generated a compilation error. It seems like the import statement can't be nested.

enter image description here

Thomas
  • 349
  • 3
  • 11

1 Answers1

0

The error message says it.
Why not import the libraries you need and use the appropriate functions based on your needs?

...

import name/library/version1 as lib1
import name/library/version2 as lib2

testVar = if condition
    lib1.fn()
else
    lib2.fn()

...
elod008
  • 1,227
  • 1
  • 5
  • 15
  • Pine Script doesn't allow for importing external data right now. In order to load a custom data feed for a particular symbol, from a range of many symbols, it's more efficient to only load the custom data for the current symbol instead of all the others. – Thomas Dec 21 '22 at 18:25
  • I think there is a misunderstanding. The only thing you can import is pine script libraries. import statement: "Used to load an external library into a script and bind its functions to a namespace". Though it seems logical that importing anything takes up more resources, it's the way pine script works. And it's also optimized for that I'm sure. If you don't call a library function it probably won't cause performance issues at all. – elod008 Dec 21 '22 at 22:15
  • I get the Pine Script library function and usage. My original desire is to only load certain libraries (in my case they are functions that return arrays of data). There are hundreds of assets for different periods of time. – Thomas Dec 21 '22 at 22:21
  • All right. You cannot import based on conditions. However if you check out a genuine pine script library like the newest zig zag you can see that it's published with function invocations, say "live demonstration" within that lib. It means unlike in eg. javascript if the libs get imported, only the code gets compiled that you're actually using, meaning only export functions with dependencies, IF called. I cannot say it for 100% sure since pine script is closed source but that's the only thing that makes sense. If you don't see a performance issue only by importing and plotting there isn't any. – elod008 Dec 22 '22 at 11:06