main_ticker.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. mmnode-ticker: Display price information for cryptocurrency and other assets
  12. """
  13. import sys,os
  14. from mmgen.common import *
  15. from .Ticker import *
  16. opts_data = {
  17. 'sets': [
  18. ('wide', True, 'percent_change', True),
  19. ('wide', True, 'name_labels', True),
  20. ('wide', True, 'thousands_comma', True),
  21. ('wide', True, 'update_time', True),
  22. ],
  23. 'text': {
  24. 'desc': 'Display prices for cryptocurrency and other assets',
  25. 'usage': '[opts] [TRADE_SPECIFIER]',
  26. 'options': f"""
  27. -h, --help Print this help message
  28. --, --longhelp Print help message for long options (common options)
  29. -A, --adjust=P Adjust prices by percentage ‘P’. In ‘trading’ mode,
  30. spot and adjusted prices are shown in separate columns.
  31. -b, --btc Fetch and display data for Bitcoin only
  32. -c, --add-columns=LIST Add columns for asset specifiers in LIST (comma-
  33. separated, see ASSET SPECIFIERS below). Can also be
  34. used to supply a USD exchange rate for missing assets.
  35. -C, --cached-data Use cached data from previous network query instead of
  36. live data from server
  37. -d, --cachedir=D Read and write cached JSON data to directory ‘D’
  38. instead of ‘~/{os.path.relpath(cachedir,start=homedir)}’
  39. -e, --add-precision=N Add ‘N’ digits of precision to columns
  40. -E, --elapsed Show elapsed time in UPDATED column (see --update-time)
  41. -F, --portfolio Display portfolio data
  42. -l, --list-ids List IDs of all available assets
  43. -n, --name-labels Label rows with asset names rather than symbols
  44. -p, --percent-change Add percentage change columns
  45. -P, --pager Pipe the output to a pager
  46. -r, --add-rows=LIST Add rows for asset specifiers in LIST (comma-separated,
  47. see ASSET SPECIFIERS below). Can also be used to supply
  48. a USD exchange rate for missing assets.
  49. -T, --thousands-comma Use comma as a thousands separator
  50. -u, --update-time Include UPDATED (last update time) column
  51. -U, --print-curl Print cURL command to standard output and exit
  52. -w, --wide Display all optional columns (equivalent to -punT)
  53. -x, --proxy=P Connect via proxy ‘P’. Set to the empty string to
  54. disable. Consult the curl manpage for --proxy usage.
  55. """,
  56. 'notes': """
  57. The script has two display modes: ‘overview’, the default, and ‘trading’, the
  58. latter being enabled when a TRADE_SPECIFIER argument (see below) is supplied
  59. on the command line.
  60. Overview mode displays prices of all configured assets, and optionally the
  61. user’s portfolio, while trading mode displays the price of a given quantity
  62. of an asset in relation to other assets, optionally comparing an offered
  63. price to the spot price.
  64. ASSETS consist of either a symbol (e.g. ‘xmr’) or full ID (see --list-ids)
  65. consisting of symbol plus label (e.g. ‘xmr-monero’). In cases where the
  66. symbol is ambiguous, the full ID must be used. Examples:
  67. chf - specify asset by symbol
  68. chf-swiss-franc-token - same as above, but use full ID instead of symbol
  69. ASSET SPECIFIERS have the following format:
  70. ASSET[:RATE]
  71. If the asset referred to by ASSET is not in the source data (see --list-ids),
  72. an arbitrarily chosen label may be used. RATE is the USD exchange rate of
  73. the asset. When RATE is postfixed with the letter ‘r’, its meaning is
  74. reversed, i.e. interpreted as ‘ASSET/USD’ instead of ‘USD/ASSET’. Asset
  75. specifier examples:
  76. inr:79.5 - INR is not in the source data, so supply rate of
  77. 79.5 INR to the Dollar (USD/INR)
  78. inr:0.01257r - same as above, but use reverse rate (INR/USD)
  79. inr-indian-rupee:79.5 - same as first example, but add an arbitrary label
  80. omr-omani-rial:2.59r - Omani Rial is pegged to the Dollar at 2.59 USD
  81. A TRADE_SPECIFIER is a single argument in the format:
  82. ASSET:AMOUNT[:TO_ASSET[:TO_AMOUNT]]
  83. Examples:
  84. xmr:17.34 - price of 17.34 XMR in all configured assets
  85. xmr-monero:17.34 - same as above, but with full ID
  86. xmr:17.34:eur - price of 17.34 XMR in EUR only
  87. xmr:17.34:eur:2800 - commission on an offer of 17.34 XMR for 2800 EUR
  88. TO_AMOUNT, if included, is used to calculate the percentage difference or
  89. commission on an offer compared to the spot price.
  90. If either ASSET or TO_ASSET refer to assets not present in the source data,
  91. a USD rate for the missing asset(s) must be supplied via the --add-columns
  92. or --add-rows options.
  93. PROXY NOTE
  94. The remote server used to obtain the price data, {api_host!r}, blocks
  95. Tor behind a Captcha wall, so a Tor proxy cannot be used directly. If you’re
  96. concerned about privacy, connect via a VPN, or better yet, VPN over Tor. Then
  97. set up an HTTP proxy (e.g. Privoxy) on the VPN’ed host and set the ‘proxy’
  98. option in the config file or --proxy on the command line accordingly. Or run
  99. the script directly on the VPN’ed host with ’proxy’ or --proxy set to the
  100. null string.
  101. Alternatively, you may download the JSON source data in a Tor-proxied browser
  102. from ‘{api_url}’, save it as ‘ticker.json’ in your
  103. configured cache directory, and run the script with the --cached-data option.
  104. RATE LIMITING NOTE
  105. To protect user privacy, all filtering and processing of data is performed
  106. client side so that the remote server does not know which assets are being
  107. examined. This means that data for ALL available assets (currently over 4000)
  108. is fetched with each invocation of the script. A rate limit of {L} seconds
  109. between calls is thus imposed to prevent abuse of the remote server. When the
  110. --btc option is in effect, this limit is reduced to {B} seconds. To bypass the
  111. rate limit entirely, use --cached-data.
  112. EXAMPLES
  113. # Basic display in ‘overview’ mode:
  114. $ mmnode-ticker
  115. # Display BTC price only:
  116. $ mmnode-ticker --btc
  117. # Wide display, add EUR and OMR columns, OMR/USD rate, extra precision and
  118. # proxy:
  119. $ mmnode-ticker -w -c eur,omr-omani-rial:2.59r -e2 -x http://vpnhost:8118
  120. # Wide display, use cached data from previous network query, show portfolio
  121. # (see above), pipe output to pager, add DOGE row:
  122. $ mmnode-ticker -wCFP -r doge
  123. # Display 17.234 XMR priced in all configured assets (‘trading’ mode):
  124. $ mmnode-ticker xmr:17.234
  125. # Same as above, but add INR price at specified USDINR rate:
  126. $ mmnode-ticker -c inr:79.5 xmr:17.234
  127. # Same as above, but view INR price only at specified rate, adding label:
  128. $ mmnode-ticker -c inr-indian-rupee:79.5 xmr:17.234:inr
  129. # Calculate commission on an offer of 2700 USD for 0.123 BTC, compared to
  130. # current spot price:
  131. $ mmnode-ticker usd:2700:btc:0.123
  132. # Calculate commission on an offer of 200000 INR for 0.1 BTC, compared to
  133. # current spot price, at specified USDINR rate:
  134. $ mmnode-ticker -n -c inr-indian-rupee:79.5 inr:200000:btc:0.1
  135. CONFIGURED ASSETS:
  136. {assets}
  137. Customize output by editing the file
  138. ~/{cfg}
  139. To add a portfolio, edit the file
  140. ~/{pf_cfg}
  141. """
  142. },
  143. 'code': {
  144. 'notes': lambda s: s.format(
  145. assets = fmt_list(assets_list_gen(cfg_in),fmt='col',indent=' '),
  146. cfg = os.path.relpath(cfg_in.cfg_file,start=homedir),
  147. pf_cfg = os.path.relpath(cfg_in.portfolio_file,start=homedir),
  148. api_host = api_host,
  149. api_url = api_url,
  150. L = ratelimit,
  151. B = btc_ratelimit,
  152. )
  153. }
  154. }
  155. cmd_args = opts.init(opts_data,do_post_init=True)
  156. cfg_in = get_cfg_in()
  157. cfg = make_cfg(cmd_args,cfg_in)
  158. opts.post_init()
  159. main(cfg,cfg_in)