-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.py
66 lines (54 loc) · 1.89 KB
/
helpers.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
import json
import os
def read_keys():
with open('api_credentials.txt') as file:
api_key = file.readline().strip()
api_secret = file.readline().strip()
return api_key, api_secret
def dump_json_to_file(file_path, json_data):
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(json_data, f, ensure_ascii=False, indent=4)
def read_json(file):
with open(file, 'w', encoding='utf-8') as f:
return json.load(f)
def calculate_avg_true_range(candles):
true_ranges = []
previous_candle = None
for i in range(len(candles)):
if i == 0:
previous_candle = candles[i]
continue
x = float(candles[i]['high']) - float(candles[i]['low'])
y = abs(float(candles[i]['high']) - float(previous_candle['close']))
z = abs(float(candles[i]['low']) - float(previous_candle['close']))
true_ranges.append(max([x, y, z]))
previous_candle = candles[i]
i += 1
return sum(true_ranges) / len(true_ranges)
def aggregate_trades(trades):
aggregated = {
"amount": 0,
"date": None,
"rate": 0,
"total": 0,
"type": None,
"adjustment": 0
}
rates = []
for t in trades:
aggregated['amount'] += float(t['amount'])
aggregated['date'] = t['date']
rates.append(float(t['rate']))
aggregated['total'] += float(t['total'])
aggregated['type'] = t['type']
if 'takerAdjustment' in t:
aggregated['adjustment'] += float(t['takerAdjustment'])
if 'makerAdjustment' in t:
aggregated['adjustment'] += float(t['makerAdjustment'])
aggregated['rate'] = round(sum(rates) / len(rates), 8)
return aggregated
def iterate_folder(folder_path):
directory = os.fsencode(folder_path)
for file in os.listdir(directory):
filename = os.fsdecode(file)
yield filename