def init(): from pydoc import locate import os import sys import jesse.helpers as jh # Python version validation. if jh.python_version() < 3.6: print( jh.color( 'Jesse has not beed tested with your Python version ({}), hence it may not work properly. Consider upgrading to >= 3.7'.format( jh.python_version()), 'red' ) ) # fix directory issue sys.path.insert(0, os.getcwd()) ls = os.listdir('.') is_jesse_project = 'strategies' in ls and 'config.py' in ls and 'storage' in ls and 'routes.py' in ls if not is_jesse_project: print( jh.color( 'Invalid directory. To use Jesse inside notebooks, create notebooks inside the root of a Jesse project.', 'red' ) ) if is_jesse_project: local_config = locate('config.config') from jesse.config import set_config set_config(local_config)
def get_general_info(has_live=False) -> dict: from jesse.modes.import_candles_mode.drivers import drivers from jesse.version import __version__ as jesse_version system_info = {'jesse_version': jesse_version} if has_live: from jesse_live.info import SUPPORTED_EXCHANGES_NAMES live_exchanges = list(sorted(SUPPORTED_EXCHANGES_NAMES)) from jesse_live.version import __version__ as live_version system_info['live_plugin_version'] = live_version else: live_exchanges = [] exchanges = list(sorted(drivers.keys())) strategies_path = os.getcwd() + "/strategies/" strategies = list( sorted([ name for name in os.listdir(strategies_path) if os.path.isdir(strategies_path + name) ])) system_info['python_version'] = '{}.{}'.format(*jh.python_version()) system_info['operating_system'] = jh.get_os() system_info['cpu_cores'] = jh.cpu_cores_count() system_info['is_docker'] = jh.is_docker() return { 'exchanges': exchanges, 'live_exchanges': live_exchanges, 'strategies': strategies, 'has_live_plugin_installed': has_live, 'system_info': system_info, }
def report_exception( description: str, traceback: str, mode: str, attach_logs: bool, session_id: str, email: str = None, has_live: bool = False ) -> JSONResponse: access_token = get_access_token() if attach_logs and session_id: path_exchange_log = None if mode == 'backtest': path_log = f'storage/logs/backtest-mode/{session_id}.txt' elif mode == 'live': path_log = f'storage/logs/live-mode/{session_id}.txt' path_exchange_log = 'storage/logs/exchange-streams.txt' else: raise ValueError('Invalid mode') # attach exchange_log if there's any files = {'log_file': open(path_log, 'rb')} if path_exchange_log and jh.file_exists(path_exchange_log): files['exchange_log'] = open(path_exchange_log, 'rb') else: files = None from jesse.version import __version__ as jesse_version info = { 'os': jh.get_os(), 'python_version': '{}.{}'.format(*jh.python_version()), 'is_docker': jh.is_docker(), 'jesse_version': jesse_version } if has_live: from jesse_live.version import __version__ as live_plugin_version info['live_plugin_version'] = live_plugin_version params = { 'description': description, 'traceback': traceback, 'email': email, 'info': json.dumps(info) } res = requests.post( 'https://jesse.trade/api/exception', data=params, files=files, headers={'Authorization': f'Bearer {access_token}'} ) success_message = 'Exception report submitted successfully' error_message = f"{res.status_code} error: {res.json()['message']}" return JSONResponse({ 'status': 'success' if res.status_code == 200 else 'error', 'message': success_message if res.status_code == 200 else error_message }, status_code=200)
def test_python_version(): import sys assert jh.python_version() == float( f'{sys.version_info[0]}.{sys.version_info[1]}')
def register_custom_exception_handler() -> None: import sys import threading import traceback import logging from jesse.services import logger as jesse_logger import click from jesse import exceptions log_format = "%(message)s" os.makedirs('./storage/logs', exist_ok=True) if jh.is_livetrading(): logging.basicConfig(filename='storage/logs/live-trade.txt', level=logging.INFO, filemode='w', format=log_format) elif jh.is_paper_trading(): logging.basicConfig(filename='storage/logs/paper-trade.txt', level=logging.INFO, filemode='w', format=log_format) elif jh.is_collecting_data(): logging.basicConfig(filename='storage/logs/collect.txt', level=logging.INFO, filemode='w', format=log_format) elif jh.is_optimizing(): logging.basicConfig(filename='storage/logs/optimize.txt', level=logging.INFO, filemode='w', format=log_format) else: logging.basicConfig(level=logging.INFO) # main thread def handle_exception(exc_type, exc_value, exc_traceback) -> None: if issubclass(exc_type, KeyboardInterrupt): sys.excepthook(exc_type, exc_value, exc_traceback) return # handle Breaking exceptions if exc_type in [ exceptions.InvalidConfig, exceptions.RouteNotFound, exceptions.InvalidRoutes, exceptions.CandleNotFoundInDatabase ]: click.clear() print(f"{'=' * 30} EXCEPTION TRACEBACK:") traceback.print_tb(exc_traceback, file=sys.stdout) print("=" * 73) print('\n', jh.color('Uncaught Exception:', 'red'), jh.color(f'{exc_type.__name__}: {exc_value}', 'yellow')) return # send notifications if it's a live session if jh.is_live(): jesse_logger.error(f'{exc_type.__name__}: {exc_value}') if jh.is_live() or jh.is_collecting_data(): logging.error("Uncaught Exception:", exc_info=(exc_type, exc_value, exc_traceback)) else: print(f"{'=' * 30} EXCEPTION TRACEBACK:") traceback.print_tb(exc_traceback, file=sys.stdout) print("=" * 73) print('\n', jh.color('Uncaught Exception:', 'red'), jh.color(f'{exc_type.__name__}: {exc_value}', 'yellow')) if jh.is_paper_trading(): print( jh.color( 'An uncaught exception was raised. Check the log file at:\nstorage/logs/paper-trade.txt', 'red')) elif jh.is_livetrading(): print( jh.color( 'An uncaught exception was raised. Check the log file at:\nstorage/logs/live-trade.txt', 'red')) elif jh.is_collecting_data(): print( jh.color( 'An uncaught exception was raised. Check the log file at:\nstorage/logs/collect.txt', 'red')) sys.excepthook = handle_exception # other threads if jh.python_version() >= (3, 8): def handle_thread_exception(args) -> None: if args.exc_type == SystemExit: return # handle Breaking exceptions if args.exc_type in [ exceptions.InvalidConfig, exceptions.RouteNotFound, exceptions.InvalidRoutes, exceptions.CandleNotFoundInDatabase ]: click.clear() print(f"{'=' * 30} EXCEPTION TRACEBACK:") traceback.print_tb(args.exc_traceback, file=sys.stdout) print("=" * 73) print( '\n', jh.color('Uncaught Exception:', 'red'), jh.color(f'{args.exc_type.__name__}: {args.exc_value}', 'yellow')) return # send notifications if it's a live session if jh.is_live(): jesse_logger.error( f'{args.exc_type.__name__}: { args.exc_value}') if jh.is_live() or jh.is_collecting_data(): logging.error("Uncaught Exception:", exc_info=(args.exc_type, args.exc_value, args.exc_traceback)) else: print(f"{'=' * 30} EXCEPTION TRACEBACK:") traceback.print_tb(args.exc_traceback, file=sys.stdout) print("=" * 73) print( '\n', jh.color('Uncaught Exception:', 'red'), jh.color(f'{args.exc_type.__name__}: {args.exc_value}', 'yellow')) if jh.is_paper_trading(): print( jh.color( 'An uncaught exception was raised. Check the log file at:\nstorage/logs/paper-trade.txt', 'red')) elif jh.is_livetrading(): print( jh.color( 'An uncaught exception was raised. Check the log file at:\nstorage/logs/live-trade.txt', 'red')) elif jh.is_collecting_data(): print( jh.color( 'An uncaught exception was raised. Check the log file at:\nstorage/logs/collect.txt', 'red')) threading.excepthook = handle_thread_exception
import os import sys # Hide the "FutureWarning: pandas.util.testing is deprecated." caused by empyrical import warnings from pydoc import locate import click import pkg_resources import jesse.helpers as jh warnings.simplefilter(action='ignore', category=FutureWarning) # Python version validation. if jh.python_version() < (3, 7): print( jh.color( f'Jesse requires Python version above 3.7. Yours is {jh.python_version()}', 'red')) # fix directory issue sys.path.insert(0, os.getcwd()) ls = os.listdir('.') is_jesse_project = 'strategies' in ls and 'config.py' in ls and 'storage' in ls and 'routes.py' in ls def validate_cwd() -> None: """ make sure we're in a Jesse project """
import os import sys from pydoc import locate import click import pkg_resources import jesse.helpers as jh # Python version validation. if jh.python_version() < 3.6: print( jh.color( 'Jesse requires Python version above 3.6. Yours is {}'.format( jh.python_version()), 'red')) # fix directory issue sys.path.insert(0, os.getcwd()) ls = os.listdir('.') is_jesse_project = 'strategies' in ls and 'config.py' in ls and 'storage' in ls and 'routes.py' in ls def validate_cwd(): """ make sure we're in a Jesse project """ if not is_jesse_project: print( jh.color( 'Current directory is not a Jesse project. You must run commands from the root of a Jesse project.',
def test_python_version(): import sys assert jh.python_version() == float('{}.{}'.format(sys.version_info[0], sys.version_info[1]))
import os import sys from pydoc import locate import click import pkg_resources import jesse.helpers as jh # Hide the "FutureWarning: pandas.util.testing is deprecated." caused by empyrical import warnings warnings.simplefilter(action='ignore', category=FutureWarning) # Python version validation. if jh.python_version() < 3.6: print( jh.color( 'Jesse requires Python version above 3.6. Yours is {}'.format(jh.python_version()), 'red' ) ) # fix directory issue sys.path.insert(0, os.getcwd()) ls = os.listdir('.') is_jesse_project = 'strategies' in ls and 'config.py' in ls and 'storage' in ls and 'routes.py' in ls def validate_cwd():
import os import sys # Hide the "FutureWarning: pandas.util.testing is deprecated." caused by empyrical import warnings from pydoc import locate import click import pkg_resources import jesse.helpers as jh warnings.simplefilter(action='ignore', category=FutureWarning) # Python version validation. if jh.python_version() < 3.7: print( jh.color( f'Jesse requires Python version above 3.7. Yours is {jh.python_version()}', 'red' ) ) # fix directory issue sys.path.insert(0, os.getcwd()) ls = os.listdir('.') is_jesse_project = 'strategies' in ls and 'config.py' in ls and 'storage' in ls and 'routes.py' in ls def validate_cwd() -> None: """
def test_python_version(): import sys assert jh.python_version() == sys.version_info[:2]