TradingView
allanster
2023年9月4日晚上8點41分

How To Input CSV List Of Symbol Data Used For Screener 

Bitcoin / United States DollarCoinbase

描述

Example of how to input multiple symbols at once using a CSV list of ticker IDs. The input list is extracted into individual ticker IDs which are then each used within an example screener function that calculates their rate of change. The results for each of the rate of changes are then plotted.

For code brevity this example only demonstrates using up to 4 symbols, but the logic is annotated to show how it can easily be expanded for use with up to 40 ticker IDs.

The CSV list used for input may contain spaces or no spaces after each comma separator, but whichever format (space or no space) is used must be used consistently throughout the list. If the list contains any invalid symbols the script will display a red exclamation mark that when clicked will display those invalid symbols.

If more than 4 ticker IDs are input then only the first 4 are used. If less than 4 ticker IDs are used then the unused screener calls will return `float(na)`. In the published chart the input list is using only 3 ticker IDs so there are only 3 plots shown instead of 4.


NOTICE: This is an example script and not meant to be used as an actual strategy. By using this script or any portion thereof, you acknowledge that you have read and understood that this is for research purposes only and I am not responsible for any financial losses you may incur by using this script!

發布通知

Revision 1
• Cosmetic cleanup of comments and code, no logic changes were made.
評論
Arun_K_Bhaskar
..
Arun_K_Bhaskar
Wow! this saves a lot of time. Always expected such a method. Thanks.
allanster
@Arun_K_Bhaskar, thanks for your interest and comment, appreciated.
liquid-trader
Thank you for sharing! Is this technique preferable over something like 'feedNoSpaces = str.replace_all(feed, " ", ""), csv = str.split(feedNoSpaces)' and then use csv.get(n) as the input to f_roc? I realize this was just an exercise and suspect there is nuance I'm unaware of, so just wanting to glean from your thought process. Appreciate your insight.
allanster
@liquid-trader, thanks for your inquiry and ideas. For this specific example where valid ticker ID strings cannot include spaces, your suggestion of using `string.replace_all()` would work nicely, as it would allow for a mix of delimiters with and without spaces. The tradeoff being if the function were to be reused elsewhere it might be less accommodating, if the CSV data elements in the other script happened to contain spaces then those would also be removed. I think preference of technique in this case depends on desired utility, the logic I chose may (or may not) offer better repurposing, but your suggestion is more efficient for this specific script's purpose. You could edit the published example's current feedback loop function to:

feed(back) => // extract tickerid and decrement list of ticker IDs loop = back // declare string variable to hold content list getT = string(na) // declare string variable to hold tickerid if str.length(loop) == 0 // if list is empty getT := string(na) // assign na to tickerid variable loop := string(na) // assign na to list of ticker Ids variable else // else extract first tickerid norm = str.replace_all(loop, ' ', '') // remove any space characters after delimiter (and elsewhere) getP = nz(str.pos(norm, ','), str.length(norm)) // get position of first comma or last character getT := str.substring(norm, 0, getP) // get tickerid in first position of list loop := str.replace(loop, getT + ',', '') // clear tickerid + delimiter character from list [getT, loop] // return tickerid in first position & truncated list

Regarding use of `str.split()`, it would create an array which would cast its string elements to series form, those in turn would not be accepted as ticker IDs by `request.security()`. In the published example I am demonstrating a way to avoid that issue.
liquid-trader
@allanster, That makes sense. Thank you for elaborating.
Arun_K_Bhaskar
@allanster, If it is possible using arrays it will be great.
allanster
@Arun_K_Bhaskar, see my previous comment in this thread:
"Regarding use of `str.split()`, it would create an array which would cast its string elements to series form, those in turn would not be accepted as ticker IDs by `request.security()`. In the published example I am demonstrating a way to avoid that issue."
更多