-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexchange.py
109 lines (87 loc) · 3.69 KB
/
exchange.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
import krakenex
from pykrakenapi import KrakenAPI
import cbpro
from abc import ABC, abstractmethod
from typing import Dict, List
from portfolio import Product
class Exchange(ABC):
def __init__(self):
pass
@abstractmethod
def get_historical(self, product_id, begin, end):
pass
@abstractmethod
def get_account(self, base_currency):
pass
@abstractmethod
def place_market_order(self, product_id, quote_amount) -> int:
pass
@abstractmethod
def get_tradable_products(self, base_currency) -> Dict[str, Dict]:
pass
@staticmethod
def build(data: Dict) -> 'Exchange':
if data['type'] == "coinbase":
return CoinbaseExchange(data)
elif data['type'] == 'kraken':
return KrakenExchange(data)
else:
raise RuntimeError(f"Unknown exchange type {data['type']}")
class CoinbaseExchange:
def __init__(self, key_data):
self.public_client = cbpro.PublicClient()
key = key_data['key']
if key is not None and key.strip() != "":
self.auth_client = cbpro.AuthenticatedClient(
key, key_data['b64secret'], key_data['passphrase'], api_url=key_data['url'])
else:
# this will break when executing orders/get account
self.auth_client = None
pass
def get_account(self, base_currency):
coinbase_accounts = self.auth_client.get_coinbase_accounts()
coinbase_account = None
for acc in coinbase_accounts:
if acc['currency'] == base_currency:
coinbase_account = acc
break
return coinbase_account
def get_tradable_products(self, base_currency) -> Dict[str, Dict]:
products = self.public_client.get_products()
tradable_products = {}
for product in products:
if not product['trading_disabled'] and product['status'] == "online" and product['quote_currency'] == base_currency and not product['post_only'] and not product['limit_only'] and not product['cancel_only']:
tradable_products[Product.build(product['id'])] = product
return tradable_products
def get_historical(self, product_id, begin, end):
tickers = self.public_client.get_product_historic_rates(
product_id, start=begin, end=end, granularity=86400)
return tickers
def place_market_order(self, product_id: str, quote_amount: float):
order = self.auth_client.place_market_order(
product_id, side="buy", funds=quote_amount)
return order
class KrakenExchange:
def __init__(self, key_data):
key = key_data['key']
if key is not None and key.strip() != "":
api = krakenex.API(key_data['key'], key_data['b64secret'])
else:
api = krakenex.API()
self.kraken = KrakenAPI(api)
def get_historical(self, product_id, begin, end):
pass
def place_market_order(self, product_id, quote_amount):
pass
def get_account(self, base_currency):
pass
def get_tradable_products(self, base_currency) -> Dict[str, Dict]:
products = self.kraken.get_tradable_asset_pairs()
for index, row in products.iterrows():
print(f"{index}: {row['ordermin']}")
print(row)
tradable_products = {}
for product in products:
if not product['trading_disabled'] and product['status'] == "online" and product['quote_currency'] == base_currency:
tradable_products[Product.build(product['id'])] = product and not product['cancel_only'] and not product['post_only'] and not product['limit_only']
return tradable_products