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

Add skipna for computing methods #44

Open
Coldayftw opened this issue May 8, 2018 · 13 comments
Open

Add skipna for computing methods #44

Coldayftw opened this issue May 8, 2018 · 13 comments

Comments

@Coldayftw
Copy link

讀不到2888 新光金

@mlouielu
Copy link
Owner

mlouielu commented May 9, 2018

請問是什麼地方讀不到呢?(realtime or normal mode)
可以的話請附上最小可重現的程式碼

thanks.

@Coldayftw
Copy link
Author

Coldayftw commented May 9, 2018 via email

@Coldayftw
Copy link
Author

@brchiu
Copy link

brchiu commented May 9, 2018

我猜想是新光金上個月 24 (04/24) 在公告併購元富證那天停牌的原因吧?
可以看看今天正崴集團底下光燿科 (3428) 、勁永 (6145) 等停牌的股票,用這個程式查詢起來是否也有問題?

我自己抓的,新光金近幾日的收盤資料

2018/04/19,34208617,4930,412330654,12.10,12.10,12.00,12.10,0.05
2018/04/20,64959897,7689,790680878,12.10,12.30,12.00,12.20,0.10
2018/04/23,134694248,13382,1682069997,12.25,12.65,12.20,12.50,0.30
2018/04/24,0,0,0,--,--,--,--,0.00
2018/04/25,110614543,13281,1336509066,12.25,12.40,11.90,12.05,0.00
2018/04/26,40725467,5533,487465337,12.05,12.10,11.90,11.90,-0.15
2018/04/27,25299875,3851,301933062,11.95,12.05,11.90,11.95,0.05
2018/04/30,58577039,5082,707630518,11.95,12.25,11.90,12.15,0.20
2018/05/02,29752648,4639,360604245,12.15,12.20,12.05,12.15,0.00
2018/05/03,37178833,5531,452127742,12.15,12.25,12.10,12.15,0.00
2018/05/04,22504928,4953,273733041,12.15,12.25,12.10,12.15,0.00
2018/05/07,23690346,5049,288633498,12.25,12.25,12.15,12.20,0.05
2018/05/08,29010007,5667,354762034,12.20,12.30,12.15,12.30,0.10
2018/05/09,31798859,7921,390355500,12.30,12.35,12.20,12.25,-0.05

@ianlini
Copy link
Contributor

ianlini commented May 9, 2018

moving_average 有問題,不能直接用 sum(),因為原始 data -- 的部份會被換成 None。應該要先拿掉 None 再計算。

@ianlini
Copy link
Contributor

ianlini commented May 9, 2018

@Coldayftw 資料是有正常讀到的,只是分析的時候出問題。另外,圖片可以直接傳上 github。

@Coldayftw
Copy link
Author

Coldayftw commented May 10, 2018 via email

@mlouielu
Copy link
Owner

Hi @Coldayftw , 通常我們在 report issue 的時候,會附上 minimum reproducible 的 code,例如說:

>>> s = Stock('2888')
>>> s.moving_average(s.price, 5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/site-packages/twstock/analytics.py", line 20, in moving_average
    result.append(round(sum(data[-days:]) / days, 2))
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

如果在精細一點的話,會把傳入的 parameter 的 value 也列出來:

>>> s.price
[11.5, 11.6, 11.35, 11.05, 11.5, 11.55, 11.45, 11.35, 11.5, 11.7, 12.1, 12.0, 12.05, 11.9, 11.9, 12.05, 12.1, 12.2, 12.5, None, 12.05, 11.9, 11.95, 12.15, 12.15, 12.15, 12.15, 12.2, 12.3, 12.25, 12.35]

這樣會比較好了解問題的所在,進而解決問題。

如同 @brchiu 以及 @ianlini 提到,2888、3428、6145 應是因為有停牌,所以回傳的 stock.price 資料有 None,導致 moving_average 出問題,問題的原因是 None 與 int 相加出現 TypeError。

兩位覺得什麼方式解決會比較好呢?我現在想到是加上一個 ignore_error 的 parameter 在 moving_average 中,當 ignore_error 為 True 的時候,會只取 valid days 的資料。

@ianlini
Copy link
Contributor

ianlini commented May 10, 2018

可以參考 pandas.DataFrame.mean:
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.mean.html

skipna 或是 skip_na 等等之類的參數。
我覺得這種 data 裡面有 NA 是很合理的,不算是 error。只要能定義出一個有意義的分析就可以了。

@mlouielu
Copy link
Owner

Cool, 那就幫 analytics 的 function 加上 skipna 吧。

@Coldayftw 有興趣來修修看嗎?

@Coldayftw
Copy link
Author

Coldayftw commented May 14, 2018 via email

@mlouielu
Copy link
Owner

@Coldayftw that is not a problem, 學習閱讀程式碼跟如何透過 GitHub 協作也是一件很重要的事情。

如果你想嘗試看看,我可以幫助你開始這個部分,主要有三件事情要做:

  1. 修正 twstock/analytics.py 裡面的 method,讓他們支援 skipna 的功能。
  2. 在 test/test_analytics.py 加上相關的測資
  3. 在 docs 裡面加上相對應的 document

完成後就是操作 git 寫上 commit,然後推上來送 PR!

@Coldayftw
Copy link
Author

Coldayftw commented May 14, 2018 via email

@mlouielu mlouielu changed the title 讀不到2888 新光金 Add skipna for computing methods Mar 19, 2019
machineCYC added a commit to machineCYC/twstock that referenced this issue Mar 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants