def test_03(self): self.logger = Logger().getLogger() self.loop_work1 = True self.loop_work2 = True self.buffer = Buffer("TEST") self.buffer.start() self.logger.info("test_03 started") threading.Thread(target=self.work1).start() threading.Thread(target=self.work2).start() time.sleep(30) self.loop_work1 = False self.loop_work2 = False self.buffer.stop() self.logger.info("test_03 stoped")
def root_test(): logger = Logger.getLogger() logger.critical("root critical") logger.error("root error") logger.warning("root warning") logger.info("root info") logger.debug("root debug") logger.log(0, "root notset")
def stream_test(): logger = Logger.getLogger("Stream") logger.critical("Stream critical") logger.error("Stream error") logger.warning("Stream warning") logger.info("Stream info") logger.debug("Stream debug") logger.log(0, "Stream notset")
def rotation_file_test(): logger = Logger.getLogger("RotatingFile") logger.critical("RotatingFile critical") logger.error("RotatingFile error") logger.warning("RotatingFile warning") logger.info("RotatingFile info") logger.debug("RotatingFile debug") logger.log(0, "RotatingFile notset")
config_location = args.config else: config_location = 'default.cfg' # End handling args. Config.init(config_location) # Config format: Config.get(category, option, default_value=False, lower_limit=False, upper_limit=False) # A default_value "None" means that the option is required and the bot will not run without it. # Do not use lower or upper limit on any config options which are not numbers. # Define the variable from the option in the module where you use it. output_currency = Config.get('BOT', 'outputCurrency', 'BTC') end_date = Config.get('BOT', 'endDate') json_output_enabled = Config.has_option( 'BOT', 'jsonfile') and Config.has_option('BOT', 'jsonlogsize') log = Logger(Config.get('BOT', 'jsonfile', ''), Decimal(Config.get('BOT', 'jsonlogsize', -1))) api = Poloniex(Config.get("API", "apikey", None), Config.get("API", "secret", None)) MaxToLend.init(Config, log) Data.init(api, log) Config.init(config_location, Data) if Config.has_option('BOT', 'analyseCurrencies'): import modules.MarketAnalysis as Analysis Analysis.init(Config, api, Data) else: Analysis = None Lending.init(Config, api, log, Data, MaxToLend, dry_run, Analysis) print 'Welcome to Poloniex Lending Bot' # Configure web server
# Do not use lower or upper limit on any config options which are not numbers. # Define the variable from the option in the module where you use it. api_key = Config.get("API", "apikey", None) api_secret = Config.get("API", "secret", None) auto_renew = int(Config.get("BOT", "autorenew", None, 0, 1)) output_currency = Config.get("BOT", "outputCurrency", "BTC") end_date = Config.get("BOT", "endDate") json_output_enabled = Config.has_option("BOT", "jsonfile") and Config.has_option("BOT", "jsonlogsize") def timestamp(): ts = time.time() return datetime.datetime.fromtimestamp(ts).strftime("%Y-%m-%d %H:%M:%S") log = Logger(Config.get("BOT", "jsonfile", ""), Config.get("BOT", "jsonlogsize", -1)) api = Poloniex(api_key, api_secret) MaxToLend.init(Config, log) Data.init(api, log) Lending.init(Config, api, log, Data, MaxToLend, dry_run) def set_auto_renew(auto): i = int(0) # counter try: action = "Clearing" if auto == 1: action = "Setting" log.log(action + " AutoRenew...(Please Wait)") crypto_lended = api.return_active_loans() loans_count = len(crypto_lended["provided"])
'BOT', 'jsonfile') and Config.has_option('BOT', 'jsonlogsize') jsonfile = Config.get('BOT', 'jsonfile', '') # Configure web server web_server_enabled = Config.getboolean('BOT', 'startWebServer') if web_server_enabled: if json_output_enabled is False: # User wants webserver enabled. Must have JSON enabled. Force logging with defaults. json_output_enabled = True jsonfile = Config.get('BOT', 'jsonfile', 'www/botlog.json') import modules.WebServer as WebServer WebServer.initialize_web_server(Config) # Configure logging log = Logger(jsonfile, Decimal(Config.get('BOT', 'jsonlogsize', 200)), exchange) # initialize the remaining stuff api = ExchangeApiFactory.createApi(exchange, Config, log) MaxToLend.init(Config, log) Data.init(api, log) Config.init(config_location, Data) notify_conf = Config.get_notification_config() if Config.has_option('MarketAnalysis', 'analyseCurrencies'): from modules.MarketAnalysis import MarketAnalysis # Analysis.init(Config, api, Data) analysis = MarketAnalysis(Config, api) analysis.run() else: analysis = None Lending.init(Config, api, log, Data, MaxToLend, dry_run, analysis, notify_conf)
def lend(): try: open('lendingbot.py', 'r') except IOError: os.chdir(os.path.dirname(sys.argv[0])) # Allow relative paths parser = argparse.ArgumentParser() # Start args. parser.add_argument("-cfg", "--config", help="Location of custom configuration file, overrides settings below") parser.add_argument("-dry", "--dryrun", help="Make pretend orders", action="store_true") args = parser.parse_args() # End args. # Start handling args. dry_run = bool(args.dryrun) if args.config: config_location = args.config else: config_location = 'default.cfg' # End handling args. # Config format: Config.get(category, option, default_value=False, lower_limit=False, upper_limit=False) # A default_value "None" means that the option is required and the bot will not run without it. # Do not use lower or upper limit on any config options which are not numbers. # Define the variable from the option in the module where you use it. Config.init(config_location) output_currency = Config.get('BOT', 'outputCurrency', 'BTC') end_date = Config.get('BOT', 'endDate') exchange = Config.get_exchange() json_output_enabled = Config.has_option('BOT', 'jsonfile') and Config.has_option('BOT', 'jsonlogsize') jsonfile = Config.get('BOT', 'jsonfile', '') # Configure web server web_server_enabled = Config.getboolean('BOT', 'startWebServer') if web_server_enabled: if json_output_enabled is False: # User wants webserver enabled. Must have JSON enabled. Force logging with defaults. json_output_enabled = True jsonfile = Config.get('BOT', 'jsonfile', 'www/botlog.json') import modules.WebServer as WebServer WebServer.initialize_web_server(Config) # Configure logging log = Logger(jsonfile, Decimal(Config.get('BOT', 'jsonlogsize', 200)), exchange) # initialize the remaining stuff api = ExchangeApiFactory.createApi(exchange, Config, log) MaxToLend.init(Config, log) Data.init(api, log) Config.init(config_location, Data) notify_conf = Config.get_notification_config() if Config.has_option('MarketAnalysis', 'analyseCurrencies'): from modules.MarketAnalysis import MarketAnalysis # Analysis.init(Config, api, Data) analysis = MarketAnalysis(Config, api) analysis.run() else: analysis = None Lending.init(Config, api, log, Data, MaxToLend, dry_run, analysis, notify_conf) # load plugins PluginsManager.init(Config, api, log, notify_conf) print 'Welcome to ' + Config.get("BOT", "label", "Lending Bot") + ' on ' + exchange try: while True: try: Data.update_conversion_rates(output_currency, json_output_enabled) PluginsManager.before_lending() Lending.transfer_balances() Lending.cancel_all() Lending.lend_all() PluginsManager.after_lending() log.refreshStatus(Data.stringify_total_lent(*Data.get_total_lent()), Data.get_max_duration(end_date, "status")) log.persistStatus() sys.stdout.flush() time.sleep(Lending.get_sleep_time()) except KeyboardInterrupt: # allow existing the main bot loop raise except Exception as ex: log.log_error(ex.message) log.persistStatus() if 'Invalid API key' in ex.message: print "!!! Troubleshooting !!!" print "Are your API keys correct? No quotation. Just plain keys." exit(1) elif 'Nonce must be greater' in ex.message: print "!!! Troubleshooting !!!" print "Are you reusing the API key in multiple applications? Use a unique key for every application." exit(1) elif 'Permission denied' in ex.message: print "!!! Troubleshooting !!!" print "Are you using IP filter on the key? Maybe your IP changed?" exit(1) elif 'timed out' in ex.message: print "Timed out, will retry in " + str(Lending.get_sleep_time()) + "sec" elif isinstance(ex, BadStatusLine): print "Caught BadStatusLine exception from Poloniex, ignoring." elif 'Error 429' in ex.message: additional_sleep = max(130.0-Lending.get_sleep_time(), 0) sum_sleep = additional_sleep + Lending.get_sleep_time() log.log_error('IP has been banned due to many requests. Sleeping for {} seconds'.format(sum_sleep)) time.sleep(additional_sleep) # Ignore all 5xx errors (server error) as we can't do anything about it (https://httpstatuses.com/) elif isinstance(ex, URLError): print "Caught {0} from exchange, ignoring.".format(ex.message) elif isinstance(ex, ApiError): print "Caught {0} reading from exchange API, ignoring.".format(ex.message) else: print traceback.format_exc() print "v{0} Unhandled error, please open a Github issue so we can fix it!".format(Data.get_bot_version()) if notify_conf['notify_caught_exception']: log.notify("{0}\n-------\n{1}".format(ex, traceback.format_exc()), notify_conf) sys.stdout.flush() time.sleep(Lending.get_sleep_time()) except KeyboardInterrupt: if web_server_enabled: WebServer.stop_web_server() PluginsManager.on_bot_exit() log.log('bye') print 'bye' os._exit(0) # Ad-hoc solution in place of 'exit(0)' TODO: Find out why non-daemon thread(s) are hanging on exit
if args.config: config_location = args.config else: config_location = 'default.cfg' # End handling args. Config.init(config_location) # Config format: Config.get(category, option, default_value=False, lower_limit=False, upper_limit=False) # A default_value "None" means that the option is required and the bot will not run without it. # Do not use lower or upper limit on any config options which are not numbers. # Define the variable from the option in the module where you use it. output_currency = Config.get('BOT', 'outputCurrency', 'BTC') end_date = Config.get('BOT', 'endDate') json_output_enabled = Config.has_option('BOT', 'jsonfile') and Config.has_option('BOT', 'jsonlogsize') log = Logger(Config.get('BOT', 'jsonfile', ''), Decimal(Config.get('BOT', 'jsonlogsize', -1))) api = Poloniex(Config.get("API", "apikey", None), Config.get("API", "secret", None)) MaxToLend.init(Config, log) Data.init(api, log) Config.init(config_location, Data) notify_conf = Config.get_notification_config() if Config.has_option('BOT', 'analyseCurrencies'): from modules.MarketAnalysis import MarketAnalysis # Analysis.init(Config, api, Data) analysis = MarketAnalysis(Config, api) analysis.run() else: analysis = None Lending.init(Config, api, log, Data, MaxToLend, dry_run, analysis, notify_conf)
import os import sys import time # https://qiita.com/reinhardhq/items/838df0bf09611f3c5872 sys.path.insert(0, os.path.abspath( os.path.join(os.path.dirname(__file__), '..'))) from modules.Data import Data from modules.Config import Config from modules.Logger import Logger from products.ProductManager import ProductManager if __name__ == "__main__": config = Config.getConfig() logger = Logger.getLogger() manager = ProductManager() manager.init(config, logger) test1 = "Sample1" manager.set_product("Sample1", test1) test2 = manager.get_product("Sample1") print(test2) test3 = "Sample2" manager.set_element("Sample2", test3) test4 = manager.get_element("Sample2") print(test4)
logger.error("File error") logger.warning("File warning") logger.info("File info") logger.debug("File debug") logger.log(0, "File notset") def rotation_file_test(): logger = Logger.getLogger("RotatingFile") logger.critical("RotatingFile critical") logger.error("RotatingFile error") logger.warning("RotatingFile warning") logger.info("RotatingFile info") logger.debug("RotatingFile debug") logger.log(0, "RotatingFile notset") logger = Logger.getLogger("RotatingFile") def multi_thread_test(): for i in range(100): logger.critical("RotatingFile critical {0}".format(i)) time.sleep(0) logger.error("RotatingFile error {0}".format(i)) time.sleep(0) logger.warning("RotatingFile warning {0}".format(i)) time.sleep(0) logger.info("RotatingFile info {0}".format(i)) time.sleep(0) logger.debug("RotatingFile debug {0}".format(i)) time.sleep(0) logger.log(0, "RotatingFile notset {0}".format(i)) time.sleep(0)
json_output_enabled = Config.has_option('BOT', 'jsonfile') and Config.has_option('BOT', 'jsonlogsize') jsonfile = Config.get('BOT', 'jsonfile', '') # Configure web server web_server_enabled = Config.getboolean('BOT', 'startWebServer') if web_server_enabled: if json_output_enabled is False: # User wants webserver enabled. Must have JSON enabled. Force logging with defaults. json_output_enabled = True jsonfile = Config.get('BOT', 'jsonfile', 'www/botlog.json') import modules.WebServer as WebServer WebServer.initialize_web_server(Config) # Configure logging log = Logger(jsonfile, Decimal(Config.get('BOT', 'jsonlogsize', 200)), exchange) # initialize the remaining stuff apikey = "KOP7HcNl9NwiBpwXoLoZ4LeGV2c5ofJA7K9egHeNOa2" secret = "e1B3ylYMgH9xyCL1IsH5NPKW44HlA0bLWWLXydSQPSS" api = ExchangeApiFactory.createApi(exchange, Config, log, apikey, secret) MaxToLend.init(Config, log) Data.init(api, log) Config.init(config_location, Data) notify_conf = Config.get_notification_config() if Config.has_option('MarketAnalysis', 'analyseCurrencies'): from modules.MarketAnalysis import MarketAnalysis # Analysis.init(Config, api, Data) analysis = MarketAnalysis(Config, api) analysis.run() else:
# pytest to work import os, sys, inspect currentdir = os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe()))) parentdir = os.path.dirname(currentdir) sys.path.insert(0, parentdir) from modules.Bitfinex import Bitfinex import modules.Configuration as Config import modules.Data as Data from modules.Logger import Logger import threading Config.init('default.cfg', Data) api = Bitfinex(Config, Logger()) start_time = time.time() def multiple_api_queries(n): try: for i in xrange(n): print(f'Thread {str(i + 1)}') thread1 = threading.Thread(target=call_get_open_loan_offers, args=[(i + 1)]) thread1.start() except Exception as e: assert False, 'Thread ' + str(i + 1) + ':' + e.message # Test fast api calls
config_location = args.config else: config_location = 'default.cfg' # End handling args. Config.init(config_location) # Config format: Config.get(category, option, default_value=False, lower_limit=False, upper_limit=False) # A default_value "None" means that the option is required and the bot will not run without it. # Do not use lower or upper limit on any config options which are not numbers. # Define the variable from the option in the module where you use it. output_currency = Config.get('BOT', 'outputCurrency', 'BTC') end_date = Config.get('BOT', 'endDate') json_output_enabled = Config.has_option('BOT', 'jsonfile') and Config.has_option('BOT', 'jsonlogsize') log = Logger(Config.get('BOT', 'jsonfile', ''), Config.get('BOT', 'jsonlogsize', -1)) api = Poloniex(Config.get("API", "apikey", None), Config.get("API", "secret", None)) MaxToLend.init(Config, log) Data.init(api, log) Config.init(config_location, Data) if Config.has_option('BOT', 'analyseCurrencies'): import modules.MarketAnalysis as Analysis Analysis.init(Config, api, Data) else: Analysis = None Lending.init(Config, api, log, Data, MaxToLend, dry_run, Analysis) print 'Welcome to Poloniex Lending Bot' # Configure web server
json_output_enabled = Config.has_option('BOT', 'jsonfile') and Config.has_option('BOT', 'jsonlogsize') jsonfile = Config.get('BOT', 'jsonfile', '') # Configure web server web_server_enabled = Config.getboolean('BOT', 'startWebServer') if web_server_enabled: if json_output_enabled is False: # User wants webserver enabled. Must have JSON enabled. Force logging with defaults. json_output_enabled = True jsonfile = Config.get('BOT', 'jsonfile', 'www/botlog.json') import modules.WebServer as WebServer WebServer.initialize_web_server(Config) # Configure logging log = Logger(jsonfile, Decimal(Config.get('BOT', 'jsonlogsize', 200)), exchange) # initialize the remaining stuff api = ExchangeApiFactory.createApi(exchange, Config, log) MaxToLend.init(Config, log) Data.init(api, log) Config.init(config_location, Data) notify_conf = Config.get_notification_config() if Config.has_option('MarketAnalysis', 'analyseCurrencies'): from modules.MarketAnalysis import MarketAnalysis # Analysis.init(Config, api, Data) analysis = MarketAnalysis(Config, api) analysis.run() else: analysis = None Lending.init(Config, api, log, Data, MaxToLend, dry_run, analysis, notify_conf)
# Hack to get relative imports - probably need to fix the dir structure instead but we need this at the minute for # pytest to work import os, sys, inspect currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) parentdir = os.path.dirname(currentdir) sys.path.insert(0, parentdir) from modules.Poloniex import Poloniex import modules.Configuration as Config import modules.Data as Data from modules.Logger import Logger import threading Config.init('default.cfg', Data) api = Poloniex(Config, Logger()) # def multiple_api_queries(n): # try: # for i in xrange(n): # print 'api_query ' + str(i + 1) + '\n' # thread1 = threading.Thread(target=api.return_open_loan_offers) # thread1.start() # except Exception as e: # assert False, 'api_query ' + str(i + 1) + ':' + e.message # # # # Test fast api calls # def test_multiple_calls(): # multiple_api_queries(9)
class BufferTest(unittest.TestCase): def setUp(self): pass def tearDown(self): pass def test_01(self): buf1 = Buffer("buf1") buf2 = Buffer("buf2") buf3 = Buffer("buf3") buf1.start() buf2.start() buf3.start() expect = "test_test_test" buf1.push(expect) self.assertEqual(1, buf1.size()) value2 = buf1.pop() self.assertEqual(0, buf1.size()) self.assertEqual(expect, value2) buf2.push(value2) self.assertEqual(1, buf2.size()) value3 = buf2.pop() self.assertEqual(0, buf2.size()) self.assertEqual(expect, value3) buf3.push(value3) self.assertEqual(1, buf3.size()) actual = buf3.pop() self.assertEqual(0, buf3.size()) self.assertEqual(expect, actual) def test_02(self): buf1 = Buffer("buf1") for i in range(1, 10): value = "TEST_{0}".format(i) buf1.push(value) self.assertEqual(i, buf1.size()) buf1.clear() self.assertEqual(0, buf1.size()) def work1(self): while(self.loop_work1): time.sleep(1) self.buffer.push("TEST {0}".format(time.time())) self.logger.info("work1 pushed") def work2(self): while(self.loop_work2): val = self.buffer.pop() self.logger.info("work2 poped({0})".format(val)) def test_03(self): self.logger = Logger().getLogger() self.loop_work1 = True self.loop_work2 = True self.buffer = Buffer("TEST") self.buffer.start() self.logger.info("test_03 started") threading.Thread(target=self.work1).start() threading.Thread(target=self.work2).start() time.sleep(30) self.loop_work1 = False self.loop_work2 = False self.buffer.stop() self.logger.info("test_03 stoped")
'BOT', 'jsonfile') and Config.has_option('BOT', 'jsonlogsize') jsonfile = Config.get('BOT', 'jsonfile', '') # Configure web server web_server_enabled = Config.getboolean('BOT', 'startWebServer') if web_server_enabled: if json_output_enabled == False: # User wants webserver enabled. Must have JSON enabled. Force logging with defaults. json_output_enabled = True jsonfile = Config.get('BOT', 'jsonfile', 'www/botlog.json') import modules.WebServer as WebServer WebServer.initialize_web_server(Config) # Configure logging log = Logger(jsonfile, Decimal(Config.get('BOT', 'jsonlogsize', 200)), exchange) # initialize the remaining stuff api = ExchangeApiFactory.createApi(exchange, Config) MaxToLend.init(Config, log) Data.init(api, log) Config.init(config_location, Data) notify_conf = Config.get_notification_config() if Config.has_option('BOT', 'analyseCurrencies'): from modules.MarketAnalysis import MarketAnalysis # Analysis.init(Config, api, Data) analysis = MarketAnalysis(Config, api) analysis.run() else: analysis = None Lending.init(Config, api, log, Data, MaxToLend, dry_run, analysis, notify_conf)