~dholbach/ubuntu-app-reviews/protoborsa

« back to all changes in this revision

Viewing changes to build/lib.linux-i686-2.7/protoborsa_lib/ystockquote.py

  • Committer: crazycoder
  • Date: 2012-07-06 23:13:09 UTC
  • Revision ID: crazycoder@gmail.com-20120706231309-ngv3mvg06glf7hpf
commit before release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
#  Copyright (c) 2007-2008, Corey Goldberg (corey@goldb.org)
 
4
#
 
5
#  license: GNU LGPL
 
6
#
 
7
#  This library is free software; you can redistribute it and/or
 
8
#  modify it under the terms of the GNU Lesser General Public
 
9
#  License as published by the Free Software Foundation; either
 
10
#  version 2.1 of the License, or (at your option) any later version.
 
11
 
 
12
import urllib
 
13
 
 
14
"""
 
15
This is the "ystockquote" module.
 
16
 
 
17
This module provides a Python API for retrieving stock data from Yahoo Finance.
 
18
 
 
19
sample usage:
 
20
>>> import ystockquote
 
21
>>> print ystockquote.get_price('GOOG')
 
22
529.46
 
23
"""
 
24
 
 
25
def __request(symbol, stat):
 
26
    url = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (symbol, stat)
 
27
    return urllib.urlopen(url).read().strip().strip('"')
 
28
 
 
29
def get_all(symbol):
 
30
    """
 
31
    Get all available quote data for the given ticker symbol.
 
32
 
 
33
    Returns a dictionary.
 
34
    """
 
35
    values = __request(symbol, 'l1c1va2xj1b4j4dyekjm3m4rr5p5p6s7').split(',')
 
36
    data = {}
 
37
    data['price'] = values[0]
 
38
    data['change'] = values[1]
 
39
    data['volume'] = values[2]
 
40
    data['avg_daily_volume'] = values[3]
 
41
    data['stock_exchange'] = values[4]
 
42
    data['market_cap'] = values[5]
 
43
    data['book_value'] = values[6]
 
44
    data['ebitda'] = values[7]
 
45
    data['dividend_per_share'] = values[8]
 
46
    data['dividend_yield'] = values[9]
 
47
    data['earnings_per_share'] = values[10]
 
48
    data['52_week_high'] = values[11]
 
49
    data['52_week_low'] = values[12]
 
50
    data['50day_moving_avg'] = values[13]
 
51
    data['200day_moving_avg'] = values[14]
 
52
    data['price_earnings_ratio'] = values[15]
 
53
    data['price_earnings_growth_ratio'] = values[16]
 
54
    data['price_sales_ratio'] = values[17]
 
55
    data['price_book_ratio'] = values[18]
 
56
    data['short_ratio'] = values[19]
 
57
    return data
 
58
 
 
59
def get_price(symbol):
 
60
    return __request(symbol, 'l1')
 
61
 
 
62
def get_change(symbol):
 
63
    return __request(symbol, 'c1')
 
64
 
 
65
def get_volume(symbol):
 
66
    return __request(symbol, 'v')
 
67
 
 
68
def get_avg_daily_volume(symbol):
 
69
    return __request(symbol, 'a2')
 
70
 
 
71
def get_stock_exchange(symbol):
 
72
    return __request(symbol, 'x')
 
73
 
 
74
def get_market_cap(symbol):
 
75
    return __request(symbol, 'j1')
 
76
 
 
77
def get_book_value(symbol):
 
78
    return __request(symbol, 'b4')
 
79
 
 
80
def get_ebitda(symbol):
 
81
    return __request(symbol, 'j4')
 
82
 
 
83
def get_dividend_per_share(symbol):
 
84
    return __request(symbol, 'd')
 
85
 
 
86
def get_dividend_yield(symbol):
 
87
    return __request(symbol, 'y')
 
88
 
 
89
def get_earnings_per_share(symbol):
 
90
    return __request(symbol, 'e')
 
91
 
 
92
def get_52_week_high(symbol):
 
93
    return __request(symbol, 'k')
 
94
 
 
95
def get_52_week_low(symbol):
 
96
    return __request(symbol, 'j')
 
97
 
 
98
def get_50day_moving_avg(symbol):
 
99
    return __request(symbol, 'm3')
 
100
 
 
101
def get_200day_moving_avg(symbol):
 
102
    return __request(symbol, 'm4')
 
103
 
 
104
def get_price_earnings_ratio(symbol):
 
105
    return __request(symbol, 'r')
 
106
 
 
107
def get_price_earnings_growth_ratio(symbol):
 
108
    return __request(symbol, 'r5')
 
109
 
 
110
def get_price_sales_ratio(symbol):
 
111
    return __request(symbol, 'p5')
 
112
 
 
113
def get_price_book_ratio(symbol):
 
114
    return __request(symbol, 'p6')
 
115
 
 
116
def get_short_ratio(symbol):
 
117
    return __request(symbol, 's7')
 
118
 
 
119
def get_historical_prices(symbol, start_date, end_date):
 
120
    """
 
121
    Get historical prices for the given ticker symbol.
 
122
    Date format is 'YYYYMMDD'
 
123
 
 
124
    Returns a nested list.
 
125
    """
 
126
    url = 'http://ichart.yahoo.com/table.csv?s=%s&' % symbol + \
 
127
          'd=%s&' % str(int(end_date[4:6]) - 1) + \
 
128
          'e=%s&' % str(int(end_date[6:8])) + \
 
129
          'f=%s&' % str(int(end_date[0:4])) + \
 
130
          'g=d&' + \
 
131
          'a=%s&' % str(int(start_date[4:6]) - 1) + \
 
132
          'b=%s&' % str(int(start_date[6:8])) + \
 
133
          'c=%s&' % str(int(start_date[0:4])) + \
 
134
          'ignore=.csv'
 
135
    print '['+url+']';
 
136
    days = urllib.urlopen(url).readlines()
 
137
    data = [day[:-2].split(',') for day in days]
 
138
    return data
 
139