async def task_CLI(loop, stop): log("*** Command Line Utilities Online. Click q to quit. *** ", 'ok') while not stop.is_set(): command = await loop.run_in_executor(None, input, "") if 'q' in command: stop.set() if 'r' in command: report() log("*** CLI Handler is shutting down ***", 'ok')
async def task_trainer(loop, stop): log('*** Trainer Starting ***', 'ok') while not stop.is_set(): for item in sync_get_bots_sorted_by_next_update(1): if stop.is_set(): break bot = Tradebot(data=item) bot.operate(autosave=True) await asyncio.wait({asyncio.sleep(0.1), stop.wait()}, return_when=asyncio.FIRST_COMPLETED) log('*** Trainer is shutting down ***', 'ok')
async def task_cache_manager(stop): log('*** Cache Manager Starting ***','ok') debug("Initializing cache...") init_cache = sync_get_market_image_minimal() for item in init_cache: market_cache[item.get('Symb')] = item.get('Info') while not stop.is_set(): result = await asyncio.create_task(async_get_market_image_minimal()) for item in result: market_cache[item.get('Symb')] = item.get('Info') debug("Cache Updated") await asyncio.wait({asyncio.sleep(30), stop.wait()}, return_when=asyncio.FIRST_COMPLETED) log("*** Cache Manager is shutting down ***",'ok')
async def task_arbiter(loop, stop): log('*** Arbiter Starting ***', 'ok') while not stop.is_set(): # Add new bots until cap added = 0 for _ in range(min([200, BOT_THRESHOLD - sync_count_bots()])): newbot = Tradebot() newbot.save() added += 1 debug('Arbiter: Added {} new trade bots into the forest'.format(added)) await asyncio.wait({asyncio.sleep(60), stop.wait()}, return_when=asyncio.FIRST_COMPLETED) log("*** Arbiter shutting down ***", 'ok')
async def task_crawler(loop, stop): log('*** Crawler Starting ***', 'ok') while not stop.is_set(): symbs = await asyncio.wait( {asyncio.create_task(async_get_all_symbols()), stop.wait()}, return_when=asyncio.FIRST_COMPLETED) if stop.is_set: break for symb in symbs: if stop.is_set(): break try: ticker = Ticker(symb) await ticker.update() if ticker.info is None: raise Exception( f"Info of {symb} is None - Can't process this.") data = { 'Symb': symb, 'Info': ticker.info, 'Data': ticker.raw, 'lastUpdate': dt.now() } await async_update_stock(data, by='Symb') debug(f'[{dt.now()}] Data acquired for {symb}\n') except Exception as e: log(f'[{dt.now()}] Error occured when parsing {symb},{e}\n', 'error', to_file=True) await asyncio.wait( {asyncio.sleep(1.5), stop.wait()}, return_when=asyncio.FIRST_COMPLETED) log('*** Crawler shutting down ***', 'ok')
async def task_scout(loop, stop): log('*** Scout Starting ***','ok') symbs = await async_get_all_symbols() for symb in stock_symbols: if stop.is_set(): break try: if symb not in symbs: debug(f"Scout detected that {symb} is not in the database. Acquiring info now") ticker = Ticker(symb) await ticker.update() data = { 'Symb': symb, 'Info': ticker.info, 'Data': ticker.raw, 'lastUpdate': dt.now() } await async_update_stock(data,by='Symb') await asyncio.wait({asyncio.sleep(1.5),stop.wait()},return_when=asyncio.FIRST_COMPLETED) except Exception as e: log(f"Error parsing info for {symb} : {e}",'error') log('*** Scout shutting down ***','ok')
''' import pymongo from time import time as now from os import system from __log import log, vlog from _global_config import DB_HOST from _static_data import stock_symbols client = pymongo.MongoClient(host=DB_HOST) try: client['Dowwin']['test'].insert_one({"test": 'test'}) client['Dowwin'].drop_collection('test') except: log("Unable to connect to database", 'error') raise ConnectionError("Unable to connect to the database") _db_market = client['Dowwin']['market'] _db_bots = client['Dowwin']['tradebots'] _db_logs = client['Dowwin']['logs'] ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' LOGGING ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' def sync_upload_log(filepath)-> None: with open(filepath) as f: _db_logs.insert_one({str(now()): f.readlines()})
from task_cache_manager import market_cache from _adapter_database import sync_get_market_image_minimal from __log import log from pprint import pformat from datetime import datetime, timedelta log("Fetching market data...") all_stock_data = sync_get_market_image_minimal() log("Data fetched. Processing...") market = {} for item in all_stock_data: market[item.get('Symb')] = item.get('Info') with open("market_data.py", 'w') as f: f.writelines(pformat(market))
def report(): log(market_cache)