In [5]:
import time

In [18]:
ISOTIMEFORMAT='%Y-%m-%d %X'

ut = time.strftime( ISOTIMEFORMAT, time.localtime() )

In [1]:
import tushare as ts

In [74]:
basics = ts.get_stock_basics()

In [75]:
basics.shape


Out[75]:
(2780, 15)

In [62]:
str(basics.ix['600077']['timeToMarket'])


Out[62]:
'19970520'

In [29]:
basics.insert(0,"update_time",ut,True)

In [44]:
import pandas as pd

In [78]:
df1 = pd.DataFrame()

In [79]:
'%.2f'%(1.445)


Out[79]:
'1.45'

In [87]:
df2 = ts.get_h_data('000001',start='2015-11-20',end='2015-11-20',autype='hfq')
df2


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-87-69b56f70b1d2> in <module>()
----> 1 df2 = ts.get_hist_data('000001',start='2015-11-20',end='2015-11-20',autype='hfq')
      2 df2

TypeError: get_hist_data() got an unexpected keyword argument 'autype'

In [93]:
df = ts.get_h_data('601727').sort_index(ascending=False).head()
df


[Getting data:]####
Out[93]:
open high close low volume amount
date
2015-09-30 11.20 11.44 11.23 11.05 30891062 346557344
2015-09-29 11.30 11.39 11.07 11.00 32921716 367696416
2015-09-28 11.59 11.69 11.51 11.31 29705536 341980576
2015-09-25 11.93 12.01 11.41 11.27 50924896 592532928
2015-09-24 12.00 12.15 12.04 11.76 54467984 650279936

In [91]:
df.sort_index?

In [82]:
float(df['high'])==0


Out[82]:
True

In [73]:
df.columns


Out[73]:
Index([u'name', u'open', u'pre_close', u'price', u'high', u'low', u'bid',
       u'ask', u'volume', u'amount', u'b1_v', u'b1_p', u'b2_v', u'b2_p',
       u'b3_v', u'b3_p', u'b4_v', u'b4_p', u'b5_v', u'b5_p', u'a1_v', u'a1_p',
       u'a2_v', u'a2_p', u'a3_v', u'a3_p', u'a4_v', u'a4_p', u'a5_v', u'a5_p',
       u'date', u'time', u'code'],
      dtype='object')

In [71]:
#df1 = pd.concat([df1,df2])
#df1.concat(df2)
df2.set_index('open')
df2


Out[71]:
open high close low volume amount
date
2015-11-03 6.6 6.75 6.58 6.50 14395031 95081248
2015-11-02 6.7 6.85 6.58 6.57 13664205 91058408

In [28]:
basics = basics.drop("update_time",axis=1)

In [20]:
ts.get_h_data('601727',autype='qfq').head()


[Getting data:]####
Out[20]:
open high close low volume amount
date
2015-09-30 11.20 11.44 11.23 11.05 30891062 346557344
2015-09-29 11.30 11.39 11.07 11.00 32921716 367696416
2015-09-28 11.59 11.69 11.51 11.31 29705536 341980576
2015-09-25 11.93 12.01 11.41 11.27 50924896 592532928
2015-09-24 12.00 12.15 12.04 11.76 54467984 650279936

In [11]:
ts.get_h_data('600077',autype='hfq').head(), ts.get_hist_data('600077').tail()


[Getting data:]####
Out[11]:
(             open   high  close    low    volume     amount
 date                                                       
 2015-11-19  55.54  55.87  55.78  54.25  15496610  105713512
 2015-11-18  54.81  57.73  55.54  54.41  24518024  170261808
 2015-11-17  56.51  57.24  54.81  54.25  23553730  162970064
 2015-11-16  53.93  55.87  55.70  53.60  15183944  102734400
 2015-11-13  56.59  56.92  55.06  53.93  28491562  195333488,
             open  high  close   low     volume  price_change  p_change    ma5  \
 date                                                                            
 2015-11-13  7.00  7.04   6.82  6.67  284915.62         -0.31     -4.35  7.162   
 2015-11-16  6.67  6.91   6.88  6.63  151839.44          0.07      1.03  7.102   
 2015-11-17  6.99  7.08   6.79  6.71  235537.30         -0.10     -1.45  6.992   
 2015-11-18  6.78  7.14   6.88  6.73  245180.23          0.10      1.48  6.898   
 2015-11-19  6.87  6.91   6.90  6.71  154966.09          0.03      0.44  6.854   
 
              ma10   ma20      v_ma5     v_ma10     v_ma20  turnover  
 date                                                                 
 2015-11-13  6.999  6.952  298855.33  248408.28  289273.85      2.61  
 2015-11-16  7.030  6.973  285167.65  249928.02  279241.10      1.39  
 2015-11-17  7.052  6.967  274435.10  259086.72  267078.04      2.16  
 2015-11-18  7.041  6.999  266778.17  261001.55  261140.28      2.25  
 2015-11-19  7.035  7.001  214487.74  251423.17  251681.04      1.42  )

In [65]:
test_code='600077'

In [66]:
test_fq_factor = ts.stock.trading._parase_fq_factor(test_code,'2015-01-01','2015-11-15').set_index('date').sort_index(ascending=False)

In [67]:
test_fq_factor.head()


Out[67]:
factor
date
2015-11-20 56.2702
2015-11-19 55.7851
2015-11-18 55.5425
2015-11-17 54.8149
2015-11-16 55.7043

In [96]:
import sys

In [98]:
sys.path.append('/home/work/dev/my_stock')

In [99]:
import lib.utils as utils

In [100]:
import pandas.io.sql as sql

In [142]:
a=sql.read_frame('select code from t_stock_basics', utils.getEngine())
a


Out[142]:
code
0 000001
1 000002
2 000004
3 000005
4 000006
5 000007
6 000008
7 000009
8 000010
9 000011
10 000012
11 000014
12 000016
13 000017
14 000018
15 000019
16 000020
17 000021
18 000022
19 000023
20 000024
21 000025
22 000026
23 000027
24 000028
25 000029
26 000030
27 000031
28 000032
29 000033
... ...
2750 603699
2751 603703
2752 603718
2753 603729
2754 603766
2755 603788
2756 603789
2757 603799
2758 603806
2759 603808
2760 603818
2761 603828
2762 603838
2763 603869
2764 603883
2765 603885
2766 603889
2767 603898
2768 603899
2769 603901
2770 603918
2771 603939
2772 603968
2773 603969
2774 603979
2775 603988
2776 603989
2777 603993
2778 603997
2779 603998

2780 rows × 1 columns


In [139]:
from pandas import DataFrame, Series

In [147]:
type(a['code'])


Out[147]:
pandas.core.series.Series

In [158]:
hfq_df = sql.read_frame('select * from t_daily_hfq_stock where code="000001"', utils.getEngine())

In [212]:
hfqDF = pd.read_sql('select * from t_daily_hfq_stock where code="%s"'%'300186', utils.getEngine()).set_index('date').sort_index(ascending=False)

In [214]:
fq = pd.read_sql('select * from t_daily_fqFactor where code="300186"', utils.getEngine())

In [220]:
codes = pd.read_sql('select code from t_stock_basics', utils.getEngine())

In [221]:
codes[codes['code']>'300186']


Out[221]:
code
1419 300187
1420 300188
1421 300189
1422 300190
1423 300191
1424 300192
1425 300193
1426 300194
1427 300195
1428 300196
1429 300197
1430 300198
1431 300199
1432 300200
1433 300201
1434 300202
1435 300203
1436 300204
1437 300205
1438 300206
1439 300207
1440 300208
1441 300209
1442 300210
1443 300211
1444 300212
1445 300213
1446 300214
1447 300215
1448 300216
... ...
2750 603699
2751 603703
2752 603718
2753 603729
2754 603766
2755 603788
2756 603789
2757 603799
2758 603806
2759 603808
2760 603818
2761 603828
2762 603838
2763 603869
2764 603883
2765 603885
2766 603889
2767 603898
2768 603899
2769 603901
2770 603918
2771 603939
2772 603968
2773 603969
2774 603979
2775 603988
2776 603989
2777 603993
2778 603997
2779 603998

1361 rows × 1 columns


In [189]:
def saveFqFactor(code):
    fq = ts.stock.trading._parase_fq_factor(code,'','')
    fq.insert(0,'code',code,True)
    fq = fq.drop_duplicates('date').set_index('date')
    #print fq
    fq.to_sql(name='t_daily_fqFactor', con=utils.getEngine(), if_exists='append',chunksize=20000)

In [207]:
codes.index[codes.index>'600000'].map(saveFqFactor)


Out[207]:
array([None, None, None, ..., None, None, None], dtype=object)

In [245]:
def check(code, start='2015-01-01'):
    hfq_df_my = pd.read_sql('select * from t_daily_qfq_stock where code="%s" and date>="%s"'%(code,start), con=utils.getEngine())
    hfq_df_ts = ts.get_h_data(code,start=start,autype='qfq')
    return any(hfq_df_my['close'].sort_index(ascending=False) == hfq_df_ts)

In [255]:
codes['code'].sample(n=10).map(check)


[Getting data:]###[Getting data:]###[Getting data:]###[Getting data:]###[Getting data:]###[Getting data:]###[Getting data:]###[Getting data:]###[Getting data:]###[Getting data:]###
Out[255]:
2669    True
783     True
320     True
1598    True
1655    True
2365    True
1825    True
290     True
794     True
2709    True
Name: code, dtype: bool

In [252]:
codes['code'].map(check)


[Getting data:]###[Getting data:]###[Getting data:]###[Getting data:]###[Getting data:]###[Getting data:]###[Getting data:]###[Getting data:]###[Getting data:]###[Getting data:]###[Getting data:]###[Getting data:]###[Getting data:]###[Getting data:]###
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-252-2d1cbd66b3d0> in <module>()
----> 1 codes['code'].map(check)

/home/work/anaconda/lib/python2.7/site-packages/pandas/core/series.pyc in map(self, arg, na_action)
   2014                                      index=self.index).__finalize__(self)
   2015         else:
-> 2016             mapped = map_f(values, arg)
   2017             return self._constructor(mapped,
   2018                                      index=self.index).__finalize__(self)

pandas/src/inference.pyx in pandas.lib.map_infer (pandas/lib.c:58435)()

<ipython-input-245-f47b60fe913b> in check(code, start)
      1 def check(code, start='2015-01-01'):
      2     hfq_df_my = pd.read_sql('select * from t_daily_qfq_stock where code="%s" and date>="%s"'%(code,start), con=utils.getEngine())
----> 3     hfq_df_ts = ts.get_h_data(code,start=start,autype='qfq')
      4     return any(hfq_df_my['close'].sort_index(ascending=False) == hfq_df_ts)

/home/work/anaconda/lib/python2.7/site-packages/tushare/stock/trading.pyc in get_h_data(code, start, end, autype, index, retry_count, pause)
    385         if autype == 'qfq':
    386             data = data.drop('factor', axis=1)
--> 387             df = _parase_fq_factor(code, start, end)
    388             df = df.drop_duplicates('date')
    389             df = df.sort('date', ascending=False)

/home/work/anaconda/lib/python2.7/site-packages/tushare/stock/trading.pyc in _parase_fq_factor(code, start, end)
    429     request = Request(ct.HIST_FQ_FACTOR_URL%(ct.P_TYPE['http'],
    430                                              ct.DOMAINS['vsf'], symbol))
--> 431     text = urlopen(request, timeout=10).read()
    432     text = text[1:len(text)-1]
    433     text = text.decode('utf-8') if ct.PY3 else text

/home/work/anaconda/lib/python2.7/urllib2.pyc in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    152     else:
    153         opener = _opener
--> 154     return opener.open(url, data, timeout)
    155 
    156 def install_opener(opener):

/home/work/anaconda/lib/python2.7/urllib2.pyc in open(self, fullurl, data, timeout)
    429             req = meth(req)
    430 
--> 431         response = self._open(req, data)
    432 
    433         # post-process response

/home/work/anaconda/lib/python2.7/urllib2.pyc in _open(self, req, data)
    447         protocol = req.get_type()
    448         result = self._call_chain(self.handle_open, protocol, protocol +
--> 449                                   '_open', req)
    450         if result:
    451             return result

/home/work/anaconda/lib/python2.7/urllib2.pyc in _call_chain(self, chain, kind, meth_name, *args)
    407             func = getattr(handler, meth_name)
    408 
--> 409             result = func(*args)
    410             if result is not None:
    411                 return result

/home/work/anaconda/lib/python2.7/urllib2.pyc in http_open(self, req)
   1225 
   1226     def http_open(self, req):
-> 1227         return self.do_open(httplib.HTTPConnection, req)
   1228 
   1229     http_request = AbstractHTTPHandler.do_request_

/home/work/anaconda/lib/python2.7/urllib2.pyc in do_open(self, http_class, req, **http_conn_args)
   1198         else:
   1199             try:
-> 1200                 r = h.getresponse(buffering=True)
   1201             except TypeError: # buffering kw not supported
   1202                 r = h.getresponse()

/home/work/anaconda/lib/python2.7/httplib.pyc in getresponse(self, buffering)
   1130 
   1131         try:
-> 1132             response.begin()
   1133             assert response.will_close != _UNKNOWN
   1134             self.__state = _CS_IDLE

/home/work/anaconda/lib/python2.7/httplib.pyc in begin(self)
    451         # read until we get a non-100 response
    452         while True:
--> 453             version, status, reason = self._read_status()
    454             if status != CONTINUE:
    455                 break

/home/work/anaconda/lib/python2.7/httplib.pyc in _read_status(self)
    407     def _read_status(self):
    408         # Initialize with Simple-Response defaults
--> 409         line = self.fp.readline(_MAXLINE + 1)
    410         if len(line) > _MAXLINE:
    411             raise LineTooLong("header line")

/home/work/anaconda/lib/python2.7/socket.pyc in readline(self, size)
    478             while True:
    479                 try:
--> 480                     data = self._sock.recv(self._rbufsize)
    481                 except error, e:
    482                     if e.args[0] == EINTR:

KeyboardInterrupt: 

In [236]:
code = '601727'

In [237]:
hfq_df_my = pd.read_sql('select * from t_daily_qfq_stock where code="%s" and date>="2015-01-01"'%code, con=utils.getEngine())

In [238]:
hfq_df_ts = ts.get_h_data(code,start='2015-01-01',autype='qfq')


[Getting data:]###

In [239]:
any(hfq_df_my['close'].sort_index(ascending=False) == hfq_df_ts)


Out[239]:
True

In [235]:
any?

In [227]:
%matplotlib inline

In [228]:
import matplotlib.pyplot as plt

In [ ]:
fig, axes = plt.subplots()

In [244]:
plt.plot(hfq_df_my['close'].sort_index(ascending=False),label='my')
plt.plot(hfq_df_ts['close'],label='ts')
plt.legend(loc='best')


Out[244]:
<matplotlib.legend.Legend at 0x7fa54a21b9d0>

In [230]:
plt.plot(hfq_df_ts['close'],label='ts')


Out[230]:
[<matplotlib.lines.Line2D at 0x7fa54aa45c90>]

In [ ]: