Guest User

Untitled

a guest
Jun 19th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. from pathlib import Path
  2. import os
  3. import json
  4. import pandas as pd
  5. from datetime import datetime
  6.  
  7. BASEPATH = Path(__file__).parent
  8.  
  9. exchange = 'binance'
  10. tradepair_state_paths = sorted(BASEPATH.glob(f'{exchange}-BTC-*-state.json'), key=os.path.getmtime, reverse=True)
  11.  
  12.  
  13. def state_file_reader(path_):
  14. with open(path_, 'r') as f:
  15. return json.loads(f.read())
  16.  
  17.  
  18. def parse_name_from_path(path_):
  19. return path_.name.split('-')[2]
  20.  
  21.  
  22. def parse_date_time(path_):
  23. mtime = path_.stat().st_mtime
  24. return datetime.utcfromtimestamp(mtime)
  25.  
  26.  
  27. state_file_data = []
  28. for jsonpath in tradepair_state_paths:
  29. data = state_file_reader(jsonpath)
  30. time = parse_date_time(jsonpath)
  31. name = parse_name_from_path(jsonpath)
  32.  
  33. data['name'] = name
  34. data['time'] = time
  35. # filterout bad names here if then append else skip
  36.  
  37. state_file_data.append(data)
  38. print('.', end='')
  39.  
  40. print()
  41.  
  42. balances = state_file_data[0]['balancesdata']
  43. positive_bal = [bal for bal in balances if balances[bal]['available'] > 0]
  44.  
  45. state_df = pd.DataFrame(state_file_data)
  46. columns = ['name', 'Bid', 'Ask', 'quoteBalance', 'baseBalance', 'time']
  47. current_df = state_df[columns]
  48. current_df = current_df[current_df['name'].isin(positive_bal)]
  49. current_df['price'] = (current_df['Bid'] + current_df['Ask']) / 2
  50. current_df['btc_value'] = current_df['price'] * current_df['quoteBalance']
  51. current_df.columns = ['name', 'bid', 'ask', 'balance', 'btc', 'time', 'price', 'btc_value']
  52.  
  53. estimated_value = current_df['btc_value'].sum() + current_df['btc'][0]
  54. print(estimated_value)
  55.  
  56. print(current_df[current_df['btc_value'] > 0.0001]['name'])
Add Comment
Please, Sign In to add comment