-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcapitalism.py
278 lines (199 loc) · 7.09 KB
/
capitalism.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python
# Capitalism for Puredata (puredata.info)
#
# Copyright (c) 2009, mark edward grimm (megrimm.net)
#
# license: GNU LGPL
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
print " "
print "---------------------------------------"
print "capitalism 0.1 - world market watch"
print "(C)2009 mark edward grimm - megrimm.net"
print "---------------------------------------"
print " "
try:
import pyext
from time import sleep
except:
print "ERROR: This script must be loaded by the PD/Max pyext external"
try:
import urllib
except:
print "ERROR: urllib NOT Found. Check Your Python Installation."
############### LIVE CLASS #####################################
class stocks(pyext._class):
"""Example of a simple class which receives messages and prints to the console"""
# number of inlets and outlets
_inlets=1
_outlets=1
def _anything_1( self, tickersymbol ):
a = float(get_price(tickersymbol))
self._outlet(1,a)
def weekhigh_1(self, tickersymbol ):
b = float(get_52_week_high( tickersymbol ))
self._outlet(1,b)
def weeklow_1(self, tickersymbol ):
c = float(get_52_week_low(tickersymbol))
self._outlet(1,c)
def change_1(self, tickersymbol ):
d = float(get_change(tickersymbol))
self._outlet(1,d)
def volume_1(self, tickersymbol ):
e = float(get_volume(tickersymbol))
self._outlet(1,e)
def avg_daily_volume_1(self, tickersymbol ):
f = float(get_avg_daily_volume(tickersymbol))
self._outlet(1,f)
def stock_exchange_1(self, tickersymbol ):
g = get_stock_exchange(tickersymbol)
self._outlet(1,g)
def market_cap_1(self, tickersymbol ):
h = get_market_cap(tickersymbol)
self._outlet(1,h)
def book_value_1(self, tickersymbol ):
i = float(get_book_value(tickersymbol))
self._outlet(1,i)
def ebitda_1(self, tickersymbol ):
j = get_ebitda(tickersymbol)
self._outlet(1,j)
def dividend_per_share_1(self, tickersymbol ):
k = float(get_dividend_per_share(tickersymbol))
self._outlet(1,k)
def dividend_yield_1(self, tickersymbol ):
l = float(get_dividend_yield(tickersymbol))
self._outlet(1,l)
def earnings_per_share_1(self, tickersymbol ):
m = float(get_earnings_per_share(tickersymbol))
self._outlet(1,m)
def fifty_day_moving_avg_1(self, tickersymbol ):
n = float(get_50day_moving_avg(tickersymbol))
self._outlet(1,n)
def twohundred_day_moving_avg_1(self, tickersymbol ):
o = float(get_200day_moving_avg(tickersymbol))
self._outlet(1,o)
def price_earnings_ratio_1(self, tickersymbol ):
p = float(get_price_earnings_ratio(tickersymbol))
self._outlet(1,p)
def price_earnings_growth_ratio_1(self, tickersymbol ):
q = float(get_price_earnings_growth_ratio(tickersymbol))
self._outlet(1,q)
def price_sales_ratio_1(self, tickersymbol ):
r = float(get_book_value(tickersymbol))
self._outlet(1,r)
def price_book_ratio_1(self, tickersymbol ):
s = float(get_price_book_ratio(tickersymbol))
self._outlet(1,s)
def short_ratio_1(self, tickersymbol ):
t = float(get_short_ratio(tickersymbol))
self._outlet(1,t)
############### SCRIPT #####################################
#
# Copyright (c) 2007-2008, Corey Goldberg ([email protected])
#
# license: GNU LGPL
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
"""
This is the "ystockquote" module.
This module provides a Python API for retrieving stock data from Yahoo Finance.
sample usage:
>>> import ystockquote
>>> print ystockquote.get_price('GOOG')
529.46
"""
def __request(symbol, stat):
url = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (symbol, stat)
return urllib.urlopen(url).read().strip().strip('"')
def get_all(symbol):
"""
Get all available quote data for the given ticker symbol.
Returns a dictionary.
"""
values = __request(symbol, 'l1c1va2xj1b4j4dyekjm3m4rr5p5p6s7').split(',')
data = {}
data['price'] = values[0]
data['change'] = values[1]
data['volume'] = values[2]
data['avg_daily_volume'] = values[3]
data['stock_exchange'] = values[4]
data['market_cap'] = values[5]
data['book_value'] = values[6]
data['ebitda'] = values[7]
data['dividend_per_share'] = values[8]
data['dividend_yield'] = values[9]
data['earnings_per_share'] = values[10]
data['52_week_high'] = values[11]
data['52_week_low'] = values[12]
data['50day_moving_avg'] = values[13]
data['200day_moving_avg'] = values[14]
data['price_earnings_ratio'] = values[15]
data['price_earnings_growth_ratio'] = values[16]
data['price_sales_ratio'] = values[17]
data['price_book_ratio'] = values[18]
data['short_ratio'] = values[19]
return data
def get_price(symbol):
return __request(symbol, 'l1')
def get_change(symbol):
return __request(symbol, 'c1')
def get_volume(symbol):
return __request(symbol, 'v')
def get_avg_daily_volume(symbol):
return __request(symbol, 'a2')
def get_stock_exchange(symbol):
return __request(symbol, 'x')
def get_market_cap(symbol):
return __request(symbol, 'j1')
def get_book_value(symbol):
return __request(symbol, 'b4')
def get_ebitda(symbol):
return __request(symbol, 'j4')
def get_dividend_per_share(symbol):
return __request(symbol, 'd')
def get_dividend_yield(symbol):
return __request(symbol, 'y')
def get_earnings_per_share(symbol):
return __request(symbol, 'e')
def get_52_week_high(symbol):
return __request(symbol, 'k')
def get_52_week_low(symbol):
return __request(symbol, 'j')
def get_50day_moving_avg(symbol):
return __request(symbol, 'm3')
def get_200day_moving_avg(symbol):
return __request(symbol, 'm4')
def get_price_earnings_ratio(symbol):
return __request(symbol, 'r')
def get_price_earnings_growth_ratio(symbol):
return __request(symbol, 'r5')
def get_price_sales_ratio(symbol):
return __request(symbol, 'p5')
def get_price_book_ratio(symbol):
return __request(symbol, 'p6')
def get_short_ratio(symbol):
return __request(symbol, 's7')
def get_historical_prices(symbol, start_date, end_date):
"""
Get historical prices for the given ticker symbol.
Date format is 'YYYYMMDD'
Returns a nested list.
"""
url = 'http://ichart.yahoo.com/table.csv?s=%s&' % symbol + \
'd=%s&' % str(int(end_date[4:6]) - 1) + \
'e=%s&' % str(int(end_date[6:8])) + \
'f=%s&' % str(int(end_date[0:4])) + \
'g=d&' + \
'a=%s&' % str(int(start_date[4:6]) - 1) + \
'b=%s&' % str(int(start_date[6:8])) + \
'c=%s&' % str(int(start_date[0:4])) + \
'ignore=.csv'
days = urllib.urlopen(url).readlines()
data = [day[:-2].split(',') for day in days]
return data