forked from realgs/streams21
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.py
148 lines (104 loc) · 4.24 KB
/
calc.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
from utils import *
def calculate_mov_avg(askbid_storage, avg_storage, window_size):
storage_slice = askbid_storage[-window_size:]
temp = []
for curr_pair in range(3):
inner_temp = []
for ask_or_bid in range(2):
summation = 0
for sample in range(0, len(storage_slice)):
summation += storage_slice[sample][curr_pair][ask_or_bid]
summation /= len(storage_slice)
inner_temp.append(summation)
temp.append(inner_temp)
avg_storage.append(temp)
def calculate_rsi(askbid_storage, rsi_storage, window_size):
storage_slice = askbid_storage[-window_size:]
temp = []
for curr_pair in range(3):
inner_temp = []
for ask_or_bid in range(2):
upward, upward_counter = 0, 0
downward, downward_counter = 0, 0
for sample in range(1, len(storage_slice)):
if storage_slice[sample-1][curr_pair][ask_or_bid] \
< storage_slice[sample][curr_pair][ask_or_bid]:
up = storage_slice[sample][curr_pair][ask_or_bid] \
- storage_slice[sample-1][curr_pair][ask_or_bid]
upward += up
upward_counter += 1
elif storage_slice[sample-1][curr_pair][ask_or_bid] \
> storage_slice[sample][curr_pair][ask_or_bid]:
down = storage_slice[sample-1][curr_pair][ask_or_bid] \
- storage_slice[sample][curr_pair][ask_or_bid]
downward += down
downward_counter += 1
if upward_counter == 0:
a = 1
else:
a = upward / upward_counter
if downward_counter == 0:
b = 1
else:
b = downward / downward_counter
try:
rsi = 100 - (100 / (1 + (a / b)))
except ZeroDivisionError:
a, b = 1, 1
rsi = 100 - (100 / (1 + (a / b)))
inner_temp.append(rsi)
temp.append(inner_temp)
rsi_storage.append(temp)
def classify_trend(rsi_storage, trend_list):
for curr_pair in range(3):
latest_ask_rsi = rsi_storage[-1][curr_pair][0]
if latest_ask_rsi >= 65:
trend_list[curr_pair] = 'upward'
elif latest_ask_rsi <= 35:
trend_list[curr_pair] = 'downward'
else:
trend_list[curr_pair] = 'horizontal'
def select_candidate(trends_list, volume_slice):
temp = []
for curr_pair in range(3):
if trends_list[curr_pair] != 'downward':
temp.append(volume_slice[curr_pair])
if temp:
highest_volume = max(temp)
return volume_slice.index(highest_volume)
else:
return None
def check_volatility(transaction_storage, pair, threshold, samples):
trans_slice = transaction_storage[-samples:]
temp = []
for sample in range(len(trans_slice)):
curr_trans = trans_slice[sample][pair]
trans_amount = len(curr_trans['items'])
inner_temp = [float(curr_trans['items'][tran]['r']) for tran in range(trans_amount)]
temp.extend(inner_temp)
try:
percentage = calculate_percent_diff(max(temp), min(temp))
except ValueError:
percentage = 0
return (lambda perc: True if perc > threshold else False)(percentage)
def check_liquidity(transaction_storage, pair, threshold):
trans_slice = transaction_storage[-1:]
curr_trans = trans_slice[0][pair]
trans_amount = len(curr_trans['items'])
temp_asks = [float(curr_trans['items'][tran]['r']) for tran in range(trans_amount)
if curr_trans['items'][tran]['ty'] == "Buy"]
temp_bids = [float(curr_trans['items'][tran]['r']) for tran in range(trans_amount)
if curr_trans['items'][tran]['ty'] == "Sell"]
try:
ask = sum(temp_asks) / len(temp_asks)
except ZeroDivisionError:
return 0
try:
bid = sum(temp_bids) / len(temp_bids)
except ZeroDivisionError:
return 0
try:
percentage = calculate_percent_diff(ask, bid)
except ValueError:
percentage = 0
return (lambda spread: True if spread < threshold else False)(percentage)