Jelajahi Sumber

add test4.py to test calendar chart

laxaurus 8 tahun lalu
induk
melakukan
78bf2a1489
2 mengubah file dengan 86 tambahan dan 1 penghapusan
  1. 45 0
      finopt/test4.py
  2. 41 1
      finopt/ystockquote.py

+ 45 - 0
finopt/test4.py

@@ -0,0 +1,45 @@
+import ystockquote
+import redis
+import json
+
+from ystockquote import Alphavantage
+
+def download_historical_px(code):
+    rs = redis.Redis('localhost', 6379,0)
+    av = Alphavantage()
+    #d = av.time_series_daily_adjusted('AUDUSD=X', 'csv', 'full')
+    
+    d = av.time_series_daily_adjusted(code, 'csv', 'full')
+    rs.set('ts-%s' % code, json.dumps(d))
+    
+
+
+def load_historical(code):
+    rs = redis.Redis('localhost', 6379,0)
+    return json.loads(rs.get('ts-%s' % code)) 
+
+
+def get_daily_percent_change(ts):
+    #return ', '.join('[new Date("%s"), %s]' % (elem[0], float(elem[4]))  for elem in ts)
+    ts = filter(lambda x: float(x[4]) <> 0.0, ts)
+    print ts
+    return ', '.join('[new Date("%s"), %0.6f]' % (elem[0], (float(elem[4]) - float(elem[1])) / float(elem[4]))  for elem in ts)
+    
+def get_daily_close(ts):
+    
+    return ', '.join('[new Date("%s"), %s]' % (elem[0], float(elem[4]))  for elem in ts)
+    
+
+
+if __name__ == '__main__':
+    #download_historical_px('000001.SS')
+#     ts = load_historical('000001.SS')
+#     ts = filter(lambda x:int(x[0][0:4]) >= 2017, ts[1:])
+#     print get_daily_percent_change(ts)
+
+    #download_historical_px('^HSI')
+    ts = load_historical('^HSI')
+    ts = filter(lambda x:int(x[0][0:4]) >= 2017, ts[1:])
+    print get_daily_close(ts)
+    print get_daily_percent_change(ts)
+    

+ 41 - 1
finopt/ystockquote.py

@@ -10,7 +10,8 @@
 #  version 2.1 of the License, or (at your option) any later version.
 
 
-import urllib
+import urllib, urllib2
+import json
 
 
 """
@@ -198,7 +199,46 @@ def get_historical_prices(symbol, start_date, end_date):
     data = [day[:-2].split(',') for day in days]
     return data
 
+class Alphavantage():
+    
+    api_key = 'RH87SY4LF3D62WNA'
+    
+    
+    
+    def __init__(self, api_key= None):
+        self.api_key = api_key if api_key <> None else self.api_key
+    
+    def url_request(self, params):
+        url = 'https://www.alphavantage.co/query?%s&apikey=%s' % (''.join('&%s=%s' % (k, v) for k, v in params.iteritems()), self.api_key)
+        return url
+    # TIME_SERIES_DAILY_ADJUSTED
+    def time_series_daily_adjusted(self, symbol, datatype=None, outputsize=None):
+        url = self.url_request({'function': 'time_series_daily_adjusted', 'symbol':symbol, 'datatype': datatype, 'outputsize':outputsize})
+        print url
+        if datatype == 'csv':
+            days = urllib2.urlopen(url).readlines()
+            data = [day[:-2].split(',') for day in days]
+            return data
+        else:
+            return json.loads(urllib2.urlopen(url))
+        
+   
+if __name__ == '__main__':
+    av = Alphavantage()
+    #d = av.time_series_daily_adjusted('AUDUSD=X', 'csv', 'full')
+    d = av.time_series_daily_adjusted('000001.SS', 'csv', 'full')
+    #d = [['timestamp', 'open', 'high', 'low', 'close', 'adjusted_close', 'volume', 'dividend_amount', 'split_coefficient'], ['2018-01-09', '3406.1116', '3417.2278', '3403.5869', '3413.8997', '3413.8997', '1968985916', '0.0000', '1.0000'], ['2018-01-08', '3391.5530', '3412.7310', '3384.5591', '3409.4800', '3409.4800', '236200', '0.0000', '1.0000']]
+    #print ', '.join('[new Date("%s"), %0.4f]' % (elem[0], (float(elem[4]) - float(elem[1])) / float(elem[4]))  for elem in d[1:])
+    print ', '.join('[new Date("%s"), %s]' % (elem[0], float(elem[4]))  for elem in d[1:])
+    print len(d)
+#ss = [["0001.HK",2.5],["0002.HK",1.86],["0003.HK",1.62],["0004.HK",1.27],["0005.HK",15.02],["0006.HK",1.44],["0011.HK",1.42],["0012.HK",0.82],["0013.HK",2.53],["0016.HK",2.53],["0017.HK",0.7],["0019.HK",1.01],["0023.HK",0.67],["0066.HK",0.67],["0083.HK",0.66],["0101.HK",0.94],["0144.HK",0.46],["0151.HK",1.14],["0267.HK",0.18],["0291.HK",0.46],["0293.HK",0.24],["0322.HK",0.71],["0330.HK",0.25],["0386.HK",1.88],["0388.HK",1.86],["0494.HK",1.06],["0688.HK",1.25],["0700.HK",4.52],["0762.HK",0.94],["0836.HK",0.49],["0857.HK",3.29],["0883.HK",4.38],["0939.HK",7.08],["0941.HK",8.08],["1044.HK",0.91],["1088.HK",1.59],["1109.HK",0.53],["1199.HK",0.27],["1299.HK",4.69],["1398.HK",5.25],["1880.HK",1.01],["1898.HK",0.43],["1928.HK",1.06],["2318.HK",1.85],["2388.HK",1.42],["2600.HK",0.19],["2628.HK",2.56],["3328.HK",0.71],["3988.HK",3.63]]
+#for s in ss:
+#    chg_percent = float(get_change(s[0]))/ float(get_price(s[0])) 
+#    print '["%s","HSI",%f,%f],' % (s[0], s[1], chg_percent)
+
 
+    
+        
 #ss = [["0001.HK",2.5],["0002.HK",1.86],["0003.HK",1.62],["0004.HK",1.27],["0005.HK",15.02],["0006.HK",1.44],["0011.HK",1.42],["0012.HK",0.82],["0013.HK",2.53],["0016.HK",2.53],["0017.HK",0.7],["0019.HK",1.01],["0023.HK",0.67],["0066.HK",0.67],["0083.HK",0.66],["0101.HK",0.94],["0144.HK",0.46],["0151.HK",1.14],["0267.HK",0.18],["0291.HK",0.46],["0293.HK",0.24],["0322.HK",0.71],["0330.HK",0.25],["0386.HK",1.88],["0388.HK",1.86],["0494.HK",1.06],["0688.HK",1.25],["0700.HK",4.52],["0762.HK",0.94],["0836.HK",0.49],["0857.HK",3.29],["0883.HK",4.38],["0939.HK",7.08],["0941.HK",8.08],["1044.HK",0.91],["1088.HK",1.59],["1109.HK",0.53],["1199.HK",0.27],["1299.HK",4.69],["1398.HK",5.25],["1880.HK",1.01],["1898.HK",0.43],["1928.HK",1.06],["2318.HK",1.85],["2388.HK",1.42],["2600.HK",0.19],["2628.HK",2.56],["3328.HK",0.71],["3988.HK",3.63]]
 #for s in ss:
 #    chg_percent = float(get_change(s[0]))/ float(get_price(s[0]))