def __init__(self): Utils.log("Initializing", LoggingModes.ALL) self.exit_lock = threading.Lock() self.menus = Menus() self.initialize_menu_item_callback_functions(self.menus) self.ui = UserInterface(self.menus, handle_fatal_exception) self.order_controller = OrderController() self.api_controller = ApiController(self.order_controller) self.config_controller = ConfigController()
def print_detailed_balance_stats(self): balances = self.get_balances() if balances is None: Utils.log("Failed to retrieve balances", LoggingModes.ERROR) return index = 1 for balance in balances: self.order_controller.print_balance(index, balance) index = index + 1
def read_secret_key(): Utils.log("Reading Secret Key from disk", LoggingModes.DEBUG) f = open('secret_key', 'r') contents = f.read() if len(contents) != Config.EXPECTED_SECRET_KEY_LENGTH: Utils.log( "Secret Key is not in the expected format. Key: " + str(contents) + " with length of: " + str(len(contents)), LoggingModes.WARN) return contents
def print_detailed_open_order_stats(self): open_orders = self.look_up_open_orders() if open_orders is None: Utils.log("Failed to retrieve open orders", LoggingModes.ERROR) return index = 1 for order in open_orders: currency = Utils.get_currency_from_exchange(order.exchange) balance = self.get_balance(currency) market_summary = ApiController.get_market_summary(order.exchange) self.order_controller.print_open_order_stats( index, order, balance, market_summary) index = index + 1
def initialize_menu_item_callback_functions(self, menus): Utils.log("Hooking Up Menu Item's Callback Lambdas", LoggingModes.DEBUG) # Main menus.main_menu.items[ "Orders"].callback = lambda: self.on_orders_activated() menus.main_menu.items[ "Configuration"].callback = lambda: self.on_config_activated() menus.main_menu.items[ "Exit"].callback = lambda: self.on_exit_activated() # Orders menus.orders_menu.items[ "Print Open Orders"].callback = lambda: self.on_print_open_orders_activated( ) menus.orders_menu.items[ "Print Balances"].callback = lambda: self.on_print_balances_activated( ) menus.orders_menu.items[ "Place Orders"].callback = lambda: self.on_place_orders_activated( ) menus.orders_menu.items[ "Back"].callback = lambda: self.on_first_tier_menu_back_activated( ) # Config menus.configuration.items[ "Enter New API Key"].callback = lambda: self.on_write_api_key_activated( ) menus.configuration.items[ "Print API Key"].callback = lambda: self.on_view_api_key_activated( ) menus.configuration.items[ "Enter New Secret Key"].callback = lambda: self.on_write_secret_key_activated( ) menus.configuration.items[ "Print Secret Key"].callback = lambda: self.on_view_secret_key_activated( ) menus.configuration.items[ "Back"].callback = lambda: self.on_first_tier_menu_back_activated( )
def place_order(self, order): market_summary = ApiController.get_market_summary(order.market) if market_summary is None: Utils.log("Could not retrieve market summary for " + order.market, LoggingModes.WARN) return high = float(market_summary.high) if high is None or math.isnan(high) or high == 0: Utils.log("Could not retrieve high price from " + order.market, LoggingModes.WARN) return if order.sell_price < high: Utils.log( order.market + " sell price ( " + str(order.sell_price) + " ) is less than high price ( " + '%f' % (high) + " ) ! ", LoggingModes.ERROR) return url = "https://bittrex.com/api/v1.1/market/selllimit?apikey=" + self.api_key + "&market=" + order.market + "&quantity=" + str( order.sell_quantity) + "&rate=" + str(order.sell_price) r = self.send_request(url) r = r.json() self.order_controller.print_placed_order(r, order, high, url)
def on_exit_activated(self): os.system("cls") Utils.log("Exiting.....", LoggingModes.ALL) print("See you next time!") sys.exit()
def handle_fatal_exception(ex): Utils.log("Fatal error", LoggingModes.FATAL, ex) print("Application will now close. Check logs for further details") os._exit(1) # let's get the hell outta here!
def on_refresh_keys(self): Utils.log("Reading API and Secret keys", LoggingModes.ALL) self.api_controller.refresh_keys()
def send_request(self, url): url = url + "&nonce=" + str(int(time.time())) hmac_hash = ApiController.create_hash(self.secret_key, url) headers = {'apisign': hmac_hash} Utils.log("Sending request to URL: " + url, LoggingModes.DEBUG) return requests.get(url, headers=headers)
def look_up_open_orders(self): Utils.log("Attempting to lookup orders", LoggingModes.INFO) r = self.send_request( "https://bittrex.com/api/v1.1/market/getopenorders?apikey=" + self.api_key) return Utils.process_open_orders(r.json())
def place_orders(self, orders): if type(orders) is not list: Utils.log("Incorrect input type in place_orders function", LoggingModes.ERROR) for order in orders: self.place_order(order)