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 change and percentage in realtime #108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
14 changes: 14 additions & 0 deletions twstock/realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ def _split_best(d):
return d.strip('_').split('_')
return d

# Process change value and percentage
def _get_change(v1, v2):
try:
v1, v2 = float(v1), float(v2)
change = '{:+.4f}'.format(v2 - v1)
change_percentage = '{:+.4f}'.format((v2 - v1) / v1 * 100)
return (change, change_percentage)
except:
return ('-', '-')


# Realtime information
result['realtime']['latest_trade_price'] = data.get('z', None)
result['realtime']['trade_volume'] = data.get('tv', None)
Expand All @@ -51,6 +62,9 @@ def _split_best(d):
result['realtime']['open'] = data.get('o', None)
result['realtime']['high'] = data.get('h', None)
result['realtime']['low'] = data.get('l', None)
result['realtime']['yesterday_close_price'] = data.get('y', None)
result['realtime']['change'], result['realtime']['change_percentage'] = _get_change(
data.get('y', None), data.get('z', None))

# Success fetching
result['success'] = True
Expand Down