scoofy / wxStocks

wxStocks: Write your own screens, ranking functions, and even high level analysis algorithms to help you review portfolio. Newest release allow SEC XBRL data and more or import your own data by writing your own custom importers. wxStocks is currently in python 3 with a ZODB database.
198 stars 54 forks source link

A suggestion for pulling stock symbols #1

Closed Soncrates closed 9 years ago

Soncrates commented 9 years ago

def get_stock_urls(exchanges=["nyse", "nasdaq"]) : # from nasdaq.com headers = { 'User-Agent': 'Mozilla/5.0 (X11; Linux x8664) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8', 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,;q=0.3', 'Accept-Encoding': 'none', 'Accept-Language': 'en-US,en;q=0.8', 'Connection': 'keep-alive', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,_/*;q=0.8' } for exchange in exchanges : yield "http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=%s&render=download" % exchange, headers

def invoke_url(url,headers) : import urllib.request response = urllib.request.Request(url, headers=headers) page = urllib.request.urlopen(response) return page.read()

def parse_csv_stock_symbol_symbols(csv): lines = csv.splitlines() rr = range(0,len(lines)) for r in rr : if r == 0 : continue ret = lines[r].decode('utf-8').split(',') if len(ret) == 0 : continue yield ','.join(map(lambda x : x.replace('"',''), ret))

this allows me to do this

for url, headers in get_ticker_urls() : csv = invoke_url(url,headers) for stock in parse_csv_ticker_symbols(csv) : print(stock)

or this

for url, headers in get_stock_urls(['amex']) : csv = invoke_url(url,headers) for stock in parse_csv_stock_symbol_symbols(csv) : print(stock)

scoofy commented 9 years ago

Cool, thanks for reading my random program, you may be the first! Ha, i'll give it a look this week.

scoofy commented 9 years ago

I've done you one better and returned stock dictionaries in the last commit. I also cleaned up the code a bit so it was more clear to me. Should be handy for initial stock creation, thanks for the suggestion.

This bit should return you what you're looking for.

for url, headers in stock_url_generator() : csv_file = return_webpage(url,headers,delay=0) count = 0 for stock in csv_stock_data_parsing_generator(csv_file) : pp.pprint(stock) sys.exit()

scoofy commented 9 years ago

Whoops, here is the code:

for url, headers in stock_url_generator() :
    csv_file = return_webpage(url,headers,delay=0)
    for stock_dict in csv_stock_data_parsing_generator(csv_file) :
        pp.pprint(stock_dict)
sys.exit()
scoofy commented 9 years ago

I've made another update, you can now create a bunch of stocks simply by using convert_nasdaq_csv_to_stock_objects()

I've also made more function name changes to make things more clear.

Soncrates commented 9 years ago

I did not mean to offend.  I have been bitten in the ass by doing things like that in the past and suddenly discovered I was spending more time looking for live code than coding.  The beauty of version control is the code lives in an archive branch for reference without polluting the code base. 

 On Thursday, March 26, 2015 6:25 PM, Matt <notifications@github.com> wrote:

Comments like this are not helpful. This is a personal project. If you don't like the style, fork it.— Reply to this email directly or view it on GitHub.

scoofy commented 9 years ago

I understand, i've started working with a partner for the first time on this project there have been quite a few style debates in the last week. I've got someone already needling the style points (i've already caved on tabs -> spaces, cause he showed me i'm doing it wrong. We're starting to write unit tests (never imagined it'd get large enough to need them, but c'est la vie).