-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from AsyncAlgoTrading/coinbase
starting work on coinbase
- Loading branch information
Showing
13 changed files
with
190 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .coinbase import CoinbaseProExchange # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
from cbpro import PublicClient, AuthenticatedClient, WebsocketClient # type: ignore | ||
|
||
from aat.core import ExchangeType, Order | ||
from aat.config import TradingType, OrderType, OrderFlag, InstrumentType | ||
from aat.exchange import Exchange | ||
from .instruments import _get_instruments | ||
|
||
|
||
class CoinbaseProExchange(Exchange): | ||
'''Coinbase Pro Exchange''' | ||
|
||
def __init__(self, | ||
trading_type, | ||
verbose, | ||
api_key='', | ||
api_secret='', | ||
api_passphrase='', | ||
**kwargs): | ||
self._trading_type = trading_type | ||
self._verbose = verbose | ||
|
||
if trading_type == TradingType.BACKTEST: | ||
raise NotImplementedError() | ||
|
||
if self._trading_type == TradingType.SANDBOX: | ||
super().__init__(ExchangeType('coinbaseprosandbox')) | ||
else: | ||
super().__init__(ExchangeType('coinbasepro')) | ||
|
||
auth = api_key and api_secret and api_passphrase | ||
self._public_client = PublicClient() | ||
|
||
if trading_type == TradingType.SANDBOX: | ||
self._auth_client = AuthenticatedClient(api_key, api_secret, api_passphrase, api_url="https://api-public.sandbox.pro.coinbase.com") if auth else None | ||
else: | ||
self._auth_client = AuthenticatedClient(api_key, api_secret, api_passphrase) if auth else None | ||
|
||
# TODO | ||
self._subscriptions = [] | ||
self._ws_client = WebsocketClient(url="wss://ws-feed.pro.coinbase.com", products="BTC-USD") | ||
|
||
# *************** # | ||
# General methods # | ||
# *************** # | ||
async def connect(self): | ||
'''connect to exchange. should be asynchronous.''' | ||
|
||
# instantiate instruments | ||
_get_instruments(self._public_client, self.exchange()) | ||
|
||
async def lookup(self, instrument): | ||
'''lookup an instrument on the exchange''' | ||
# TODO | ||
raise NotImplementedError() | ||
|
||
# ******************* # | ||
# Market Data Methods # | ||
# ******************* # | ||
async def tick(self): | ||
'''return data from exchange''' | ||
|
||
async def subscribe(self, instrument): | ||
self._subscriptions.append(instrument) | ||
|
||
# ******************* # | ||
# Order Entry Methods # | ||
# ******************* # | ||
async def accounts(self): | ||
'''get accounts from source''' | ||
# TODO | ||
raise NotImplementedError() | ||
|
||
async def newOrder(self, order): | ||
'''submit a new order to the exchange. should set the given order's `id` field to exchange-assigned id''' | ||
if not self._auth_client: | ||
raise NotImplementedError() | ||
|
||
if order.instrument.type != InstrumentType.PAIR: | ||
raise NotImplementedError() | ||
|
||
if order.type == OrderType.LIMIT: | ||
time_in_force = 'GTC' | ||
if order.flag == OrderFlag.FILL_OR_KILL: | ||
time_in_force = 'FOK' | ||
elif order.flag == OrderFlag.IMMEDIATE_OR_CANCEL: | ||
time_in_force = 'IOC' | ||
|
||
ret = self._auth_client.place_limit_order(product_id='{}-{}'.format(order.instrument.leg1.name, order.instrument.leg2.name), | ||
side=order.side.value.lower(), | ||
price=order.price, | ||
size=order.volume, | ||
time_in_force=time_in_force) | ||
|
||
elif order.type == OrderType.MARKET: | ||
ret = self._auth_client.place_limit_order(product_id='{}-{}'.format(order.instrument.leg1.name, order.instrument.leg2.name), | ||
side=order.side.value.lower(), | ||
funds=order.price * order.volume) | ||
|
||
elif order.type == OrderType.STOP: | ||
# TODO | ||
raise NotImplementedError() | ||
# self._auth_client.place_stop_order(product_id='BTC-USD', | ||
# side='buy', | ||
# price='200.00', | ||
# size='0.01') | ||
|
||
# Set ID | ||
order.id = ret['id'] | ||
return order | ||
|
||
async def cancelOrder(self, order: Order): | ||
'''cancel a previously submitted order to the exchange.''' | ||
self._auth_client.cancel_order(order.id) | ||
return order | ||
|
||
|
||
Exchange.registerExchange('coinbase', CoinbaseProExchange) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from functools import lru_cache | ||
from aat import Instrument, InstrumentType, Side | ||
|
||
|
||
@lru_cache(None) | ||
def _get_currency(symbol): | ||
return Instrument(name=symbol, type=InstrumentType.CURRENCY) | ||
|
||
|
||
@lru_cache(None) | ||
def _get_instruments(public_client, exchange): | ||
ret = [] | ||
|
||
products = public_client.get_products() | ||
|
||
for product in products: | ||
first = product['base_currency'] | ||
second = product['quote_currency'] | ||
|
||
ret.append( | ||
Instrument(name='{}/{}'.format(first, second), | ||
type=InstrumentType.PAIR, | ||
exchange=exchange, | ||
leg1=_get_currency(first), | ||
leg2=_get_currency(second), | ||
leg1_side=Side.BUY, | ||
leg2_side=Side.SELL) | ||
) | ||
return ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from aat import Strategy, Event | ||
from pprint import pprint | ||
|
||
|
||
class ReadOnlyStrategy(Strategy): | ||
def __init__(self, *args, **kwargs) -> None: | ||
super(ReadOnlyStrategy, self).__init__(*args, **kwargs) | ||
|
||
async def onStart(self, event: Event) -> None: | ||
pprint(self.instruments()) | ||
|
||
async def onTrade(self, event: Event) -> None: | ||
pprint(event) | ||
|
||
async def onOrder(self, event): | ||
pprint(event) | ||
|
||
async def onExit(self, event: Event) -> None: | ||
print('Finishing...') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[general] | ||
verbose=0 | ||
trading_type=sandbox | ||
|
||
[exchange] | ||
exchanges= | ||
aat.exchange.crypto.coinbase:CoinbaseProExchange | ||
|
||
[strategy] | ||
strategies = | ||
aat.strategy.sample.readonly:ReadOnlyStrategy |