TQDB API 範例 (Python版)

首先我們自定義連結TQDB取出報價資料的函數

  • 預設Series作為資料輸出格式,也可以選擇DataFrame
  • 資料主機的位置,我們先預設為本機IP:127.0.0.1

In [ ]:
%matplotlib inline
import math
import matplotlib.pyplot as plt
import requests
import datetime
import urllib
from  pandas import DataFrame,Series
# Define function to fetch remote data # demonstartion only #
def TQDB(symbol='WTX',startDate='2014-6-30',endDate='2015-7-01',type='Series',server='127.0.0.1'):
    querystr={'symbol':symbol, 'BEG':startDate, 'END': endDate}
    url = "http://"+server+"/cgi-bin/q1min.py?"+urllib.urlencode(querystr)
    r = requests.get(url)
    lines = r.content.split('\n')
    x = []
    H=[]
    L=[]
    C=[]
    O=[]
    i=0
    for line in lines:
        i=i+1
        items=line.split(',')
        if len(items) < 5:
            continue
        dt=datetime.datetime.strptime(items[0]+items[1], '%Y%m%d%H%M%S')
        x.append(dt)

        C.append(float(items[5]))
        L.append(float(items[4]))
        H.append(float(items[3]))
        O.append(float(items[2]))
    d = {'O' :O,'H':H,'L':L,'C':C}
    if len(O)==0:
        print "no data available. Please select different date"
        return
    if type=="DataFrame":
        return DataFrame(d,index=x, columns=['O','H','L','C'])
    elif type=="Series":
        return Series(C,index=x)
    else:
        print 'type is not defined'

In [5]:
s='2015-7-2'
e='2015-7-3'
a=TQDB("TWSE",type='Series',startDate=s,endDate=e)
b=TQDB("TXA",type='Series',startDate=s,endDate=e)
c=TQDB("WTX",type='Series',startDate=s,endDate=e)

In [6]:
a.plot()#blue
b.plot()#Green
c.plot()#red


Out[6]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f32e3436f50>

In [24]:
(a-b).plot()#blue
#b.plot()#Green


Out[24]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f98a2b57190>

In [11]:
s='2014-5-2'
e='2015-7-3'
stw=TQDB("STW",type='Series',startDate=s,endDate=e)
#b=TQDB("TXA",type='Series',startDate=s,endDate=e)
tx=TQDB("WTX",type='Series',startDate=s,endDate=e)

In [8]:
p=1.5*30*stw-tx*2

In [10]:
p


Out[10]:
2015-06-02 00:00:00       NaN
2015-06-02 00:02:00       NaN
2015-06-02 00:03:00       NaN
2015-06-02 00:04:00       NaN
2015-06-02 00:05:00       NaN
2015-06-02 00:07:00       NaN
2015-06-02 00:08:00       NaN
2015-06-02 00:09:00       NaN
2015-06-02 00:10:00       NaN
2015-06-02 00:11:00       NaN
2015-06-02 00:12:00       NaN
2015-06-02 00:13:00       NaN
2015-06-02 00:14:00       NaN
2015-06-02 00:16:00       NaN
2015-06-02 00:17:00       NaN
2015-06-02 00:18:00       NaN
2015-06-02 00:19:00       NaN
2015-06-02 00:20:00       NaN
2015-06-02 00:21:00       NaN
2015-06-02 00:22:00       NaN
2015-06-02 00:23:00       NaN
2015-06-02 00:24:00       NaN
2015-06-02 00:25:00       NaN
2015-06-02 00:27:00       NaN
2015-06-02 00:28:00       NaN
2015-06-02 00:30:00       NaN
2015-06-02 00:31:00       NaN
2015-06-02 00:32:00       NaN
2015-06-02 00:34:00       NaN
2015-06-02 00:37:00       NaN
                        ...  
2015-07-02 13:18:00   -2996.5
2015-07-02 13:19:00   -2996.5
2015-07-02 13:20:00   -2998.5
2015-07-02 13:21:00   -3000.5
2015-07-02 13:22:00   -3002.5
2015-07-02 13:23:00   -3002.5
2015-07-02 13:24:00   -2998.5
2015-07-02 13:25:00   -3000.5
2015-07-02 13:26:00   -2991.5
2015-07-02 13:27:00   -2989.5
2015-07-02 13:28:00   -2996.5
2015-07-02 13:29:00   -2997.0
2015-07-02 13:30:00   -2997.0
2015-07-02 13:31:00   -2997.0
2015-07-02 13:32:00   -2991.0
2015-07-02 13:33:00   -2995.5
2015-07-02 13:34:00   -2999.0
2015-07-02 13:35:00   -3003.0
2015-07-02 13:36:00   -3003.5
2015-07-02 13:37:00   -2997.5
2015-07-02 13:38:00   -2995.5
2015-07-02 13:39:00   -2997.5
2015-07-02 13:40:00   -2997.5
2015-07-02 13:41:00   -3000.0
2015-07-02 13:42:00   -2996.5
2015-07-02 13:43:00   -2988.0
2015-07-02 13:44:00   -2983.5
2015-07-02 13:45:00   -2980.0
2015-07-02 13:46:00   -2984.0
2015-07-02 13:51:00       NaN
dtype: float64

In [9]:
p.plot()


Out[9]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f32e3350750>

In [ ]: