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

Fix bug44 #63

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/reference/analytics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
:type data: list
:param days: 天數
:type days: int
:param skipna: 是否過濾缺失資料
:type: bool

分析 ``data`` 中之 ``days`` 日之平均數::

Expand Down
8 changes: 8 additions & 0 deletions test/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ def test_moving_average(self):
self.assertEqual(ng_result, legacy_result)
self.assertEqual(ng_result, [55.0, 65.0, 72.5])

data = [10, 11, 12, 15, 10, 12, None, 10, 5]

# Legacy moving_average will affect data argument's data
ng_result = self.ng.moving_average(data, 2)
legacy_result = self.legacy.moving_average(data, 2)
self.assertEqual(ng_result, legacy_result)
self.assertEqual(ng_result, [10.5, 11.5, 13.5, 12.5, 11.0, 11.0, 7.5])

def test_ma_bias_ratio(self):
data = [50, 60, 70, 75, 80, 88, 102, 105, 106]
self.ng.price = data
Expand Down
5 changes: 4 additions & 1 deletion twstock/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ def continuous(self, data):
break
return cont * diff[0]

def moving_average(self, data, days):
def moving_average(self, data, days, skipna=True):
if skipna:
data = [x for x in data if x is not None]

result = []
data = data[:]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

這行可以移到上面那個 if 之後的 else,避免多複製一次 data

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯嗯! 我修改過了! 謝謝

for _ in range(len(data) - days + 1):
Expand Down