Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

twstock.realtime.get() 查無資料 #101

Open
furrowheart opened this issue Nov 16, 2021 · 19 comments
Open

twstock.realtime.get() 查無資料 #101

furrowheart opened this issue Nov 16, 2021 · 19 comments

Comments

@furrowheart
Copy link

我使用line bot接收訊息,透過twstock.realtime.get('股票代號')取的股價資訊再回傳給line user。
app是掛在heroku上,但遇到一個問題,如果輸入股票代號有某幾個五位數字(例如:00878,00881、00783)這種的就會有問題。
{'msgArray': [], 'referer': '', 'userDelay': 5000, 'rtcode': '5001', 'queryTime': {'sysDate': '20211116', 'stockInfoItem': 2936, 'stockInfo': 415644, 'sessionStr': 'UserSession', 'sysTime': '22:51:00', 'showChart': False, 'sessionFromTime': -1, 'sessionLatestTime': -1}, 'rtmessage': 'Empty Query.', 'success': False}
但又不是所有的五位數代碼股票都會有問題(例如00646、00858)就都沒問題。另外不是五位數的代碼(例如:2330、006208)也都正常。
但是local端的jupyter notebook測試直接用twstock.realtime.get('股票代號')都OK,可以正常回傳資訊。
確認過twstcok version, local端跟heroku都一樣是1.3.1版
請教是哪邊有問題
謝謝

@mlouielu
Copy link
Owner

你需要先更新你的 codes: https://github.com/mlouielu/twstock#update-codes

更新完成之後再嘗試一次

@furrowheart
Copy link
Author

我有更新codes,是用
import twstock
twstock.__update_codes()
twstcok version都是1.3.1
還是一樣的問題
但再local端沒有這各問題

@mlouielu
Copy link
Owner

另外一個問題是,晚上10點的時候不會有即時資料,如果你現在再嘗試一次呢?

@furrowheart
Copy link
Author

您好
目前測試還是一樣的狀況
另外,目前發現另一個狀況
有些股價的latest_trade_price': '-', 請問這各狀況是甚麼原因
謝謝
'006208': {'timestamp': 1637114828.0,
'info': {'code': '006208',
'channel': '006208.tw',
'name': '富邦台50',
'fullname': '富邦台灣采吉50證券投資信託基金',
'time': '2021-11-17 10:07:08'},
'realtime': {'latest_trade_price': '-',
'trade_volume': '-',
'accumulate_trade_volume': '616',
'best_bid_price': ['79.5500', '79.5000', '79.4500', '79.4000', '79.3500'],
'best_bid_volume': ['65', '60', '71', '189', '118'],
'best_ask_price': ['79.6000', '79.6500', '79.7000', '79.7500', '79.8000'],
'best_ask_volume': ['216', '170', '200', '204', '227'],
'open': '79.7500',
'high': '79.8500',
'low': '79.5500'},
'success': True},
'success': True}

@mlouielu
Copy link
Owner

mlouielu commented Nov 17, 2021

Latest price 是正常情況,他是顯示即時撮合的結果而不是最後一筆撮合結果。如果當下沒有撮合,則會顯示 --。在台股 ETF 這種交易量偏小的情況下容易發生。

https://mis.twse.com.tw/stock/fibest.jsp?stock=2330
https://mis.twse.com.tw/stock/fibest.jsp?stock=006208

@furrowheart
Copy link
Author

Latest price 是正常情況,他是顯示即時撮合的結果而不是最後一筆撮合結果。如果當下沒有撮合,則會顯示 --。在台股 ETF 這種交易量偏小的情況下容易發生。

https://mis.twse.com.tw/stock/fibest.jsp?stock=2330 https://mis.twse.com.tw/stock/fibest.jsp?stock=006208

看了API我的理解是
realtime的股價request基本市況報導網站的資料
基本市況報導網站又是5秒update一次資料
變成造成沒有最後一筆撮合結果,會顯示 --
不知道這樣理解有沒有錯誤

另外請問這有辦法如果request當下success == True
但'latest_trade_price': '-' 的時候,能夠回去查最近上一筆的成交價嗎
我看realtime.get回傳的資料內並沒有這一項

謝謝

@mlouielu
Copy link
Owner

Latest price 是正常情況,他是顯示即時撮合的結果而不是最後一筆撮合結果。如果當下沒有撮合,則會顯示 --。在台股 ETF 這種交易量偏小的情況下容易發生。
https://mis.twse.com.tw/stock/fibest.jsp?stock=2330 https://mis.twse.com.tw/stock/fibest.jsp?stock=006208

看了API我的理解是 realtime的股價request基本市況報導網站的資料 基本市況報導網站又是5秒update一次資料 變成造成沒有最後一筆撮合結果,會顯示 -- 不知道這樣理解有沒有錯誤

對,這是 source 造成的問題

另外請問這有辦法如果request當下success == True 但'latest_trade_price': '-' 的時候,能夠回去查最近上一筆的成交價嗎 我看realtime.get回傳的資料內並沒有這一項

這應該是要由使用者這邊去處理,因為 twstock 只是單純的從 twse API 去取得資料而已。

你可以考慮:

  1. 改變你的 line bot 架構,當你輸入一組號碼之後,他會在background 持續接收,自己做 cache 存最後一筆撮合資料
  2. 直接顯示最佳五檔 bid/ask 最相近的數值 (假設你只是想要看價格,不做任何signal的話)

第二個方法應該是最快的了。

@furrowheart
Copy link
Author

這應該是要由使用者這邊去處理,因為 twstock 只是單純的從 twse API 去取得資料而已。

你可以考慮:

  1. 改變你的 line bot 架構,當你輸入一組號碼之後,他會在background 持續接收,自己做 cache 存最後一筆撮合資料
  2. 直接顯示最佳五檔 bid/ask 最相近的數值 (假設你只是想要看價格,不做任何signal的話)

第二個方法應該是最快的了。

OK 非常感謝

@furrowheart
Copy link
Author

furrowheart commented Nov 17, 2021

另外一個問題是,晚上10點的時候不會有即時資料,如果你現在再嘗試一次呢?

我直接heroku上呼叫.py檔
還是無法取的某些特定代碼的資料
.py內容如下

import twstock
app = Flask(__name__)
twstock.__update_codes()
stock_list = np.append(stock_list,str('00878'))
stock_list = stock_list.tolist()
stocks=twstock.realtime.get(stock_list)
print (stocks)
if __name__ == "__main__":
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

@mlouielu
Copy link
Owner

mlouielu commented Nov 17, 2021

另外一個問題是,晚上10點的時候不會有即時資料,如果你現在再嘗試一次呢?

我直接heroku上呼叫.py檔 還是無法取的某些特定代碼的資料 .py內容如下

import twstock
app = Flask(__name__)
twstock.__update_codes()
stock_list = np.append(stock_list,str('00878'))
stock_list = stock_list.tolist()
stocks=twstock.realtime.get(stock_list)
print (stocks)
if __name__ == "__main__":
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

BTW, stock_list 那邊有點多餘,你可以直接 stock_list = ["00878"]

➜  twstock git:(master) ✗ python
Python 3.9.7 (default, Oct 10 2021, 15:13:22) 
[GCC 11.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>  import twstock
>>> sl = ['00878']
>>> twstock.realtime.get(sl)
{'msgArray': [{'tv': '-', 'ps': '-', 'nu': 'https://www.cathaysite.com.tw/funds/etf/estimate.aspx?t=&fc=CN', 'pz': '-', 'bp': '0', 'a': '18.6200_18.6300_18.6400_18.6500_18.6600_', 'b': '18.6100_18.6000_18.5900_18.5800_18.5700_', 'c': '00878', 'd': '20211117', 'ch': '00878.tw', 'tlong': '1637118859000', 'f': '758_453_591_890_671_', 'ip': '0', 'g': '519_919_530_1535_927_', 'mt': '790654', 'h': '18.6400', 'it': '02', 'l': '18.5700', 'n': '國泰永續高股息', 'o': '18.6000', 'p': '0', 'ex': 'tse', 's': '-', 't': '11:14:19', 'u': '20.4300', 'v': '9795', 'w': '16.7300', 'nf': '國泰台灣高股息傘型證券投資信託基金之台灣ESG永續高股息ETF證券投資信託基金', 'y': '18.5800', 'z': '-', 'ts': '0'}], 'referer': '', 'userDelay': 5000, 'rtcode': '0000', 'queryTime': {'sysDate': '20211117', 'stockInfoItem': 2061, 'stockInfo': 484635, 'sessionStr': 'UserSession', 'sysTime': '11:14:23', 'showChart': False, 'sessionFromTime': -1, 'sessionLatestTime': -1}, 'rtmessage': 'OK'}
{'00878': {'timestamp': 1637118859.0, 'info': {'code': '00878', 'channel': '00878.tw', 'name': '國泰永續高股息', 'fullname': '國泰台灣高股息傘型證券投資信託基金之台灣ESG永續高股息ETF證券投資信託基金', 'time': '2021-11-17 11:14:19'}, 'realtime': {'latest_trade_price': '-', 'trade_volume': '-', 'accumulate_trade_volume': '9795', 'best_bid_price': ['18.6100', '18.6000', '18.5900', '18.5800', '18.5700'], 'best_bid_volume': ['519', '919', '530', '1535', '927'], 'best_ask_price': ['18.6200', '18.6300', '18.6400', '18.6500', '18.6600'], 'best_ask_volume': ['758', '453', '591', '890', '671'], 'open': '18.6000', 'high': '18.6400', 'low': '18.5700'}, 'success': True}, 'success': True}

@mlouielu
Copy link
Owner

mlouielu commented Nov 17, 2021

Heruku 的部份,要看 output 是什麼,可能你踩到 query limit 了?

你可以在你自己的電腦上測試看看上面的 code.

@furrowheart
Copy link
Author

Heruku 的部份,要看 output 是什麼,可能你踩到 query limit 了?

sl = ['00878'],output會傳回這樣
{'msgArray': [], 'referer': '', 'userDelay': 5000, 'rtcode': '5001', 'queryTime': {'sysDate': '20211117', 'stockInfoItem': 3791, 'stockInfo': 744248, 'sessionStr': 'UserSession', 'sysTime': '11:35:07', 'showChart': False, 'sessionFromTime': -1, 'sessionLatestTime': -1}, 'rtmessage': 'Empty Query.', 'success': False}

sl = ['0050'],可以正常回傳資料

你可以在你自己的電腦上測試看看上面的 code.

同樣的code在電腦上測試
sl = ['00878']或是sl = ['0050']
都是正常

@mlouielu
Copy link
Owner

你可以提供兩個東西嗎:

  1. heroku 上成功的回傳值
  2. 在 heroku 上 確認 symbol 是否在 codes 當中:
>>> '00878' in twstock.codes
True

@furrowheart
Copy link
Author

你可以提供兩個東西嗎:

  1. heroku 上成功的回傳值

code是
twstock.__update_codes()
print (twstock.version)
print ('00878' in twstock.codes)
sl = ['00878','0050']
stock=twstock.realtime.get(sl)
print (stock)

回傳結果是
2021-11-17T04:06:50.674271+00:00 app[web.1]: 1.3.1
2021-11-17T04:06:50.674283+00:00 app[web.1]: False
2021-11-17T04:06:53.084824+00:00 app[web.1]: {'0050': {'timestamp': 1637122009.0, 'info': {'code': '0050', 'channel': '0050.tw', 'name': '元大台灣50', 'fullname': '元大台灣卓越50證券投資信託基金', 'time': '2021-11-17 04:06:49'}, 'realtime': {'latest_trade_price': '-', 'trade_volume': '-', 'accumulate_trade_volume': '2606', 'best_bid_price': ['141.4000', '141.3500', '141.3000', '141.2500', '141.2000'], 'best_bid_volume': ['98', '7', '152', '87', '106'], 'best_ask_price': ['141.4500', '141.5000', '141.5500', '141.6000', '141.6500'], 'best_ask_volume': ['3', '53', '27', '133', '171'], 'open': '141.3000', 'high': '141.6500', 'low': '141.1500'}, 'success': True}, 'success': True}

  1. 在 heroku 上 確認 symbol 是否在 codes 當中:
>>> '00878' in twstock.codes

這各回傳是false
'00878' in twstock.codes

@mlouielu
Copy link
Owner

回傳 false 代表 codes 沒有成功更新,所以沒有辦法取得結果。

Update codes 應該會印出東西,他寫了什麼?

@furrowheart
Copy link
Author

回傳 false 代表 codes 沒有成功更新,所以沒有辦法取得結果。

Update codes 應該會印出東西,他寫了什麼?

Update codes好像沒有印出東西
完整log如下
2021-11-17T04:30:03.000000+00:00 app[api]: Build started by user cycom1220@gmail.com
2021-11-17T04:30:25.484003+00:00 heroku[web.1]: State changed from crashed to starting
2021-11-17T04:30:25.015867+00:00 app[api]: Release v21 created by user cycom1220@gmail.com
2021-11-17T04:30:25.015867+00:00 app[api]: Deploy f641b1c4 by user cycom1220@gmail.com
2021-11-17T04:30:29.108188+00:00 heroku[web.1]: Starting process with command python app.py
2021-11-17T04:30:35.000000+00:00 app[api]: Build succeeded
2021-11-17T04:31:02.287580+00:00 app[web.1]: 1.3.1
2021-11-17T04:31:02.287605+00:00 app[web.1]: False
2021-11-17T04:31:05.138798+00:00 heroku[web.1]: State changed from starting to up
2021-11-17T04:31:04.951227+00:00 app[web.1]: {'0050': {'timestamp': 1637123459.0, 'info': {'code': '0050', 'channel': '0050.tw', 'name': '元大台灣50', 'fullname': '元大台灣卓越50證券投資信託基金', 'time': '2021-11-17 04:30:59'}, 'realtime': {'latest_trade_price': '141.4000', 'trade_volume': '25', 'accumulate_trade_volume': '3082', 'best_bid_price': ['141.3500', '141.3000', '141.2500', '141.2000', '141.1500'], 'best_bid_volume': ['24', '30', '98', '147', '157'], 'best_ask_price': ['141.4000', '141.4500', '141.5000', '141.5500', '141.6000'], 'best_ask_volume': ['301', '397', '287', '120', '147'], 'open': '141.3000', 'high': '141.6500', 'low': '141.1500'}, 'success': True}, 'success': True}

@mlouielu
Copy link
Owner

你在 heroku 裡面可以有 console 嗎?

試試看用 console 更新:

➜  twstock git:(master) ✗ python -m twstock -U
Start to update codes
Done!

@furrowheart
Copy link
Author

furrowheart commented Nov 18, 2021

你在 heroku 裡面可以有 console 嗎?

試試看用 console 更新:

➜  twstock git:(master) ✗ python -m twstock -U
Start to update codes
Done!

我用console

python -m twstock -U
Start to update codes
Done!

logs

2021-11-18T05:52:46.209356+00:00 app[api]: Starting process with command `python -m twstock -U` by user 
2021-11-18T05:52:51.406116+00:00 heroku[run.2234]: Awaiting client
2021-11-18T05:52:51.421275+00:00 heroku[run.2234]: Starting process with command `python -m twstock -U`
2021-11-18T05:52:51.613529+00:00 heroku[run.2234]: State changed from starting to up
2021-11-18T05:53:30.141219+00:00 heroku[run.2234]: Process exited with status 0
2021-11-18T05:53:30.314058+00:00 heroku[run.2234]: State changed from up to complete

但結果還是一樣
{'msgArray': [], 'referer': '', 'userDelay': 5000, 'rtcode': '5001', 'queryTime': {'sysDate': '20211118', 'stockInfoItem': 1946, 'stockInfo': 407742, 'sessionStr': 'UserSession', 'sysTime': '13:53:53', 'showChart': False, 'sessionFromTime': -1, 'sessionLatestTime': -1}, 'rtmessage': 'Empty Query.', 'success': False}

@vvv65467
Copy link

請問一下,我目前卡在他一直說搜尋不到twstock

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants