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

L5 #158

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

L5 #158

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions API/Api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import requests
from abc import ABC, abstractmethod


def request(url):
headers = {'content-type': 'application/json'}
response = requests.request("GET", url, headers=headers)
if 199 < response.status_code < 300:
return response.json()
elif 300 < response.status_code < 399:
raise Exception('Redirection. Need additional action in order to complete request.')
elif 400 < response.status_code < 499:
raise Exception('Client error. Server cannot understand the request.')
elif 500 < response.status_code < 599:
raise Exception('Server error. Please contact with API providers.')
return None


class Api(ABC):
def __init__(self, name, base_url):
self.__name = name
self.__base_url = base_url

@property
def name(self):
return self.__name

@property
def url(self):
return self.__base_url

@abstractmethod
def markets(self):
pass

@abstractmethod
def orderbook(self, first, second):
pass

@abstractmethod
def transfer_fee(self, currency):
pass

@abstractmethod
def taker_fee(self):
pass
32 changes: 32 additions & 0 deletions API/Bitbay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from API.Api import Api, request
from API.const import transfer


class Bitbay(Api):

def __init__(self):
super().__init__('bitbay', 'https://api.bitbay.net/rest/trading')
self.__fees = transfer

@property
def markets(self):
markets = request(f'{self.url}/ticker')
return list(markets['items'].keys())

def orderbook(self, first, second):
orderbook = request(f'{self.url}/orderbook/{first}-{second}')
asks = list(map(lambda x: {'rate': float(x['ra']), 'quantity': float(
x['ca'])}, orderbook['sell']))
bids = list(map(lambda x: {'rate': float(x['ra']), 'quantity': float(
x['ca'])}, orderbook['buy']))
return asks, bids

def transfer_fee(self, currency):
if currency in self.__fees:
return self.__fees[currency]
else:
raise ValueError(f'Unidentified currency symbol - {currency}')

@property
def taker_fee(self):
return 0.0042
32 changes: 32 additions & 0 deletions API/Bittrex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from API.Api import Api, request


class Bittrex(Api):
def __init__(self):
super().__init__('bittrex', 'https://api.bittrex.com/v3')

fees = request(f'{self.url}/currencies')
self.__fees = {x['symbol']: float(x['txFee']) for x in fees}

@property
def markets(self):
markets = request(f'{self.url}/markets')
return list(map(lambda x: x['symbol'], markets))

def orderbook(self, first, second):
orderbook = request(f'{self.url}/markets/{first}-{second}/orderbook')
bids = list(map(lambda x: {'rate': float(x['rate']), 'quantity': float(
x['quantity'])}, orderbook['bid']))
asks = list(map(lambda x: {'rate': float(x['rate']), 'quantity': float(
x['quantity'])}, orderbook['ask']))
return asks, bids

def transfer_fee(self, currency):
if currency in self.__fees:
return self.__fees[currency]
else:
raise ValueError(f'Unidentified currency symbol - {currency}')

@property
def taker_fee(self):
return 0.0075
28 changes: 28 additions & 0 deletions API/EOD.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from API import const
from API.Api import Api, request
from API.NBP import NBP


class EOD(Api):
def __init__(self):
super().__init__("EOD", "https://eodhistoricaldata.com/api/real-time/")

def markets(self):
pass

def orderbook(self, first, second):
pass

def transfer_fee(self, currency):
pass

def taker_fee(self):
pass

def get_price(self, stock_name, base_currency):
json = request(f'{self.url}{stock_name}.WAR?api_token={const.EOD_TOKEN}&fmt=json')
if json['high'] == 'NA' or json['low'] == 'NA':
price = NBP().convert('PLN', json['previousClose'], base_currency)
else:
price = NBP().convert('PLN', (json['high'] + json['low']) / 2, base_currency)
return price
31 changes: 31 additions & 0 deletions API/NBP.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from API.Api import request, Api


class NBP(Api):
def __init__(self):
super().__init__('NBP', 'http://api.nbp.pl/api/exchangerates/rates/c/')

def markets(self):
pass

def orderbook(self, first, second):
pass

def transfer_fee(self, currency):
pass

def taker_fee(self):
pass

def convert(self, cur_from: str, quantity: float, cur_to: str):
if cur_from == cur_to:
return quantity
if cur_from == 'PLN':
json = request(f'{self.url}{cur_to}')
return round(quantity / float(json['rates'][0]['ask']), 2)
elif cur_to == 'PLN':
json = request(f'{self.url}' + f'{cur_from}')
return round(quantity * float(json['rates'][0]['bid']), 2)

conv_value = self.convert(cur_from, quantity, 'PLN')
return self.convert('PLN', conv_value, cur_to)
30 changes: 30 additions & 0 deletions API/Yahoo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import yfinance as yf

from API.Api import Api
from API.NBP import NBP


class Yahoo(Api):
def markets(self):
pass

def orderbook(self, first, second):
pass

def transfer_fee(self, currency):
pass

def taker_fee(self):
pass

def __init__(self):
super().__init__('Yahoo', "")

@classmethod
def get_price(cls, stock_name, base_currency):
ticker = yf.Ticker(stock_name)
price = (ticker.info["dayLow"] + ticker.info["dayHigh"]) / 2
if ticker.info['currency'] == base_currency:
return price
else:
return NBP().convert(ticker.info['currency'], price, base_currency)
Binary file added API/__pycache__/Api.cpython-38.pyc
Binary file not shown.
Binary file added API/__pycache__/Bitbay.cpython-38.pyc
Binary file not shown.
Binary file added API/__pycache__/Bittrex.cpython-38.pyc
Binary file not shown.
Binary file added API/__pycache__/EOD.cpython-38.pyc
Binary file not shown.
Binary file added API/__pycache__/NBP.cpython-38.pyc
Binary file not shown.
Binary file added API/__pycache__/Yahoo.cpython-38.pyc
Binary file not shown.
Binary file added API/__pycache__/const.cpython-38.pyc
Binary file not shown.
8 changes: 8 additions & 0 deletions API/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
transfer = {
'AAVE': 0.23, 'BAT': 29, 'BSV': 0.003, 'BTC': 0.0005, 'COMP': 0.025, 'DAI': 19, 'DOT': 0.1, 'EOS': 0.1, 'ETH': 0.01,
'EUR': 3, 'GAME': 279, 'GRT': 11, 'LINK': 1.85, 'LSK': 0.3, 'LTC': 0.001, 'LUNA': 0.02, 'MANA': 27, 'MKR': 0.014,
'NPXS': 22400, 'OMG': 3.5, 'PAY': 278, 'SRN': 2905, 'TRX': 1, 'UNI': 0.7, 'USD': 3, 'USDC': 75.5, 'USDT': 37,
'XLM': 0.005, 'XRP': 0.1, 'XTZ': 0.1, 'ZRX': 16
}

EOD_TOKEN = '60af85057712e4.51092124'
Loading