CREATE TABLE `exchange` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL, PRIMARY KEY (id) ) ENGINE=MyISAM;
CREATE TABLE `market` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(50) NOT NULL, `id_exchange` int(11) NOT NULL, PRIMARY KEY (id) ) ENGINE=MyISAM; CREATE INDEX id_exchange ON market (id_exchange);
CREATE TABLE `ticker` ( `id` int(11) NOT NULL AUTO_INCREMENT, `id_market` int(5) NOT NULL, `local_time` int(9) NOT NULL, `timestamp` int(9) NOT NULL, `last` DECIMAL (11,11) NOT NULL, `low` DECIMAL (11,11), `high` DECIMAL (11,11), `bid` DECIMAL (11,11), `ask` DECIMAL (11,11), PRIMARY KEY (id) ) ENGINE=MyISAM; CREATE INDEX id_market ON ticker (id_market);
name_exchange = ["acx", "binance", "bitfinex", "bitfinex2", "wex"] def create_exchange_and_market_in_db(): exchanges = {} for id in name_exchange: exchange = getattr(ccxt, id) exchanges[id] = exchange() id_exchage = db_helper.insert_exchage_to_db(exchanges[id],cnx,cursor) markets = exchanges[id].load_markets() for mark in markets: id_market = db_helper.insert_market_to_db( id_exchage, mark, cnx,cursor)
def save(): markets = db_helper.get_data_exch() exchanges = {} for id in name_exchange: exchange = getattr(ccxt, id) #isHas = exchange.hasFetchTickers #if isHas: exchanges[id] = exchange({ 'enableRateLimit': True, # or .enableRateLimit = True later }) cnx = db_helper.CreateConnection() cursor = cnx.cursor() loop = asyncio.get_event_loop() while True: start_time = time.time() input_coroutines = [fetch_ticker(exchanges, name) for name in exchanges] exch_tickers = loop.run_until_complete(asyncio.gather(*input_coroutines, return_exceptions=True)) count_exchange = 0 delta = time.time() - start_time for tickers in exch_tickers: if tickers is not None: count_exchange+=1 inserted_start = time.time() db_helper.insert_tick(markets,exch_tickers,cnx,cursor) inserted_time = time.time() print(count_exchange," ", delta, ' ', inserted_start - inserted_time)
async def fetch_ticker(exchanges, name): item = exchanges[name] try: ticker = await item.fetchTickers() return {name:ticker}
SELECT ex.name as exchange_name, m.name as market_name, count(*) as count_deals FROM exchange ex LEFT JOIN market m ON m.id_exchange = ex.id LEFT JOIN ticker t ON t.id_market =m.id GROUP BY ex.id, t.id_market ORDER BY m.name HAVING count_deals > 10000;
Source: https://habr.com/ru/post/436062/