Skip to content
This repository has been archived by the owner on Nov 19, 2021. It is now read-only.

Commit

Permalink
fixed runtimeerror
Browse files Browse the repository at this point in the history
  • Loading branch information
wardbradt committed Aug 29, 2018
1 parent c55bd39 commit 5238dce
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 28 deletions.
2 changes: 1 addition & 1 deletion cythonperegrine/CollectionBuilder.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ccxt.async as ccxt
import ccxt.async_support as ccxt
import asyncio
import json

Expand Down
2 changes: 1 addition & 1 deletion cythonperegrine/OpportunityFinder.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ccxt.async as ccxt
import ccxt.async_support as ccxt
import asyncio
import json

Expand Down
51 changes: 41 additions & 10 deletions peregrinearb/async_build_markets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ccxt.async as ccxt
import ccxt.async_support as ccxt
import asyncio
import json
import networkx as nx
Expand Down Expand Up @@ -43,6 +43,7 @@ def build_all_collections(self, write=True, ccxt_errors=False):
:return: A dictionary where keys are market names and values are lists of exchanges which support the respective
market name
"""

asyncio.get_event_loop().run_until_complete(self.async_build_all_collections(write, ccxt_errors))

return self.collections
Expand Down Expand Up @@ -250,12 +251,22 @@ def build_collections(blacklist=False, write=True, ccxt_errors=False):
ccxt_errors, has={'fetchOrderBook': True})


def build_specific_collections(blacklist=False, write=False, ccxt_errors=False, **kwargs):
async def async_build_specific_collections(blacklist=False, write=False, ccxt_errors=False, **kwargs):
builder = SpecificCollectionBuilder(blacklist, **kwargs)
return builder.build_all_collections(write, ccxt_errors)
return await builder.async_build_all_collections(write, ccxt_errors)


def build_all_collections(write=True, ccxt_errors=False):
def build_specific_collections(blacklist=False, write=False, ccxt_errors=False, **kwargs):
try:
asyncio.get_event_loop().run_until_complete(
async_build_specific_collections(blacklist, write, ccxt_errors, **kwargs)
)
except RuntimeError:
raise RuntimeError('build_specific_collections was called from an asynchronous context. The event loop is '
'already running. You probably meant to use async_build_specific_collections')


async def async_build_all_collections(write=True, ccxt_errors=False):
"""
Be careful when using this. build_collections is typically preferred over this method because build_collections only
accounts for exchanges which have a private API (and thus can be traded on).
Expand All @@ -264,26 +275,46 @@ def build_all_collections(write=True, ccxt_errors=False):
:return:
"""
builder = CollectionBuilder()
return builder.build_all_collections(write=write, ccxt_errors=ccxt_errors)
return await builder.async_build_all_collections(write, ccxt_errors)


def get_exchanges_for_market(market_ticker):
def build_all_collections(write=True, ccxt_errors=False):
"""
Synchronous wrapper for async_build_all_collections
"""
asyncio.get_event_loop().run_until_complete(async_build_all_collections(write, ccxt_errors))


async def async_get_exchanges_for_market(symbol):
"""
Returns the list of exchanges on which a market is traded
"""
try:
with open('./collections/collections.json') as f:
collections = json.load(f)
for market_name, exchanges in collections.items():
if market_name == market_ticker:
if market_name == symbol:
return exchanges
except FileNotFoundError:
return build_specific_collections(symbols=[market_ticker])
return build_specific_collections(symbols=[symbol])

with open('./collections/singularly_available_markets.json') as f:
singularly_available_markets = json.load(f)
for market_name, exchange in singularly_available_markets:
if market_name == market_ticker:
if market_name == symbol:
return [exchange]

raise ExchangeNotInCollectionsError(market_ticker)
raise ExchangeNotInCollectionsError(symbol)


def get_exchanges_for_market(symbol):
"""
Synchronous wrapper for async_build_all_collections
"""
try:
asyncio.get_event_loop().run_until_complete(
async_get_exchanges_for_market(symbol)
)
except RuntimeError:
raise RuntimeError('build_specific_collections was called from an asynchronous context. The event loop is '
'already running. You probably meant to use async_build_specific_collections')
18 changes: 10 additions & 8 deletions peregrinearb/async_find_opportunities.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ccxt.async as ccxt
from .async_build_markets import get_exchanges_for_market
import ccxt.async_support as ccxt
from .async_build_markets import async_get_exchanges_for_market
import asyncio


Expand All @@ -12,11 +12,10 @@ def __init__(self, market_name, exchanges=None, name=True):
highest market bid price.
"""
if exchanges is None:
if not name:
raise ValueError("if parameter name == False, parameter exchanges cannot be None.")
exchanges = get_exchanges_for_market(market_name)
exchanges = []

exchanges = [getattr(ccxt, exchange_id)() for exchange_id in exchanges]
if not name:
exchanges = [getattr(ccxt, exchange_id)() for exchange_id in exchanges]

self.exchange_list = exchanges
self.market_name = market_name
Expand Down Expand Up @@ -64,6 +63,9 @@ async def find_min_max(self):
'lowest_ask': self.lowest_ask}


async def get_opportunity_for_market(ticker, exchanges=None, name=True):
finder = OpportunityFinder(ticker, exchanges=exchanges, name=name)
async def get_opportunity_for_market(symbol, exchanges=None, name=True):
if exchanges is None:
exchanges = await async_get_exchanges_for_market(symbol)
name = True
finder = OpportunityFinder(symbol, exchanges=exchanges, name=name)
return await finder.find_min_max()
2 changes: 1 addition & 1 deletion peregrinearb/outliers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ccxt.async as ccxt
import ccxt.async_support as ccxt
import asyncio
import time
import numpy as np
Expand Down
2 changes: 1 addition & 1 deletion peregrinearb/tests/test_build_markets.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest import TestCase
from peregrinearb.async_build_markets import build_all_collections, build_specific_collections, build_collections, \
build_arbitrage_graph_for_exchanges
import ccxt.async as ccxt
import ccxt.async_support as ccxt
import asyncio


Expand Down
7 changes: 5 additions & 2 deletions peregrinearb/utils/multi_exchange.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import math
import networkx as nx
from ccxt import async as ccxt
from ccxt import async_support as ccxt
import warnings


Expand Down Expand Up @@ -51,7 +51,10 @@ def create_weighted_multi_exchange_digraph(exchanges: list, name=True, log=False
else:
exchanges = [{'object': exchange} for exchange in exchanges]

loop = asyncio.get_event_loop()
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.get_running_loop()
futures = [asyncio.ensure_future(exchange_dict['object'].load_markets()) for exchange_dict in exchanges]
loop.run_until_complete(asyncio.gather(*futures))

Expand Down
2 changes: 1 addition & 1 deletion peregrinearb/utils/single_exchange.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import math
import networkx as nx
from ccxt import async as ccxt
from ccxt import async_support as ccxt
import warnings


Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tweepy
git+git://github.com/wardbradt/networkx.git#egg=networkx
numpy==1.14.0
numpy
ccxt
# Only necessary if you will be modifying or using cythonperegrine
Cython==0.26.1
Cython
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='peregrinearb',
version='1.1.2',
version='1.2.0',
description='A Python library which provides several algorithms to detect arbitrage opportunities across over 90 cryptocurrency markets in 34 countries',
author='Ward Bradt',
author_email='[email protected]',
Expand Down

0 comments on commit 5238dce

Please sign in to comment.