-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
53 lines (38 loc) · 1.84 KB
/
utils.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
import ccxt
import pandas as pd
def get_tokens_from_trading_pairs(trading_pairs):
return list(set([token for pair in trading_pairs for token in pair.split("-")]))
def hbot_trading_pairs_to_ccxt_trading_pairs(trading_pairs):
return [pair.replace("-", "/") for pair in trading_pairs]
def get_exchange(exchange_id, api_key, api_secret):
exchange_class = getattr(ccxt, exchange_id)
exchange = exchange_class({
'apiKey': api_key,
'secret': api_secret,
})
exchange.load_markets()
return exchange
def get_balances(exchange, trading_pairs):
tokens = get_tokens_from_trading_pairs(trading_pairs)
balances = {token: balance for token, balance in exchange.fetch_balance()["total"].items() if balance > 0}
return {token: balances.get(token, 0) for token in tokens}
def get_prices(exchange, trading_pairs):
tokens = get_tokens_from_trading_pairs(trading_pairs)
prices = exchange.fetch_tickers([f"{token}/USDT" for token in tokens if token != "USDT"])
return {pair.split(":")[0].replace("/", "-"): ticker["last"] for pair, ticker in prices.items()}
def get_trades(exchange, trading_pairs, start_time, end_time):
trading_pairs = hbot_trading_pairs_to_ccxt_trading_pairs(trading_pairs)
start_time_ts = exchange.parse8601(start_time)
end_time_ts = exchange.parse8601(end_time)
trades = []
for trading_pair in trading_pairs:
initial_time = start_time_ts
while initial_time < end_time_ts:
new_trades = exchange.fetch_my_trades(symbol=trading_pair, since=initial_time)
if len(new_trades):
last_trade = new_trades[-1]
initial_time = last_trade['timestamp'] + 1
else:
initial_time += 432000000 # 5 days
trades = trades + new_trades
return pd.DataFrame([trade["info"] for trade in trades])