@@ -13,9 +13,12 @@ class YStock:
13
13
>>> h = google.historical()
14
14
>>> last_adjusted_close = h[-1]['adjusted_close']
15
15
>>> last_log_return = h[-1]['log_return']
16
+ previous version of this code user Yahoo for historical data
17
+ but Yahoo changed API and blocked them, moving to Google finance.
16
18
"""
17
19
URL_CURRENT = 'http://finance.yahoo.com/d/quotes.csv?s=%(symbol)s&f=%(columns)s'
18
- URL_HISTORICAL = 'http://ichart.yahoo.com/table.csv?s=%(s)s&a=%(a)s&b=%(b)s&c=%(c)s&d=%(d)s&e=%(e)s&f=%(f)s'
20
+ URL_HISTORICAL = 'https://www.google.com/finance/historical?output=csv&q=%(symbol)s'
21
+
19
22
def __init__ (self ,symbol ):
20
23
self .symbol = symbol .upper ()
21
24
@@ -52,24 +55,25 @@ def current(self):
52
55
current [row [0 ]] = raw_data [i ]
53
56
return current
54
57
55
- def historical (self ,start = None , stop = None ):
58
+ def historical (self , start = None , stop = None ):
56
59
import datetime , time , urllib , math
57
- start = start or datetime .date (1900 ,1 ,1 )
58
- stop = stop or datetime .date .today ()
59
- url = self .URL_HISTORICAL % dict (
60
- s = self .symbol ,
61
- a = start .month - 1 ,b = start .day ,c = start .year ,
62
- d = stop .month - 1 ,e = stop .day ,f = stop .year )
60
+ url = self .URL_HISTORICAL % dict (symbol = self .symbol )
63
61
# Date,Open,High,Low,Close,Volume,Adj Close
64
62
lines = urllib .urlopen (url ).readlines ()
65
- raw_data = [row .split (',' ) for row in lines [1 :] if row .count (',' )== 6 ]
63
+ if any ('CAPTCHA' in line for line in lines ):
64
+ print url
65
+ raise
66
+ raw_data = [row .split (',' ) for row in lines [1 :] if 5 <= row .count (',' ) <= 6 ]
66
67
previous_adjusted_close = 0
67
68
series = []
68
69
raw_data .reverse ()
69
70
for row in raw_data :
71
+ if row [1 ] == '-' : continue
72
+ date = datetime .datetime .strptime (row [0 ],'%d-%b-%y' )
73
+ if (start and date < start ) or (stop and date > stop ): continue
70
74
open , high , low = float (row [1 ]), float (row [2 ]), float (row [3 ])
71
75
close , vol = float (row [4 ]), float (row [5 ])
72
- adjusted_close = float (row [6 ])
76
+ adjusted_close = float (row [5 ]) if len ( row ) > 5 else close
73
77
adjustment = adjusted_close / close
74
78
if previous_adjusted_close :
75
79
arithmetic_return = adjusted_close / previous_adjusted_close - 1.0
@@ -79,7 +83,7 @@ def historical(self,start=None, stop=None):
79
83
arithmetic_return = log_return = None
80
84
previous_adjusted_close = adjusted_close
81
85
series .append (dict (
82
- date = datetime . datetime . strptime ( row [ 0 ], '%Y-%m-%d' ) ,
86
+ date = date ,
83
87
open = open ,
84
88
high = high ,
85
89
low = low ,
@@ -96,7 +100,7 @@ def historical(self,start=None, stop=None):
96
100
97
101
@staticmethod
98
102
def download (symbol = 'goog' ,what = 'adjusted_close' ,start = None ,stop = None ):
99
- return [d [what ] for d in YStock (symbol ).historical (start ,stop )]
103
+ return [d [what ] for d in YStock (symbol ).historical (start , stop )]
100
104
101
105
import os
102
106
import uuid
@@ -1093,7 +1097,7 @@ def eval_fitting_function(f,c,x):
1093
1097
weight = 1.0 / points [i ][2 ] if len (points [i ])> 2 else 1.0
1094
1098
b [i ,0 ] = weight * float (points [i ][1 ])
1095
1099
for j in xrange (A .ncols ):
1096
- A [i ,j ] = weight * f [j ](points [i ][0 ])
1100
+ A [i ,j ] = weight * f [j ](float ( points [i ][0 ]) )
1097
1101
c = (1.0 / (A .T * A ))* (A .T * b )
1098
1102
chi = A * c - b
1099
1103
chi2 = norm (chi ,2 )** 2
@@ -1447,7 +1451,7 @@ def optimize_newton_multi(f, x, ap=1e-6, rp=1e-4, ns=20):
1447
1451
if k > 2 and norm (x - x_old )< max (ap ,norm (x )* rp ): return x .flatten ()
1448
1452
raise ArithmeticError ('no convergence' )
1449
1453
1450
- def optimize_newton_multi_imporved (f , x , ap = 1e-6 , rp = 1e-4 , ns = 20 , h = 10.0 ):
1454
+ def optimize_newton_multi_improved (f , x , ap = 1e-6 , rp = 1e-4 , ns = 20 , h = 10.0 ):
1451
1455
"""
1452
1456
Finds the extreme of multidimensional function f near point x.
1453
1457
@@ -1482,7 +1486,7 @@ def g(b, data=data, f=fs, constraint=constraint):
1482
1486
if constraint : chi2 += constraint (b )
1483
1487
return chi2
1484
1488
if isinstance (b ,(list ,tuple )):
1485
- b = optimize_newton_multi_imporved (g ,b ,ap ,rp ,ns )
1489
+ b = optimize_newton_multi_improved (g ,b ,ap ,rp ,ns )
1486
1490
else :
1487
1491
b = optimize_newton (g ,b ,ap ,rp ,ns )
1488
1492
return b , g (b ,data ,constraint = None )
@@ -1503,7 +1507,7 @@ def g(b,data=data,fs=fs,constraint=constraint):
1503
1507
if constraint :
1504
1508
chi += constraint (b )
1505
1509
return chi2
1506
- b = optimize_newton_multi_imporved (g ,b ,ap ,rp ,ns )
1510
+ b = optimize_newton_multi_improved (g ,b ,ap ,rp ,ns )
1507
1511
a , chi2 = core (b ,data ,fs )
1508
1512
return a + b ,chi2
1509
1513
@@ -1572,7 +1576,7 @@ def next(self):
1572
1576
def random (self ):
1573
1577
return float (self .next ())/ self .m
1574
1578
1575
- class MarsenneTwister (object ):
1579
+ class MersenneTwister (object ):
1576
1580
"""
1577
1581
based on:
1578
1582
Knuth 1981, The Art of Computer Programming
@@ -2590,4 +2594,4 @@ def test132():
2590
2594
if __name__ == '__main__' :
2591
2595
import os ,doctest
2592
2596
if not os .path .exists ('images' ): os .mkdir ('images' )
2593
- doctest .testmod (optionflags = doctest .ELLIPSIS )
2597
+ doctest .testmod (optionflags = doctest .ELLIPSIS )
0 commit comments