153 lines
5.9 KiB
Python
153 lines
5.9 KiB
Python
import time
|
|
from libraries.balance_accounts import balance_accounts
|
|
|
|
|
|
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 set_step_size(self, step_size):
|
|
self.step_size = step_size
|
|
return self.step_size
|
|
|
|
def get_step_size(self):
|
|
return self.step_size
|
|
|
|
def set_percentage(self, percentage):
|
|
self.percentage = percentage
|
|
return self.percentage
|
|
|
|
def get_percentage(self):
|
|
return self.percentage
|
|
|
|
def set_minimum_amount_in_trading_account(self, minimum_amount_in_trading_account):
|
|
self.minimum_amount_in_trading_account = minimum_amount_in_trading_account
|
|
return self.minimum_amount_in_trading_account
|
|
|
|
def get_minimum_amount_in_trading_account(self):
|
|
return self.minimum_amount_in_trading_account
|
|
|
|
def set_time_between_subscriptions(self, time_between_subscriptions):
|
|
self.time_between_subscriptions = time_between_subscriptions
|
|
return self.time_between_subscriptions
|
|
|
|
def get_time_between_subscriptions(self):
|
|
return self.time_between_subscriptions
|
|
|
|
def set_time_between_redemptions(self, time_between_redemptions):
|
|
self.time_between_redemptions = time_between_redemptions
|
|
return self.time_between_redemptions
|
|
|
|
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 = "| PAUSED - NOT SUBSCRIBING NOR REDEEMING"
|
|
|
|
#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"{self} | {balances_string} | {percentages_string}"
|
|
|