Make stock quote formatter work with diff streams

marketstore_integration
Tyler Goodlet 2020-05-31 20:05:06 -04:00
parent 213a19b191
commit 335cee63b2
1 changed files with 31 additions and 15 deletions

View File

@ -6,6 +6,7 @@ import inspect
import time import time
from datetime import datetime from datetime import datetime
from functools import partial from functools import partial
from itertools import chain
import configparser import configparser
from typing import List, Tuple, Dict, Any, Iterator, NamedTuple from typing import List, Tuple, Dict, Any, Iterator, NamedTuple
@ -839,29 +840,43 @@ def format_stock_quote(
and the second is the same but with all values converted to a and the second is the same but with all values converted to a
"display-friendly" string format. "display-friendly" string format.
""" """
last = quote['lastTradePrice']
symbol = quote['symbol'] symbol = quote['symbol']
previous = symbol_data[symbol]['prevDayClosePrice'] previous = symbol_data[symbol]['prevDayClosePrice']
change = percent_change(previous, last)
share_count = symbol_data[symbol].get('outstandingShares', None) computed = {'symbol': symbol}
mktcap = share_count * last if (last and share_count) else 0 last = quote.get('lastTradePrice')
computed = { if last:
'symbol': quote['symbol'], change = percent_change(previous, last)
'%': round(change, 3), share_count = symbol_data[symbol].get('outstandingShares', None)
'MC': mktcap, mktcap = share_count * last if (last and share_count) else 0
# why QT do you have to be an asshole shipping null values!!! computed.update({
'$ vol': round((quote['VWAP'] or 0) * (quote['volume'] or 0), 3), # 'symbol': quote['symbol'],
'close': previous, '%': round(change, 3),
} 'MC': mktcap,
# why questrade do you have to be an asshole shipping null values!!!
# '$ vol': round((quote['VWAP'] or 0) * (quote['volume'] or 0), 3),
'close': previous,
})
vwap = quote.get('VWAP')
volume = quote.get('volume')
if volume is not None: # could be 0
# why questrade do you have to be an asshole shipping null values!!!
computed['$ vol'] = round((vwap or 0) * (volume or 0), 3)
new = {} new = {}
displayable = {} displayable = {}
for key, new_key in keymap.items(): for key, value in chain(quote.items(), computed.items()):
display_value = value = computed.get(key) or quote.get(key) new_key = keymap.get(key)
if not new_key:
continue
# API servers can return `None` vals when markets are closed (weekend) # API servers can return `None` vals when markets are closed (weekend)
value = 0 if value is None else value value = 0 if value is None else value
display_value = value
# convert values to a displayble format using available formatting func # convert values to a displayble format using available formatting func
if isinstance(new_key, tuple): if isinstance(new_key, tuple):
new_key, func = new_key new_key, func = new_key
@ -870,6 +885,7 @@ def format_stock_quote(
new[new_key] = value new[new_key] = value
displayable[new_key] = display_value displayable[new_key] = display_value
return new, displayable return new, displayable
@ -939,7 +955,7 @@ def format_option_quote(
"display-friendly" string format. "display-friendly" string format.
""" """
# TODO: need historical data.. # TODO: need historical data..
# (cause why would QT keep their quote structure consistent across # (cause why would questrade keep their quote structure consistent across
# assets..) # assets..)
# previous = symbol_data[symbol]['prevDayClosePrice'] # previous = symbol_data[symbol]['prevDayClosePrice']
# change = percent_change(previous, last) # change = percent_change(previous, last)