twstock icon indicating copy to clipboard operation
twstock copied to clipboard

Add skipna for computing methods

Open Coldayftw opened this issue 6 years ago • 13 comments

讀不到2888 新光金

Coldayftw avatar May 08 '18 11:05 Coldayftw

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

thanks.

mlouielu avatar May 09 '18 06:05 mlouielu

其他公司的資料很正常

但新光金會有這個問題 ​

2018-05-09 14:33 GMT+08:00 Louie Lu [email protected]:

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

thanks.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/mlouielu/twstock/issues/44#issuecomment-387634898, or mute the thread https://github.com/notifications/unsubscribe-auth/Aey_MB_YdGQrwmauAseBKjo3t_R84UZIks5two2_gaJpZM4T2fn7 .

Coldayftw avatar May 09 '18 13:05 Coldayftw

https://imgur.com/a/jzB2U7j

Coldayftw avatar May 09 '18 13:05 Coldayftw

我猜想是新光金上個月 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

brchiu avatar May 09 '18 13:05 brchiu

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

ianlini avatar May 09 '18 15:05 ianlini

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

ianlini avatar May 09 '18 15:05 ianlini

好的 謝謝你

2018-05-09 23:30 GMT+08:00 Ian Lin [email protected]:

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

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mlouielu/twstock/issues/44#issuecomment-387779151, or mute the thread https://github.com/notifications/unsubscribe-auth/Aey_MNPFS5btwGl7UnIYFpLjnpVsot74ks5twwuYgaJpZM4T2fn7 .

Coldayftw avatar May 10 '18 05:05 Coldayftw

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 的資料。

mlouielu avatar May 10 '18 09:05 mlouielu

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

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

ianlini avatar May 10 '18 15:05 ianlini

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

@Coldayftw 有興趣來修修看嗎?

mlouielu avatar May 14 '18 06:05 mlouielu

我上週才學會怎麼安裝.PY的檔案耶XDD

2018-05-14 14:14 GMT+08:00 Louie Lu [email protected]:

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

@Coldayftw https://github.com/Coldayftw 有興趣來修修看嗎?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mlouielu/twstock/issues/44#issuecomment-388707889, or mute the thread https://github.com/notifications/unsubscribe-auth/Aey_MJW3cRsev1X-RAF0NOA-7cJIi_Ogks5tySDBgaJpZM4T2fn7 .

Coldayftw avatar May 14 '18 10:05 Coldayftw

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

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

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

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

mlouielu avatar May 14 '18 13:05 mlouielu

我慢慢看慢慢學XDD 最近有些考試跟報告 可能要2週後才有空

Louie Lu [email protected] 於 2018年5月14日 下午9:59 寫道:

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

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

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

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

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mlouielu/twstock/issues/44#issuecomment-388826701, or mute the thread https://github.com/notifications/unsubscribe-auth/Aey_MCNYfyjt6CYHAW1FIwxfafBDukMLks5tyY3IgaJpZM4T2fn7 .

Coldayftw avatar May 14 '18 14:05 Coldayftw