Python array of top 140 ETFs

Discussion in 'App Development' started by MoneyMatthew, Feb 17, 2020.

  1. WealthSignals

    WealthSignals Sponsor

    Nice effort MoneyMatthew. Many technical analysis software include various scanners which can be run on symbol universes. In a platform like Wealth-Lab, power users can also utilize online scanners (think black box formulas etc.)

    Just for kicks, I've created a short piece of Wealth-Lab C# code that gets and prints out the list of Top 100 ETFs by Volume dynamically. Takes just 70 lines.

    Source:

    "Most Popular ETFs: Top 100 ETFs By Trading Volume" @ https://etfdb.com/compare/volume/

    -Eugene

    Code:
    using System;
    using System.Collections.Generic;
    using WealthLab;
    using System.Net;
    using System.Linq;
    using System.Globalization;
    using System.Web;
    using HtmlAgilityPack;
    using System.Linq;
    using System.Xml;
    
    namespace WealthLab.Strategies
    {
       public class PrintTop100ETFs : WealthScript
       {
         protected override void Execute()
         {
           //Prints out the dynamic list of "Most Popular ETFs: Top 100 ETFs By Trading Volume" from ETFDB.com
           //https://etfdb.com/compare/volume/
    
           PrintDebug("Top 100 ETFs");
    
           foreach (var etf in ETFDBParser.GetTop100ETFs())
           {
             PrintDebug(etf);
           }
         }
       }
    
       public class ETFDBParser
       {
         static string url = "https://etfdb.com/compare/volume/";
    
         static string CachedPage { get; set; }
    
         public static List<string> GetTop100ETFs()
         {
           var lst = new List<string>();
    
           using (WebClient wc = new WebClient())
           {
             wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0");
             wc.Headers.Add("Content-Type", "text/html, application/xhtml+xml, image/jxr, */*");
             wc.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
             wc.Headers.Add("Accept-Language", "en-US,en;q=0.5");
             wc.Headers.Add("Referer", "https://www.etfdb.com/");
             string result = string.Empty;
    
             if (string.IsNullOrEmpty(CachedPage))
             {
               result = wc.DownloadString(url);
               CachedPage = result;
             }
    
             var doc = new HtmlAgilityPack.HtmlDocument();
             doc.LoadHtml(CachedPage);
    
             var table = doc.DocumentNode.SelectNodes("//table")
               .Descendants("tr")
               .Skip(1)
               .Where(tr => tr.Elements("td").Count() > 1)
               .Select(tr => tr.Elements("td").Select(td => td.InnerText.Trim()).ToList())
               .ToList().Select(n => n[0] );
    
             lst.AddRange(table);
    
             return lst;
           }
         }
       }
    }
     
    Last edited: Feb 22, 2020
    #11     Feb 22, 2020