1.python基礎篇---監控股票


使用tushare庫

獲取股票信息

1.安裝tushare庫

  win+R輸入cmd進入控制台

  輸入pip install tushare

2.獲取股票信息

1 import tushare,time
2 #導入tushare庫
3 data = tushare.get_realtime_quotes(‘000581’)
4 #獲取股票代碼為000581的股票信息
5 print(data)

 

3.根據自定義要求,獲取指定數據

因為tushare獲取的返回值是一個pandas類型數據。可以用loc方式獲取單獨數據

data = tushare.get_realtime_quotes(share.code)
share.name = data.loc[0][0]
share.open = float(data.loc[0][1])
share.price = float(data.loc[0][3])
share.high = float(data.loc[0][4])
share.low = float(data.loc[0][5])

4.tushare庫詳細用法

參考資料:http://tushare.waditu.com/trading.html

完整代碼-------------------------------------------------------------------

 1 import tushare,time
 2 
 3 def getrealtimedata(share):
 4     data = tushare.get_realtime_quotes(share.code)
 5     share.name = data.loc[0][0]
 6     share.open = float(data.loc[0][1])
 7     share.price = float(data.loc[0][3])
 8     share.high = float(data.loc[0][4])
 9     share.low = float(data.loc[0][5])
10     share.describe='股票編號:{},股票名稱:{},今日開盤價:{},當前價格:{},今日最高價:{},今日最低價:{}'.format(share.code,share.name,share.open,share.price,share.high,share.low)
11     return share
12 
13 class Share():
14     def __init__(self,code,buy,sale):
15         self.name = ''
16         self.open = ''
17         self.price = ''
18         self.high = ''
19         self.low = ''
20         self.describe=''
21         self.code = code
22         self.buy = buy
23         self.sale = sale
24 
25 def main(sharelist):
26     # share = Share(code)
27     for share in sharelist:
28         sss=getrealtimedata(share)
29         print(sss.describe)
30 
31         if sss.price <=sss.buy:
32             print('價格超低,趕緊買入!')
33         elif sss.price >= sss.sale:
34             print('趕緊賣出。大賺了!')
35         else:
36             print('靜觀其變……')
37 
38 while True:
39     share1=Share("000581",18.7,19.0)
40     share2=Share("600106",18.7,19.0)
41     share3=Share("000591",18.7,19.0)
42     sharelist = [share1,share2,share3]
43     main(sharelist)
44     time.sleep(5)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM