def recreate_trading_api_periodically(): global trading_api while True: # every 10 minutes, recreate session. time.sleep(600) trading_api_mutex.acquire() try: trading_api = TradingAPI(credentials=credentials) trading_api.connect() trading_api.get_account_info() except: logger.error('failed to recreate TradingAPI.') finally: trading_api_mutex.release()
# SETUP LOGGING LEVEL logging.basicConfig(level=logging.DEBUG) # SETUP CONFIG DICT with open('config/config.json') as config_file: config_dict = json.load(config_file) # SETUP CREDENTIALS int_account = config_dict['int_account'] username = config_dict['username'] password = config_dict['password'] credentials = Credentials( int_account=int_account, username=username, password=password, ) # SETUP TRADING API trading_api = TradingAPI(credentials=credentials) # CONNECT trading_api.connect() # FETCH DATA account_info_table = trading_api.get_account_info() # DISPLAY DATA account_info_pretty = json.dumps(account_info_table, sort_keys=True, indent=4) print(account_info_pretty)
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s') logger = logging.getLogger('degiro-preorder') # parameters PREORDERS_FILE = '/app/preorders.json' FINNHUB_TOKEN = os.environ['FINNHUB_TOKEN'] credentials = Credentials( int_account=int(os.environ['DG_INTACCOUNT']), username=os.environ['DG_USERNAME'], password=os.environ['DG_PASSWORD'], ) trading_api_mutex = Lock() trading_api = TradingAPI(credentials=credentials) trading_api.connect() trading_api.get_account_info() logger.info('connected to DEGIRO API.') # set logging levels for libs to critical or higher for key, value in logging.Logger.manager.loggerDict.items(): if key.startswith('trading'): logging.getLogger(key).setLevel(logging.CRITICAL + 1) elif key != 'degiro-preorder': logging.getLogger(key).setLevel(logging.CRITICAL) # test writing to preorders file preorders = load(open(PREORDERS_FILE, 'r')) dump(preorders, open(PREORDERS_FILE, 'w'), indent=4, sort_keys=True) # poll trading_api periodically to keep session alive