Ticker.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
  4. # Copyright (C)2013-2022 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen https://github.com/mmgen/mmgen-node-tools
  9. # https://gitlab.com/mmgen/mmgen https://gitlab.com/mmgen/mmgen-node-tools
  10. """
  11. mmgen_node_tools.Ticker: Display price information for cryptocurrency and other assets
  12. """
  13. api_host = 'api.coinpaprika.com'
  14. api_url = f'https://{api_host}/v1/ticker'
  15. ratelimit = 240
  16. btc_ratelimit = 10
  17. # We use deprecated coinpaprika ‘ticker’ API for now because it returns ~45% less data.
  18. # Old ‘ticker’ API (/v1/ticker): data['BTC']['price_usd']
  19. # New ‘tickers’ API (/v1/tickers): data['BTC']['quotes']['USD']['price']
  20. # Possible alternatives:
  21. # - https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC,LTC&tsyms=USD,EUR
  22. import sys,os,time,json,yaml
  23. from subprocess import run,PIPE,CalledProcessError
  24. from decimal import Decimal
  25. from collections import namedtuple
  26. from mmgen.opts import opt
  27. from mmgen.globalvars import g
  28. from mmgen.color import *
  29. from mmgen.util import die,fmt_list,msg,msg_r,Msg,vmsg,suf,fmt,stdout_or_pager
  30. homedir = os.getenv('HOME')
  31. cachedir = os.path.join(homedir,'.cache','mmgen-node-tools')
  32. cfg_fn = 'ticker-cfg.yaml'
  33. portfolio_fn = 'ticker-portfolio.yaml'
  34. def assets_list_gen(cfg_in):
  35. for k,v in cfg_in.cfg['assets'].items():
  36. yield('')
  37. yield(k.upper())
  38. for e in v:
  39. yield(' {:4s} {}'.format(*e.split('-',1)))
  40. def gen_data(data):
  41. """
  42. Filter the raw data and return it as a dict keyed by the IDs of the assets
  43. we want to display.
  44. Add dummy entry for USD and entry for user-specified asset, if any.
  45. Since symbols in source data are not guaranteed to be unique (e.g. XAG), we
  46. must search the data twice: first for unique IDs, then for symbols while
  47. checking for duplicates.
  48. """
  49. def dup_sym_errmsg(dup_sym):
  50. return (
  51. f'The symbol {dup_sym!r} is shared by the following assets:\n' +
  52. '\n ' + '\n '.join(d['id'] for d in data if d['symbol'] == dup_sym) +
  53. '\n\nPlease specify the asset by one of the full IDs listed above\n' +
  54. f'instead of {dup_sym!r}'
  55. )
  56. def check_assets_found(wants,found,keys=['symbol','id']):
  57. error = False
  58. for k in keys:
  59. missing = wants[k] - found[k]
  60. if missing:
  61. msg(
  62. ('The following IDs were not found in source data:\n{}' if k == 'id' else
  63. 'The following symbols could not be resolved:\n{}').format(
  64. fmt_list(missing,fmt='col',indent=' ')
  65. ))
  66. error = True
  67. if error:
  68. die(1,'Missing data, exiting')
  69. rows_want = {
  70. 'id': {r.id for r in cfg.rows if getattr(r,'id',None)} - {'usd-us-dollar'},
  71. 'symbol': {r.symbol for r in cfg.rows if isinstance(r,tuple) and r.id is None} - {'USD'},
  72. }
  73. usr_rate_assets = tuple(u.rate_asset for u in cfg.usr_rows + cfg.usr_columns if u.rate_asset)
  74. usr_rate_assets_want = {
  75. 'id': {a.id for a in usr_rate_assets if a.id},
  76. 'symbol': {a.symbol for a in usr_rate_assets if not a.id}
  77. }
  78. usr_assets = cfg.usr_rows + cfg.usr_columns + tuple(c for c in (cfg.query or ()) if c)
  79. usr_wants = {
  80. 'id': (
  81. {a.id for a in usr_assets + usr_rate_assets if a.id} -
  82. {a.id for a in usr_assets if a.rate and a.id} - {'usd-us-dollar'} )
  83. ,
  84. 'symbol': (
  85. {a.symbol for a in usr_assets + usr_rate_assets if not a.id} -
  86. {a.symbol for a in usr_assets if a.rate} - {'USD'} ),
  87. }
  88. found = { 'id': set(), 'symbol': set() }
  89. rate_assets = {}
  90. for k in ['id','symbol']:
  91. wants = rows_want[k] | usr_wants[k]
  92. if wants:
  93. for d in data:
  94. if d[k] in wants:
  95. if d[k] in found[k]:
  96. die(1,dup_sym_errmsg(d[k]))
  97. yield (d['id'],d)
  98. found[k].add(d[k])
  99. if d[k] in usr_rate_assets_want[k]:
  100. rate_assets[d['symbol']] = d # NB: using symbol instead of ID
  101. if k == 'id' and len(found[k]) == len(wants):
  102. break
  103. for d in data:
  104. if d['id'] == 'btc-bitcoin':
  105. btcusd = Decimal(d['price_usd'])
  106. break
  107. for asset in (cfg.usr_rows + cfg.usr_columns):
  108. if asset.rate:
  109. """
  110. User-supplied rate overrides rate from source data.
  111. """
  112. _id = asset.id or f'{asset.symbol}-user-asset-{asset.symbol}'.lower()
  113. ra_rate = Decimal(rate_assets[asset.rate_asset.symbol]['price_usd']) if asset.rate_asset else 1
  114. yield ( _id, {
  115. 'symbol': asset.symbol,
  116. 'id': _id,
  117. 'price_usd': str(Decimal(ra_rate/asset.rate)),
  118. 'price_btc': str(Decimal(ra_rate/asset.rate/btcusd)),
  119. 'last_updated': int(now),
  120. })
  121. yield ('usd-us-dollar', {
  122. 'symbol': 'USD',
  123. 'id': 'usd-us-dollar',
  124. 'price_usd': '1.0',
  125. 'price_btc': str(Decimal(1/btcusd)),
  126. 'last_updated': int(now),
  127. })
  128. check_assets_found(usr_wants,found)
  129. def get_src_data(curl_cmd):
  130. tor_captcha_msg = f"""
  131. If you’re using Tor, the API request may have failed due to Captcha protection.
  132. A workaround for this issue is to retrieve the JSON data with a browser from
  133. the following URL:
  134. {api_url}
  135. and save it to:
  136. ‘{cfg.cachedir}/ticker.json’
  137. Then invoke the program with --cached-data and without --btc
  138. """
  139. def rate_limit_errmsg(timeout,elapsed):
  140. return (
  141. f'Rate limit exceeded! Retry in {timeout-elapsed} seconds' +
  142. ('' if cfg.btc_only else ', or use --cached-data or --btc')
  143. )
  144. if not os.path.exists(cachedir):
  145. os.makedirs(cachedir)
  146. if cfg.btc_only:
  147. fn = os.path.join(cfg.cachedir,'ticker-btc.json')
  148. timeout = 5 if g.test_suite else btc_ratelimit
  149. else:
  150. fn = os.path.join(cfg.cachedir,'ticker.json')
  151. timeout = 5 if g.test_suite else ratelimit
  152. fn_rel = os.path.relpath(fn,start=homedir)
  153. if not os.path.exists(fn):
  154. open(fn,'w').write('{}')
  155. if opt.cached_data:
  156. json_text = open(fn).read()
  157. else:
  158. elapsed = int(time.time() - os.stat(fn).st_mtime)
  159. if elapsed >= timeout:
  160. msg_r(f'Fetching data from {api_host}...')
  161. vmsg('')
  162. try:
  163. cp = run(curl_cmd,check=True,stdout=PIPE)
  164. except CalledProcessError as e:
  165. msg('')
  166. from .Misc import curl_exit_codes
  167. msg(red(curl_exit_codes[e.returncode]))
  168. msg(red('Command line:\n {}'.format( ' '.join((repr(i) if ' ' in i else i) for i in e.cmd) )))
  169. from mmgen.exception import MMGenCalledProcessError
  170. raise MMGenCalledProcessError(f'Subprocess returned non-zero exit status {e.returncode}')
  171. json_text = cp.stdout.decode()
  172. msg('done')
  173. else:
  174. die(1,rate_limit_errmsg(timeout,elapsed))
  175. try:
  176. data = json.loads(json_text)
  177. except:
  178. msg(json_text[:1024] + '...')
  179. msg(orange(fmt(tor_captcha_msg,strip_char='\t')))
  180. die(2,'Retrieved data is not valid JSON, exiting')
  181. if not data:
  182. if opt.cached_data:
  183. die(1,'No cached data! Run command without --cached-data option to retrieve data from remote host')
  184. else:
  185. die(2,'Remote host returned no data!')
  186. elif 'error' in data:
  187. die(1,data['error'])
  188. if opt.cached_data:
  189. msg(f'Using cached data from ~/{fn_rel}')
  190. else:
  191. open(fn,'w').write(json_text)
  192. msg(f'JSON data cached to ~/{fn_rel}')
  193. return data
  194. def main(cfg_parm,cfg_in_parm):
  195. def update_sample_file(usr_cfg_file):
  196. src_data = files('mmgen_node_tools').joinpath('data',os.path.basename(usr_cfg_file)).read_text()
  197. sample_file = usr_cfg_file + '.sample'
  198. sample_data = open(sample_file).read() if os.path.exists(sample_file) else None
  199. if src_data != sample_data:
  200. os.makedirs(os.path.dirname(sample_file),exist_ok=True)
  201. msg('{} {}'.format(
  202. ('Updating','Creating')[sample_data is None],
  203. sample_file ))
  204. open(sample_file,'w').write(src_data)
  205. def get_curl_cmd():
  206. return ([
  207. 'curl',
  208. '--tr-encoding',
  209. '--compressed', # adds 'Accept-Encoding: gzip'
  210. '--header', 'Accept: application/json',
  211. ] +
  212. (['--proxy', cfg.proxy] if cfg.proxy else []) +
  213. (['--silent'] if not opt.verbose else []) +
  214. [api_url + ('/btc-bitcoin' if cfg.btc_only else '')]
  215. )
  216. global cfg,cfg_in
  217. cfg = cfg_parm
  218. cfg_in = cfg_in_parm
  219. try:
  220. from importlib.resources import files # Python 3.9
  221. except ImportError:
  222. from importlib_resources import files
  223. update_sample_file(cfg_in.cfg_file)
  224. update_sample_file(cfg_in.portfolio_file)
  225. if opt.portfolio and not cfg_in.portfolio:
  226. die(1,'No portfolio configured!\nTo configure a portfolio, edit the file ~/{}'.format(
  227. os.path.relpath(cfg_in.portfolio_file,start=homedir)))
  228. curl_cmd = get_curl_cmd()
  229. if opt.print_curl:
  230. Msg(curl_cmd + '\n' + ' '.join(curl_cmd))
  231. return
  232. parsed_json = [get_src_data(curl_cmd)] if cfg.btc_only else get_src_data(curl_cmd)
  233. if opt.list_ids:
  234. from mmgen.ui import do_pager
  235. do_pager('\n'.join(e['id'] for e in parsed_json))
  236. return
  237. global now
  238. now = 1659465400 if g.test_suite else time.time() # 1659524400 1659445900
  239. stdout_or_pager(
  240. '\n'.join(getattr(Ticker,cfg.clsname)(dict(gen_data(parsed_json))).gen_output()) + '\n'
  241. )
  242. def make_cfg(cmd_args,cfg_in):
  243. def get_rows_from_cfg(add_data=None):
  244. def gen():
  245. for n,(k,v) in enumerate(cfg_in.cfg['assets'].items()):
  246. yield(k)
  247. if add_data and k in add_data:
  248. v += tuple(add_data[k])
  249. for e in v:
  250. yield parse_asset_id(e,True)
  251. return tuple(gen())
  252. def parse_asset_id(s,require_label=False):
  253. sym,label = (*s.split('-',1),None)[:2]
  254. if require_label and not label:
  255. die(1,f'{s!r}: asset label is missing')
  256. return asset_tuple( sym.upper(), (s.lower() if label else None) )
  257. def parse_usr_asset_arg(s):
  258. """
  259. asset_id[:rate[:rate_asset]]
  260. """
  261. def parse_parm(s):
  262. ss = s.split(':')
  263. assert len(ss) in (1,2,3), f'{s}: malformed argument'
  264. asset_id,rate,rate_asset = (*ss,None,None)[:3]
  265. parsed_id = parse_asset_id(asset_id)
  266. return asset_data(
  267. symbol = parsed_id.symbol,
  268. id = parsed_id.id,
  269. amount = None,
  270. rate = (
  271. None if rate is None else
  272. 1 / Decimal(rate[:-1]) if rate.lower().endswith('r') else
  273. Decimal(rate) ),
  274. rate_asset = parse_asset_id(rate_asset) if rate_asset else None )
  275. return tuple(parse_parm(s2) for s2 in s.split(',')) if s else ()
  276. def parse_query_arg(s):
  277. """
  278. asset_id:amount[:to_asset_id[:to_amount]]
  279. """
  280. def parse_query_asset(asset_id,amount):
  281. parsed_id = parse_asset_id(asset_id)
  282. return asset_data(
  283. symbol = parsed_id.symbol,
  284. id = parsed_id.id,
  285. amount = None if amount is None else Decimal(amount),
  286. rate = None,
  287. rate_asset = None )
  288. ss = s.split(':')
  289. assert len(ss) in (2,3,4), f'{s}: malformed argument'
  290. asset_id,amount,to_asset_id,to_amount = (*ss,None,None)[:4]
  291. return query_tuple(
  292. asset = parse_query_asset(asset_id,amount),
  293. to_asset = parse_query_asset(to_asset_id,to_amount) if to_asset_id else None
  294. )
  295. def gen_uniq(obj_list,key,preload=None):
  296. found = set([getattr(obj,key) for obj in preload if hasattr(obj,key)] if preload else ())
  297. for obj in obj_list:
  298. id = getattr(obj,key)
  299. if id not in found:
  300. yield obj
  301. found.add(id)
  302. def get_usr_assets():
  303. return (
  304. 'user_added',
  305. usr_rows +
  306. (tuple(asset for asset in query if asset) if query else ()) +
  307. usr_columns )
  308. def get_portfolio_assets(ret=()):
  309. if cfg_in.portfolio and opt.portfolio:
  310. ret = (parse_asset_id(e,True) for e in cfg_in.portfolio)
  311. return ( 'portfolio', tuple(e for e in ret if (not opt.btc) or e.symbol == 'BTC') )
  312. def get_portfolio():
  313. return {k:Decimal(v) for k,v in cfg_in.portfolio.items() if (not opt.btc) or k == 'btc-bitcoin'}
  314. def parse_add_precision(s):
  315. if not s:
  316. return 0
  317. if not (s.isdigit() and s.isascii()):
  318. die(1,f'{s}: invalid parameter for --add-precision (not an integer)')
  319. if int(s) > 30:
  320. die(1,f'{s}: invalid parameter for --add-precision (value >30)')
  321. return int(s)
  322. def create_rows():
  323. rows = (
  324. ('trade_pair',) + query if (query and query.to_asset) else
  325. ('bitcoin',parse_asset_id('btc-bitcoin')) if opt.btc else
  326. get_rows_from_cfg( add_data={'fiat':['usd-us-dollar']} if opt.add_columns else None )
  327. )
  328. for hdr,data in (
  329. (get_usr_assets(),) if query else
  330. (get_usr_assets(), get_portfolio_assets())
  331. ):
  332. if data:
  333. uniq_data = tuple(gen_uniq(data,'symbol',preload=rows))
  334. if uniq_data:
  335. rows += (hdr,) + uniq_data
  336. return rows
  337. cfg_tuple = namedtuple('global_cfg',[
  338. 'rows',
  339. 'usr_rows',
  340. 'usr_columns',
  341. 'query',
  342. 'adjust',
  343. 'clsname',
  344. 'btc_only',
  345. 'add_prec',
  346. 'cachedir',
  347. 'proxy',
  348. 'portfolio' ])
  349. query_tuple = namedtuple('query',['asset','to_asset'])
  350. asset_data = namedtuple('asset_data',['symbol','id','amount','rate','rate_asset'])
  351. asset_tuple = namedtuple('asset_tuple',['symbol','id'])
  352. usr_rows = parse_usr_asset_arg(opt.add_rows)
  353. usr_columns = parse_usr_asset_arg(opt.add_columns)
  354. query = parse_query_arg(cmd_args[0]) if cmd_args else None
  355. return cfg_tuple(
  356. rows = create_rows(),
  357. usr_rows = usr_rows,
  358. usr_columns = usr_columns,
  359. query = query,
  360. adjust = ( lambda x: (100 + x) / 100 if x else 1 )( Decimal(opt.adjust or 0) ),
  361. clsname = 'trading' if query else 'overview',
  362. btc_only = opt.btc,
  363. add_prec = parse_add_precision(opt.add_precision),
  364. cachedir = opt.cachedir or cfg_in.cfg.get('cachedir') or cachedir,
  365. proxy = None if opt.proxy == '' else (opt.proxy or cfg_in.cfg.get('proxy')),
  366. portfolio = get_portfolio() if cfg_in.portfolio and opt.portfolio and not query else None
  367. )
  368. def get_cfg_in():
  369. ret = namedtuple('cfg_in_data',['cfg','portfolio','cfg_file','portfolio_file'])
  370. cfg_file,portfolio_file = (
  371. [os.path.join(g.data_dir_root,'node_tools',fn) for fn in (cfg_fn,portfolio_fn)]
  372. )
  373. cfg_data,portfolio_data = (
  374. [yaml.safe_load(open(fn).read()) if os.path.exists(fn) else None for fn in (cfg_file,portfolio_file)]
  375. )
  376. return ret(
  377. cfg = cfg_data or {
  378. 'assets': {
  379. 'coin': [ 'btc-bitcoin', 'eth-ethereum', 'xmr-monero' ],
  380. 'commodity': [ 'xau-gold-spot-token', 'xag-silver-spot-token', 'xbr-brent-crude-oil-spot' ],
  381. 'fiat': [ 'gbp-pound-sterling-token', 'eur-euro-token' ],
  382. 'index': [ 'dj30-dow-jones-30-token', 'spx-sp-500', 'ndx-nasdaq-100-token' ],
  383. },
  384. 'proxy': 'http://vpn-gw:8118'
  385. },
  386. portfolio = portfolio_data,
  387. cfg_file = cfg_file,
  388. portfolio_file = portfolio_file,
  389. )
  390. class Ticker:
  391. class base:
  392. offer = None
  393. to_asset = None
  394. def __init__(self,data):
  395. self.comma = ',' if opt.thousands_comma else ''
  396. self.col1_wid = max(len('TOTAL'),(
  397. max(len(self.create_label(d['id'])) for d in data.values()) if opt.name_labels else
  398. max(len(d['symbol']) for d in data.values())
  399. )) + 1
  400. self.rows = [row._replace(id=self.get_id(row)) if isinstance(row,tuple) else row for row in cfg.rows]
  401. self.col_usd_prices = {k:Decimal(self.data[k]['price_usd']) for k in self.col_ids}
  402. self.prices = {row.id:self.get_row_prices(row.id)
  403. for row in self.rows if isinstance(row,tuple) and row.id in data}
  404. self.prices['usd-us-dollar'] = self.get_row_prices('usd-us-dollar')
  405. def format_last_update_col(self,cross_assets=()):
  406. if opt.elapsed:
  407. from mmgen.util2 import format_elapsed_hr
  408. fmt_func = format_elapsed_hr
  409. else:
  410. fmt_func = lambda t,now: time.strftime('%F %X',time.gmtime(t)) # ticker API
  411. # t.replace('T',' ').replace('Z','') # tickers API
  412. d = self.data
  413. max_w = 0
  414. min_t = min( (int(d[a.id]['last_updated']) for a in cross_assets), default=None )
  415. for row in self.rows:
  416. if isinstance(row,tuple):
  417. try:
  418. t = int(d[row.id]['last_updated'])
  419. except KeyError:
  420. pass
  421. else:
  422. t_fmt = d[row.id]['last_updated_fmt'] = fmt_func( (min(t,min_t) if min_t else t), now )
  423. max_w = max(len(t_fmt),max_w)
  424. self.upd_w = max_w
  425. def init_prec(self):
  426. exp = [(a.id,Decimal.adjusted(self.prices[a.id]['usd-us-dollar'])) for a in self.usr_col_assets]
  427. self.uprec = { k: max(0,v+4) + cfg.add_prec for k,v in exp }
  428. self.uwid = { k: 12 + max(0, abs(v)-6) + cfg.add_prec for k,v in exp }
  429. def get_id(self,asset):
  430. if asset.id:
  431. return asset.id
  432. else:
  433. for d in self.data.values():
  434. if d['symbol'] == asset.symbol:
  435. return d['id']
  436. def create_label(self,id):
  437. return ' '.join(id.split('-')[1:]).upper()
  438. def gen_output(self):
  439. yield 'Current time: {} UTC'.format(time.strftime('%F %X',time.gmtime(now)))
  440. for asset in self.usr_col_assets:
  441. if asset.symbol != 'USD':
  442. usdprice = Decimal(self.data[asset.id]['price_usd'])
  443. yield '{} ({}) = {:{}.{}f} USD'.format(
  444. asset.symbol,
  445. self.create_label(asset.id),
  446. usdprice,
  447. self.comma,
  448. max(2,int(-usdprice.adjusted())+4) )
  449. if hasattr(self,'subhdr'):
  450. yield self.subhdr
  451. if self.show_adj:
  452. yield (
  453. ('Offered price differs from spot' if self.offer else 'Adjusting prices')
  454. + ' by '
  455. + yellow('{:+.2f}%'.format( (self.adjust-1) * 100 ))
  456. )
  457. yield ''
  458. if cfg.portfolio:
  459. yield blue('PRICES')
  460. if self.table_hdr:
  461. yield self.table_hdr
  462. for row in self.rows:
  463. if isinstance(row,str):
  464. yield ('-' * self.hl_wid)
  465. else:
  466. try:
  467. yield self.fmt_row(self.data[row.id])
  468. except KeyError:
  469. yield gray(f'(no data for {row.id})')
  470. yield '-' * self.hl_wid
  471. if cfg.portfolio:
  472. self.fs_num = self.fs_num2
  473. self.fs_str = self.fs_str2
  474. yield ''
  475. yield blue('PORTFOLIO')
  476. yield self.table_hdr
  477. yield '-' * self.hl_wid
  478. for sym,amt in cfg.portfolio.items():
  479. try:
  480. yield self.fmt_row(self.data[sym],amt=amt)
  481. except KeyError:
  482. yield gray(f'(no data for {sym})')
  483. yield '-' * self.hl_wid
  484. if not cfg.btc_only:
  485. yield self.fs_num.format(
  486. lbl = 'TOTAL', pc1='', pc2='', upd='', amt='',
  487. **{ k.replace('-','_'): v for k,v in self.prices['total'].items() }
  488. )
  489. class overview(base):
  490. def __init__(self,data):
  491. self.data = data
  492. self.adjust = cfg.adjust
  493. self.show_adj = self.adjust != 1
  494. self.usr_col_assets = [asset._replace(id=self.get_id(asset)) for asset in cfg.usr_columns]
  495. self.col_ids = ('usd-us-dollar',) + tuple(a.id for a in self.usr_col_assets) + ('btc-bitcoin',)
  496. super().__init__(data)
  497. self.format_last_update_col()
  498. if cfg.portfolio:
  499. self.prices['total'] = { col_id: sum(self.prices[row.id][col_id] * cfg.portfolio[row.id]
  500. for row in self.rows if isinstance(row,tuple) and row.id in cfg.portfolio and row.id in data)
  501. for col_id in self.col_ids }
  502. self.init_prec()
  503. self.init_fs()
  504. def get_row_prices(self,id):
  505. if id in self.data:
  506. d = self.data[id]
  507. return { k: (
  508. Decimal(d['price_btc']) if k == 'btc-bitcoin' else
  509. Decimal(d['price_usd']) / self.col_usd_prices[k]
  510. ) * self.adjust for k in self.col_ids }
  511. def fmt_row(self,d,amt=None,amt_fmt=None):
  512. def fmt_pct(d):
  513. if d in ('',None):
  514. return gray(' --')
  515. n = Decimal(d)
  516. return (red,green)[n>=0](f'{n:+7.2f}')
  517. p = self.prices[d['id']]
  518. if amt is not None:
  519. amt_fmt = f'{amt:{19+cfg.add_prec}{self.comma}.{8+cfg.add_prec}f}'
  520. if '.' in amt_fmt:
  521. amt_fmt = amt_fmt.rstrip('0').rstrip('.')
  522. return self.fs_num.format(
  523. lbl = (self.create_label(d['id']) if opt.name_labels else d['symbol']),
  524. pc1 = fmt_pct(d.get('percent_change_7d')),
  525. pc2 = fmt_pct(d.get('percent_change_24h')),
  526. upd = d.get('last_updated_fmt'),
  527. amt = amt_fmt,
  528. **{ k.replace('-','_'): v * (1 if amt is None else amt) for k,v in p.items() }
  529. )
  530. def init_fs(self):
  531. col_prec = {'usd-us-dollar':2+cfg.add_prec,'btc-bitcoin':8+cfg.add_prec } # | self.uprec # Python 3.9
  532. col_prec.update(self.uprec)
  533. col_wid = {'usd-us-dollar':8+cfg.add_prec,'btc-bitcoin':12+cfg.add_prec } # """
  534. col_wid.update(self.uwid)
  535. max_row = max(
  536. ( (k,v['btc-bitcoin']) for k,v in self.prices.items() ),
  537. key = lambda a: a[1]
  538. )
  539. widths = { k: len('{:{}.{}f}'.format( self.prices[max_row[0]][k], self.comma, col_prec[k] ))
  540. for k in self.col_ids }
  541. fd = namedtuple('format_str_data',['fs_str','fs_num','wid'])
  542. col_fs_data = {
  543. 'label': fd(f'{{lbl:{self.col1_wid}}}',f'{{lbl:{self.col1_wid}}}',self.col1_wid),
  544. 'pct7d': fd(' {pc1:7}', ' {pc1:7}', 8),
  545. 'pct24h': fd(' {pc2:7}', ' {pc2:7}', 8),
  546. 'update_time': fd(' {upd}', ' {upd}', max((19 if cfg.portfolio else 0),self.upd_w) + 2),
  547. 'amt': fd(' {amt}', ' {amt}', 21),
  548. }
  549. # } | { k: fd( # Python 3.9
  550. col_fs_data.update({ k: fd(
  551. ' {{{}:>{}}}'.format( k.replace('-','_'), widths[k] ),
  552. ' {{{}:{}{}.{}f}}'.format( k.replace('-','_'), widths[k], self.comma, col_prec[k] ),
  553. widths[k]+2
  554. ) for k in self.col_ids
  555. })
  556. cols = (
  557. ['label','usd-us-dollar'] +
  558. [asset.id for asset in self.usr_col_assets] +
  559. [a for a,b in (
  560. ( 'btc-bitcoin', not cfg.btc_only ),
  561. ( 'pct7d', opt.percent_change ),
  562. ( 'pct24h', opt.percent_change ),
  563. ( 'update_time', opt.update_time ),
  564. ) if b]
  565. )
  566. cols2 = list(cols)
  567. if opt.update_time:
  568. cols2.pop()
  569. cols2.append('amt')
  570. self.fs_str = ''.join(col_fs_data[c].fs_str for c in cols)
  571. self.fs_num = ''.join(col_fs_data[c].fs_num for c in cols)
  572. self.hl_wid = sum(col_fs_data[c].wid for c in cols)
  573. self.fs_str2 = ''.join(col_fs_data[c].fs_str for c in cols2)
  574. self.fs_num2 = ''.join(col_fs_data[c].fs_num for c in cols2)
  575. self.hl_wid2 = sum(col_fs_data[c].wid for c in cols2)
  576. @property
  577. def table_hdr(self):
  578. return self.fs_str.format(
  579. lbl = '',
  580. pc1 = ' CHG_7d',
  581. pc2 = 'CHG_24h',
  582. upd = 'UPDATED',
  583. amt = ' AMOUNT',
  584. usd_us_dollar = 'USD',
  585. btc_bitcoin = ' BTC',
  586. **{ a.id.replace('-','_'): a.symbol for a in self.usr_col_assets }
  587. )
  588. class trading(base):
  589. def __init__(self,data):
  590. self.data = data
  591. self.asset = cfg.query.asset._replace(id=self.get_id(cfg.query.asset))
  592. self.to_asset = (
  593. cfg.query.to_asset._replace(id=self.get_id(cfg.query.to_asset))
  594. if cfg.query.to_asset else None )
  595. self.col_ids = [self.asset.id]
  596. self.adjust = cfg.adjust
  597. if self.to_asset:
  598. self.offer = self.to_asset.amount
  599. if self.offer:
  600. real_price = (
  601. self.asset.amount
  602. * Decimal(data[self.asset.id]['price_usd'])
  603. / Decimal(data[self.to_asset.id]['price_usd'])
  604. )
  605. if self.adjust != 1:
  606. die(1,'the --adjust option may not be combined with TO_AMOUNT in the trade specifier')
  607. self.adjust = self.offer / real_price
  608. self.hl_ids = [self.asset.id,self.to_asset.id]
  609. else:
  610. self.hl_ids = [self.asset.id]
  611. self.show_adj = self.adjust != 1 or self.offer
  612. super().__init__(data)
  613. self.usr_col_assets = [self.asset] + ([self.to_asset] if self.to_asset else [])
  614. for a in self.usr_col_assets:
  615. self.prices[a.id]['usd-us-dollar'] = Decimal(data[a.id]['price_usd'])
  616. self.format_last_update_col(cross_assets=self.usr_col_assets)
  617. self.init_prec()
  618. self.init_fs()
  619. def get_row_prices(self,id):
  620. if id in self.data:
  621. d = self.data[id]
  622. return { k: self.col_usd_prices[self.asset.id] / Decimal(d['price_usd']) for k in self.col_ids }
  623. def init_fs(self):
  624. self.max_wid = max(
  625. len('{:{}{}.{}f}'.format(
  626. v[self.asset.id] * self.asset.amount,
  627. 16 + cfg.add_prec,
  628. self.comma,
  629. 8 + cfg.add_prec
  630. ))
  631. for v in self.prices.values()
  632. )
  633. self.fs_str = '{lbl:%s} {p_spot}' % self.col1_wid
  634. self.hl_wid = self.col1_wid + self.max_wid + 1
  635. if self.show_adj:
  636. self.fs_str += ' {p_adj}'
  637. self.hl_wid += self.max_wid + 1
  638. if opt.update_time:
  639. self.fs_str += ' {upd}'
  640. self.hl_wid += self.upd_w + 2
  641. def fmt_row(self,d):
  642. id = d['id']
  643. p = self.prices[id][self.asset.id] * self.asset.amount
  644. p_spot = '{:{}{}.{}f}'.format( p, self.max_wid, self.comma, 8+cfg.add_prec )
  645. p_adj = (
  646. '{:{}{}.{}f}'.format( p*self.adjust, self.max_wid, self.comma, 8+cfg.add_prec )
  647. if self.show_adj else '' )
  648. return self.fs_str.format(
  649. lbl = (self.create_label(id) if opt.name_labels else d['symbol']),
  650. p_spot = green(p_spot) if id in self.hl_ids else p_spot,
  651. p_adj = yellow(p_adj) if id in self.hl_ids else p_adj,
  652. upd = d.get('last_updated_fmt'),
  653. )
  654. @property
  655. def table_hdr(self):
  656. return self.fs_str.format(
  657. lbl = '',
  658. p_spot = '{t:>{w}}'.format(
  659. t = 'SPOT PRICE',
  660. w = self.max_wid ),
  661. p_adj = '{t:>{w}}'.format(
  662. t = ('OFFERED' if self.offer else 'ADJUSTED') + ' PRICE',
  663. w = self.max_wid ),
  664. upd = 'UPDATED'
  665. )
  666. @property
  667. def subhdr(self):
  668. return (
  669. '{a}: {b:{c}} {d}'.format(
  670. a = 'Offer' if self.offer else 'Amount',
  671. b = self.asset.amount,
  672. c = self.comma,
  673. d = self.asset.symbol
  674. ) + (
  675. (
  676. ' =>' +
  677. (' {:{}}'.format(self.offer,self.comma) if self.offer else '') +
  678. ' {} ({})'.format(
  679. self.to_asset.symbol,
  680. self.create_label(self.to_asset.id) )
  681. ) if self.to_asset else '' )
  682. )