Skip to content

Commit

Permalink
Merge pull request #20 from olned/coinbase
Browse files Browse the repository at this point in the history
Coinbase Pro added
  • Loading branch information
olned authored Jun 18, 2020
2 parents 1225948 + ce434ee commit fd486bb
Show file tree
Hide file tree
Showing 25 changed files with 636 additions and 200 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Oleg Nedbaylo
Copyright (c) 2019-2020 Oleg Nedbaylo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
8 changes: 3 additions & 5 deletions examples/bitfinex_basic_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
conn = Bitfinex()


async def handle_subscription(data, self):
def handle_subscription(data, self):
print(data, self.receipt_time * 1000 - data[-1])


Expand All @@ -23,15 +23,13 @@ async def subscribe():

conn.on_connect_ws = subscribe

loop = asyncio.get_event_loop()


def stop():
print("STOP")
asyncio.ensure_future(conn.stop())


loop.call_later(10, stop)
loop = asyncio.get_event_loop()
loop.call_later(3600, stop)

try:
loop.run_until_complete(conn.run_receiver())
Expand Down
2 changes: 1 addition & 1 deletion examples/bitfinex_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, instrument):
self.top_bid = 0.
self.top_ask = 0.

async def handle_book(self, message, connector):
def handle_book(self, message, connector):
if type(message[1]) is str:
if message[1] == "cs":
pass
Expand Down
3 changes: 2 additions & 1 deletion examples/book_watcher.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from ssc2ce.common.abstract_parser import AbstractParser
from ssc2ce.deribit.l2_book import L2Book


class BookWatcher:
def __init__(self, parser, print_it: bool = True):
def __init__(self, parser: AbstractParser, print_it: bool = True):
self.betters = {}
self.print_it = print_it
parser.set_on_book_setup(self.handle_book_setup)
Expand Down
53 changes: 53 additions & 0 deletions examples/coinbase_pro_basic_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python
import asyncio
import logging

from ssc2ce import Coinbase

logging.basicConfig(format='%(asctime)s %(name)s %(funcName)s %(levelname)s %(message)s', level=logging.INFO)
logger = logging.getLogger("bitfinex-basic-example")


def handle_message(message: str):
logger.info(message)


conn = Coinbase()
conn.on_message = handle_message


async def subscribe():
await conn.ws.send_json({
"type": "subscribe",
"product_ids": [
"BTC-USD",
"ETH-BTC"
],
"channels": [
"level2",
"heartbeat"
]
})


def handle_heartbeat(data: dict) -> bool:
global last_time
last_time = data["time"]
logger.info(f"{repr(data)}")
return True


conn.on_connect_ws = subscribe


def stop():
asyncio.ensure_future(conn.stop())


loop = asyncio.get_event_loop()
loop.call_later(3600, stop)

try:
loop.run_until_complete(conn.run_receiver())
except KeyboardInterrupt:
logger.info("Application closed by KeyboardInterrupt.")
68 changes: 68 additions & 0 deletions examples/coinbase_pro_writer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python
import asyncio
import json
import logging
from ssc2ce import Coinbase

conn = Coinbase(False)

pending = {}

logging.basicConfig(format='%(asctime)s %(name)s %(funcName)s %(levelname)s %(message)s', level=logging.INFO)
logger = logging.getLogger("deribit-writer")

instruments = []


async def subscribe_books():
await conn.ws.send_json({
"type": "subscribe",
"product_ids": instruments,
"channels": [
"level2",
"heartbeat"
]
})


output = open("coinbase_dump.txt", "w")


def dump(msg: str):
output.write(msg)
output.write('\n')


def stop():
asyncio.ensure_future(conn.ws.close())


async def run():
# from pprint import pprint
all_products = [x for x in await conn.get_products()]
# pprint(all_products)

my_products = {x['id']: x for x in all_products if x['quote_currency'] == 'EUR'}
base_currencies = [x['base_currency'] for x in my_products.values()]

for product in all_products:
if product["id"] in my_products:
continue

if product['quote_currency'] in base_currencies:
my_products[product["id"]] = product

global instruments
instruments += list(my_products.keys())
await conn.run_receiver()

conn.on_before_handling = dump
conn.on_connect_ws = subscribe_books

loop = asyncio.get_event_loop()
loop.call_later(3600, stop)

try:
loop.run_until_complete(run())
except KeyboardInterrupt:
print("Application closed by KeyboardInterrupt.")
2 changes: 0 additions & 2 deletions examples/deribit_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
conn = Deribit()
parser = DeribitParser()
watcher = BookWatcher(parser)

# parser.set_on_unprocessed_message(conn.handle_message)
conn.on_message = parser.parse

pending = {}
Expand Down
47 changes: 0 additions & 47 deletions examples/deribit_parser.py

This file was deleted.

7 changes: 4 additions & 3 deletions examples/deribit_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async def handle_instruments(data: dict):

if not pending:
print(instruments)
await subscribe_books(instruments)
await subscribe_books()


async def handle_book_summary_by_currency(data: dict):
Expand All @@ -50,7 +50,8 @@ async def get_currencies():
await conn.get_currencies(handle_currencies)


async def subscribe_books(instruments: list):
async def subscribe_books():
global instruments
await conn.send_public(request={
"method": "public/subscribe",
"params": {
Expand All @@ -66,7 +67,7 @@ async def subscribe_books(instruments: list):
})


output = open("dump.txt", "w")
output = open("deribit_dump.txt", "w")


def dump(msg: str):
Expand Down
64 changes: 64 additions & 0 deletions examples/parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import asyncio
import logging
import argparse
from time import time

from examples.book_watcher import BookWatcher

import sys

from ssc2ce import create_parser

logging.basicConfig(format='%(asctime)s %(name)s %(funcName)s %(levelname)s %(message)s', level=logging.INFO)
logger = logging.getLogger("deribit-parser")


class FileController:
def __init__(self, exchange: str, is_cpp: bool):

self.parser = create_parser(exchange, is_cpp)
if self.parser is None:
exit(1)

self.counter = 0
self.watcher = BookWatcher(self.parser, False)

def run(self, filename: str):
start = time()
i = 0
with open(filename) as f:
for line in f:
i += 1
if not self.parser.parse(line):
self.handle_message(line)

logger.info(f"{i} in {time() - start} sec. {self.counter}")

def handle_message(self, message: str) -> None:
self.counter += 1
# logger.info(message[:-1])


def main():
parser = argparse.ArgumentParser('ssc2ce parser example.')
parser.add_argument('exchange',
type=str,
help='...')
parser.add_argument('file',
type=str,
help='...')
parser.add_argument('-c', '--cpp',
dest='is_cpp',
action='store_true',
default=False,
help='...')

args = parser.parse_args()

FileController(args.exchange, args.is_cpp).run(args.file)


if __name__ == '__main__':
main()


2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setuptools.setup(
name='ssc2ce',
version="0.12.2",
version="0.13.0",
author='Oleg Nedbaylo',
author_email='olned64@gmail.com',
description='A Set of Simple Connectors for access To Cryptocurrency Exchanges',
Expand Down
10 changes: 6 additions & 4 deletions ssc2ce/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from .deribit import Deribit
from .deribit import Deribit, DeribitParser, DeribitL2Book
from .common import AuthType
from .bitfinex import Bitfinex

from .bitfinex import Bitfinex, BitfinexL2Book
from .coinbase_pro import Coinbase, CoinbaseParser, CoinbaseL2Book
from .factory import create_parser
from pkg_resources import get_distribution, DistributionNotFound

try:
__version__ = get_distribution(__name__).version
except DistributionNotFound:
# package is not installed
pass
pass
Loading

0 comments on commit fd486bb

Please sign in to comment.