forked from cyberjunky/3commas-cyber-bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalancereport.py
executable file
·154 lines (129 loc) · 4.45 KB
/
balancereport.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python3
"""Cyberjunky's 3Commas bot helpers."""
import argparse
import configparser
import os
import sys
import time
from datetime import datetime, timedelta
from pathlib import Path
import pandas as pd
from helpers.logging import Logger, NotificationHandler
from helpers.misc import wait_time_interval
from helpers.threecommas import (
get_threecommas_account_balance,
get_threecommas_account_balance_chart_data,
get_threecommas_accounts,
init_threecommas_api,
)
def load_config():
"""Create default or load existing config file."""
cfg = configparser.ConfigParser()
if cfg.read(f"{datadir}/{program}.ini"):
return cfg
cfg["settings"] = {
"timezone": "Europe/Amsterdam",
"timeinterval": 3600,
"debug": False,
"logrotate": 7,
"3c-apikey": "Your 3Commas API Key",
"3c-apisecret": "Your 3Commas API Secret",
"notifications": False,
"notify-urls": ["notify-url1"],
}
with open(f"{datadir}/{program}.ini", "w") as cfgfile:
cfg.write(cfgfile)
return None
# Start application
program = Path(__file__).stem
# Parse and interpret options.
parser = argparse.ArgumentParser(description="Cyberjunky's 3Commas bot helper.")
parser.add_argument(
"-d", "--datadir", help="directory to use for config and logs files", type=str
)
args = parser.parse_args()
if args.datadir:
datadir = args.datadir
else:
datadir = os.getcwd()
# Create or load configuration file
config = load_config()
if not config:
# Initialise temp logging
logger = Logger(datadir, program, None, 7, False, False)
logger.info(
f"Created example config file '{datadir}/{program}.ini', edit it and restart the program"
)
sys.exit(0)
else:
# Handle timezone
if hasattr(time, "tzset"):
os.environ["TZ"] = config.get(
"settings", "timezone", fallback="Europe/Amsterdam"
)
time.tzset()
# Init notification handler
notification = NotificationHandler(
program,
config.getboolean("settings", "notifications"),
config.get("settings", "notify-urls"),
)
# Initialise logging
logger = Logger(
datadir,
program,
notification,
int(config.get("settings", "logrotate", fallback=7)),
config.getboolean("settings", "debug"),
config.getboolean("settings", "notifications"),
)
logger.info(f"Loaded configuration from '{datadir}/{program}.ini'")
# Initialize 3Commas API
api = init_threecommas_api(config)
# Create balance report
while True:
# Reload config files and data to catch changes
config = load_config()
logger.info(f"Reloaded configuration from '{datadir}/{program}.ini'")
# Configuration settings
timeint = int(config.get("settings", "timeinterval"))
accounts = get_threecommas_accounts(logger, api)
yesterday = (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d")
today = (datetime.now()).strftime("%Y-%m-%d")
list_of_dicts = []
# Fetch all accounts
for account in accounts:
balance = get_threecommas_account_balance(logger, api, account["id"])
historical_balance = get_threecommas_account_balance_chart_data(
logger, api, account["id"], yesterday, today
)
logger.debug(historical_balance)
logger.info("Getting account data for %s" % balance["name"])
try:
amount_yesterday = float(
historical_balance[0]["usd"]
) # 0 = yesterday, 1 = today midnight, 2 = now
amount_today = float(historical_balance[-1]["usd"])
gain_amount = amount_today - amount_yesterday
if amount_yesterday:
gain_percentage = round(
100 * (amount_today - amount_yesterday) / amount_yesterday, 2
)
else:
gain_percentage = "NA"
thisdict = {
"Name": balance["name"],
"Exchange": balance["exchange_name"],
"USD": round(float(historical_balance[-1]["usd"]), 0),
"Gain USD": round(gain_amount, 0),
"Gain %": gain_percentage,
}
list_of_dicts.append(thisdict)
except IndexError:
pass
df = pd.DataFrame(
list_of_dicts, columns=["Name", "Exchange", "USD", "Gain USD", "Gain %"]
)
print(df.to_string(index=False))
if not wait_time_interval(logger, notification, timeint):
break