Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

L6 #241

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
59 changes: 59 additions & 0 deletions api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import requests
from utils import *


def get_data(crypto_pairs, data_storage, askbid_storage):

curr_temp, askbid_temp = ([] for _ in range(2))

for pair in crypto_pairs:
try:
request_orders = requests.get(
f"https://bitbay.net/API/Public/{pair[0]}{pair[1]}/ticker.json"
)
orders = request_orders.json()
curr_temp.append([f'{pair[0]}-{pair[1]}', (orders['ask'], orders['bid'])])
askbid_temp.append((orders['ask'], orders['bid']))

except requests.exceptions.RequestException:
print("Connection problem with the ticker API.")
return None

askbid_storage.append(askbid_temp)
data_storage.append(curr_temp)


def get_transactions(crypto_pairs, transaction_storage, limit, timeframe):

trans_temp = []

for pair in crypto_pairs:

unix_epoch_time = get_unix_time(timeframe)

try:
request_volume = requests.get(
f"https://api.bitbay.net/rest/trading/transactions/{pair[0]}-{pair[1]}",
params={'limit': limit, 'fromTime': unix_epoch_time}
)
transactions = request_volume.json()
trans_temp.append(transactions)

except requests.exceptions.RequestException:
print("Connection problem with the transactions API.")
trans_temp.append(None)

transaction_storage.append(trans_temp)


def get_volume(transaction_storage, volume_storage):

vol_temp = []

for curr_pair in range(3):
latest_trans = transaction_storage[-1][curr_pair]
trans_amount = len(latest_trans['items'])
volume = sum([float(latest_trans['items'][tran]['a']) for tran in range(trans_amount)])
vol_temp.append(volume)

volume_storage.append(vol_temp)
148 changes: 148 additions & 0 deletions calc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)
Loading