import time from libraries.balance_accounts import balance_accounts from libraries.colors import colors class earner: def __init__(self, connector, config): self.connector = connector self.currency = config["currency"] self.minimum_amount_in_trading_account = config["minimum_amount_in_trading_account"] self.step_size = config["step_size"] self.percentage = config["percentage"] self.time_between_subscriptions = config["time_between_subscriptions"] self.time_between_redemptions = config["time_between_redemptions"] self.last_subscription_time = 0 self.last_redemption_time = 0 self.start_time = time.time() self.trading_balance = 0 self.earning_balance = 0 self.is_paused = True self.status_string = "" def __str__(self): return str(self.connector) def toggle_pause(self): self.is_paused = not self.is_paused return self.is_paused def get_is_paused(self): return self.is_paused def set_step_size(self, step_size): try: self.step_size = float(step_size) return self.step_size except Exception as e: print(e) return 0 def get_step_size(self): return self.step_size def set_percentage(self, percentage): try: self.percentage = float(percentage) return self.percentage except Exception as e: print(e) return 0 def get_percentage(self): return self.percentage def set_minimum_amount_in_trading_account(self, minimum_amount_in_trading_account): try: self.minimum_amount_in_trading_account = float(minimum_amount_in_trading_account) return self.minimum_amount_in_trading_account except Exception as e: print(e) return 0 def get_minimum_amount_in_trading_account(self): return self.minimum_amount_in_trading_account def set_time_between_subscriptions(self, time_between_subscriptions): try: self.time_between_subscriptions = float(time_between_subscriptions) return self.time_between_subscriptions except Exception as e: print(e) return 0 def get_time_between_subscriptions(self): return self.time_between_subscriptions def set_time_between_redemptions(self, time_between_redemptions): try: self.time_between_redemptions = float(time_between_redemptions) return self.time_between_redemptions except Exception as e: print(e) return 0 def get_time_between_redemptions(self): return self.time_between_redemptions def get_last_subscription(self): return {str(self.connector): self.last_subscription_time} def get_last_redemption(self): return {str(self.connector): self.last_redemption_time} def get_status_string(self): return self.status_string def get_trading_balance(self): return float(self.trading_balance) def get_earning_balance(self): return float(self.earning_balance) def subscribe(self,amount): print(f"{str(self.connector)} | Subscribing {amount} {self.currency}") if True: self.last_subscription_time = time.time() return 0 return 1 def redeem(self,amount): print(f"{str(self.connector)} | Redeeming {amount} {self.currency}") if True: self.last_redemption_time = time.time() return 0 return 1 def run(self): ''' 1. Get trading and earning balances 2. Call balance_accounts 3. If there are differences: a. If subscribe side, subscribe if time_since_last_subscription>time_between_subscriptions b. If redeem side, redeem if time_since_last_redeem>time_between_redemptions 4. Output status to status_string ''' #Get trading and earning balances trading_balance_dict = self.connector.get_trading_balance(self.currency) if "Error" not in trading_balance_dict: self.trading_balance = trading_balance_dict[self.currency] else: self.trading_balance = None earning_balance_dict = self.connector.get_position(self.currency) if earning_balance_dict=={}: self.earning_balance = 0 elif "Error" not in earning_balance_dict: self.earning_balance = earning_balance_dict["amount"] else: self.earning_balance = None if not self.is_paused: target_trading_amount, target_earning_amount = balance_accounts(float(self.trading_balance), float(self.earning_balance), self.minimum_amount_in_trading_account, self.step_size, self.percentage) earning_delta = 0 if self.earning_balance is None: print(f"{str(self.connector)} - There was an error fetching earning balance") else: if float(self.earning_balance)!=0: earning_delta = target_earning_amount - float(self.earning_balance) if earning_delta>self.step_size and time.time()-self.last_subscription_time>self.time_between_subscriptions: self.subscribe(earning_delta) if earning_delta<-self.step_size and time.time()-self.last_redemption_time>self.time_between_redemptions: self.redeem(-earning_delta) print(f"{str(self.connector)} - Difference: {earning_delta}") else: paused_string = "| "+colors.yellow+"PAUSED"+colors.white #Output status to status_string balances_string = f"Trading balance: {float(self.trading_balance):.2f} {self.currency} | Earning balance: {float(self.earning_balance):.2f} {self.currency}" percentages_string = f"On earn: {float(self.earning_balance)/float(self.trading_balance):.2%} {paused_string}" self.status_string = f"{colors.cyan}{self}{colors.white} | {balances_string} | {percentages_string}"