<jupyter_start><jupyter_text>TQDB API 範例 (Python版)首先我們自定義連結TQDB取出報價資料的函數* 預設Series作為資料輸出格式，也可以選擇DataFrame* 資料主機的位置，我們先預設為本機IP：127.0.0.1<jupyter_code>%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'

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)
a.plot()#blue
b.plot()#Green
c.plot()#red
(a-b).plot()#blue
#b.plot()#Green
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)
p=1.5*30*stw-tx*2
p
p.plot()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.199679658302189, 'fraction_numerical': 0.03523758675920982, 'mean_word_length': 4.176795580110498, 'pattern_counts': {'":': 2, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '374610', 'license': 'Apache-2.0', 'n_tokens_mistral': 833, 'n_tokens_neox': 829, 'n_words': 113, 'path': 'Demo.ipynb', 'repo_name': 'wldtw2008/tqdb', 'size': '1873'}