Tag Archives: Dow Jones

Create csv file ordered with highest yield dividend paying stock

For this script to work Yahoo Finance is needed. It can be easily installed with pip. Stock tickers are taken from a file called tickers.txt.

#!/usr/bin/env python

import time
from yahoo_finance import Share

dict = {}
f = open("tickers.txt", "r")
for ticker in f.readlines():
        dividend = Share(ticker).get_dividend_yield()
        if dividend is not None:
                print ticker.rstrip()+": "+dividend
                if float(dividend) > 0:
                        dict[float(dividend)] = ticker.rstrip()

filename = "dividends-"+time.strftime("%Y%m%d")+".csv"
f = open(filename, "a")
f.write("Ticker, Dividend Yield, Dividend Share, Stock Price\n")
print "Ticker; Dividend Yield; Dividend Share; Stock Price\n"
for k in reversed(sorted(dict.keys())):
        escribir = dict[k]+","+str(k)+","+str(float(Share(dict[k]).get_dividend_share()))+","+str(float(Share(dict[k]).get_price()))+"\n"
        print dict[k]+";"+str(k)+";"+Share(dict[k]).get_dividend_share()+";"+Share(dict[k]).get_price()

When done it creates a csv (named dividends-YYYYMMDD.csv) file ordered by highest dividend yield paying stocks.

00:55:40 [me@server Finance]$ head -5 dividends-20160414.csv
Ticker, Dividend Yield, Dividend Share, Stock Price
00:55:49 [me@server Finance]$

Python script to download all NASDAQ and NYSE ticker symbols

Below is a short python script to get all NASDAQ and NYSE common stock tickers. You can then use the resulting file to get a lot of info using yahoofinance library.

#!/usr/bin/env python

import ftplib
import os
import re

# Connect to ftp.nasdaqtrader.com
ftp = ftplib.FTP('ftp.nasdaqtrader.com', 'anonymous', 'anonymous@debian.org')

# Download files nasdaqlisted.txt and otherlisted.txt from ftp.nasdaqtrader.com
for ficheiro in ["nasdaqlisted.txt", "otherlisted.txt"]:
        localfile = open(ficheiro, 'wb')
        ftp.retrbinary('RETR ' + ficheiro, localfile.write)

# Grep for common stock in nasdaqlisted.txt and otherlisted.txt
for ficheiro in ["nasdaqlisted.txt", "otherlisted.txt"]:
        localfile = open(ficheiro, 'r')
        for line in localfile:
                if re.search("Common Stock", line):
                        ticker = line.split("|")[0]
                        # Append tickers to file tickers.txt
                        open("tickers.txt","a+").write(ticker + "\n")

Check your stocks with Perl

I wrote this small script to check the value of stocks with Perl. It doesn’t shows the value of the stock in the pre and post market, but it does its job.

     1  #!/usr/bin/perl -w
     3  use strict;
     4  # Loading modules with use
     5  use LWP::UserAgent;
     6  use HTTP::Request;
     7  use HTML::Strip;
     9  my $length = $#ARGV + 1;
    10  my $stock;
    12  if ($length <= 0) {
    13          print "Usage: ./stock.pl ticker1 ticker2 .. tickern\n";
    14          exit 1 }
    16  my $ua = LWP::UserAgent->new;
    17  #$ua->agent("Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)");
    18  $ua->agent("Perl Script checking stock value");
    20  # Getting Google finance site website
    21  foreach $stock (0 .. $#ARGV) {
    22  my $url = "http://www.google.com/finance?q=$ARGV[$stock]";
    23  my $req = HTTP::Request->new(GET => $url);
    24  my $response = $ua->request($req);
    25  my $content = $response->content();
    27  # Splitting the HTML code previously requested
    28  my @values = split(' ', $content);
    29  my @valor = grep(/ref_(\d+)_l/, @values);
    31  foreach my $val (@valor) {
    32          # Using HTML::Strip to clean HTML tag
    33          my $hs = HTML::Strip->new();
    34          my $clean_text = $hs->parse( $val );
    35          $hs->eof;
    36          # Using split to remove > 
    37          my @value_2 = split(/\>/, $clean_text);
    38                  # Printing stock value after removing >
    39                  print "$ARGV[$stock] price is $value_2[1]\n"; 
    40    }
    41  }

Here is an usage example.

xavi@liberdade:/tmp$ ./stockparse.pl ge cat
ge price is 16.05
cat price is 69.72

Suggestions are always welcome.

Wall Street and the World Cup

I was walking through Wall St. this morning and couldn’t help but notice that they had replaced some of the american flags with the flags of the FIFA World Cup finalist.

NY Stock Exchange

Good to know the NY Stock Exchange is not only about money and profits. The Dow Jones index has been going up for three straight days, let’s see how it opens up on Monday after a new World Cup champion.