plot-crypto 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #!/usr/bin/python3.6
  2. import matplotlib
  3. import matplotlib.pyplot as pp
  4. import numpy as np
  5. import json
  6. import sys
  7. import getopt
  8. #load theme colors
  9. theme_file = open("colors.json", 'r')
  10. theme_objs = json.load(theme_file)
  11. theme_file.close()
  12. tickers = ['btc', 'eth']
  13. stable_tickers = ['usdt', 'usdc']
  14. def compile_prices(ticker):
  15. ohlc_db = open('dbs/crypto-ohlc-'+ticker+'.db.json', 'r')
  16. ohlc_objs = json.load(ohlc_db)
  17. ohlc_db.close()
  18. prices = {'timestamps': [], 'values': []}
  19. for timestamp, price in ohlc_objs.items():
  20. prices['timestamps'].append( (int(timestamp)//3600)-1 )
  21. prices['values'].append(float(price))
  22. return prices
  23. def aggregate_stable_outs():
  24. txs_db = {}
  25. txs_objs = {}
  26. for ticker in stable_tickers:
  27. txs_db[ticker] = open('dbs/crypto-txs-'+ticker+'.db.json', 'r')
  28. txs_objs[ticker] = json.load(txs_db[ticker])
  29. txs_db[ticker].close()
  30. aggregated_outs = {} # [timestamp] = net_val
  31. for ticker in stable_tickers:
  32. for timestamp in txs_objs[ticker]:
  33. aggregated_outs[timestamp] = 0
  34. for ticker in stable_tickers:
  35. for timestamp, accumulator in txs_objs[ticker].items():
  36. aggregated_outs[timestamp] += accumulator['out']
  37. return aggregated_outs
  38. def aggregate_stable_ins():
  39. txs_db = {}
  40. txs_objs = {}
  41. for ticker in stable_tickers:
  42. txs_db[ticker] = open('dbs/crypto-txs-'+ticker+'.db.json', 'r')
  43. txs_objs[ticker] = json.load(txs_db[ticker])
  44. txs_db[ticker].close()
  45. aggregated_ins = {} # [timestamp] = net_val
  46. for ticker in stable_tickers:
  47. for timestamp in txs_objs[ticker]:
  48. aggregated_ins[timestamp] = 0
  49. for ticker in stable_tickers:
  50. for timestamp, accumulator in txs_objs[ticker].items():
  51. aggregated_ins[timestamp] += accumulator['in']
  52. return aggregated_ins
  53. def compile_stable_ins(t):
  54. aggregated_ins = aggregate_stable_ins()
  55. ins = {'timestamps': [], 'values': []}
  56. for timestamp, value in aggregated_ins.items():
  57. ins['timestamps'].append( int(timestamp)//3600 )
  58. ins['values'].append( value )
  59. return ins
  60. def compile_stable_net(t):
  61. ins = aggregate_stable_ins()
  62. outs = aggregate_stable_outs()
  63. nets = {'timestamps': [], 'values': []}
  64. for timestamp in ins:
  65. nets['timestamps'].append( int(timestamp)//3600 )
  66. nets['values'].append( ins[timestamp] - outs[timestamp] )
  67. return nets
  68. def compile_ins(ticker):
  69. txs_db = open('dbs/crypto-txs-'+ticker+'.db.json', 'r')
  70. txs_objs = json.load(txs_db)
  71. txs_db.close()
  72. ins = {'timestamps': [], 'values': []}
  73. for timestamp, accumulator in txs_objs.items():
  74. ins['timestamps'].append(int(timestamp)//3600)
  75. ins['values'].append( accumulator['in'] )
  76. return ins
  77. def compile_net(ticker):
  78. txs_db = open('dbs/crypto-txs-'+ticker+'.db.json', 'r')
  79. txs_objs = json.load(txs_db)
  80. txs_db.close()
  81. nets = {'timestamps': [], 'values': []}
  82. for timestamp, accumulator in txs_objs.items():
  83. nets['timestamps'].append(int(timestamp)//3600)
  84. nets['values'].append( accumulator['in'] - accumulator['out'] )
  85. return nets
  86. def compile_dif_ins(ticker):
  87. stable_ins = aggregate_stable_ins()
  88. txs_db = open('dbs/crypto-txs-'+ticker+'.db.json', 'r')
  89. txs_objs = json.load(txs_db)
  90. txs_db.close()
  91. dif_ins = {'timestamps': [], 'values': []}
  92. for timestamp, accumulator in txs_objs.items():
  93. dif_ins['timestamps'].append(int(timestamp)//3600)
  94. dif_ins['values'].append( stable_ins[timestamp] - accumulator['in'] )
  95. return dif_ins
  96. def compile_dif_net(ticker):
  97. stable_ins = aggregate_stable_ins()
  98. stable_outs = aggregate_stable_outs()
  99. stable_nets = {}
  100. for timestamp in stable_ins:
  101. stable_nets[timestamp] = stable_ins[timestamp] - stable_outs[timestamp]
  102. txs_db = open('dbs/crypto-txs-'+ticker+'.db.json', 'r')
  103. txs_objs = json.load(txs_db)
  104. txs_db.close()
  105. dif_net = {'timestamps': [], 'values': []}
  106. for timestamp, accumulator in txs_objs.items():
  107. dif_net['timestamps'].append(int(timestamp)//3600)
  108. dif_net['values'].append( stable_nets[timestamp] - (accumulator['in'] - accumulator['out']) )
  109. return dif_net
  110. functions = { '--i': compile_ins, '--n': compile_net, '--di': compile_dif_ins, '--dn': compile_dif_net, '--si': compile_stable_ins, '--sn': compile_stable_net }
  111. def main():
  112. argv = sys.argv[1:]
  113. opts, args = getopt.getopt(argv, '', longopts=['i','n','di','dn','si','sn' ])
  114. print(opts)
  115. if len(args) < 1:
  116. print("error: no ticker")
  117. sys.exit()
  118. valid_args = False
  119. for ticker in tickers:
  120. if args[0] == ticker:
  121. valid_args = True
  122. break
  123. if not valid_args:
  124. print("unknown ticker")
  125. sys.exit()
  126. coin = args[0]
  127. #matplotlib.rcParams['toolbar'] = 'None'
  128. data_arrays = []
  129. data_arrays.append( compile_prices(coin) )
  130. for option in opts:
  131. data_arrays.append( functions[option[0]](coin) )
  132. plot_arrays( data_arrays )
  133. def plot_arrays( data_arrays ):
  134. axes = pp.subplot(len(data_arrays),1,1)
  135. price_axes = axes
  136. axes.plot(data_arrays[0]['timestamps'], data_arrays[0]['values'], marker='o',linestyle=' ', color=theme_objs['colors']['color1'])
  137. for spine, s in axes.spines.items():
  138. s.set_color(theme_objs['colors']['color15'])
  139. for ticklabel in axes.xaxis.get_ticklabels():
  140. ticklabel.set_color(theme_objs['special']['foreground'])
  141. for ticklabel in axes.yaxis.get_ticklabels():
  142. ticklabel.set_color(theme_objs['special']['foreground'])
  143. for gridline in axes.get_xgridlines():
  144. gridline.set_color(theme_objs['colors']['color8'])
  145. for gridline in axes.get_ygridlines():
  146. gridline.set_color(theme_objs['colors']['color8'])
  147. axes.yaxis.get_offset_text().set_color(theme_objs['special']['foreground'])
  148. axes.xaxis.get_offset_text().set_color(theme_objs['special']['foreground'])
  149. axes.grid()
  150. axes.set_facecolor(theme_objs['special']['background'])
  151. iteration = 2
  152. for i in range( 1, len(data_arrays) ):
  153. axes = pp.subplot(len(data_arrays),1,iteration, sharex=price_axes)
  154. axes.plot(data_arrays[i]['timestamps'], data_arrays[i]['values'], marker='o',linestyle=' ', color=theme_objs['colors']['color'+str(iteration)])
  155. for spine, s in axes.spines.items():
  156. s.set_color(theme_objs['colors']['color15'])
  157. for ticklabel in axes.xaxis.get_ticklabels():
  158. ticklabel.set_color(theme_objs['special']['foreground'])
  159. for ticklabel in axes.yaxis.get_ticklabels():
  160. ticklabel.set_color(theme_objs['special']['foreground'])
  161. for gridline in axes.get_xgridlines():
  162. gridline.set_color(theme_objs['colors']['color8'])
  163. for gridline in axes.get_ygridlines():
  164. gridline.set_color(theme_objs['colors']['color8'])
  165. axes.yaxis.get_offset_text().set_color(theme_objs['special']['foreground'])
  166. axes.xaxis.get_offset_text().set_color(theme_objs['special']['foreground'])
  167. axes.grid()
  168. axes.set_facecolor(theme_objs['special']['background'])
  169. iteration += 1
  170. pp.gcf().set_facecolor(theme_objs['special']['background'])
  171. pp.show()
  172. '''
  173. net_inflow = False
  174. if net_inflow:
  175. for i in range( 0, len(txs_timestamp) ):
  176. net_inflow_timestamp.append(txs_timestamp[i])
  177. net_inflow_amount.append(txs_stable_net_amount_in[i] - txs_total_in[i]) #TODO shohudl probably subtract all high cap coins from all stables
  178. #figure, axes = pp.subplots(2,1)
  179. if net_inflow:
  180. axes_price = pp.subplot(2,1,1)
  181. else:
  182. axes_price = pp.subplot(3,1,1)
  183. axes_price.plot(prices['timestamps'], prices['values'], marker='o',linestyle=' ', color=theme_objs['colors']['color1'])
  184. for spine, s in axes_price.spines.items():
  185. s.set_color(theme_objs['colors']['color15'])
  186. for ticklabel in axes_price.xaxis.get_ticklabels():
  187. ticklabel.set_color(theme_objs['special']['foreground'])
  188. for ticklabel in axes_price.yaxis.get_ticklabels():
  189. ticklabel.set_color(theme_objs['special']['foreground'])
  190. for gridline in axes_price.get_xgridlines():
  191. gridline.set_color(theme_objs['colors']['color8'])
  192. for gridline in axes_price.get_ygridlines():
  193. gridline.set_color(theme_objs['colors']['color8'])
  194. axes_price.yaxis.get_offset_text().set_color(theme_objs['special']['foreground'])
  195. axes_price.xaxis.get_offset_text().set_color(theme_objs['special']['foreground'])
  196. axes_price.grid()
  197. axes_price.set_facecolor(theme_objs['special']['background'])
  198. #axes[0].plot(unix_hours, prices, marker='o',linestyle=' ', color='#FFAA00')
  199. #axes[0].grid()
  200. if net_inflow:
  201. axes_txs = pp.subplot(2,1,2, sharex=axes_price)
  202. axes_txs.plot(net_inflow_timestamp, net_inflow_amount, marker='o',linestyle=' ', color=theme_objs['colors']['color2'])
  203. for spine, s in axes_txs.spines.items():
  204. s.set_color(theme_objs['colors']['color15'])
  205. for ticklabel in axes_txs.xaxis.get_ticklabels():
  206. ticklabel.set_color(theme_objs['special']['foreground'])
  207. for ticklabel in axes_txs.yaxis.get_ticklabels():
  208. ticklabel.set_color(theme_objs['special']['foreground'])
  209. for gridline in axes_txs.get_xgridlines():
  210. gridline.set_color(theme_objs['colors']['color8'])
  211. for gridline in axes_txs.get_ygridlines():
  212. gridline.set_color(theme_objs['colors']['color8'])
  213. axes_txs.yaxis.get_offset_text().set_color(theme_objs['special']['foreground'])
  214. axes_txs.xaxis.get_offset_text().set_color(theme_objs['special']['foreground'])
  215. axes_txs.grid()
  216. axes_txs.set_facecolor(theme_objs['special']['background'])
  217. pp.axhline(color=theme_objs['colors']['color15'])
  218. else:
  219. axes_txs = pp.subplot(3,1,2, sharex=axes_price)
  220. axes_txs.plot(nets['timestamps'], nets['values'], marker='o',linestyle=' ', color=theme_objs['colors']['color2'])
  221. for spine, s in axes_txs.spines.items():
  222. s.set_color(theme_objs['colors']['color15'])
  223. for ticklabel in axes_txs.xaxis.get_ticklabels():
  224. ticklabel.set_color(theme_objs['special']['foreground'])
  225. for ticklabel in axes_txs.yaxis.get_ticklabels():
  226. ticklabel.set_color(theme_objs['special']['foreground'])
  227. for gridline in axes_txs.get_xgridlines():
  228. gridline.set_color(theme_objs['colors']['color8'])
  229. for gridline in axes_txs.get_ygridlines():
  230. gridline.set_color(theme_objs['colors']['color8'])
  231. axes_txs.yaxis.get_offset_text().set_color(theme_objs['special']['foreground'])
  232. axes_txs.xaxis.get_offset_text().set_color(theme_objs['special']['foreground'])
  233. axes_txs.grid()
  234. axes_txs.set_facecolor(theme_objs['special']['background'])
  235. pp.axhline(color=theme_objs['colors']['color15'])
  236. #axes[1].plot(txs_unix_hours, txs_usds, 'ro')
  237. #axes[1].grid()
  238. axes_txs_stable = pp.subplot(3,1,3, sharex=axes_price)
  239. axes_txs_stable.plot(stable_nets['timestamps'], stable_nets['values'], marker='o',linestyle=' ', color=theme_objs['colors']['color3'])
  240. for spine, s in axes_txs_stable.spines.items():
  241. s.set_color(theme_objs['colors']['color15'])
  242. for ticklabel in axes_txs_stable.xaxis.get_ticklabels():
  243. ticklabel.set_color(theme_objs['special']['foreground'])
  244. for ticklabel in axes_txs_stable.yaxis.get_ticklabels():
  245. ticklabel.set_color(theme_objs['special']['foreground'])
  246. for gridline in axes_txs_stable.get_xgridlines():
  247. gridline.set_color(theme_objs['colors']['color8'])
  248. for gridline in axes_txs_stable.get_ygridlines():
  249. gridline.set_color(theme_objs['colors']['color8'])
  250. axes_txs_stable.yaxis.get_offset_text().set_color(theme_objs['special']['foreground'])
  251. axes_txs_stable.xaxis.get_offset_text().set_color(theme_objs['special']['foreground'])
  252. axes_txs_stable.grid()
  253. axes_txs_stable.set_facecolor(theme_objs['special']['background'])
  254. pp.axhline(color=theme_objs['colors']['color15'])
  255. pp.gcf().set_facecolor(theme_objs['special']['background'])
  256. pp.show()
  257. '''
  258. main()