Created
June 17, 2018 22:34
-
-
Save dgnsrekt/0c432cb23bdc3b20f721b389f6c30c33 to your computer and use it in GitHub Desktop.
idea for parsing bags and estimated value from gunbot.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
import os | |
import json | |
import pandas as pd | |
from datetime import datetime | |
BASEPATH = Path(__file__).parent | |
exchange = 'binance' | |
tradepair_state_paths = sorted(BASEPATH.glob(f'{exchange}-BTC-*-state.json'), key=os.path.getmtime, reverse=True) | |
def state_file_reader(path_): | |
with open(path_, 'r') as f: | |
return json.loads(f.read()) | |
def parse_name_from_path(path_): | |
return path_.name.split('-')[2] | |
def parse_date_time(path_): | |
mtime = path_.stat().st_mtime | |
return datetime.utcfromtimestamp(mtime) | |
state_file_data = [] | |
for jsonpath in tradepair_state_paths: | |
data = state_file_reader(jsonpath) | |
time = parse_date_time(jsonpath) | |
name = parse_name_from_path(jsonpath) | |
data['name'] = name | |
data['time'] = time | |
# filterout bad names here if then append else skip | |
state_file_data.append(data) | |
print('.', end='') | |
print() | |
balances = state_file_data[0]['balancesdata'] | |
positive_bal = [bal for bal in balances if balances[bal]['available'] > 0] | |
state_df = pd.DataFrame(state_file_data) | |
columns = ['name', 'Bid', 'Ask', 'quoteBalance', 'baseBalance', 'time'] | |
current_df = state_df[columns] | |
current_df = current_df[current_df['name'].isin(positive_bal)] | |
current_df['price'] = (current_df['Bid'] + current_df['Ask']) / 2 | |
current_df['btc_value'] = current_df['price'] * current_df['quoteBalance'] | |
current_df.columns = ['name', 'bid', 'ask', 'balance', 'btc', 'time', 'price', 'btc_value'] | |
estimated_value = current_df['btc_value'].sum() + current_df['btc'][0] | |
print(estimated_value) | |
print(current_df[current_df['btc_value'] > 0.0001]['name']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment